示例#1
0
def _validate_vm_create_storage_account(namespace):

    if namespace.storage_account:
        storage_id = parse_resource_id(namespace.storage_account)
        rg = storage_id.get('resource_group', namespace.resource_group_name)
        if check_existence(storage_id['name'], rg, 'Microsoft.Storage', 'storageAccounts'):
            # 1 - existing storage account specified
            namespace.storage_account_type = 'existing'
            logger.debug("using specified existing storage account '%s'", storage_id['name'])
        else:
            # 2 - params for new storage account specified
            namespace.storage_account_type = 'new'
            logger.debug("specified storage account '%s' not found and will be created", storage_id['name'])
    else:
        from azure.cli.core.profiles import ResourceType
        from azure.cli.core.commands.client_factory import get_mgmt_service_client
        storage_client = get_mgmt_service_client(ResourceType.MGMT_STORAGE).storage_accounts

        # find storage account in target resource group that matches the VM's location
        sku_tier = 'Premium' if 'Premium' in namespace.storage_sku else 'Standard'
        account = next(
            (a for a in storage_client.list_by_resource_group(namespace.resource_group_name)
             if a.sku.tier.value == sku_tier and a.location == namespace.location), None)

        if account:
            # 3 - nothing specified - find viable storage account in target resource group
            namespace.storage_account = account.name
            namespace.storage_account_type = 'existing'
            logger.debug("suitable existing storage account '%s' will be used", account.name)
        else:
            # 4 - nothing specified - create a new storage account
            namespace.storage_account_type = 'new'
            logger.debug('no suitable storage account found. One will be created.')
示例#2
0
def _validate_vm_create_storage_account(namespace):

    if namespace.storage_account:
        storage_id = parse_resource_id(namespace.storage_account)
        rg = storage_id.get('resource_group', namespace.resource_group_name)
        if check_existence(storage_id['name'], rg, 'Microsoft.Storage', 'storageAccounts'):
            # 1 - existing storage account specified
            namespace.storage_account_type = 'existing'
        else:
            # 2 - params for new storage account specified
            namespace.storage_account_type = 'new'
    else:
        from azure.mgmt.storage import StorageManagementClient
        from azure.cli.core.commands.client_factory import get_mgmt_service_client
        storage_client = get_mgmt_service_client(StorageManagementClient).storage_accounts

        # find storage account in target resource group that matches the VM's location
        sku_tier = 'Premium' if 'Premium' in namespace.storage_sku else 'Standard'
        account = next(
            (a for a in storage_client.list_by_resource_group(namespace.resource_group_name)
             if a.sku.tier.value == sku_tier and a.location == namespace.location), None)

        if account:
            # 3 - nothing specified - find viable storage account in target resource group
            namespace.storage_account = account.name
            namespace.storage_account_type = 'existing'
        else:
            # 4 - nothing specified - create a new storage account
            namespace.storage_account_type = 'new'
示例#3
0
def _validate_vmss_create_load_balancer(namespace):
    # convert the single_placement_group to boolean for simpler logic beyond
    if namespace.single_placement_group is None:
        namespace.single_placement_group = namespace.instance_count <= 100
    else:
        namespace.single_placement_group = (
            namespace.single_placement_group == 'true')

    if not namespace.single_placement_group:
        if namespace.load_balancer:
            raise CLIError(
                '--load-balancer is not applicable when --single-placement-group is '
                'explictly turned off or implictly turned off for 100+ instance count'
            )
        namespace.load_balancer = ''

    if namespace.load_balancer:
        if check_existence(namespace.load_balancer,
                           namespace.resource_group_name, 'Microsoft.Network',
                           'loadBalancers'):
            namespace.load_balancer_type = 'existing'
        else:
            namespace.load_balancer_type = 'new'
    elif namespace.load_balancer == '':
        namespace.load_balancer_type = None
    elif namespace.load_balancer is None:
        namespace.load_balancer_type = 'new'
示例#4
0
def _validate_vm_create_storage_account(namespace):

    if namespace.storage_account:
        storage_id = parse_resource_id(namespace.storage_account)
        rg = storage_id.get('resource_group', namespace.resource_group_name)
        if check_existence(storage_id['name'], rg, 'Microsoft.Storage',
                           'storageAccounts'):
            # 1 - existing storage account specified
            namespace.storage_account_type = 'existing'
        else:
            # 2 - params for new storage account specified
            namespace.storage_account_type = 'new'
    else:
        from azure.mgmt.storage import StorageManagementClient
        from azure.cli.core.commands.client_factory import get_mgmt_service_client
        storage_client = get_mgmt_service_client(
            StorageManagementClient).storage_accounts

        # find storage account in target resource group that matches the VM's location
        sku_tier = 'Premium' if 'Premium' in namespace.storage_sku else 'Standard'
        account = next((a for a in storage_client.list_by_resource_group(
            namespace.resource_group_name) if a.sku.tier.value == sku_tier
                        and a.location == namespace.location), None)

        if account:
            # 3 - nothing specified - find viable storage account in target resource group
            namespace.storage_account = account.name
            namespace.storage_account_type = 'existing'
        else:
            # 4 - nothing specified - create a new storage account
            namespace.storage_account_type = 'new'
