services | platforms | author |
---|---|---|
sql-database |
python |
lmazuel |
This sample shows how to manage SQL Server using the Azure Storage Resource Provider for Python.
On this page
-
If you don't already have it, install Python.
-
We recommend using a virtual environment to run this example, but it's not mandatory. You can initialize a virtual environment this way:
pip install virtualenv virtualenv mytestenv cd mytestenv source bin/activate
-
Clone the repository.
git clone https://github.com/Azure-Samples/sql-database-python-manage.git
-
Install the dependencies using pip.
cd sql-database-python-manage pip install -r requirements.txt
-
Create an Azure service principal, using Azure CLI, PowerShell or Azure Portal.
-
Export these environment variables into your current shell.
export AZURE_TENANT_ID={your tenant id} export AZURE_CLIENT_ID={your client id} export AZURE_CLIENT_SECRET={your client secret} export AZURE_SUBSCRIPTION_ID={your subscription id}
-
Run the sample.
python example.py
The sample walks you through several SQL Server operations. It starts by setting up a ResourceManagementClient and SQLManagementClient objects using your subscription and credentials.
#
# Create the Resource Manager Client with an Application (service principal) token provider
#
subscription_id = os.environ.get(
'AZURE_SUBSCRIPTION_ID',
'11111111-1111-1111-1111-111111111111') # your Azure Subscription Id
credentials = ServicePrincipalCredentials(
client_id=os.environ['AZURE_CLIENT_ID'],
secret=os.environ['AZURE_CLIENT_SECRET'],
tenant=os.environ['AZURE_TENANT_ID']
)
resource_client = ResourceManagementClient(credentials, subscription_id)
sql_client = SqlManagementClient(credentials, subscription_id)
# You MIGHT need to add SQL as a valid provider for these credentials
# If so, this operation has to be done only once for each credential
resource_client.providers.register('Microsoft.Sql')
# Create Resource group
resource_group_params = {'location':'westus'}
resource_client.resource_groups.create_or_update(GROUP_NAME, resource_group_params)
There are also a few supporting functions (print_item
, print_metrics
, and print_properties
).
server = sql_client.servers.create_or_update(
GROUP_NAME,
SERVER_NAME,
{
'location': REGION,
'version': '12.0', # Required for create
'administrator_login': 'mysecretname', # Required for create
'administrator_login_password': 'HusH_Sec4et' # Required for create
}
)
firewall_rule = sql_client.servers.create_or_update_firewall_rule(
GROUP_NAME,
SERVER_NAME,
"firewall_rule_name_123.123.123.123",
"123.123.123.123", # Start ip range
"123.123.123.123" # End ip range
)
server = sql_client.servers.get_by_resource_group(
GROUP_NAME,
SERVER_NAME,
)
sql_client.servers.list_by_resource_group(GROUP_NAME)
sql_client.servers.list()
sql_client.servers.list_usages(GROUP_NAME, SERVER_NAME)
async_db_create = sql_client.databases.create_or_update(
GROUP_NAME,
SERVER_NAME,
DATABASE_NAME,
{
'location': REGION
}
)
database = async_db_create.result() # Wait for completion and return created object
database = sql_client.databases.get(
GROUP_NAME,
SERVER_NAME,
DATABASE_NAME
)
sql_client.databases.list_by_server(GROUP_NAME, SERVER_NAME)
sql_client.databases.list_usages(GROUP_NAME, SERVER_NAME, DATABASE_NAME)
sql_client.databases.delete(GROUP_NAME, SERVER_NAME, DATABASE_NAME)
sql_client.servers.delete(GROUP_NAME, SERVER_NAME)