Example #1
0
def get_distributed_network_backing(stub_config, dv_portgroup_name,
                                    datacenter_name):
    """
    Gets a distributed portgroup network backing for a given Datacenter
    Note: The method assumes that there is only one distributed portgroup
    and datacenter with the mentioned names.
    """
    datacenter = datacenter_helper.get_datacenter(stub_config, datacenter_name)
    if not datacenter:
        print("Datacenter '{}' not found".format(datacenter_name))
        return None

    network_svc = Network(stub_config)
    filter = Network.FilterSpec(datacenters=set([datacenter]),
                                names=set([dv_portgroup_name]),
                                types=set([Network.Type.DISTRIBUTED_PORTGROUP
                                           ]))
    network_summaries = network_svc.list(filter=filter)

    network = None
    if len(network_summaries) > 0:
        network = network_summaries[0].network
        print("Selecting Distributed Portgroup Network '{}' ({})".format(
            dv_portgroup_name, network))
        return network
    else:
        print("Distributed Portgroup Network not found in Datacenter '{}'".
              format(datacenter_name))
        return None
def get_network_backing(client,
                        porggroup_name,
                        datacenter_name,
                        portgroup_type):
    """
    Gets a standard portgroup network backing for a given Datacenter
    Note: The method assumes that there is only one standard portgroup
    and datacenter with the mentioned names.
    """
    datacenter = datacenter_helper.get_datacenter(client, datacenter_name)
    if not datacenter:
        print("Datacenter '{}' not found".format(datacenter_name))
        return None

    filter = Network.FilterSpec(datacenters=set([datacenter]),
                                names=set([porggroup_name]),
                                types=set([portgroup_type]))
    network_summaries = client.vcenter.Network.list(filter=filter)

    if len(network_summaries) > 0:
        network = network_summaries[0].network
        print("Selecting {} Portgroup Network '{}' ({})".
              format(portgroup_type, porggroup_name, network))
        return network
    else:
        print("Portgroup Network not found in Datacenter '{}'".
              format(datacenter_name))
        return None
    def deploy_ovf_template(self):

        # Build the deployment target with resource pool ID and folder ID
        rp_filter_spec = ResourcePool.FilterSpec(names=set([self.resourcepoolname]))
        resource_pool_summaries = self.client.vcenter.ResourcePool.list(rp_filter_spec)
        if not resource_pool_summaries:
            raise ValueError("Resource pool with name '{}' not found".
                             format(self.resourcepoolname))
        resource_pool_id = resource_pool_summaries[0].resource_pool
        print('Resource pool ID: {}'.format(resource_pool_id))

        folder_filter_spec = Folder.FilterSpec(names=set([self.foldername]))
        folder_summaries = self.client.vcenter.Folder.list(folder_filter_spec)
        if not folder_summaries:
            raise ValueError("Folder with name '{}' not found".
                             format(self.foldername))
        folder_id = folder_summaries[0].folder
        print('Folder ID: {}'.format(folder_id))

        deployment_target = LibraryItem.DeploymentTarget(
            resource_pool_id=resource_pool_id,
            folder_id=folder_id
        )

        # Find the library item
        find_spec = Item.FindSpec(name=self.lib_item_name)
        lib_item_ids = self.client.content.library.Item.find(find_spec)
        if not lib_item_ids:
            raise ValueError("Library item with name '{}' not found".
                             format(self.lib_item_name))
        lib_item_id = lib_item_ids[0]
        print('Library item ID: {}'.format(lib_item_id))
        ovf_summary = self.client.vcenter.ovf.LibraryItem.filter(
            ovf_library_item_id=lib_item_id,
            target=deployment_target)
        print('Found an OVF template: {} to deploy.'.format(ovf_summary.name))

        # Build the deployment spec
        deployment_spec = LibraryItem.ResourcePoolDeploymentSpec(
            name=self.vm_name,
            annotation=ovf_summary.annotation,
            accept_all_eula=True,
            network_mappings=None,
            storage_mappings=None,
            storage_provisioning=None,
            storage_profile_id=None,
            locale=None,
            flags=None,
            additional_parameters=None,
            default_datastore_id=None)

        # Deploy the ovf template
        result = self.client.vcenter.ovf.LibraryItem.deploy(
            lib_item_id,
            deployment_target,
            deployment_spec,
            client_token=generate_random_uuid())

        # The type and ID of the target deployment is available in the deployment result.
        if result.succeeded:
            print('Deployment successful. VM Name: "{}", ID: "{}"'
                  .format(self.vm_name, result.resource_id.id))
            self.vm_id = result.resource_id.id
            error = result.error
            if error is not None:
                for warning in error.warnings:
                    print('OVF warning: {}'.format(warning.message))

        else:
            print('Deployment failed.')
            for error in result.error.errors:
                print('OVF error: {}'.format(error.message))

        # Add an opaque network portgroup to the deployed VM
        if self.opaquenetworkname:
            filter = Network.FilterSpec(
                names=set([self.opaquenetworkname]),
                types=set([Network.Type.OPAQUE_NETWORK]))
            network_summaries = self.client.vcenter.Network.list(filter=filter)
            if not network_summaries:
                raise ValueError("Opaque network {} can not find".format(
                    self.opaquenetworkname))
            network = network_summaries[0].network

            nic_create_spec = Ethernet.CreateSpec(
                start_connected=True,
                mac_type=Ethernet.MacAddressType.GENERATED,
                backing=Ethernet.BackingSpec(
                    type=Ethernet.BackingType.OPAQUE_NETWORK,
                    network=network))
            print('vm.hardware.Ethernet.create({}, {}) -> {}'.format(
                self.vm_id, nic_create_spec, network))
            nic = self.client.vcenter.vm.hardware.Ethernet.create(
                self.vm_id, nic_create_spec)

            nic_info = self.client.vcenter.vm.hardware.Ethernet.get(self.vm_id, nic)
            print('vm.hardware.Ethernet.get({}, {}) -> {}'.format(
                self.vm_id, nic, pp(nic_info)))
Example #4
0
folder_summaries = client.vcenter.Folder.list(filter_spec)
folder = folder_summaries[0].folder
print(folder)

filter_spec = Host.FilterSpec(names=set(['esxi-1.prod.vmware.haf']))
host_summaries = client.vcenter.Host.list(filter_spec)
host = host_summaries[0]
print(host)

filter_spec = Datastore.FilterSpec(names=set(['Kingston120G']))
datastore_summaries = client.vcenter.Datastore.list(filter_spec)
datastore = datastore_summaries[0]
print(datastore)

filter_spec = Network.FilterSpec(#datacenters=set(['Datacenter']),
                                 names=set(['VM Network']),
                                 types=set([Network.Type.STANDARD_PORTGROUP])
                                 )
network_summaries = client.vcenter.Network.list(filter=filter_spec)
print(network_summaries)
network_summaries = client.vcenter.Network.list()
network1 = network_summaries[0]
print(network1)
network2 = network_summaries[1]
print(network2)


placement_spec = VM.PlacementSpec(folder=folder,
    #resource_pool=resource_pool,
    #host='esxi-1.prod.vmware.haf',
    host=host.host,
    datastore=datastore.datastore)