示例#5
0
def _validate_vm_create_storage_account(namespace):

    if namespace.storage_account:
        storage_id = parse_resource_id(namespace.storage_account)
        rg = storage_id.get('resource_group', namespace.resource_group_name)
        if check_existence(storage_id['name'], rg, 'Microsoft.Storage', 'storageAccounts'):
            # 1 - existing storage account specified
            namespace.storage_account_type = 'existing'
            logger.debug("using specified existing storage account '%s'", storage_id['name'])
        else:
            # 2 - params for new storage account specified
            namespace.storage_account_type = 'new'
            logger.debug("specified storage account '%s' not found and will be created", storage_id['name'])
    else:
        from azure.cli.core.profiles import ResourceType
        from azure.cli.core.commands.client_factory import get_mgmt_service_client
        storage_client = get_mgmt_service_client(ResourceType.MGMT_STORAGE).storage_accounts

        # find storage account in target resource group that matches the VM's location
        sku_tier = 'Premium' if 'Premium' in namespace.storage_sku else 'Standard'
        account = next(
            (a for a in storage_client.list_by_resource_group(namespace.resource_group_name)
             if a.sku.tier.value == sku_tier and a.location == namespace.location), None)

        if account:
            # 3 - nothing specified - find viable storage account in target resource group
            namespace.storage_account = account.name
            namespace.storage_account_type = 'existing'
            logger.debug("suitable existing storage account '%s' will be used", account.name)
        else:
            # 4 - nothing specified - create a new storage account
            namespace.storage_account_type = 'new'
            logger.debug('no suitable storage account found. One will be created.')
示例#6
0
def _validate_vm_create_vnet(namespace, for_scale_set=False):

    vnet = namespace.vnet_name
    subnet = namespace.subnet
    rg = namespace.resource_group_name
    location = namespace.location
    nics = getattr(namespace, 'nics', None)

    if not vnet and not subnet and not nics:  # pylint: disable=too-many-nested-blocks
        # if nothing specified, try to find an existing vnet and subnet in the target resource group
        from azure.mgmt.network import NetworkManagementClient
        from azure.cli.core.commands.client_factory import get_mgmt_service_client
        client = get_mgmt_service_client(
            NetworkManagementClient).virtual_networks

        # find VNET in target resource group that matches the VM's location with a matching subnet
        for vnet_match in (v for v in client.list(rg)
                           if v.location == location and v.subnets):

            # 1 - find a suitable existing vnet/subnet
            result = None
            if not for_scale_set:
                result = next((s for s in vnet_match.subnets
                               if s.name.lower() != 'gatewaysubnet'), None)
            else:
                for s in vnet_match.subnets:
                    if s.name.lower() != 'gatewaysubnet':
                        subnet_mask = s.address_prefix.split('/')[-1]
                        if _subnet_capacity_check(subnet_mask,
                                                  namespace.instance_count):
                            result = s
                            break
            if not result:
                continue
            namespace.subnet = result.name
            namespace.vnet_name = vnet_match.name
            namespace.vnet_type = 'existing'
            return

    if subnet:
        subnet_is_id = is_valid_resource_id(subnet)
        if (subnet_is_id and vnet) or (not subnet_is_id and not vnet):
            raise CLIError("incorrect '--subnet' usage: --subnet SUBNET_ID | "
                           "--subnet SUBNET_NAME --vnet-name VNET_NAME")

        subnet_exists = \
            check_existence(subnet, rg, 'Microsoft.Network', 'subnets', vnet, 'virtualNetworks')

        if subnet_is_id and not subnet_exists:
            raise CLIError("Subnet '{}' does not exist.".format(subnet))
        elif subnet_exists:
            # 2 - user specified existing vnet/subnet
            namespace.vnet_type = 'existing'
            return
    # 3 - create a new vnet/subnet
    namespace.vnet_type = 'new'
