def __get_mysql_steps(self, cmd, name: str, location: str, admin: str,
                          password: str):
        from azure.cli.command_modules.rdbms._client_factory import (
            cf_mysql_servers, cf_mysql_db, cf_mysql_firewall_rules)
        from azure.mgmt.rdbms.mysql.models import (
            ServerPropertiesForDefaultCreate, ServerForCreate, StorageProfile,
            Sku)

        mysql_server_client = cf_mysql_servers(cmd.cli_ctx, None)
        mysql_database_client = cf_mysql_db(cmd.cli_ctx, None)
        mysql_firewall_client = cf_mysql_firewall_rules(cmd.cli_ctx, None)

        admin = admin[:16]

        steps = []

        server_params = {
            'resource_group_name':
            name,
            'server_name':
            name,
            'parameters':
            ServerForCreate(sku=Sku(name='B_Gen5_1'),
                            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)))
        }
        steps.append(
            DatabaseCreationStep('database server', mysql_server_client.create,
                                 server_params))

        database_params = {
            'resource_group_name': name,
            'server_name': name,
            'database_name': name
        }
        steps.append(
            DatabaseCreationStep('database',
                                 mysql_database_client.create_or_update,
                                 database_params))

        firewall_params = {
            'resource_group_name': name,
            'server_name': name,
            'firewall_rule_name': 'Azure',
            'start_ip_address': '0.0.0.0',
            'end_ip_address': '0.0.0.0'
        }

        steps.append(
            DatabaseCreationStep('database firewall',
                                 mysql_firewall_client.create_or_update,
                                 firewall_params))
        return steps
Beispiel #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