def process_gallery_image_version_namespace()

in src/azure-cli/azure/cli/command_modules/vm/_validators.py [0:0]


def process_gallery_image_version_namespace(cmd, namespace):
    from azure.cli.core.azclierror import InvalidArgumentValueError

    if namespace.target_regions:
        if hasattr(namespace, 'target_region_encryption') and namespace.target_region_encryption:
            if len(namespace.target_regions) != len(namespace.target_region_encryption):
                raise InvalidArgumentValueError(
                    'usage error: Length of --target-region-encryption should be as same as length of target regions')

        if hasattr(namespace, 'target_region_cvm_encryption') and namespace.target_region_cvm_encryption:
            if len(namespace.target_regions) != len(namespace.target_region_cvm_encryption):
                raise InvalidArgumentValueError(
                    'usage error: Length of --target_region_cvm_encryption should be as same as '
                    'length of target regions')

        storage_account_types_list = [item.lower() for item in ['Standard_LRS', 'Standard_ZRS', 'Premium_LRS']]
        storage_account_types_str = ", ".join(storage_account_types_list)

        regions_info = []
        for i, t in enumerate(namespace.target_regions):
            parts = t.split('=', 2)
            replica_count = None
            storage_account_type = None

            # Region specified, but also replica count or storage account type
            if len(parts) == 2:
                try:
                    replica_count = int(parts[1])
                except ValueError:
                    storage_account_type = parts[1]
                    if parts[1].lower() not in storage_account_types_list:
                        raise ArgumentUsageError(
                            "usage error: {} is an invalid target region argument. "
                            "The second part is neither an integer replica count or a valid storage account type. "
                            "Storage account types must be one of {}.".format(t, storage_account_types_str))

            # Region specified, but also replica count and storage account type
            elif len(parts) == 3:
                try:
                    replica_count = int(parts[1])   # raises ValueError if this is not a replica count, try other order.
                    storage_account_type = parts[2]
                    if storage_account_type not in storage_account_types_list:
                        raise ArgumentUsageError(
                            "usage error: {} is an invalid target region argument. "
                            "The third part is not a valid storage account type. "
                            "Storage account types must be one of {}.".format(t, storage_account_types_str))
                except ValueError:
                    raise ArgumentUsageError(
                        "usage error: {} is an invalid target region argument. "
                        "The second part must be a valid integer replica count.".format(t))

            # Parse target region encryption, example: ['des1,0,des2,1,des3', 'null', 'des4']
            encryption = None
            os_disk_image = None
            data_disk_images = None
            if hasattr(namespace, 'target_region_encryption') and namespace.target_region_encryption:
                terms = namespace.target_region_encryption[i].split(',')
                # OS disk
                os_disk_image = terms[0]
                if os_disk_image == 'null':
                    os_disk_image = None
                else:
                    des_id = _disk_encryption_set_format(cmd, namespace, os_disk_image)
                    os_disk_image = {"disk_encryption_set_id": des_id}
                # Data disk
                if len(terms) > 1:
                    data_disk_images = terms[1:]
                    data_disk_images_len = len(data_disk_images)
                    if data_disk_images_len % 2 != 0:
                        raise ArgumentUsageError(
                            'usage error: LUN and disk encryption set for data disk should appear in pair in '
                            '--target-region-encryption. Example: osdes,0,datades0,1,datades1')
                    data_disk_image_encryption_list = []
                    for j in range(int(data_disk_images_len / 2)):
                        lun = data_disk_images[j * 2]
                        des_id = data_disk_images[j * 2 + 1]
                        des_id = _disk_encryption_set_format(cmd, namespace, des_id)
                        try:
                            data_disk_image_encryption_list.append({"lun": int(lun), "disk_encryption_set_id": des_id})
                        except:
                            raise ArgumentUsageError(
                                "usage error: {} is an invalid target region encryption argument. "
                                "LUN and disk encryption set for data disk should appear in pair in "
                                "--target-region-encryption. Example: osdes,0,datades0,1,datades1"
                            )

                    data_disk_images = data_disk_image_encryption_list

            if hasattr(namespace, 'target_region_cvm_encryption') and namespace.target_region_cvm_encryption:
                cvm_terms = namespace.target_region_cvm_encryption[i].split(',')
                if not cvm_terms or len(cvm_terms) != 2:
                    raise ArgumentUsageError(
                        "usage error: {} is an invalid target region cvm encryption. "
                        "Both os_cvm_encryption_type and os_cvm_des parameters are required.".format(cvm_terms))

                storage_profile_types = [
                    "EncryptedVMGuestStateOnlyWithPmk",
                    "EncryptedWithPmk",
                    "EncryptedWithCmk",
                    "NonPersistedTPM"
                ]
                storage_profile_types_str = ", ".join(storage_profile_types)
                if cvm_terms[0] not in storage_profile_types:
                    raise ArgumentUsageError(
                        "usage error: {} is an invalid os_cvm_encryption_type. "
                        "The valid values for os_cvm_encryption_type are {}".format(
                            cvm_terms, storage_profile_types_str))
                cvm_des_id = None
                if cvm_terms[1]:
                    cvm_des_id = _disk_encryption_set_format(cmd, namespace, cvm_terms[1])
                security_profile = {"confidential_vm_encryption_type": cvm_terms[0],
                                    "secure_vm_disk_encryption_set_id": cvm_des_id}
                if os_disk_image:
                    os_disk_image["security_profile"] = security_profile
                else:
                    os_disk_image = {"security_profile": security_profile}

            if os_disk_image or data_disk_images:
                encryption = {"os_disk_image": os_disk_image, "data_disk_images": data_disk_images}

            # At least the region is specified
            if len(parts) >= 1:
                regions_info.append({"name": parts[0],
                                     "regional_replica_count": replica_count,
                                     "storage_account_type": storage_account_type,
                                     "encryption": encryption})

        namespace.target_regions = regions_info

    if hasattr(namespace, 'target_edge_zones') and namespace.target_edge_zones:
        if len(namespace.target_edge_zones) == 1 and namespace.target_edge_zones[0].lower() == 'none':
            namespace.target_edge_zones = []
            return
        if hasattr(namespace, 'target_zone_encryption') and namespace.target_zone_encryption:
            if len(namespace.target_edge_zones) != len(namespace.target_zone_encryption):
                raise InvalidArgumentValueError(
                    'usage error: Length of --target-edge-zone-encryption '
                    'should be as same as length of --target-edge-zones')

        storage_account_types_list = [item.lower() for item in
                                      ['Standard_LRS', 'Standard_ZRS', 'Premium_LRS', 'StandardSSD_LRS']]
        storage_account_types_str = ", ".join(storage_account_types_list)

        edge_zone_info = []
        for i, t in enumerate(namespace.target_edge_zones):
            parts = t.split('=', 3)
            # At least the region and edge zone are specified
            if len(parts) < 2:
                continue

            region = parts[0]
            edge_zone = parts[1]
            replica_count = None
            storage_account_type = None

            # Both "region" and "edge zone" are specified,
            # but only one of "replica count" and "storage account type" is specified
            if len(parts) == 3:
                try:
                    replica_count = int(parts[2])
                except ValueError:
                    storage_account_type = parts[2]
                    if parts[2].lower() not in storage_account_types_list:
                        raise ArgumentUsageError(
                            "usage error: {} is an invalid target edge zone argument. "
                            "The third part is neither an integer replica count or a valid storage account type. "
                            "Storage account types must be one of {}.".format(t, storage_account_types_str))

            # Not only "region" and "edge zone" are specified,
            # but also "replica count" and "storage account type" are specified
            elif len(parts) == 4:
                try:
                    replica_count = int(parts[2])  # raises ValueError if this is not a replica count, try other order.
                    storage_account_type = parts[3]
                    if storage_account_type not in storage_account_types_list:
                        raise ArgumentUsageError(
                            "usage error: {} is an invalid target edge zone argument. "
                            "The forth part is not a valid storage account type. "
                            "Storage account types must be one of {}.".format(t, storage_account_types_str))
                except ValueError:
                    raise ArgumentUsageError(
                        "usage error: {} is an invalid target edge zone argument. "
                        "The third part must be a valid integer replica count.".format(t))

            # Parse target edge zone encryption,
            # example: ['microsoftlosangeles1', 'des1, 0, des2, 1, des3', 'null', 'des4']
            encryption = None
            os_disk_image = None
            data_disk_images = None
            if hasattr(namespace, 'target_zone_encryption') and namespace.target_zone_encryption:
                terms = namespace.target_zone_encryption[i].split(',')
                if len(terms) < 2:
                    break
                # OS disk
                os_disk_image = terms[1]
                if os_disk_image == 'null':
                    os_disk_image = None
                else:
                    des_id = _disk_encryption_set_format(cmd, namespace, os_disk_image)
                    os_disk_image = {"disk_encryption_set_id": des_id}
                # Data disk
                if len(terms) > 2:
                    data_disk_images = terms[2:]
                    data_disk_images_len = len(data_disk_images)
                    if data_disk_images_len % 2 != 0:
                        raise ArgumentUsageError(
                            'usage error: LUN and disk encryption set for data disk should appear in pair in '
                            '--target-edge-zone-encryption. Example: 1,osdes,0,datades0,1,datades1')
                    data_disk_image_encryption_list = []
                    for j in range(int(data_disk_images_len / 2)):
                        lun = data_disk_images[j * 2]
                        des_id = data_disk_images[j * 2 + 1]
                        des_id = _disk_encryption_set_format(cmd, namespace, des_id)
                        try:
                            data_disk_image_encryption_list.append({"lun": int(lun), "disk_encryption_set_id": des_id})
                        except:
                            raise ArgumentUsageError(
                                "usage error: {} is an invalid target edge zone encryption. "
                                "LUN and disk encryption set for data disk should appear in pair in "
                                "--target-edge-zone-encryption. Example: 1,osdes,0,datades0,1,datades1"
                            )
                    data_disk_images = data_disk_image_encryption_list

            if os_disk_image or data_disk_images:
                encryption = {"os_disk_image": os_disk_image, "data_disk_images": data_disk_images}

            extended_location = {"name": edge_zone, "type": "EdgeZone"}

            edge_zone_info.append(
                {
                    "name": region,
                    "extended_location_replica_count": replica_count,
                    "extended_location": extended_location,
                    "storage_account_type": storage_account_type,
                    "encryption": encryption
                }
            )

        namespace.target_edge_zones = edge_zone_info