示例#7
0
def _validate_vm_vmss_create_vnet(namespace, for_scale_set=False):

    vnet = namespace.vnet_name
    subnet = namespace.subnet
    rg = namespace.resource_group_name
    location = namespace.location
    nics = getattr(namespace, 'nics', None)

    if not vnet and not subnet and not nics:
        logger.debug('no subnet specified. Attempting to find an existing Vnet and subnet...')

        # if nothing specified, try to find an existing vnet and subnet in the target resource group
        client = get_network_client().virtual_networks

        # find VNET in target resource group that matches the VM's location with a matching subnet
        for vnet_match in (v for v in client.list(rg) if v.location == location and v.subnets):

            # 1 - find a suitable existing vnet/subnet
            result = None
            if not for_scale_set:
                result = next((s for s in vnet_match.subnets if s.name.lower() != 'gatewaysubnet'), None)
            else:
                def _check_subnet(s):
                    if s.name.lower() == 'gatewaysubnet':
                        return False
                    subnet_mask = s.address_prefix.split('/')[-1]
                    return _subnet_capacity_check(subnet_mask, namespace.instance_count)

                result = next((s for s in vnet_match.subnets if _check_subnet(s)), None)
            if not result:
                continue
            namespace.subnet = result.name
            namespace.vnet_name = vnet_match.name
            namespace.vnet_type = 'existing'
            logger.debug("existing vnet '%s' and subnet '%s' found", namespace.vnet_name, namespace.subnet)
            return

    if subnet:
        subnet_is_id = is_valid_resource_id(subnet)
        if (subnet_is_id and vnet) or (not subnet_is_id and not vnet):
            raise CLIError("incorrect '--subnet' usage: --subnet SUBNET_ID | "
                           "--subnet SUBNET_NAME --vnet-name VNET_NAME")

        subnet_exists = \
            check_existence(subnet, rg, 'Microsoft.Network', 'subnets', vnet, 'virtualNetworks')

        if subnet_is_id and not subnet_exists:
            raise CLIError("Subnet '{}' does not exist.".format(subnet))
        elif subnet_exists:
            # 2 - user specified existing vnet/subnet
            namespace.vnet_type = 'existing'
            logger.debug("using specified vnet '%s' and subnet '%s'", namespace.vnet_name, namespace.subnet)
            return
    # 3 - create a new vnet/subnet
    namespace.vnet_type = 'new'
    logger.debug('no suitable subnet found. One will be created.')
示例#8
0
def _validate_vm_vmss_create_vnet(namespace, for_scale_set=False):

    vnet = namespace.vnet_name
    subnet = namespace.subnet
    rg = namespace.resource_group_name
    location = namespace.location
    nics = getattr(namespace, 'nics', None)

    if not vnet and not subnet and not nics:
        logger.debug('no subnet specified. Attempting to find an existing Vnet and subnet...')

        # if nothing specified, try to find an existing vnet and subnet in the target resource group
        client = get_network_client().virtual_networks

        # find VNET in target resource group that matches the VM's location with a matching subnet
        for vnet_match in (v for v in client.list(rg) if v.location == location and v.subnets):

            # 1 - find a suitable existing vnet/subnet
            result = None
            if not for_scale_set:
                result = next((s for s in vnet_match.subnets if s.name.lower() != 'gatewaysubnet'), None)
            else:
                def _check_subnet(s):
                    if s.name.lower() == 'gatewaysubnet':
                        return False
                    subnet_mask = s.address_prefix.split('/')[-1]
                    return _subnet_capacity_check(subnet_mask, namespace.instance_count)

                result = next((s for s in vnet_match.subnets if _check_subnet(s)), None)
            if not result:
                continue
            namespace.subnet = result.name
            namespace.vnet_name = vnet_match.name
            namespace.vnet_type = 'existing'
            logger.debug("existing vnet '%s' and subnet '%s' found", namespace.vnet_name, namespace.subnet)
            return

    if subnet:
        subnet_is_id = is_valid_resource_id(subnet)
        if (subnet_is_id and vnet) or (not subnet_is_id and not vnet):
            raise CLIError("incorrect '--subnet' usage: --subnet SUBNET_ID | "
                           "--subnet SUBNET_NAME --vnet-name VNET_NAME")

        subnet_exists = \
            check_existence(subnet, rg, 'Microsoft.Network', 'subnets', vnet, 'virtualNetworks')

        if subnet_is_id and not subnet_exists:
            raise CLIError("Subnet '{}' does not exist.".format(subnet))
        elif subnet_exists:
            # 2 - user specified existing vnet/subnet
            namespace.vnet_type = 'existing'
            logger.debug("using specified vnet '%s' and subnet '%s'", namespace.vnet_name, namespace.subnet)
            return
    # 3 - create a new vnet/subnet
    namespace.vnet_type = 'new'
    logger.debug('no suitable subnet found. One will be created.')
示例#9
0
def _validate_vm_create_public_ip(namespace):
    if namespace.public_ip_address:
        if check_existence(namespace.public_ip_address, namespace.resource_group_name,
                           'Microsoft.Network', 'publicIPAddresses'):
            namespace.public_ip_type = 'existing'
        else:
            namespace.public_ip_type = 'new'
    elif namespace.public_ip_address == '':
        namespace.public_ip_type = None
    elif namespace.public_ip_address is None:
        namespace.public_ip_type = 'new'
示例#10
0
def _validate_vm_create_public_ip(namespace):
    if namespace.public_ip_address:
        if check_existence(namespace.public_ip_address, namespace.resource_group_name,
                           'Microsoft.Network', 'publicIPAddresses'):
            namespace.public_ip_type = 'existing'
        else:
            namespace.public_ip_type = 'new'
    elif namespace.public_ip_address == '':
        namespace.public_ip_type = None
    elif namespace.public_ip_address is None:
        namespace.public_ip_type = 'new'
示例#11
0
def _validate_vmss_create_load_balancer(namespace):
    if namespace.load_balancer:
        if check_existence(namespace.load_balancer, namespace.resource_group_name,
                           'Microsoft.Network', 'loadBalancers'):
            namespace.load_balancer_type = 'existing'
        else:
            namespace.load_balancer_type = 'new'
    elif namespace.load_balancer == '':
        namespace.load_balancer_type = None
    elif namespace.load_balancer is None:
        namespace.load_balancer_type = 'new'
