Esempio n. 1
0
def create_virtual_network(
    cmd,
    client: VirtualNetworksOperations,
    resource_group_name,
    resource_name,
    custom_location,
    location,
    vcenter,
    inventory_item,
    tags=None,
    no_wait=False,
):

    custom_location_id = get_resource_id(
        cmd,
        resource_group_name,
        EXTENDED_LOCATION_NAMESPACE,
        CUSTOM_LOCATION_RESOURCE_TYPE,
        custom_location,
    )

    extended_location = ExtendedLocation(
        type=EXTENDED_LOCATION_TYPE, name=custom_location_id
    )

    inventory_item_id = get_resource_id(
        cmd,
        resource_group_name,
        VMWARE_NAMESPACE,
        VCENTER_RESOURCE_TYPE,
        vcenter,
        INVENTORY_ITEM_TYPE,
        inventory_item,
    )

    virtual_network = VirtualNetwork(
        location=location,
        extended_location=extended_location,
        inventory_item_id=inventory_item_id,
        tags=tags
    )

    return sdk_no_wait(
        no_wait,
        client.begin_create,
        resource_group_name,
        resource_name,
        virtual_network,
    )
Esempio n. 2
0
def enable_guest_agent(
    cmd,
    client: GuestAgentOperations,
    resource_group_name,
    vm_name,
    username,
    password,
):
    """
    Enable guest agent on the given virtual machine.
    """

    vm_client = cf_virtual_machine(cmd.cli_ctx)

    if is_system_identity_enabled(vm_client, resource_group_name, vm_name) is False:
        enable_system_identity(vm_client, resource_group_name, vm_name)

    vm_creds = GuestCredential(username=username, password=password)

    resource_id = get_resource_id(cmd, resource_group_name, VMWARE_NAMESPACE, VIRTUALMACHINE_RESOURCE_TYPE, vm_name)

    guest_agent = GuestAgent(
        id=resource_id,
        type=VIRTUALMACHINE_RESOURCE_TYPE,
        name=DEFAULT_GUEST_AGENT_NAME,
        credentials=vm_creds,
        provisioning_action=GUEST_AGENT_PROVISIONING_ACTION_INSTALL,
    )

    return client.begin_create(resource_group_name, vm_name, DEFAULT_GUEST_AGENT_NAME, guest_agent)
Esempio n. 3
0
def add_nic(
    cmd,
    client: VirtualMachinesOperations,
    resource_group_name,
    vm_name,
    nic_name,
    network,
    nic_type=NICType.vmxnet3.name,
    power_on_boot=PowerOnBootOption.disabled.name,
    no_wait=False,
):
    """
    Add virtual network interface to a virtual machine.
    """

    network_id = get_resource_id(
        cmd,
        resource_group_name,
        VMWARE_NAMESPACE,
        VIRTUALNETWORK_RESOURCE_TYPE,
        network,
    )

    nic_to_add = NetworkInterfaceUpdate(
        name=nic_name,
        network_id=network_id,
        power_on_boot=power_on_boot,
        nic_type=nic_type,
    )

    nics_update = []
    vm = client.get(resource_group_name, vm_name)
    if (
        vm.network_profile is not None and
        vm.network_profile.network_interfaces is not None
    ):
        for nic in vm.network_profile.network_interfaces:
            nic_update = NetworkInterfaceUpdate(
                name=nic.name,
                network_id=nic.network_id,
                power_on_boot=nic.power_on_boot,
                nic_type=nic.nic_type,
                device_key=nic.device_key,
            )
            nics_update.append(nic_update)

    nics_update.append(nic_to_add)
    network_profile = NetworkProfileUpdate(network_interfaces=nics_update)
    vm_update = VirtualMachineUpdate(network_profile=network_profile)

    return sdk_no_wait(
        no_wait, client.begin_update, resource_group_name, vm_name, vm_update
    )
Esempio n. 4
0
def get_network_interfaces(
    cmd, resource_group_name, input_nics
):
    """
    Gets network interfaces from the given input.
    """

    nics = []

    for input_nic in input_nics:
        nic = NetworkInterface(
            power_on_boot=PowerOnBootOption.enabled, nic_type=NICType.vmxnet3
        )

        ip_settings = NicIPSettings(allocation_method=IPAddressAllocationMethod.dynamic)

        for key, value in input_nic.items():
            if key == NETWORK:
                nic.network_id = get_resource_id(
                    cmd,
                    resource_group_name,
                    VMWARE_NAMESPACE,
                    VIRTUALNETWORK_RESOURCE_TYPE,
                    value,
                )
            elif key == NAME_PARAMETER:
                nic.name = value
            elif key == DEVICE_KEY:
                nic.device_key = value
            elif key == NIC_TYPE:
                nic.nic_type = value
            elif key == POWER_ON_BOOT:
                nic.power_on_boot = value
            elif key == ALLOCATION_METHOD:
                ip_settings.allocation_method = value
            elif key == IP_ADDRESS:
                ip_settings.ip_address = value
            elif key == SUBNET_MASK:
                ip_settings.subnet_mask = value
            elif key == GATEWAY:
                ip_settings.gateway = value.split(GATEWAY_SEPERATOR)
            else:
                raise CLIError(
                    'Invalid parameter: {name} specified for nic.'.format(name=key)
                )

        nic.ip_settings = ip_settings
        nics.append(nic)
    return nics
