rearranged options to be more declarative

This commit is contained in:
Brian Coca 2016-04-26 14:32:28 -04:00
parent b963b090bb
commit 9f3660893e

View file

@ -47,8 +47,7 @@ options:
description: description:
- Assert the state of the virtual machine. - Assert the state of the virtual machine.
- State 'present' will check that the machine exists with the requested configuration. If the configuration - State 'present' will check that the machine exists with the requested configuration. If the configuration
of the existing machine does not match, the machine will be updated. Use options started, stopped, of the existing machine does not match, the machine will be updated. Use options started, allocated and restarted to change the machine's power state.
deallocated and restarted to change the machine's power state.
- State 'absent' will remove the virtual machine. - State 'absent' will remove the virtual machine.
default: present default: present
required: false required: false
@ -57,18 +56,13 @@ options:
- present - present
started: started:
description: description:
- Use with state 'present' to start the machine. - Use with state 'present' to start the machine. Set to false to have the machine be 'stopped'.
default: true default: true
required: false required: false
stopped: allocated:
description: description:
- Use with state 'present' to stop the machine. - Toggle that controls if the machine is allocated/deallocated, only useful with state='present'.
default: false default: True
required: false
deallocated:
description:
- Use with state 'present' to put the VM in a deallocated state.
default: false
required: false required: false
restarted: restarted:
description: description:
@ -208,24 +202,12 @@ options:
- virtual_network - virtual_network
default: null default: null
required: false required: false
delete_network_interfaces: remove_on_absent:
description: description:
- When removing a VM using state 'absent', also remove any network interfaces associate with the VM. - When removing a VM using state 'absent', also remove associated resources
default: true - "It can be 'all' or a list with any of the following: ['network_interfaces', 'virtual_storage', 'public_ips']"
aliases: - Any other input will be ignored
- delete_nics default: ['all']
required: false
delete_virtual_storage:
description:
- When removing a VM using state 'absent', also remove any storage blobs associated with the VM.
default: true
aliases:
- delete_vhd
required: false
delete_public_ips:
description:
- When removing a VM using state 'absent', also remove any public IP addresses associate with the VM.
default: true
required: false required: false
extends_documentation_fragment: extends_documentation_fragment:
@ -272,13 +254,13 @@ EXAMPLES = '''
azure_rm_virtualmachine: azure_rm_virtualmachine:
resource_group: Testing resource_group: Testing
name: testvm002 name: testvm002
stopped: yes started: no
- name: Deallocate - name: Deallocate
azure_rm_virtualmachine: azure_rm_virtualmachine:
resource_group: Testing resource_group: Testing
name: testvm002 name: testvm002
deallocated: yes allocated: no
- name: Power On - name: Power On
azure_rm_virtualmachine: azure_rm_virtualmachine:
@ -291,16 +273,17 @@ EXAMPLES = '''
name: testvm002 name: testvm002
restarted: yes restarted: yes
- name: remove vm and all resources except public ips
azure_rm_virtualmachine:
resource_group: Testing
name: testvm002
state: absent
remove_on_absent:
- network_interfaces
- virtual_storage
''' '''
RETURN = ''' RETURN = '''
actions:
description: List of descriptive actions performed by the module.
returned: always
type: list
sample: [
"Powered on virtual machine testvm10"
]
powerstate: powerstate:
description: Indicates if the state is running, stopped, deallocated description: Indicates if the state is running, stopped, deallocated
returned: always returned: always
@ -493,15 +476,12 @@ class AzureRMVirtualMachine(AzureRMModuleBase):
aliases=['public_ip_allocation']), aliases=['public_ip_allocation']),
open_ports=dict(type='list'), open_ports=dict(type='list'),
network_interface_names=dict(type='list', aliases=['network_interfaces']), network_interface_names=dict(type='list', aliases=['network_interfaces']),
delete_network_interfaces=dict(type='bool', default=True, aliases=['delete_nics']), remove_on_absent=dict(type='list', default=['all']),
delete_virtual_storage=dict(type='bool', default=True, aliases=['delete_vhd']),
delete_public_ips=dict(type='bool', default=True),
virtual_network_name=dict(type='str', aliases=['virtual_network']), virtual_network_name=dict(type='str', aliases=['virtual_network']),
subnet_name=dict(type='str', aliases=['subnet']), subnet_name=dict(type='str', aliases=['subnet']),
deallocated=dict(type='bool', default=False), allocated=dict(type='bool', default=True),
restarted=dict(type='bool', default=False), restarted=dict(type='bool', default=False),
started=dict(type='bool', default=True), started=dict(type='bool', default=True),
stopped=dict(type='bool', default=False),
) )
for key in VirtualMachineSizeTypes: for key in VirtualMachineSizeTypes:
@ -524,19 +504,16 @@ class AzureRMVirtualMachine(AzureRMModuleBase):
self.os_type = None self.os_type = None
self.os_disk_caching = None self.os_disk_caching = None
self.network_interface_names = None self.network_interface_names = None
self.delete_network_interfaces = None self.remove_on_absent = []
self.delete_virtual_storage = None
self.delete_public_ips = None
self.tags = None self.tags = None
self.force = None self.force = None
self.public_ip_allocation_method = None self.public_ip_allocation_method = None
self.open_ports = None self.open_ports = None
self.virtual_network_name = None self.virtual_network_name = None
self.subnet_name = None self.subnet_name = None
self.deallocated = None self.allocated = None
self.restarted = None self.restarted = None
self.started = None self.started = None
self.stopped = None
self.differences = None self.differences = None
self.results = dict( self.results = dict(
@ -549,6 +526,9 @@ class AzureRMVirtualMachine(AzureRMModuleBase):
super(AzureRMVirtualMachine, self).__init__(derived_arg_spec=self.module_arg_spec, super(AzureRMVirtualMachine, self).__init__(derived_arg_spec=self.module_arg_spec,
supports_check_mode=True) supports_check_mode=True)
# make sure options are lower case
self.remove_on_absent = [resource.lower() for resource in self.remove_on_absent]
def exec_module(self, **kwargs): def exec_module(self, **kwargs):
for key in self.module_arg_spec.keys() + ['tags']: for key in self.module_arg_spec.keys() + ['tags']:
@ -661,12 +641,12 @@ class AzureRMVirtualMachine(AzureRMModuleBase):
.format(self.name, vm_dict['powerstate'])) .format(self.name, vm_dict['powerstate']))
changed = True changed = True
powerstate_change = 'restarted' powerstate_change = 'restarted'
elif self.state == 'present' and self.deallocated and vm_dict['powerstate'] != 'deallocated': elif self.state == 'present' and not self.allocated and vm_dict['powerstate'] != 'deallocated':
self.log("CHANGED: virtual machine {0} {1} and requested state 'deallocated'" self.log("CHANGED: virtual machine {0} {1} and requested state 'deallocated'"
.format(self.name, vm_dict['powerstate'])) .format(self.name, vm_dict['powerstate']))
changed = True changed = True
powerstate_change = 'deallocated' powerstate_change = 'deallocated'
elif self.stopped and vm_dict['powerstate'] == 'running': elif not self.started and vm_dict['powerstate'] == 'running':
self.log("CHANGED: virtual machine {0} running and requested state 'stopped'".format(self.name)) self.log("CHANGED: virtual machine {0} running and requested state 'stopped'".format(self.name))
changed = True changed = True
powerstate_change = 'poweroff' powerstate_change = 'poweroff'
@ -976,14 +956,14 @@ class AzureRMVirtualMachine(AzureRMModuleBase):
nic_names = [] nic_names = []
pip_names = [] pip_names = []
if self.delete_virtual_storage: if self.remove_on_absent.intersection(['all','virtual_storage']):
# store the attached vhd info so we can nuke it after the VM is gone # store the attached vhd info so we can nuke it after the VM is gone
self.log('Storing VHD URI for deletion') self.log('Storing VHD URI for deletion')
vhd_uris.append(vm.storage_profile.os_disk.vhd.uri) vhd_uris.append(vm.storage_profile.os_disk.vhd.uri)
self.log("VHD URIs to delete: {0}".format(', '.join(vhd_uris))) self.log("VHD URIs to delete: {0}".format(', '.join(vhd_uris)))
self.results['deleted_vhd_uris'] = vhd_uris self.results['deleted_vhd_uris'] = vhd_uris
if self.delete_network_interfaces: if self.remove_on_absent.intersection(['all','network_interfaces']):
# store the attached nic info so we can nuke them after the VM is gone # store the attached nic info so we can nuke them after the VM is gone
self.log('Storing NIC names for deletion.') self.log('Storing NIC names for deletion.')
for interface in vm.network_profile.network_interfaces: for interface in vm.network_profile.network_interfaces:
@ -991,7 +971,7 @@ class AzureRMVirtualMachine(AzureRMModuleBase):
nic_names.append(id_dict['networkInterfaces']) nic_names.append(id_dict['networkInterfaces'])
self.log('NIC names to delete {0}'.format(', '.join(nic_names))) self.log('NIC names to delete {0}'.format(', '.join(nic_names)))
self.results['deleted_network_interfaces'] = nic_names self.results['deleted_network_interfaces'] = nic_names
if self.delete_public_ips: if self.remove_on_absent.intersection(['all','public_ips']):
# also store each nic's attached public IPs and delete after the NIC is gone # also store each nic's attached public IPs and delete after the NIC is gone
for name in nic_names: for name in nic_names:
nic = self.get_network_interface(name) nic = self.get_network_interface(name)
@ -1013,16 +993,16 @@ class AzureRMVirtualMachine(AzureRMModuleBase):
# TODO: parallelize nic, vhd, and public ip deletions with begin_deleting # TODO: parallelize nic, vhd, and public ip deletions with begin_deleting
# TODO: best-effort to keep deleting other linked resources if we encounter an error # TODO: best-effort to keep deleting other linked resources if we encounter an error
if self.delete_virtual_storage: if self.remove_on_absent.intersection(['all','virtual_storage']):
self.log('Deleting virtual storage') self.log('Deleting virtual storage')
self.delete_vm_storage(vhd_uris) self.delete_vm_storage(vhd_uris)
if self.delete_network_interfaces: if self.remove_on_absent.intersection(['all','network_interfaces']):
self.log('Deleting network interfaces') self.log('Deleting network interfaces')
for name in nic_names: for name in nic_names:
self.delete_nic(name) self.delete_nic(name)
if self.delete_public_ips: if self.remove_on_absent.intersection(['all','public_ips']):
self.log('Deleting public IPs') self.log('Deleting public IPs')
for name in pip_names: for name in pip_names:
self.delete_pip(name) self.delete_pip(name)