示例#12
0
def _validate_vm_create_nsg(namespace):

    if namespace.nsg:
        if check_existence(namespace.nsg, namespace.resource_group_name,
                           'Microsoft.Network', 'networkSecurityGroups'):
            namespace.nsg_type = 'existing'
        else:
            namespace.nsg_type = 'new'
    elif namespace.nsg == '':
        namespace.nsg_type = None
    elif namespace.nsg is None:
        namespace.nsg_type = 'new'
def _validate_vm_create_vnet(namespace, for_scale_set=False):

    vnet = namespace.vnet_name
    subnet = namespace.subnet
    rg = namespace.resource_group_name
    location = namespace.location
    nics = getattr(namespace, 'nics', None)

    if not vnet and not subnet and not nics:  # pylint: disable=too-many-nested-blocks
        # if nothing specified, try to find an existing vnet and subnet in the target resource group
        from azure.cli.core.profiles import ResourceType
        from azure.cli.core.commands.client_factory import get_mgmt_service_client
        client = get_mgmt_service_client(ResourceType.MGMT_NETWORK).virtual_networks

        # find VNET in target resource group that matches the VM's location with a matching subnet
        for vnet_match in (v for v in client.list(rg) if v.location == location and v.subnets):

            # 1 - find a suitable existing vnet/subnet
            result = None
            if not for_scale_set:
                result = next((s for s in vnet_match.subnets if s.name.lower() != 'gatewaysubnet'),
                              None)
            else:
                for s in vnet_match.subnets:
                    if s.name.lower() != 'gatewaysubnet':
                        subnet_mask = s.address_prefix.split('/')[-1]
                        if _subnet_capacity_check(subnet_mask, namespace.instance_count):
                            result = s
                            break
            if not result:
                continue
            namespace.subnet = result.name
            namespace.vnet_name = vnet_match.name
            namespace.vnet_type = 'existing'
            return

    if subnet:
        subnet_is_id = is_valid_resource_id(subnet)
        if (subnet_is_id and vnet) or (not subnet_is_id and not vnet):
            raise CLIError("incorrect '--subnet' usage: --subnet SUBNET_ID | "
                           "--subnet SUBNET_NAME --vnet-name VNET_NAME")

        subnet_exists = \
            check_existence(subnet, rg, 'Microsoft.Network', 'subnets', vnet, 'virtualNetworks')

        if subnet_is_id and not subnet_exists:
            raise CLIError("Subnet '{}' does not exist.".format(subnet))
        elif subnet_exists:
            # 2 - user specified existing vnet/subnet
            namespace.vnet_type = 'existing'
            return
    # 3 - create a new vnet/subnet
    namespace.vnet_type = 'new'
示例#14
0
def _validate_vm_create_nsg(namespace):

    if namespace.nsg:
        if check_existence(namespace.nsg, namespace.resource_group_name,
                           'Microsoft.Network', 'networkSecurityGroups'):
            namespace.nsg_type = 'existing'
        else:
            namespace.nsg_type = 'new'
    elif namespace.nsg == '':
        namespace.nsg_type = None
    elif namespace.nsg is None:
        namespace.nsg_type = 'new'
示例#15
0
def _validate_vm_create_public_ip(namespace):
    if namespace.public_ip_address:
        if check_existence(namespace.public_ip_address, namespace.resource_group_name,
                           'Microsoft.Network', 'publicIPAddresses'):
            namespace.public_ip_type = 'existing'
            logger.debug("using existing specified public IP '%s'", namespace.public_ip_address)
        else:
            namespace.public_ip_type = 'new'
            logger.debug("specified public IP '%s' not found. It will be created.", namespace.public_ip_address)
    elif namespace.public_ip_address == '':
        namespace.public_ip_type = None
        logger.debug('no public IP address will be used')
    elif namespace.public_ip_address is None:
        namespace.public_ip_type = 'new'
        logger.debug('new public IP address will be created')
示例#16
0
def _validate_vm_create_public_ip(namespace):
    if namespace.public_ip_address:
        if check_existence(namespace.public_ip_address, namespace.resource_group_name,
                           'Microsoft.Network', 'publicIPAddresses'):
            namespace.public_ip_type = 'existing'
            logger.debug("using existing specified public IP '%s'", namespace.public_ip_address)
        else:
            namespace.public_ip_type = 'new'
            logger.debug("specified public IP '%s' not found. It will be created.", namespace.public_ip_address)
    elif namespace.public_ip_address == '':
        namespace.public_ip_type = None
        logger.debug('no public IP address will be used')
    elif namespace.public_ip_address is None:
        namespace.public_ip_type = 'new'
        logger.debug('new public IP address will be created')