Esempio n. 5
0
def enable_guest_agent(
    cmd,
    client: GuestAgentsOperations,
    resource_group_name,
    vm_name,
    username,
    password,
    https_proxy=None,
    no_wait=False,
):
    """
    Enable guest agent on the given virtual machine.
    """

    vm_client = cf_virtual_machine(cmd.cli_ctx)

    if is_system_identity_enabled(vm_client, resource_group_name, vm_name) is False:
        enable_system_identity(vm_client, resource_group_name, vm_name, no_wait)

    vm_creds = GuestCredential(username=username, password=password)

    resource_id = get_resource_id(cmd, resource_group_name, VMWARE_NAMESPACE, VIRTUALMACHINE_RESOURCE_TYPE, vm_name)

    https_proxy_config = None

    if https_proxy:
        https_proxy_config = HttpProxyConfiguration(https_proxy=https_proxy)

    guest_agent = GuestAgent(
        id=resource_id,
        type=VIRTUALMACHINE_RESOURCE_TYPE,
        name=DEFAULT_GUEST_AGENT_NAME,
        credentials=vm_creds,
        provisioning_action=GUEST_AGENT_PROVISIONING_ACTION_INSTALL,
        http_proxy_config=https_proxy_config,
    )

    return sdk_no_wait(
        no_wait,
        client.begin_create,
        resource_group_name,
        vm_name,
        DEFAULT_GUEST_AGENT_NAME,
        guest_agent
    )
Esempio n. 6
0
def connect_vcenter(
    cmd,
    client: VCentersOperations,
    resource_group_name,
    resource_name,
    fqdn,
    custom_location,
    location,
    username=None,
    password=None,
    port=DEFAULT_VCENTER_PORT,
    tags=None,
    no_wait=False,
):

    if username is None or password is None:
        raise CLIError(
            "Missing vcenter credentials, provide username/password")

    username_creds = VICredential(username=username, password=password)

    custom_location_id = get_resource_id(
        cmd,
        resource_group_name,
        EXTENDED_LOCATION_NAMESPACE,
        CUSTOM_LOCATION_RESOURCE_TYPE,
        custom_location,
    )

    extended_location = ExtendedLocation(type=EXTENDED_LOCATION_TYPE,
                                         name=custom_location_id)

    vcenter = VCenter(
        location=location,
        fqdn=fqdn,
        port=port,
        extended_location=extended_location,
        credentials=username_creds,
    )

    return sdk_no_wait(no_wait, client.begin_create, resource_group_name,
                       resource_name, vcenter)
