Exemplo n.º 1
0
    def __call__(self, parser, namespace, values, option_string=None):
        if namespace.locations is None:
            namespace._deprecated_location_format = False
            namespace.locations = []

        if any("regionname" in s.lower() for s in values):
            keys_found = set()
            _name = ""
            _failover = 0
            _is_zr = False
            for item in values:
                kvp = item.split('=', 1)
                _key = kvp[0].lower()
                if _key in keys_found:
                    # pylint: disable=line-too-long
                    raise CLIError(
                        'usage error: --locations [KEY=VALUE ...]. KEY {} provided multiple times'
                        .format(_key))
                keys_found.add(_key)
                if _key == "regionname":
                    _name = kvp[1]
                elif _key == "failoverpriority":
                    _failover = int(kvp[1])
                elif _key == "iszoneredundant":
                    _is_zr = kvp[1].lower() == "true"
                else:
                    # pylint: disable=line-too-long
                    raise CLIError(
                        'usage error: --locations [KEY=VALUE ...]. KEY {} not supported. Valid keys are regionName, failoverPriority, and isZoneRedundant'
                        .format(_key))
            namespace.locations.append(
                Location(location_name=_name,
                         failover_priority=_failover,
                         is_zone_redundant=_is_zr))
        else:
            # pylint: disable=line-too-long
            if not namespace._deprecated_location_format:
                logger.warning(
                    'The regionName=failoverPriority method of specifying locations is deprecated. Use --locations KEY=VALUE [KEY=VALUE ...] to specify the regionName, failoverPriority, and isZoneRedundant properties of the location. Multiple locations can be specified by including more than one --locations argument.'
                )
            namespace._deprecated_location_format = True

            for item in values:
                comps = item.split('=', 1)
                namespace.locations.append(
                    Location(location_name=comps[0],
                             failover_priority=int(comps[1]),
                             is_zone_redundant=False))
Exemplo n.º 2
0
def cli_cosmosdb_create(cmd,
                        client,
                        resource_group_name,
                        account_name,
                        locations=None,
                        tags=None,
                        kind=DatabaseAccountKind.global_document_db.value,
                        default_consistency_level=None,
                        max_staleness_prefix=100,
                        max_interval=5,
                        ip_range_filter=None,
                        enable_automatic_failover=None,
                        capabilities=None,
                        enable_virtual_network=None,
                        virtual_network_rules=None,
                        enable_multiple_write_locations=None,
                        disable_key_based_metadata_write_access=None):
    """Create a new Azure Cosmos DB database account."""
    consistency_policy = None
    if default_consistency_level is not None:
        consistency_policy = ConsistencyPolicy(
            default_consistency_level=default_consistency_level,
            max_staleness_prefix=max_staleness_prefix,
            max_interval_in_seconds=max_interval)

    from azure.cli.core.commands.client_factory import get_mgmt_service_client
    from azure.cli.core.profiles import ResourceType
    resource_client = get_mgmt_service_client(
        cmd.cli_ctx, ResourceType.MGMT_RESOURCE_RESOURCES)

    rg = resource_client.resource_groups.get(resource_group_name)
    resource_group_location = rg.location  # pylint: disable=no-member

    if not locations:
        locations = []
        locations.append(
            Location(location_name=resource_group_location,
                     failover_priority=0,
                     is_zone_redundant=False))

    params = DatabaseAccountCreateUpdateParameters(
        location=resource_group_location,
        locations=locations,
        tags=tags,
        kind=kind,
        consistency_policy=consistency_policy,
        ip_range_filter=ip_range_filter,
        is_virtual_network_filter_enabled=enable_virtual_network,
        enable_automatic_failover=enable_automatic_failover,
        capabilities=capabilities,
        virtual_network_rules=virtual_network_rules,
        enable_multiple_write_locations=enable_multiple_write_locations,
        disable_key_based_metadata_write_access=
        disable_key_based_metadata_write_access)

    async_docdb_create = client.create_or_update(resource_group_name,
                                                 account_name, params)
    docdb_account = async_docdb_create.result()
    docdb_account = client.get(resource_group_name, account_name)  # Workaround
    return docdb_account