示例#17
0
def _validate_vm_create_nsg(namespace):

    if namespace.nsg:
        if check_existence(namespace.nsg, namespace.resource_group_name,
                           'Microsoft.Network', 'networkSecurityGroups'):
            namespace.nsg_type = 'existing'
            logger.debug("using specified NSG '%s'", namespace.nsg)
        else:
            namespace.nsg_type = 'new'
            logger.debug("specified NSG '%s' not found. It will be created.", namespace.nsg)
    elif namespace.nsg == '':
        namespace.nsg_type = None
        logger.debug('no NSG will be used')
    elif namespace.nsg is None:
        namespace.nsg_type = 'new'
        logger.debug('new NSG will be created')
示例#18
0
def _validate_vm_create_availability_set(namespace):
    from azure.cli.core.commands.client_factory import get_subscription_id
    if namespace.availability_set:
        as_id = parse_resource_id(namespace.availability_set)
        name = as_id['name']
        rg = as_id.get('resource_group', namespace.resource_group_name)

        if not check_existence(name, rg, 'Microsoft.Compute', 'availabilitySets'):
            raise CLIError("Availability set '{}' does not exist.".format(name))

        namespace.availability_set = resource_id(
            subscription=get_subscription_id(),
            resource_group=rg,
            namespace='Microsoft.Compute',
            type='availabilitySets',
            name=name)
示例#19
0
def _validate_vm_create_availability_set(namespace):
    from azure.cli.core.commands.client_factory import get_subscription_id
    if namespace.availability_set:
        as_id = parse_resource_id(namespace.availability_set)
        name = as_id['name']
        rg = as_id.get('resource_group', namespace.resource_group_name)

        if not check_existence(name, rg, 'Microsoft.Compute', 'availabilitySets'):
            raise CLIError("Availability set '{}' does not exist.".format(name))

        namespace.availability_set = resource_id(
            subscription=get_subscription_id(),
            resource_group=rg,
            namespace='Microsoft.Compute',
            type='availabilitySets',
            name=name)
示例#20
0
def _validate_vm_create_nsg(namespace):

    if namespace.nsg:
        if check_existence(namespace.nsg, namespace.resource_group_name,
                           'Microsoft.Network', 'networkSecurityGroups'):
            namespace.nsg_type = 'existing'
            logger.debug("using specified NSG '%s'", namespace.nsg)
        else:
            namespace.nsg_type = 'new'
            logger.debug("specified NSG '%s' not found. It will be created.", namespace.nsg)
    elif namespace.nsg == '':
        namespace.nsg_type = None
        logger.debug('no NSG will be used')
    elif namespace.nsg is None:
        namespace.nsg_type = 'new'
        logger.debug('new NSG will be created')
示例#21
0
def _validate_vm_create_vnet(namespace):

    vnet = namespace.vnet_name
    subnet = namespace.subnet
    rg = namespace.resource_group_name
    location = namespace.location
    nics = getattr(namespace, 'nics', None)

    if not vnet and not subnet and not nics:
        # if nothing specified, try to find an existing vnet and subnet in the target resource group
        from azure.mgmt.network import NetworkManagementClient
        from azure.cli.core.commands.client_factory import get_mgmt_service_client
        client = get_mgmt_service_client(NetworkManagementClient).virtual_networks

        # find VNET in target resource group that matches the VM's location and has a subnet
        for vnet_match in (v for v in client.list(rg) if v.location == location and v.subnets):

            # 1 - find a suitable existing vnet/subnet
            subnet_match = next(
                (s.name for s in vnet_match.subnets if s.name.lower() != 'gatewaysubnet'), None
            )
            if not subnet_match:
                continue
            namespace.subnet = subnet_match
            namespace.vnet_name = vnet_match.name
            namespace.vnet_type = 'existing'
            return

    if subnet:
        subnet_is_id = is_valid_resource_id(subnet)
        if (subnet_is_id and vnet) or (not subnet_is_id and not vnet):
            raise CLIError("incorrect '--subnet' usage: --subnet SUBNET_ID | "
                           "--subnet SUBNET_NAME --vnet-name VNET_NAME")

        subnet_exists = \
            check_existence(subnet, rg, 'Microsoft.Network', 'subnets', vnet, 'virtualNetworks')

        if subnet_is_id and not subnet_exists:
            raise CLIError("Subnet '{}' does not exist.".format(subnet))
        elif subnet_exists:
            # 2 - user specified existing vnet/subnet
            namespace.vnet_type = 'existing'
            return

    # 3 - create a new vnet/subnet
    namespace.vnet_type = 'new'
示例#22
0
def _validate_vmss_create_load_balancer(namespace):
    # convert the single_placement_group to boolean for simpler logic beyond
    if namespace.single_placement_group is None:
        namespace.single_placement_group = namespace.instance_count <= 100
    else:
        namespace.single_placement_group = (namespace.single_placement_group == 'true')

    if not namespace.single_placement_group:
        if namespace.load_balancer:
            raise CLIError('--load-balancer is not applicable when --single-placement-group is '
                           'explictly turned off or implictly turned off for 100+ instance count')
        namespace.load_balancer = ''

    if namespace.load_balancer:
        if check_existence(namespace.load_balancer, namespace.resource_group_name,
                           'Microsoft.Network', 'loadBalancers'):
            namespace.load_balancer_type = 'existing'
        else:
            namespace.load_balancer_type = 'new'
    elif namespace.load_balancer == '':
        namespace.load_balancer_type = None
    elif namespace.load_balancer is None:
        namespace.load_balancer_type = 'new'
