def __get_sql_steps(self, cmd, name, location, admin, password):
        from azure.cli.command_modules.sql._util import (
            get_sql_management_client)
        from azure.cli.command_modules.sql.custom import (
            server_create as sql_server_create, db_create as
            sql_database_create, firewall_rule_allow_all_azure_ips as
            sql_firewall_allow_azure, ComputeModelType)
        from azure.mgmt.sql.models import (Sku)

        sql_client = get_sql_management_client(cmd.cli_ctx)

        steps = []

        server_parameters = {
            'client': sql_client.servers,
            'resource_group_name': name,
            'server_name': name,
            'location': location,
            'version': '12.0',
            'administrator_login': admin,
            'administrator_login_password': password
        }
        steps.append(
            DatabaseCreationStep('server', sql_server_create,
                                 server_parameters))

        sku = Sku(capacity=10, name='Standard', tier='Standard', family='Gen5')

        database_parameters = {
            'cmd': cmd,
            'client': sql_client.databases,
            'database_name': name,
            'server_name': name,
            'resource_group_name': name,
            'location': location,
            'sku': sku,
            'elastic_pool_id': None,
            'compute_model': ComputeModelType('Serverless')
        }
        steps.append(
            DatabaseCreationStep('database', sql_database_create,
                                 database_parameters))

        firewall_parameters = {
            'client': sql_client.firewall_rules,
            'server_name': name,
            'resource_group_name': name
        }
        steps.append(
            DatabaseCreationStep('firewall', sql_firewall_allow_azure,
                                 firewall_parameters))
        return steps
Пример #2
0
def create_database(cmd, database_provider, name, location, admin, password):
    database_settings = {}
    if database_provider.lower() == 'mysql':
        server_client = cf_mysql_servers(cmd.cli_ctx, None)
        database_client = cf_mysql_db(cmd.cli_ctx, None)
        admin = admin[:16]
        database_settings['engine'] = 'django.db.backends.mysql'

        def server_creator():
            sku_name = 'B_Gen5_1'
            parameters = ServerForCreate(
                sku=MySqlSku(name=sku_name),
                location=location,
                properties=ServerPropertiesForDefaultCreate(
                    administrator_login=admin,
                    administrator_login_password=password,
                    version='5.7',
                    ssl_enforcement='Enabled',
                    storage_profile=StorageProfile(
                        backup_retention_days=0,
                        geo_redundant_backup='Disabled',
                        storage_mb=5120)))
            return server_client.create(resource_group_name=name,
                                        server_name=name,
                                        parameters=parameters)

        def database_creator():
            return database_client.create_or_update(resource_group_name=name,
                                                    server_name=name,
                                                    database_name=name)
    elif database_provider.lower() == 'cosmosdb' or database_provider.lower(
    ) == 'mongodb':
        from azure.mgmt.cosmosdb import CosmosDB
        cosmosdb_client = get_mgmt_service_client(cmd.cli_ctx, CosmosDB)

        from azure.mgmt.cosmosdb.models import (
            DatabaseAccountCreateUpdateParameters, DatabaseAccountKind,
            Location, ConsistencyPolicy)

        def server_creator():
            params = DatabaseAccountCreateUpdateParameters(
                location=location,
                locations=[
                    Location(location_name=location,
                             failover_priority=0,
                             is_zone_redundant=False)
                ],
                kind=DatabaseAccountKind.mongo_db.value,
                consistency_policy=ConsistencyPolicy(
                    default_consistency_level=1,
                    max_staleness_prefix=100,
                    max_interval_in_seconds=5))
            return cosmosdb_client.database_accounts.create_or_update(
                name, name, params)

        def database_creator():
            return cosmosdb_client.database_accounts.create_update_mongo_db_database(
                resource_group_name=name,
                account_name=name,
                database_name=name,
                resource={'id': name},
                options={})
    else:
        sql_client = get_sql_management_client(cmd.cli_ctx)
        sql_parameters = {
            'location': location,  # "self.region" is 'west-us' by default
            'version': '12.0',
            'administrator_login': admin,
            'administrator_login_password': password
        }
        sku = Sku(capacity=10, name='Standard', tier='Standard')

        def server_creator():
            return sql_server_create(sql_client.servers, name, name,
                                     **sql_parameters)

        def database_creator():
            return sql_database_create(cmd,
                                       sql_client.databases,
                                       database_name=name,
                                       server_name=name,
                                       resource_group_name=name,
                                       location=location,
                                       sku=sku,
                                       elastic_pool_id=None)

    wrapper = DatabaseCreator(database_creator)
    poller = server_creator()
    poller.add_done_callback(wrapper.create_database)
    # poller.add_done_callback(print(poller.result()))
    return wrapper