Exemplo n.º 1
0
def _use_gallery_image(cli_ctx, namespace):
    """ Retrieve gallery image from lab and update namespace """
    from azure.mgmt.devtestlabs.models import GalleryImageReference
    gallery_image_operation = get_devtestlabs_management_client(
        cli_ctx, None).gallery_images
    odata_filter = ODATA_NAME_FILTER.format(namespace.image)
    gallery_images = list(
        gallery_image_operation.list(namespace.resource_group_name,
                                     namespace.lab_name,
                                     filter=odata_filter))

    if not gallery_images:
        err = "Unable to find image name '{}' in the '{}' lab Gallery.".format(
            namespace.image, namespace.lab_name)
        raise CLIError(err)
    if len(gallery_images) > 1:
        err = "Found more than 1 image with name '{}'. Please pick one from {}"
        raise CLIError(
            err.format(namespace.image, [x.name for x in gallery_images]))
    namespace.gallery_image_reference = \
        GalleryImageReference(offer=gallery_images[0].image_reference.offer,
                              publisher=gallery_images[0].image_reference.publisher,
                              os_type=gallery_images[0].image_reference.os_type,
                              sku=gallery_images[0].image_reference.sku,
                              version=gallery_images[0].image_reference.version)
    namespace.os_type = gallery_images[0].image_reference.os_type
Exemplo n.º 2
0
def _use_custom_image(cli_ctx, namespace):
    """ Retrieve custom image from lab and update namespace """
    from msrestazure.tools import is_valid_resource_id
    if is_valid_resource_id(namespace.image):
        namespace.custom_image_id = namespace.image
    else:
        custom_image_operation = get_devtestlabs_management_client(cli_ctx, None).custom_images
        odata_filter = ODATA_NAME_FILTER.format(namespace.image)
        custom_images = list(custom_image_operation.list(namespace.resource_group_name,
                                                         namespace.lab_name,
                                                         filter=odata_filter))
        if not custom_images:
            err = "Unable to find custom image name '{}' in the '{}' lab.".format(namespace.image, namespace.lab_name)
            raise CLIError(err)
        elif len(custom_images) > 1:
            err = "Found more than 1 image with name '{}'. Please pick one from {}"
            raise CLIError(err.format(namespace.image, [x.name for x in custom_images]))
        else:
            namespace.custom_image_id = custom_images[0].id

            if custom_images[0].vm is not None:
                if custom_images[0].vm.windows_os_info is not None:
                    os_type = "Windows"
                else:
                    os_type = "Linux"
            elif custom_images[0].vhd is not None:
                os_type = custom_images[0].vhd.os_type
            else:
                raise CLIError("OS type cannot be inferred from the custom image {}".format(custom_images[0].id))

            namespace.os_type = os_type
Exemplo n.º 3
0
def _use_custom_image(cli_ctx, namespace):
    """ Retrieve custom image from lab and update namespace """
    from msrestazure.tools import is_valid_resource_id
    if is_valid_resource_id(namespace.image):
        namespace.custom_image_id = namespace.image
    else:
        custom_image_operation = get_devtestlabs_management_client(cli_ctx, None).custom_images
        odata_filter = ODATA_NAME_FILTER.format(namespace.image)
        custom_images = list(custom_image_operation.list(namespace.resource_group_name,
                                                         namespace.lab_name,
                                                         filter=odata_filter))
        if not custom_images:
            err = "Unable to find custom image name '{}' in the '{}' lab.".format(namespace.image, namespace.lab_name)
            raise CLIError(err)
        elif len(custom_images) > 1:
            err = "Found more than 1 image with name '{}'. Please pick one from {}"
            raise CLIError(err.format(namespace.image, [x.name for x in custom_images]))
        else:
            namespace.custom_image_id = custom_images[0].id

            if custom_images[0].vm is not None:
                if custom_images[0].vm.windows_os_info is not None:
                    os_type = "Windows"
                else:
                    os_type = "Linux"
            elif custom_images[0].vhd is not None:
                os_type = custom_images[0].vhd.os_type
            else:
                raise CLIError("OS type cannot be inferred from the custom image {}".format(custom_images[0].id))

            namespace.os_type = os_type
Exemplo n.º 4
0
def _validate_location(cli_ctx, namespace):
    """
    Selects the default location of the lab when location is not provided.
    """
    if namespace.location is None:
        lab_operation = get_devtestlabs_management_client(cli_ctx, None).labs
        lab = lab_operation.get(namespace.resource_group_name, namespace.lab_name)
        namespace.location = lab.location
Exemplo n.º 5
0
def _validate_location(cli_ctx, namespace):
    """
    Selects the default location of the lab when location is not provided.
    """
    if namespace.location is None:
        lab_operation = get_devtestlabs_management_client(cli_ctx, None).labs
        lab = lab_operation.get(namespace.resource_group_name, namespace.lab_name)
        namespace.location = lab.location
Exemplo n.º 6
0
def _get_formula(cli_ctx, namespace):
    """ Retrieve formula image from lab """
    formula_operation = get_devtestlabs_management_client(cli_ctx, None).formulas
    odata_filter = ODATA_NAME_FILTER.format(namespace.formula)
    formula_images = list(formula_operation.list(namespace.resource_group_name,
                                                 namespace.lab_name,
                                                 filter=odata_filter))
    if not formula_images:
        err = "Unable to find formula name '{}' in the '{}' lab.".format(namespace.formula, namespace.lab_name)
        raise CLIError(err)
    elif len(formula_images) > 1:
        err = "Found more than 1 formula with name '{}'. Please pick one from {}"
        raise CLIError(err.format(namespace.formula, [x.name for x in formula_images]))
    return formula_images[0]