示例#23
0
def _validate_vmss_create_load_balancer_or_app_gateway(namespace):

    INSTANCE_THRESHOLD = _get_vmss_create_instance_threshold()

    # convert the single_placement_group to boolean for simpler logic beyond
    if namespace.single_placement_group is None:
        namespace.single_placement_group = namespace.instance_count <= INSTANCE_THRESHOLD
    else:
        namespace.single_placement_group = (
            namespace.single_placement_group == 'true')

    if not namespace.single_placement_group and namespace.load_balancer:
        raise CLIError(
            '--load-balancer is not applicable when --single-placement-group is turned off.'
        )

    if namespace.load_balancer and namespace.application_gateway:
        raise CLIError('incorrect usage: --load-balancer NAME_OR_ID | '
                       '--application-gateway NAME_OR_ID')

    if namespace.instance_count > INSTANCE_THRESHOLD and namespace.load_balancer:
        raise CLIError(
            '--load-balancer cannot be used with --instance_count is > {}'.
            format(INSTANCE_THRESHOLD))

    # Resolve the type of balancer (if any) being used
    balancer_type = 'None'
    if namespace.load_balancer is None and namespace.application_gateway is None:
        # use defaulting rules to determine
        balancer_type = 'loadBalancer' if namespace.instance_count <= INSTANCE_THRESHOLD \
            else 'applicationGateway'
        logger.debug("defaulting to '%s' because instance count <= %s",
                     balancer_type, INSTANCE_THRESHOLD)
    elif namespace.load_balancer:
        balancer_type = 'loadBalancer'
    elif namespace.application_gateway:
        balancer_type = 'applicationGateway'

    if balancer_type == 'applicationGateway':

        if namespace.application_gateway:
            client = get_network_client().application_gateways
            try:
                client.get(namespace.resource_group_name,
                           namespace.application_gateway)
                namespace.app_gateway_type = 'existing'
                namespace.backend_pool_name = namespace.backend_pool_name or \
                    _get_default_address_pool(namespace.resource_group_name,
                                              namespace.application_gateway,
                                              'application_gateways')
                logger.debug(
                    "using specified existing application gateway '%s'",
                    namespace.application_gateway)
            except CloudError:
                namespace.app_gateway_type = 'new'
                logger.debug(
                    "application gateway '%s' not found. It will be created.",
                    namespace.application_gateway)
        elif namespace.application_gateway == '':
            namespace.app_gateway_type = None
            logger.debug('no application gateway will be used')
        elif namespace.application_gateway is None:
            namespace.app_gateway_type = 'new'
            logger.debug('new application gateway will be created')

        # AppGateway frontend
        required = []
        if namespace.app_gateway_type == 'new':
            required.append('app_gateway_sku')
            required.append('app_gateway_capacity')
            if namespace.vnet_type != 'new':
                required.append('app_gateway_subnet_address_prefix')
        elif namespace.app_gateway_type == 'existing':
            required.append('backend_pool_name')
        forbidden = ['nat_pool_name', 'load_balancer']
        validate_parameter_set(
            namespace,
            required,
            forbidden,
            description='network balancer: application gateway')

    elif balancer_type == 'loadBalancer':
        # LoadBalancer frontend
        required = []
        forbidden = [
            'app_gateway_subnet_address_prefix', 'application_gateway',
            'app_gateway_sku', 'app_gateway_capacity'
        ]
        validate_parameter_set(namespace,
                               required,
                               forbidden,
                               description='network balancer: load balancer')

        if namespace.load_balancer:
            if check_existence(namespace.load_balancer,
                               namespace.resource_group_name,
                               'Microsoft.Network', 'loadBalancers'):
                namespace.load_balancer_type = 'existing'
                namespace.backend_pool_name = namespace.backend_pool_name or \
                    _get_default_address_pool(namespace.resource_group_name,
                                              namespace.load_balancer,
                                              'load_balancers')
                logger.debug("using specified existing load balancer '%s'",
                             namespace.load_balancer)
            else:
                namespace.load_balancer_type = 'new'
                logger.debug(
                    "load balancer '%s' not found. It will be created.",
                    namespace.load_balancer)
        elif namespace.load_balancer == '':
            namespace.load_balancer_type = None
            logger.debug('no load balancer will be used')
        elif namespace.load_balancer is None:
            namespace.load_balancer_type = 'new'
            logger.debug('new load balancer will be created')