Esempio n. 7
0
def create_vm(
    cmd,
    client: VirtualMachinesOperations,
    resource_group_name,
    resource_name,
    custom_location,
    location,
    vcenter=None,
    vm_template=None,
    resource_pool=None,
    cluster=None,
    host=None,
    datastore=None,
    inventory_item=None,
    admin_username=None,
    admin_password=None,
    num_CPUs=None,
    num_cores_per_socket=None,
    memory_size=None,
    nics=None,
    disks=None,
    tags=None,
    no_wait=False,
):

    if not any([vm_template, inventory_item, datastore]):
        raise CLIError(
            "either vm_template, inventory_item id or datastore must be provided."
        )

    if vm_template is not None or datastore is not None:
        if not any([resource_pool, cluster, host]):
            raise CLIError(
                "either resource_pool, cluster or host must be provided while creating a VM."
            )

    if len([i for i in [resource_pool, cluster, host] if i is not None]) > 1:
        raise CLIError(
            "at max one of resource_pool, cluster or host can be provided."
        )

    if inventory_item is not None:
        if vm_template is not None:
            raise CLIError(
                "both vm_template and inventory_item id cannot be provided together."
            )

        if any([resource_pool, cluster, host, datastore]):
            raise CLIError(
                "Placement input cannot be provided together with inventory_item."
            )

    hardware_profile = None
    os_profile = None
    network_profile = None
    storage_profile = None

    if num_CPUs is not None or memory_size is not None:
        hardware_profile = HardwareProfile(
            memory_size_mb=memory_size,
            num_cp_us=num_CPUs,
            num_cores_per_socket=num_cores_per_socket,
        )

    if admin_password is not None:
        os_profile = OsProfile(
            admin_username=admin_username, admin_password=admin_password
        )

    if nics is not None:
        network_profile = NetworkProfile(
            network_interfaces=get_network_interfaces(
                cmd, resource_group_name, nics
            )
        )

    if disks is not None:
        storage_profile = StorageProfile(
            disks=get_disks(disks)
        )

    custom_location_id = get_resource_id(
        cmd,
        resource_group_name,
        EXTENDED_LOCATION_NAMESPACE,
        CUSTOM_LOCATION_RESOURCE_TYPE,
        custom_location,
    )

    extended_location = ExtendedLocation(
        type=EXTENDED_LOCATION_TYPE, name=custom_location_id
    )

    inventory_item_id = None
    vcenter_id = None
    vm = None
    vm_template_id = None
    resource_pool_id = None
    cluster_id = None
    host_id = None
    datastore_id = None
    placement_profile = None

    if inventory_item is not None:
        inventory_item_id = get_resource_id(
            cmd,
            resource_group_name,
            VMWARE_NAMESPACE,
            VCENTER_RESOURCE_TYPE,
            vcenter,
            INVENTORY_ITEM_TYPE,
            inventory_item,
        )

        vm = VirtualMachine(
            location=location,
            extended_location=extended_location,
            hardware_profile=hardware_profile,
            os_profile=os_profile,
            network_profile=network_profile,
            storage_profile=storage_profile,
            inventory_item_id=inventory_item_id,
        )
    else:
        if vcenter is None:
            raise CLIError("Missing parameter, provide vcenter name or id.")

        vcenter_id = get_resource_id(
            cmd, resource_group_name, VMWARE_NAMESPACE, VCENTER_RESOURCE_TYPE, vcenter
        )

        if vm_template is not None:
            vm_template_id = get_resource_id(
                cmd,
                resource_group_name,
                VMWARE_NAMESPACE,
                VMTEMPLATE_RESOURCE_TYPE,
                vm_template,
            )

        if resource_pool is not None:
            resource_pool_id = get_resource_id(
                cmd,
                resource_group_name,
                VMWARE_NAMESPACE,
                RESOURCEPOOL_RESOURCE_TYPE,
                resource_pool,
            )

        if cluster is not None:
            cluster_id = get_resource_id(
                cmd,
                resource_group_name,
                VMWARE_NAMESPACE,
                CLUSTER_RESOURCE_TYPE,
                cluster,
            )

        if host is not None:
            host_id = get_resource_id(
                cmd,
                resource_group_name,
                VMWARE_NAMESPACE,
                HOST_RESOURCE_TYPE,
                host,
            )

        if datastore is not None:
            datastore_id = get_resource_id(
                cmd,
                resource_group_name,
                VMWARE_NAMESPACE,
                DATASTORE_RESOURCE_TYPE,
                datastore,
            )

        placement_profile = PlacementProfile(
            resource_pool_id=resource_pool_id,
            cluster_id=cluster_id,
            host_id=host_id,
            datastore_id=datastore_id,
        )

        if vm_template is not None:
            vm = VirtualMachine(
                location=location,
                extended_location=extended_location,
                v_center_id=vcenter_id,
                template_id=vm_template_id,
                placement_profile=placement_profile,
                hardware_profile=hardware_profile,
                os_profile=os_profile,
                network_profile=network_profile,
                storage_profile=storage_profile,
                tags=tags
            )
        else:
            vm = VirtualMachine(
                location=location,
                extended_location=extended_location,
                v_center_id=vcenter_id,
                placement_profile=placement_profile,
                hardware_profile=hardware_profile,
                os_profile=os_profile,
                network_profile=network_profile,
                storage_profile=storage_profile,
                tags=tags
            )

    return sdk_no_wait(
        no_wait, client.begin_create, resource_group_name, resource_name, vm
    )