Exemplo n.º 3
0
def cli_cosmosdb_update(client,
                        resource_group_name,
                        account_name,
                        locations=None,
                        default_consistency_level=None,
                        max_staleness_prefix=None,
                        max_interval=None,
                        ip_range_filter=None,
                        enable_automatic_failover=None):
    """Update an existing Azure Cosmos DB database account. """
    existing = client.get(resource_group_name, account_name)

    update_consistency_policy = False
    if max_interval is not None or \
       max_staleness_prefix is not None or \
       default_consistency_level is not None:
        update_consistency_policy = True

    if max_staleness_prefix is None:
        max_staleness_prefix = existing.consistency_policy.max_staleness_prefix

    if max_interval is None:
        max_interval = existing.consistency_policy.max_interval_in_seconds

    if default_consistency_level is None:
        default_consistency_level = existing.consistency_policy.default_consistency_level

    consistency_policy = None
    if update_consistency_policy:
        consistency_policy = ConsistencyPolicy(default_consistency_level,
                                               max_staleness_prefix,
                                               max_interval)
    else:
        consistency_policy = existing.consistency_policy

    if not locations:
        for loc in existing.read_locations:
            locations.append(
                Location(location_name=loc.location_name,
                         failover_priority=loc.failover_priority))

    if ip_range_filter is None:
        ip_range_filter = existing.ip_range_filter

    if enable_automatic_failover is None:
        enable_automatic_failover = existing.enable_automatic_failover

    params = DatabaseAccountCreateUpdateParameters(
        existing.location,
        locations,
        kind=existing.kind,
        consistency_policy=consistency_policy,
        ip_range_filter=ip_range_filter)

    async_docdb_create = client.create_or_update(resource_group_name,
                                                 account_name, params)
    docdb_account = async_docdb_create.result()
    docdb_account = client.get(resource_group_name, account_name)  # Workaround
    return docdb_account
Exemplo n.º 4
0
    def __get_cosmosdb_steps(self, cmd, name, location):
        from azure.mgmt.cosmosdb import CosmosDBManagementClient
        from azure.cli.core.commands.client_factory import get_mgmt_service_client

        steps = []

        cosmosdb_client = get_mgmt_service_client(cmd.cli_ctx,
                                                  CosmosDBManagementClient)

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

        server_params = {
            'resource_group_name':
            name,
            'account_name':
            name,
            '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))
        }
        steps.append(
            DatabaseCreationStep(
                'server', cosmosdb_client.database_accounts.create_or_update,
                server_params))

        database_params = {
            'resource_group_name': name,
            'account_name': name,
            'database_name': name,
            'resource': {
                'id': name
            },
            'options': {}
        }
        steps.append(
            DatabaseCreationStep(
                'database', cosmosdb_client.mongo_db_resources.
                create_update_mongo_db_database, database_params))

        def retrieve_password():
            result = cosmosdb_client.database_accounts.list_keys(
                resource_group_name=name, account_name=name)
            self.password = result.primary_master_key

        steps.append(
            DatabaseCreationStep('retrieve password', retrieve_password, None))
        return steps