示例#24
0
def _validate_vmss_create_load_balancer_or_app_gateway(namespace):

    INSTANCE_THRESHOLD = _get_vmss_create_instance_threshold()

    # convert the single_placement_group to boolean for simpler logic beyond
    if namespace.single_placement_group is None:
        namespace.single_placement_group = namespace.instance_count <= INSTANCE_THRESHOLD
    else:
        namespace.single_placement_group = (namespace.single_placement_group == 'true')

    if not namespace.single_placement_group and namespace.load_balancer:
        raise CLIError(
            '--load-balancer is not applicable when --single-placement-group is turned off.')

    if namespace.load_balancer and namespace.application_gateway:
        raise CLIError('incorrect usage: --load-balancer NAME_OR_ID | '
                       '--application-gateway NAME_OR_ID')

    if namespace.instance_count > INSTANCE_THRESHOLD and namespace.load_balancer:
        raise CLIError(
            '--load-balancer cannot be used with --instance_count is > {}'.format(
                INSTANCE_THRESHOLD))

    # Resolve the type of balancer (if any) being used
    balancer_type = 'None'
    if namespace.load_balancer is None and namespace.application_gateway is None:
        # use defaulting rules to determine
        balancer_type = 'loadBalancer' if namespace.instance_count <= INSTANCE_THRESHOLD \
            else 'applicationGateway'
    elif namespace.load_balancer:
        balancer_type = 'loadBalancer'
    elif namespace.application_gateway:
        balancer_type = 'applicationGateway'

    if balancer_type == 'applicationGateway':

        if namespace.application_gateway:
            client = get_network_client().application_gateways
            try:
                client.get(namespace.resource_group_name, namespace.application_gateway)
                namespace.app_gateway_type = 'existing'
            except CloudError:
                namespace.app_gateway_type = 'new'
        elif namespace.application_gateway == '':
            namespace.app_gateway_type = None
        elif namespace.application_gateway is None:
            namespace.app_gateway_type = 'new'

        # AppGateway frontend
        required = []
        if namespace.app_gateway_type == 'new' and namespace.vnet_type != 'new':
            required.append('app_gateway_subnet_address_prefix')
        elif namespace.app_gateway_type == 'existing':
            required.append('backend_pool_name')
        forbidden = ['nat_pool_name', 'load_balancer']
        _validate_network_balancer_required_forbidden_parameters(
            namespace, required, forbidden, 'application gateway')

    elif balancer_type == 'loadBalancer':
        # LoadBalancer frontend
        required = []
        forbidden = ['app_gateway_subnet_address_prefix', 'application_gateway']
        _validate_network_balancer_required_forbidden_parameters(
            namespace, required, forbidden, 'load balancer')

        if namespace.load_balancer:
            if check_existence(namespace.load_balancer, namespace.resource_group_name,
                               'Microsoft.Network', 'loadBalancers'):
                namespace.load_balancer_type = 'existing'
            else:
                namespace.load_balancer_type = 'new'
        elif namespace.load_balancer == '':
            namespace.load_balancer_type = None
        elif namespace.load_balancer is None:
            namespace.load_balancer_type = 'new'
示例#25
0
def _validate_vmss_create_load_balancer_or_app_gateway(namespace):

    INSTANCE_THRESHOLD = _get_vmss_create_instance_threshold()

    # convert the single_placement_group to boolean for simpler logic beyond
    if namespace.single_placement_group is None:
        namespace.single_placement_group = namespace.instance_count <= INSTANCE_THRESHOLD
    else:
        namespace.single_placement_group = (namespace.single_placement_group == 'true')

    if not namespace.single_placement_group and namespace.load_balancer:
        raise CLIError(
            '--load-balancer is not applicable when --single-placement-group is turned off.')

    if namespace.load_balancer and namespace.application_gateway:
        raise CLIError('incorrect usage: --load-balancer NAME_OR_ID | '
                       '--application-gateway NAME_OR_ID')

    if namespace.instance_count > INSTANCE_THRESHOLD and namespace.load_balancer:
        raise CLIError(
            '--load-balancer cannot be used with --instance_count is > {}'.format(
                INSTANCE_THRESHOLD))

    # Resolve the type of balancer (if any) being used
    balancer_type = 'None'
    if namespace.load_balancer is None and namespace.application_gateway is None:
        # use defaulting rules to determine
        balancer_type = 'loadBalancer' if namespace.instance_count <= INSTANCE_THRESHOLD \
            else 'applicationGateway'
    elif namespace.load_balancer:
        balancer_type = 'loadBalancer'
    elif namespace.application_gateway:
        balancer_type = 'applicationGateway'

    if balancer_type == 'applicationGateway':

        if namespace.application_gateway:
            if check_existence(namespace.application_gateway, namespace.resource_group_name,
                               'Microsoft.Network', 'applicationGateways'):
                namespace.app_gateway_type = 'existing'
            else:
                namespace.app_gateway_type = 'new'
        elif namespace.application_gateway == '':
            namespace.app_gateway_type = None
        elif namespace.application_gateway is None:
            namespace.app_gateway_type = 'new'

        # AppGateway frontend
        required = []
        if namespace.app_gateway_type == 'new':
            required.append('app_gateway_subnet_address_prefix')
        elif namespace.app_gateway_type == 'existing':
            required.append('backend_pool_name')
        forbidden = ['nat_pool_name', 'load_balancer']
        _validate_network_balancer_required_forbidden_parameters(
            namespace, required, forbidden, 'application gateway')

    elif balancer_type == 'loadBalancer':
        # LoadBalancer frontend
        required = []
        forbidden = ['app_gateway_subnet_address_prefix', 'application_gateway']
        _validate_network_balancer_required_forbidden_parameters(
            namespace, required, forbidden, 'load balancer')

        if namespace.load_balancer:
            if check_existence(namespace.load_balancer, namespace.resource_group_name,
                               'Microsoft.Network', 'loadBalancers'):
                namespace.load_balancer_type = 'existing'
            else:
                namespace.load_balancer_type = 'new'
        elif namespace.load_balancer == '':
            namespace.load_balancer_type = None
        elif namespace.load_balancer is None:
            namespace.load_balancer_type = 'new'
