GithubHelp home page GithubHelp logo

216giorgiy / sql-database-python-manage Goto Github PK

View Code? Open in Web Editor NEW

This project forked from lodexinc/sql-database-python-manage

0.0 1.0 0.0 7 KB

An example illustrating how to use Python to manage your SQL Azure databases

License: MIT License

Python 100.00%

sql-database-python-manage's Introduction

services platforms author
sql-database
python
lmazuel

Getting Started with Azure SQL Management in Python

This sample shows how to manage SQL Server using the Azure Storage Resource Provider for Python.

On this page

Run this sample

  1. If you don't already have it, install Python.

  2. 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
    
  3. Clone the repository.

    git clone https://github.com/Azure-Samples/sql-database-python-manage.git
    
  4. Install the dependencies using pip.

    cd sql-database-python-manage
    pip install -r requirements.txt
    
  5. Create an Azure service principal, using Azure CLI, PowerShell or Azure Portal.

  6. 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}
    
  7. Run the sample.

    python example.py
    

What is example.py doing?

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).

Create a SQL Server instance

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
    }
)

Create a firewall rule

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
)

Get a server

server = sql_client.servers.get_by_resource_group(
    GROUP_NAME,
    SERVER_NAME,
)

List servers by resource group

sql_client.servers.list_by_resource_group(GROUP_NAME)

List servers by subscription

sql_client.servers.list()

List server usages

sql_client.servers.list_usages(GROUP_NAME, SERVER_NAME)

Create a database

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

Get a database

database = sql_client.databases.get(
    GROUP_NAME,
    SERVER_NAME,
    DATABASE_NAME
)

Get a list of databases

sql_client.databases.list_by_server(GROUP_NAME, SERVER_NAME)

Get a list of database usages

sql_client.databases.list_usages(GROUP_NAME, SERVER_NAME, DATABASE_NAME)

Delete a database

sql_client.databases.delete(GROUP_NAME, SERVER_NAME, DATABASE_NAME)

Delete a SQL Server instance

sql_client.servers.delete(GROUP_NAME, SERVER_NAME)

More information

sql-database-python-manage's People

Contributors

allclark avatar lisawong19 avatar lmazuel avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.