Exemplo n.º 5
0
def cli_cosmosdb_network_rule_add(cmd,
                                  client,
                                  resource_group_name,
                                  account_name,
                                  subnet,
                                  virtual_network=None,
                                  ignore_missing_vnet_service_endpoint=False):
    """ Adds a virtual network rule to an existing Cosmos DB database account """
    subnet = _get_virtual_network_id(cmd, resource_group_name, subnet,
                                     virtual_network)
    existing = client.get(resource_group_name, account_name)

    virtual_network_rules = []
    rule_already_exists = False
    for rule in existing.virtual_network_rules:
        virtual_network_rules.append(
            VirtualNetworkRule(id=rule.id,
                               ignore_missing_vnet_service_endpoint=rule.
                               ignore_missing_vnet_service_endpoint))
        if rule.id == subnet:
            rule_already_exists = True
            logger.warning("The rule exists and will be overwritten")

    if not rule_already_exists:
        virtual_network_rules.append(
            VirtualNetworkRule(id=subnet,
                               ignore_missing_vnet_service_endpoint=
                               ignore_missing_vnet_service_endpoint))

    locations = []
    for loc in existing.read_locations:
        locations.append(
            Location(location_name=loc.location_name,
                     failover_priority=loc.failover_priority,
                     is_zone_redundant=loc.is_zone_redundant))

    params = DatabaseAccountCreateUpdateParameters(
        location=existing.location,
        locations=locations,
        tags=existing.tags,
        kind=existing.kind,
        consistency_policy=existing.consistency_policy,
        ip_range_filter=existing.ip_range_filter,
        enable_automatic_failover=existing.enable_automatic_failover,
        capabilities=existing.capabilities,
        is_virtual_network_filter_enabled=True,
        virtual_network_rules=virtual_network_rules,
        enable_multiple_write_locations=existing.
        enable_multiple_write_locations,
        enable_cassandra_connector=existing.enable_cassandra_connector,
        connector_offer=existing.connector_offer)

    async_docdb_create = client.create_or_update(resource_group_name,
                                                 account_name, params)
    docdb_account = async_docdb_create.result()
    docdb_account = client.get(resource_group_name, account_name)  # Workaround
    return docdb_account
Exemplo n.º 6
0
def validate_locations(ns):
    """ Extracts multiple space-separated locations in regionName=failoverPriority format """
    from azure.mgmt.cosmosdb.models import Location
    if ns.locations is None:
        ns.locations = []
        return
    loc_dict = []
    for item in ns.locations:
        comps = item.split('=', 1)
        loc_dict.append(Location(location_name=comps[0], failover_priority=int(comps[1])))
    ns.locations = loc_dict
Exemplo n.º 7
0
def cli_cosmosdb_network_rule_remove(cmd,
                                     client,
                                     resource_group_name,
                                     account_name,
                                     subnet,
                                     virtual_network=None):
    """ Adds a virtual network rule to an existing Cosmos DB database account """
    subnet = _get_virtual_network_id(cmd, resource_group_name, subnet,
                                     virtual_network)
    existing = client.get(resource_group_name, account_name)

    virtual_network_rules = []
    rule_removed = False
    for rule in existing.virtual_network_rules:
        if rule.id != subnet:
            virtual_network_rules.append(
                VirtualNetworkRule(id=rule.id,
                                   ignore_missing_vnet_service_endpoint=rule.
                                   ignore_missing_vnet_service_endpoint))
        else:
            rule_removed = True
    if not rule_removed:
        raise CLIError("This rule does not exist for the Cosmos DB account")

    locations = []
    for loc in existing.read_locations:
        locations.append(
            Location(location_name=loc.location_name,
                     failover_priority=loc.failover_priority,
                     is_zone_redundant=loc.is_zone_redundant))

    params = DatabaseAccountCreateUpdateParameters(
        location=existing.location,
        locations=locations,
        tags=existing.tags,
        kind=existing.kind,
        consistency_policy=existing.consistency_policy,
        ip_range_filter=existing.ip_range_filter,
        enable_automatic_failover=existing.enable_automatic_failover,
        capabilities=existing.capabilities,
        is_virtual_network_filter_enabled=True,
        virtual_network_rules=virtual_network_rules,
        enable_multiple_write_locations=existing.
        enable_multiple_write_locations)

    async_docdb_create = client.create_or_update(resource_group_name,
                                                 account_name, params)
    docdb_account = async_docdb_create.result()
    docdb_account = client.get(resource_group_name, account_name)  # Workaround
    return docdb_account
Exemplo n.º 8
0
 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)