Esempio n. 8
0
def update_nic(
    cmd,
    client: VirtualMachinesOperations,
    resource_group_name,
    vm_name,
    nic_name=None,
    network=None,
    power_on_boot=None,
    device_key=None,
    no_wait=False,
):
    """
    Update virtual network interface of a virtual machine.
    """

    if nic_name is None and device_key is None:
        raise CLIError(
            "Either nic name or device key must be specified to update the nic."
        )

    network_id = None
    if network is not None:
        network_id = get_resource_id(
            cmd,
            resource_group_name,
            VMWARE_NAMESPACE,
            VIRTUALNETWORK_RESOURCE_TYPE,
            network,
        )

    nics_update = []
    nic_found = False
    vm = client.get(resource_group_name, vm_name)
    if (
        vm.network_profile is not None and
        vm.network_profile.network_interfaces is not None
    ):
        for nic in vm.network_profile.network_interfaces:
            nic_update = NetworkInterfaceUpdate(
                name=nic.name,
                network_id=nic.network_id,
                power_on_boot=nic.power_on_boot,
                nic_type=nic.nic_type,
                device_key=nic.device_key,
            )

            if (nic_name is not None and nic.name == nic_name) or (
                device_key is not None and nic.device_key == device_key
            ):

                # Validate nic name is matching with the expected device key if both were given in
                # the input when name is already assigned to the nic.
                if (
                    nic_name is not None and
                    nic.name is not None and
                    nic.name != nic_name
                ) or (device_key is not None and nic.device_key != device_key):
                    raise CLIError(
                        "Incorrect nic-name and device-key combination, Expected " +
                        "nic-name: " +
                        nic.name +
                        ", device-key: " +
                        str(nic.device_key) +
                        "."
                    )

                nic_found = True
                if nic.name is None and nic_name is not None:
                    nic_update.name = nic_name
                if network_id is not None:
                    nic_update.network_id = network_id
                if power_on_boot is not None:
                    nic_update.power_on_boot = power_on_boot

            nics_update.append(nic_update)

    if not nic_found:
        raise CLIError("Given nic is not present in the virtual machine.")

    network_profile = NetworkProfileUpdate(network_interfaces=nics_update)
    vm_update = VirtualMachineUpdate(network_profile=network_profile)

    return sdk_no_wait(
        no_wait, client.begin_update, resource_group_name, vm_name, vm_update
    )
Esempio n. 9
0
def connect_vcenter(
    cmd,
    client: VCentersOperations,
    resource_group_name,
    resource_name,
    custom_location,
    location,
    fqdn=None,
    username=None,
    password=None,
    port=None,
    tags=None,
    no_wait=False,
):

    creds_ok = all(inp is not None for inp in [fqdn, username, password])
    while not creds_ok:
        creds = {
            'fqdn': fqdn,
            'username': username,
            'password': password,
        }
        if fqdn is None:
            print('Please provide vcenter FQDN or IP address: ', end='')
            creds['fqdn'] = input()
        if username is None:
            print('Please provide vcenter username: '******'')
            creds['username'] = input()
        if password is None:
            creds['password'] = getpass('Please provide vcenter password: '******'Confirm vcenter details? [Y/n]: ', end='')
        res = input().lower()
        if res in ['y', '']:
            for cred_type, cred_val in creds.items():
                if not cred_val:
                    print(f'{cred_type} cannot be empty. Please try again.')
                    continue
            fqdn, username, password = creds['fqdn'], creds['username'], creds['password']
            creds_ok = True
        elif res != 'n':
            print('Please type y/n or leave empty.')

    username_creds = VICredential(username=username, password=password)

    custom_location_id = get_resource_id(
        cmd,
        resource_group_name,
        EXTENDED_LOCATION_NAMESPACE,
        CUSTOM_LOCATION_RESOURCE_TYPE,
        custom_location,
    )

    extended_location = ExtendedLocation(
        type=EXTENDED_LOCATION_TYPE, name=custom_location_id
    )

    vcenter = VCenter(
        location=location,
        fqdn=fqdn,
        port=port,
        extended_location=extended_location,
        credentials=username_creds,
        tags=tags
    )

    return sdk_no_wait(
        no_wait, client.begin_create, resource_group_name, resource_name, vcenter
    )
