Migrating server_volume from AnsibleModule to OpenStackModule

Change-Id: I55cc89b9d043952c9bec6dccf3857a8c7713a7bb
This commit is contained in:
frenzy_friday 2020-11-24 21:36:44 +01:00 committed by Shnaidman Sagi (Sergey)
parent 981d268039
commit 9783fbb972

View File

@ -55,10 +55,7 @@ EXAMPLES = '''
device: /dev/vdb device: /dev/vdb
''' '''
from ansible.module_utils.basic import AnsibleModule from ansible_collections.openstack.cloud.plugins.module_utils.openstack import OpenStackModule
from ansible_collections.openstack.cloud.plugins.module_utils.openstack import (openstack_full_argument_spec,
openstack_module_kwargs,
openstack_cloud_from_module)
def _system_state_change(state, device): def _system_state_change(state, device):
@ -74,48 +71,44 @@ def _system_state_change(state, device):
return False return False
def main(): class ServerVolumeModule(OpenStackModule):
argument_spec = openstack_full_argument_spec(
argument_spec = dict(
server=dict(required=True), server=dict(required=True),
volume=dict(required=True), volume=dict(required=True),
device=dict(default=None), # None == auto choose device name device=dict(default=None), # None == auto choose device name
state=dict(default='present', choices=['absent', 'present']), state=dict(default='present', choices=['absent', 'present']),
) )
module_kwargs = openstack_module_kwargs() def run(self):
module = AnsibleModule(argument_spec,
supports_check_mode=True,
**module_kwargs)
state = module.params['state'] state = self.params['state']
wait = module.params['wait'] wait = self.params['wait']
timeout = module.params['timeout'] timeout = self.params['timeout']
sdk, cloud = openstack_cloud_from_module(module) server = self.conn.get_server(self.params['server'])
try: volume = self.conn.get_volume(self.params['volume'])
server = cloud.get_server(module.params['server'])
volume = cloud.get_volume(module.params['volume'])
if not volume: if not volume:
module.fail_json(msg='volume %s is not found' % module.params['volume']) self.fail(msg='volume %s is not found' % self.params['volume'])
dev = cloud.get_volume_attach_device(volume, server.id) dev = self.conn.get_volume_attach_device(volume, server.id)
if module.check_mode: if self.ansible.check_mode:
module.exit_json(changed=_system_state_change(state, dev)) self.exit(changed=_system_state_change(state, dev))
if state == 'present': if state == 'present':
changed = False changed = False
if not dev: if not dev:
changed = True changed = True
cloud.attach_volume(server, volume, module.params['device'], self.conn.attach_volume(server, volume, self.params['device'],
wait=wait, timeout=timeout) wait=wait, timeout=timeout)
server = cloud.get_server(module.params['server']) # refresh server = self.conn.get_server(self.params['server']) # refresh
volume = cloud.get_volume(module.params['volume']) # refresh volume = self.conn.get_volume(self.params['volume']) # refresh
hostvars = cloud.get_openstack_vars(server) hostvars = self.conn.get_openstack_vars(server)
module.exit_json( self.exit(
changed=changed, changed=changed,
id=volume['id'], id=volume['id'],
attachments=volume['attachments'], attachments=volume['attachments'],
@ -125,16 +118,18 @@ def main():
elif state == 'absent': elif state == 'absent':
if not dev: if not dev:
# Volume is not attached to this server # Volume is not attached to this server
module.exit_json(changed=False) self.exit(changed=False)
cloud.detach_volume(server, volume, wait=wait, timeout=timeout) self.conn.detach_volume(server, volume, wait=wait, timeout=timeout)
module.exit_json( self.exit(
changed=True, changed=True,
result='Detached volume from server' result='Detached volume from server'
) )
except (sdk.exceptions.OpenStackCloudException, sdk.exceptions.ResourceTimeout) as e:
module.fail_json(msg=str(e)) def main():
module = ServerVolumeModule()
module()
if __name__ == '__main__': if __name__ == '__main__':