Exemplo n.º 9
0
def cli_cosmosdb_create(cmd,
                        client,
                        resource_group_name,
                        account_name,
                        locations=None,
                        tags=None,
                        kind=DatabaseAccountKind.global_document_db.value,
                        default_consistency_level=None,
                        max_staleness_prefix=100,
                        max_interval=5,
                        ip_range_filter=None,
                        enable_automatic_failover=None,
                        capabilities=None):
    """Create a new Azure Cosmos DB database account."""
    consistency_policy = None
    if default_consistency_level is not None:
        consistency_policy = ConsistencyPolicy(default_consistency_level,
                                               max_staleness_prefix,
                                               max_interval)

    from azure.mgmt.resource import ResourceManagementClient
    from azure.cli.core.commands.client_factory import get_mgmt_service_client
    resource_client = get_mgmt_service_client(cmd.cli_ctx,
                                              ResourceManagementClient)
    rg = resource_client.resource_groups.get(resource_group_name)
    resource_group_location = rg.location  # pylint: disable=no-member

    if not locations:
        locations.append(
            Location(location_name=resource_group_location,
                     failover_priority=0))

    params = DatabaseAccountCreateUpdateParameters(
        resource_group_location,
        locations,
        tags=tags,
        kind=kind,
        consistency_policy=consistency_policy,
        ip_range_filter=ip_range_filter,
        enable_automatic_failover=enable_automatic_failover,
        capabilities=capabilities)

    async_docdb_create = client.create_or_update(resource_group_name,
                                                 account_name, params)
    docdb_account = async_docdb_create.result()
    docdb_account = client.get(resource_group_name, account_name)  # Workaround
    return docdb_account
Exemplo n.º 10
0
def setup_cosmosdb(cosmosdb_client, database_account_name):
  log.info("Creating CosmosDB")
  mongo = cosmosdb_client.database_accounts.create_or_update(
    Azure.resource_group,
    database_account_name,
    DatabaseAccountCreateUpdateParameters(
      location=Azure.location,
      locations=[Location(location_name=Azure.location)],
      kind=DatabaseAccountKind.mongo_db
    ),
    database_account_offer_type=DatabaseAccountOfferType.standard
  ).result()

  log.info("Fetching CosmosDB connection details")
  keys = cosmosdb_client.database_accounts.list_keys(Azure.resource_group, database_account_name)
  connection_strings = cosmosdb_client.database_accounts.list_connection_strings(Azure.resource_group, database_account_name).connection_strings
  connection_string = head(cs for cs in connection_strings if cs.description == "Primary MongoDB Connection String").connection_string
  return mongo, keys, connection_string
