<s> import atexit <EOL> import requests <EOL> from tools import cli <EOL> from pyVmomi import vim <EOL> from pyVim . connect import SmartConnect , Disconnect <EOL> from tools import tasks <EOL> if hasattr ( requests . packages . urllib3 , '<STR_LIT>' ) : <EOL> requests . packages . urllib3 . disable_warnings ( ) <EOL> def delete_virtual_disk ( si , vm_obj , disk_number ) : <EOL> """<STR_LIT>""" <EOL> hdd_prefix_label = '<STR_LIT>' <EOL> hdd_label = hdd_prefix_label + str ( disk_number ) <EOL> virtual_hdd_device = None <EOL> for dev in vm_obj . config . hardware . device : <EOL> if isinstance ( dev , vim . vm . device . VirtualDisk ) and dev . deviceInfo . label == hdd_label : <EOL> virtual_hdd_device = dev <EOL> if not virtual_hdd_device : <EOL> raise RuntimeError ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( virtual_hdd_device ) ) <EOL> virtual_hdd_spec = vim . vm . device . VirtualDeviceSpec ( ) <EOL> virtual_hdd_spec . operation = vim . vm . device . VirtualDeviceSpec . Operation . remove <EOL> virtual_hdd_spec . device = virtual_hdd_device <EOL> spec = vim . vm . ConfigSpec ( ) <EOL> spec . deviceChange = [ virtual_hdd_spec ] <EOL> task = vm_obj . ReconfigVM_Task ( spec = spec ) <EOL> tasks . wait_for_tasks ( si , [ task ] ) <EOL> return True <EOL> def get_args ( ) : <EOL> parser = cli . build_arg_parser ( ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , required = True , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , required = True , <EOL> help = '<STR_LIT>' , type = int ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , <EOL> help = '<STR_LIT>' , action = '<STR_LIT:store_true>' ) <EOL> my_args = parser . parse_args ( ) <EOL> return cli . prompt_for_password ( my_args ) <EOL> def get_obj ( content , vim_type , name ) : <EOL> obj = None <EOL> container = content . viewManager . CreateContainerView ( <EOL> content . rootFolder , vim_type , True ) <EOL> for c in container . view : <EOL> if c . name == name : <EOL> obj = c <EOL> break <EOL> return obj <EOL> def prompt_y_n_question ( question , default = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> valid = { "<STR_LIT:yes>" : True , "<STR_LIT:y>" : True , "<STR_LIT>" : True , <EOL> "<STR_LIT>" : False , "<STR_LIT:n>" : False } <EOL> if default is None : <EOL> prompt = "<STR_LIT>" <EOL> elif default == "<STR_LIT:yes>" : <EOL> prompt = "<STR_LIT>" <EOL> elif default == "<STR_LIT>" : <EOL> prompt = "<STR_LIT>" <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" . format ( default ) ) <EOL> while True : <EOL> print ( question + prompt ) <EOL> choice = raw_input ( ) . lower ( ) <EOL> if default is not None and choice == '<STR_LIT>' : <EOL> return valid [ default ] <EOL> elif choice in valid : <EOL> return valid [ choice ] <EOL> else : <EOL> print ( "<STR_LIT>" ) <EOL> def main ( ) : <EOL> args = get_args ( ) <EOL> si = SmartConnect ( <EOL> host = args . host , <EOL> user = args . user , <EOL> pwd = args . password , <EOL> port = args . port ) <EOL> atexit . register ( Disconnect , si ) <EOL> content = si . RetrieveContent ( ) <EOL> print ( '<STR_LIT>' . format ( args . vmname ) ) <EOL> vm_obj = get_obj ( content , [ vim . VirtualMachine ] , args . vmname ) <EOL> if vm_obj : <EOL> if not args . yes : <EOL> cli . prompt_y_n_question ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" . format ( args . unitnumber ) , <EOL> default = '<STR_LIT>' ) <EOL> delete_virtual_disk ( si , vm_obj , args . unitnumber ) <EOL> print ( '<STR_LIT>' . format ( args . unitnumber ) ) <EOL> else : <EOL> print ( '<STR_LIT>' ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( ) </s>