示例#26
0
def _validate_vmss_create_load_balancer_or_app_gateway(namespace):

    INSTANCE_THRESHOLD = _get_vmss_create_instance_threshold()

    # convert the single_placement_group to boolean for simpler logic beyond
    if namespace.single_placement_group is None:
        namespace.single_placement_group = namespace.instance_count <= INSTANCE_THRESHOLD
    else:
        namespace.single_placement_group = (namespace.single_placement_group == 'true')

    if not namespace.single_placement_group and namespace.load_balancer:
        raise CLIError(
            '--load-balancer is not applicable when --single-placement-group is turned off.')

    if namespace.load_balancer and namespace.application_gateway:
        raise CLIError('incorrect usage: --load-balancer NAME_OR_ID | '
                       '--application-gateway NAME_OR_ID')

    if namespace.instance_count > INSTANCE_THRESHOLD and namespace.load_balancer:
        raise CLIError(
            '--load-balancer cannot be used with --instance_count is > {}'.format(
                INSTANCE_THRESHOLD))

    # Resolve the type of balancer (if any) being used
    balancer_type = 'None'
    if namespace.load_balancer is None and namespace.application_gateway is None:
        # use defaulting rules to determine
        balancer_type = 'loadBalancer' if namespace.instance_count <= INSTANCE_THRESHOLD \
            else 'applicationGateway'
        logger.debug("defaulting to '%s' because instance count <= %s", balancer_type, INSTANCE_THRESHOLD)
    elif namespace.load_balancer:
        balancer_type = 'loadBalancer'
    elif namespace.application_gateway:
        balancer_type = 'applicationGateway'

    if balancer_type == 'applicationGateway':

        if namespace.application_gateway:
            client = get_network_client().application_gateways
            try:
                rg = parse_resource_id(namespace.application_gateway).get(
                    'resource_group', namespace.resource_group_name)
                ag_name = parse_resource_id(namespace.application_gateway)['name']
                client.get(rg, ag_name)
                namespace.app_gateway_type = 'existing'
                namespace.backend_pool_name = namespace.backend_pool_name or \
                    _get_default_address_pool(rg, ag_name, 'application_gateways')
                logger.debug("using specified existing application gateway '%s'", namespace.application_gateway)
            except CloudError:
                namespace.app_gateway_type = 'new'
                logger.debug("application gateway '%s' not found. It will be created.", namespace.application_gateway)
        elif namespace.application_gateway == '':
            namespace.app_gateway_type = None
            logger.debug('no application gateway will be used')
        elif namespace.application_gateway is None:
            namespace.app_gateway_type = 'new'
            logger.debug('new application gateway will be created')

        # AppGateway frontend
        required = []
        if namespace.app_gateway_type == 'new':
            required.append('app_gateway_sku')
            required.append('app_gateway_capacity')
            if namespace.vnet_type != 'new':
                required.append('app_gateway_subnet_address_prefix')
        elif namespace.app_gateway_type == 'existing':
            required.append('backend_pool_name')
        forbidden = ['nat_pool_name', 'load_balancer']
        validate_parameter_set(namespace, required, forbidden, description='network balancer: application gateway')

    elif balancer_type == 'loadBalancer':
        # LoadBalancer frontend
        required = []
        forbidden = ['app_gateway_subnet_address_prefix', 'application_gateway', 'app_gateway_sku',
                     'app_gateway_capacity']
        validate_parameter_set(namespace, required, forbidden, description='network balancer: load balancer')

        if namespace.load_balancer:
            rg = parse_resource_id(namespace.load_balancer).get('resource_group', namespace.resource_group_name)
            lb_name = parse_resource_id(namespace.load_balancer)['name']
            if check_existence(lb_name, rg, 'Microsoft.Network', 'loadBalancers'):
                namespace.load_balancer_type = 'existing'
                namespace.backend_pool_name = namespace.backend_pool_name or \
                    _get_default_address_pool(rg, lb_name, 'load_balancers')
                logger.debug("using specified existing load balancer '%s'", namespace.load_balancer)
            else:
                namespace.load_balancer_type = 'new'
                logger.debug("load balancer '%s' not found. It will be created.", namespace.load_balancer)
        elif namespace.load_balancer == '':
            namespace.load_balancer_type = None
            logger.debug('no load balancer will be used')
        elif namespace.load_balancer is None:
            namespace.load_balancer_type = 'new'
            logger.debug('new load balancer will be created')