Exemplo n.º 11
0
def cli_cosmosdb_update(client,
                        resource_group_name,
                        account_name,
                        locations=None,
                        tags=None,
                        default_consistency_level=None,
                        max_staleness_prefix=None,
                        max_interval=None,
                        ip_range_filter=None,
                        enable_automatic_failover=None,
                        capabilities=None,
                        enable_virtual_network=None,
                        virtual_network_rules=None):
    """Update an existing Azure Cosmos DB database account. """
    existing = client.get(resource_group_name, account_name)

    # Workaround until PATCH support for all properties
    # pylint: disable=too-many-boolean-expressions
    if capabilities is not None:
        if locations or \
                default_consistency_level is not None or \
                max_staleness_prefix is not None or \
                max_interval is not None or \
                ip_range_filter is not None or \
                enable_automatic_failover is not None or \
                enable_virtual_network is not None or \
                virtual_network_rules is not None:
            raise CLIError(
                "Cannot set capabilities and update properties at the same time. {0}"
                .format(locations))

        else:
            async_docdb_create = client.patch(resource_group_name,
                                              account_name,
                                              tags=tags,
                                              capabilities=capabilities)
            docdb_account = async_docdb_create.result()
            docdb_account = client.get(resource_group_name, account_name)
            return docdb_account

    # Workaround until PATCH support for all properties
    # pylint: disable=too-many-boolean-expressions
    if tags is not None:
        if not locations and \
                default_consistency_level is None and \
                max_staleness_prefix is None and \
                max_interval is None and \
                ip_range_filter is None and \
                enable_automatic_failover is None and \
                enable_virtual_network is None and \
                virtual_network_rules is None:
            async_docdb_create = client.patch(resource_group_name,
                                              account_name,
                                              tags=tags,
                                              capabilities=capabilities)
            docdb_account = async_docdb_create.result()
            docdb_account = client.get(resource_group_name, account_name)
            return docdb_account

    update_consistency_policy = False
    if max_interval is not None or \
            max_staleness_prefix is not None or \
            default_consistency_level is not None:
        update_consistency_policy = True

    if max_staleness_prefix is None:
        max_staleness_prefix = existing.consistency_policy.max_staleness_prefix

    if max_interval is None:
        max_interval = existing.consistency_policy.max_interval_in_seconds

    if default_consistency_level is None:
        default_consistency_level = existing.consistency_policy.default_consistency_level

    consistency_policy = None
    if update_consistency_policy:
        consistency_policy = ConsistencyPolicy(
            default_consistency_level=default_consistency_level,
            max_staleness_prefix=max_staleness_prefix,
            max_interval_in_seconds=max_interval)
    else:
        consistency_policy = existing.consistency_policy

    if not locations:
        for loc in existing.read_locations:
            locations.append(
                Location(location_name=loc.location_name,
                         failover_priority=loc.failover_priority))

    if ip_range_filter is None:
        ip_range_filter = existing.ip_range_filter

    if enable_automatic_failover is None:
        enable_automatic_failover = existing.enable_automatic_failover

    if enable_virtual_network is None:
        enable_virtual_network = existing.is_virtual_network_filter_enabled

    if virtual_network_rules is None:
        virtual_network_rules = existing.virtual_network_rules

    if tags is None:
        tags = existing.tags

    params = DatabaseAccountCreateUpdateParameters(
        location=existing.location,
        locations=locations,
        tags=tags,
        kind=existing.kind,
        consistency_policy=consistency_policy,
        ip_range_filter=ip_range_filter,
        enable_automatic_failover=enable_automatic_failover,
        capabilities=existing.capabilities,
        is_virtual_network_filter_enabled=enable_virtual_network,
        virtual_network_rules=virtual_network_rules)

    async_docdb_create = client.create_or_update(resource_group_name,
                                                 account_name, params)
    docdb_account = async_docdb_create.result()
    docdb_account = client.get(resource_group_name, account_name)  # Workaround
    return docdb_account
    def __get_cosmosdb_steps(self, cmd, name, location):
        from azure.mgmt.cosmosdb import CosmosDBManagementClient
        from azure.mgmt.cosmosdb.models import Capability
        from azure.cli.core.commands.client_factory import get_mgmt_service_client

        steps = []

        cosmosdb_client = get_mgmt_service_client(cmd.cli_ctx,
                                                  CosmosDBManagementClient)

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

        server_params = {
            'resource_group_name':
            name,
            'account_name':
            name,
            'create_update_parameters':
            DatabaseAccountCreateUpdateParameters(
                location=location,
                locations=[
                    Location(location_name=location,
                             failover_priority=0,
                             is_zone_redundant=False)
                ],
                kind=DatabaseAccountKind.mongo_db.value,
                capabilities=[
                    Capability(name='EnableMongo'),
                    Capability(name='EnableServerless'),
                    Capability(name='DisableRateLimitingResponses')
                ],
                tags={'CosmosAccountType': 'Non-Production'})
        }
        steps.append(
            DatabaseCreationStep(
                'server', cosmosdb_client.database_accounts.create_or_update,
                server_params))

        database_params = {
            'resource_group_name':
            name,
            'account_name':
            name,
            'database_name':
            name,
            'create_update_mongo_db_database_parameters':
            MongoDBDatabaseCreateUpdateParameters(resource={'id': name},
                                                  options={})
        }
        steps.append(
            DatabaseCreationStep(
                'database', cosmosdb_client.mongo_db_resources.
                create_update_mongo_db_database, database_params))

        def retrieve_password():
            result = cosmosdb_client.database_accounts.list_keys(
                resource_group_name=name, account_name=name)
            self.password = result.primary_master_key

        steps.append(
            DatabaseCreationStep('retrieve password', retrieve_password, None))
        return steps