def get_datacenter_by_name(self, datacenter_name): """ Returns the identifier of a datacenter Note: The method assumes only one datacenter with the mentioned name. """ filter_spec = Datacenter.FilterSpec(names=set([datacenter_name])) datacenter_summaries = self.api_client.vcenter.Datacenter.list(filter_spec) datacenter = datacenter_summaries[0].datacenter if len(datacenter_summaries) > 0 else None return datacenter
def get_datacenter(client, datacenter_name): """ Returns the identifier of a datacenter Note: The method assumes only one datacenter with the mentioned name. """ filter_spec = Datacenter.FilterSpec(names=set([datacenter_name])) datacenter_summaries = client.vcenter.Datacenter.list(filter_spec) if len(datacenter_summaries) > 0: datacenter = datacenter_summaries[0].datacenter return datacenter else: return None
def detect_datacenter(context, datacenter_name): """Find the datacenter with the given name""" names = set([datacenter_name]) datacenter_summaries = context.client.vcenter.Datacenter.list( Datacenter.FilterSpec(names=names)) if len(datacenter_summaries) > 0: datacenter = datacenter_summaries[0].datacenter print("Detected Datacenter '{}' as {}".format(datacenter_name, datacenter)) context.testbed.entities['DATACENTER_IDS'][ datacenter_name] = datacenter return True else: print("Datacenter '{}' missing".format(datacenter_name)) return False
def cleanup_datacenters(context): """Cleanup datacenters after sample run""" # Look for the two datacenters datacenter1_name = context.testbed.config['DATACENTER1_NAME'] datacenter2_name = context.testbed.config['DATACENTER2_NAME'] names = set([datacenter1_name, datacenter2_name]) datacenter_summaries = context.client.vcenter.Datacenter.list( Datacenter.FilterSpec(names=names)) print("Found {} Datacenters matching names {}".format( len(datacenter_summaries), ", ".join(["'{}'".format(n) for n in names]))) for datacenter_summary in datacenter_summaries: datacenter = datacenter_summary.datacenter print("Deleting Datacenter '{}' ({})".format(datacenter, datacenter_summary.name)) context.client.vcenter.Datacenter.delete(datacenter, force=True)
# Disable cert verification for demo purpose. # This is not recommended in a production environment. session.verify = False # Disable the secure connection warning for demo purpose. # This is not recommended in a production environment. urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) # Connect to a vCenter Server using username and password config = VsCreadential.load('.credentials.yaml') client = create_vsphere_client(server=config.hostname, username=config.username, password=config.password, session=session) context = None # get_unverified_context filter_spec = Datacenter.FilterSpec(names=set(['Datacenter'])) datacenter_summaries = client.vcenter.Datacenter.list(filter_spec) datacenter = datacenter_summaries[0].datacenter filter_spec = Host.FilterSpec(names=set(['esxi-1.prod.vmware.haf'])) host_summaries = client.vcenter.Host.list(filter_spec) host = host_summaries[0].host print(client.vcenter.Host.list()) # List all VMs inside the vCenter Server for vm in client.vcenter.VM.list(): print(vm) VM = client.vcenter.VM.get(vm.vm)
def check_dc_state(self): names = set([self.datacenter_name]) datacenter_summaries = self.dc_service.Datacenter.list(Datacenter.FilterSpec(names=names)) if len(datacenter_summaries) > 0: return True return False
def _get_vm_filter_spec(self): vm_filter_spec = VM.FilterSpec() datacenters = self.get_option('datacenters') if datacenters: temp_dcs = [] for datacenter_name in datacenters: dc_filter_spec = Datacenter.FilterSpec( names=set([datacenter_name])) datacenter_summaries = self.pyv.rest_content.vcenter.Datacenter.list( dc_filter_spec) if len(datacenter_summaries) > 0: temp_dcs.append(datacenter_summaries[0].datacenter) else: self._handle_error(message="Unable to find datacenter %s" % datacenter_name) vm_filter_spec.datacenters = set(temp_dcs) clusters = self.get_option('clusters') if clusters: temp_clusters = [] for cluster_name in clusters: ccr_filter_spec = Cluster.FilterSpec(names=set([cluster_name])) cluster_summaries = self.pyv.rest_content.vcenter.Cluster.list( ccr_filter_spec) if len(cluster_summaries) > 0: temp_clusters.append(cluster_summaries[0].cluster) else: self._handle_error(message="Unable to find cluster %s" % cluster_name) vm_filter_spec.clusters = set(temp_clusters) folders = self.get_option('folders') if folders: temp_folders = [] for folder_name in folders: folder_filter_spec = Folder.FilterSpec( names=set([folder_name])) folder_summaries = self.pyv.rest_content.vcenter.Folder.list( folder_filter_spec) if len(folder_summaries) > 0: temp_folders.append(folder_summaries[0].folder) else: self._handle_error(message="Unable to find folder %s" % folder_name) vm_filter_spec.folders = set(temp_folders) esxi_hosts = self.get_option('esxi_hostsystems') if esxi_hosts: temp_hosts = [] for esxi_name in esxi_hosts: esxi_filter_spec = Host.FilterSpec(names=set([esxi_name])) esxi_summaries = self.pyv.rest_content.vcenter.Host.list( esxi_filter_spec) if len(esxi_summaries) > 0: temp_hosts.append(esxi_summaries[0].host) else: self._handle_error( message="Unable to find esxi hostsystem %s" % esxi_name) vm_filter_spec.folders = set(temp_hosts) resource_pools = self.get_option('resource_pools') if resource_pools: temp_rps = [] for rp_name in resource_pools: rp_filter_spec = ResourcePool.FilterSpec(names=set([rp_name])) rp_summaries = self.pyv.rest_content.vcenter.ResourcePool.list( rp_filter_spec) if len(rp_summaries) > 0: temp_rps.append(rp_summaries[0].resourcepool) else: self._handle_error( message="Unable to find resource pool %s" % rp_name) vm_filter_spec.folders = set(temp_rps) return vm_filter_spec