def delete_file(client, service_instance, description, datacenter_name, datastore_path): """Delete a file from specific datacenter""" (datastore_name, path) = parse_datastore_path(datastore_path) datastore_mo = get_datastore_mo(client, service_instance._stub, datacenter_name, datastore_name) if not datastore_mo: return dsfile = datastore_file.File(datastore_mo) if dsfile.exists(datastore_path): print("Deleting {} file '{}'.".format(description, datastore_path)) dsfile.delete(path)
def detect_vmdk(client, soap_stub, datacenter_name, datastore_name, datastore_path): """Find vmdk in specific datastore""" datastore_mo = get_datastore_mo(client, soap_stub, datacenter_name, datastore_name) if not datastore_mo: return False dsfile = datastore_file.File(datastore_mo) if dsfile.exists(datastore_path): return True else: return False
def setup(context=None): global client, service_instance, cleardata if context: # Run sample suite via setup script vm_name = testbed.config['VM_NAME_DEFAULT'] client = context.client service_instance = context.service_instance else: # Run sample in standalone mode server, username, password, cleardata, skip_verification, vm_name = \ parse_cli_args_vm(testbed.config['VM_NAME_DEFAULT']) session = get_unverified_session() if skip_verification else None # Connect to vSphere client client = create_vsphere_client(server=server, username=username, password=password, session=session) # Connect to VIM API Endpoint on vCenter system context = None if skip_verification: context = get_unverified_context() service_instance = SmartConnect(host=server, user=username, pwd=password, sslContext=context) atexit.register(Disconnect, service_instance) global vm, datacenter_name, datastore_name global datastore_mo, datacenter_mo, datastore_root_path vm = get_vm(client, vm_name) if not vm: raise Exception('Sample requires an existing vm with name ({}). ' 'Please create the vm first.'.format(vm_name)) print("Using VM '{}' ({}) for Disk Sample".format(vm_name, vm)) # Get the datacenter and datastore managed objects to be able to create and # delete VMDKs, which are backings for a VM Disk. datacenter_name = testbed.config['VM_DATACENTER_NAME'] datastore_name = testbed.config['VM_DATASTORE_NAME'] datastore_mo = get_datastore_mo(client, service_instance._stub, datacenter_name, datastore_name) datacenter_mo = get_datacenter_for_datastore(datastore_mo) # The datastore_root_path is path in the datastore where the additional # VMDK files will be created for this sample. datastore_root_path = testbed.config['DISK_DATASTORE_ROOT_PATH']
def delete_directory(context, description, datacenter_name, datastore_path): """Delete directory from specific datacenter""" (datastore_name, path) = parse_datastore_path(datastore_path) datastore_mo = get_datastore_mo(context.stub_config, context.service_instance._stub, datacenter_name, datastore_name) if not datastore_mo: return dsfile = datastore_file.File(datastore_mo) if dsfile.exists(datastore_path): print("Deleting {} directory '{}'.".format(description, datastore_path)) dsfile.delete2(path)
def create_directory(context, description, datacenter_name, datastore_path): """Create directory in specific datacenter""" (datastore_name, path) = parse_datastore_path(datastore_path) datastore_mo = get_datastore_mo(context.client, context.service_instance._stub, datacenter_name, datastore_name) if not datastore_mo: raise Exception("Could not find datastore '{}'".format(datastore_name)) dsfile = datastore_file.File(datastore_mo) if not dsfile.exists(datastore_path): print("Creating {} directory '{}'".format(description, datastore_path)) dsfile.mkdir(path, parent=True) else: # TODO Need to check that this is actually a directory. print("{} directory '{}' exists.".format(description, datastore_path))
def setup_iso_image(context): """Copy iso image used to run vcenter samples""" iso_src_url = context.testbed.config['ISO_SRC_URL'] datacenter_name = context.testbed.config['ISO_DATACENTER_NAME'] datastore_path = context.testbed.config['ISO_DATASTORE_PATH'] (datastore_name, path) = parse_datastore_path(datastore_path) datastore_mo = get_datastore_mo(context.client, context.service_instance._stub, datacenter_name, datastore_name) if not datastore_mo: raise Exception("Could not find datastore '{}'".format(datastore_name)) # See if the ISO image exists. Copy it into the system if it does not exist dsfile = datastore_file.File(datastore_mo) if not dsfile.exists(datastore_path): print("Putting ISO image file from '{}' at '{}'".format( iso_src_url, datastore_path)) dsfile.put(path=path, src_url=iso_src_url)
def detect_file(context, description, datacenter_name, datastore_path): """Find specific file in specific datacenter""" (datastore_name, path) = parse_datastore_path(datastore_path) datastore_mo = get_datastore_mo(context.client, context.service_instance._stub, datacenter_name, datastore_name) if not datastore_mo: raise Exception("Could not find datastore '{}'".format(datastore_name)) dsfile = datastore_file.File(datastore_mo) f = dsfile.list(datastore_path) if len(f) == 0: print("Failed to detect {} file '{}'".format(description, datastore_path)) return False if f.type != datastore_file.FILE: print("Path '{}' is not a file".format(datastore_path)) return False return True
def detect_directory(context, description, datacenter_name, datastore_path): """Find directory based on specific datacenter and datastore path""" (datastore_name, path) = parse_datastore_path(datastore_path) datastore_mo = get_datastore_mo(context.stub_config, context.service_instance._stub, datacenter_name, datastore_name) if not datastore_mo: raise Exception("Could not find datastore '{}'".format(datastore_name)) dsfile = datastore_file.File(datastore_mo) f = dsfile.list(datastore_path) if len(f) == 0: print("Failed to detect {} directory '{}'".format( description, datastore_path)) return False if f.type != datastore_file.FOLDER: print("Path '{}' is not a directory".format(datastore_path)) return False return True