Esempio n. 10
0
def create_vm(
    cmd,
    client: VirtualMachinesOperations,
    resource_group_name,
    resource_name,
    custom_location,
    location,
    vcenter=None,
    resource_pool=None,
    vm_template=None,
    inventory_item=None,
    admin_username=None,
    admin_password=None,
    num_CPUs=None,
    num_cores_per_socket=None,
    memory_size=None,
    nics=None,
    disks=None,
    tags=None,
    no_wait=False,
):

    if vm_template is None and inventory_item is None:
        raise CLIError(
            "Missing parameter, provide either vm_template or inventory_item id."
        )

    hardware_profile = None
    os_profile = None
    network_profile = None
    storage_profile = None

    if num_CPUs is not None or memory_size is not None:
        hardware_profile = HardwareProfile(
            memory_size_mb=memory_size,
            num_cp_us=num_CPUs,
            num_cores_per_socket=num_cores_per_socket,
        )

    if admin_password is not None:
        os_profile = OsProfile(admin_username=admin_username,
                               admin_password=admin_password)

    if nics is not None:
        network_profile = NetworkProfile(
            network_interfaces=get_network_interfaces(cmd, resource_group_name,
                                                      nics))

    if disks is not None:
        storage_profile = StorageProfile(disks=get_disks(disks))

    custom_location_id = get_resource_id(
        cmd,
        resource_group_name,
        EXTENDED_LOCATION_NAMESPACE,
        CUSTOM_LOCATION_RESOURCE_TYPE,
        custom_location,
    )

    extended_location = ExtendedLocation(type=EXTENDED_LOCATION_TYPE,
                                         name=custom_location_id)

    inventory_item_id = None
    vcenter_id = None
    vm_template_id = None
    resource_pool_id = None

    if inventory_item is not None:
        inventory_item_id = get_resource_id(
            cmd,
            resource_group_name,
            VMWARE_NAMESPACE,
            VCENTER_RESOURCE_TYPE,
            vcenter,
            INVENTORY_ITEM_TYPE,
            inventory_item,
        )
    else:
        if vcenter is None:
            raise CLIError("Missing parameter, provide vcenter name or id.")

        vcenter_id = get_resource_id(cmd, resource_group_name,
                                     VMWARE_NAMESPACE, VCENTER_RESOURCE_TYPE,
                                     vcenter)

    if vm_template is not None:
        vm_template_id = get_resource_id(
            cmd,
            resource_group_name,
            VMWARE_NAMESPACE,
            VMTEMPLATE_RESOURCE_TYPE,
            vm_template,
        )

    if resource_pool is not None:
        resource_pool_id = get_resource_id(
            cmd,
            resource_group_name,
            VMWARE_NAMESPACE,
            RESOURCEPOOL_RESOURCE_TYPE,
            resource_pool,
        )

    if inventory_item_id is not None:
        vm = VirtualMachine(
            location=location,
            extended_location=extended_location,
            hardware_profile=hardware_profile,
            os_profile=os_profile,
            network_profile=network_profile,
            storage_profile=storage_profile,
            inventory_item_id=inventory_item_id,
        )
    else:
        vm = VirtualMachine(
            location=location,
            extended_location=extended_location,
            v_center_id=vcenter_id,
            resource_pool_id=resource_pool_id,
            template_id=vm_template_id,
            hardware_profile=hardware_profile,
            os_profile=os_profile,
            network_profile=network_profile,
            storage_profile=storage_profile,
        )

    return sdk_no_wait(no_wait, client.begin_create, resource_group_name,
                       resource_name, vm)
Esempio n. 11
0
def create_vm_template(
    cmd,
    client: VirtualMachineTemplatesOperations,
    resource_group_name,
    resource_name,
    custom_location,
    location,
    vcenter=None,
    mo_ref_id=None,
    inventory_item=None,
    tags=None,
    no_wait=True,
):

    if mo_ref_id is None and inventory_item is None:
        raise CLIError(
            "Missing parameter, provide either mo_ref_id or inventory_item id."
        )

    custom_location_id = get_resource_id(
        cmd,
        resource_group_name,
        EXTENDED_LOCATION_NAMESPACE,
        CUSTOM_LOCATION_RESOURCE_TYPE,
        custom_location,
    )

    extended_location = ExtendedLocation(type=EXTENDED_LOCATION_TYPE,
                                         name=custom_location_id)

    inventory_item_id = None
    vcenter_id = None

    if inventory_item is not None:
        inventory_item_id = get_resource_id(
            cmd,
            resource_group_name,
            VMWARE_NAMESPACE,
            VCENTER_RESOURCE_TYPE,
            vcenter,
            INVENTORY_ITEM_TYPE,
            inventory_item,
        )
    else:
        if vcenter is None:
            raise CLIError("Missing parameter, provide vcenter name or id.")

        vcenter_id = get_resource_id(cmd, resource_group_name,
                                     VMWARE_NAMESPACE, VCENTER_RESOURCE_TYPE,
                                     vcenter)

    if inventory_item_id is not None:
        vm_template = VirtualMachineTemplate(
            location=location,
            extended_location=extended_location,
            inventory_item_id=inventory_item_id,
        )
    else:
        vm_template = VirtualMachineTemplate(
            location=location,
            extended_location=extended_location,
            v_center_id=vcenter_id,
            mo_ref_id=mo_ref_id,
        )

    return sdk_no_wait(no_wait, client.begin_create, resource_group_name,
                       resource_name, vm_template)