Exemplo n.º 7
0
def _get_formula(cli_ctx, namespace):
    """ Retrieve formula image from lab """
    formula_operation = get_devtestlabs_management_client(cli_ctx, None).formulas
    odata_filter = ODATA_NAME_FILTER.format(namespace.formula)
    formula_images = list(formula_operation.list(namespace.resource_group_name,
                                                 namespace.lab_name,
                                                 filter=odata_filter))
    if not formula_images:
        err = "Unable to find formula name '{}' in the '{}' lab.".format(namespace.formula, namespace.lab_name)
        raise CLIError(err)
    elif len(formula_images) > 1:
        err = "Found more than 1 formula with name '{}'. Please pick one from {}"
        raise CLIError(err.format(namespace.formula, [x.name for x in formula_images]))
    return formula_images[0]
Exemplo n.º 8
0
def _validate_network_parameters(cli_ctx, namespace, formula=None):
    """ Updates namespace for virtual network and subnet parameters """
    vnet_operation = get_devtestlabs_management_client(cli_ctx,
                                                       None).virtual_networks
    lab_vnet = None

    if formula and formula.formula_content:
        if formula.formula_content.lab_virtual_network_id:
            namespace.vnet_name = \
                namespace.vnet_name or \
                formula.formula_content.lab_virtual_network_id.split('/')[-1]
        if formula.formula_content.lab_subnet_name:
            namespace.subnet = \
                namespace.subnet or \
                formula.formula_content.lab_subnet_name
            namespace.disallow_public_ip_address = formula.formula_content.disallow_public_ip_address

    # User did not provide vnet and not selected from formula
    if not namespace.vnet_name:
        lab_vnets = list(
            vnet_operation.list(namespace.resource_group_name,
                                namespace.lab_name,
                                top=1))
        if not lab_vnets:
            err = "Unable to find any virtual network in the '{}' lab.".format(
                namespace.lab_name)
            raise CLIError(err)
        else:
            lab_vnet = lab_vnets[0]
            namespace.vnet_name = lab_vnet.name
            namespace.lab_virtual_network_id = lab_vnet.id
    # User did provide vnet or has been selected from formula
    else:
        lab_vnet = vnet_operation.get(namespace.resource_group_name,
                                      namespace.lab_name, namespace.vnet_name)
        namespace.lab_virtual_network_id = lab_vnet.id

    # User did not provide subnet and not selected from formula
    if not namespace.subnet:
        namespace.subnet = lab_vnet.subnet_overrides[0].lab_subnet_name

    _validate_ip_configuration(namespace, lab_vnet)
Exemplo n.º 9
0
def _use_gallery_image(cli_ctx, namespace):
    """ Retrieve gallery image from lab and update namespace """
    from azure.mgmt.devtestlabs.models import GalleryImageReference
    gallery_image_operation = get_devtestlabs_management_client(cli_ctx, None).gallery_images
    odata_filter = ODATA_NAME_FILTER.format(namespace.image)
    gallery_images = list(gallery_image_operation.list(namespace.resource_group_name,
                                                       namespace.lab_name,
                                                       filter=odata_filter))

    if not gallery_images:
        err = "Unable to find image name '{}' in the '{}' lab Gallery.".format(namespace.image,
                                                                               namespace.lab_name)
        raise CLIError(err)
    elif len(gallery_images) > 1:
        err = "Found more than 1 image with name '{}'. Please pick one from {}"
        raise CLIError(err.format(namespace.image, [x.name for x in gallery_images]))
    else:
        namespace.gallery_image_reference = \
            GalleryImageReference(offer=gallery_images[0].image_reference.offer,
                                  publisher=gallery_images[0].image_reference.publisher,
                                  os_type=gallery_images[0].image_reference.os_type,
                                  sku=gallery_images[0].image_reference.sku,
                                  version=gallery_images[0].image_reference.version)
        namespace.os_type = gallery_images[0].image_reference.os_type
Exemplo n.º 10
0
def _validate_network_parameters(cli_ctx, namespace, formula=None):
    """ Updates namespace for virtual network and subnet parameters """
    vnet_operation = get_devtestlabs_management_client(cli_ctx, None).virtual_networks
    lab_vnet = None

    if formula and formula.formula_content:
        if formula.formula_content.lab_virtual_network_id:
            namespace.vnet_name = \
                namespace.vnet_name or \
                formula.formula_content.lab_virtual_network_id.split('/')[-1]
        if formula.formula_content.lab_subnet_name:
            namespace.subnet = \
                namespace.subnet or \
                formula.formula_content.lab_subnet_name
            namespace.disallow_public_ip_address = formula.formula_content.disallow_public_ip_address

    # User did not provide vnet and not selected from formula
    if not namespace.vnet_name:
        lab_vnets = list(vnet_operation.list(namespace.resource_group_name, namespace.lab_name, top=1))
        if not lab_vnets:
            err = "Unable to find any virtual network in the '{}' lab.".format(namespace.lab_name)
            raise CLIError(err)
        else:
            lab_vnet = lab_vnets[0]
            namespace.vnet_name = lab_vnet.name
            namespace.lab_virtual_network_id = lab_vnet.id
    # User did provide vnet or has been selected from formula
    else:
        lab_vnet = vnet_operation.get(namespace.resource_group_name, namespace.lab_name, namespace.vnet_name)
        namespace.lab_virtual_network_id = lab_vnet.id

    # User did not provide subnet and not selected from formula
    if not namespace.subnet:
        namespace.subnet = lab_vnet.subnet_overrides[0].lab_subnet_name

    _validate_ip_configuration(namespace, lab_vnet)