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
'''
from ansible.module_utils.basic import AnsibleModule
from ansible_collections.openstack.cloud.plugins.module_utils.openstack import (openstack_full_argument_spec,
openstack_module_kwargs,
openstack_cloud_from_module)
from ansible_collections.openstack.cloud.plugins.module_utils.openstack import OpenStackModule
def _system_state_change(state, device):
@ -74,48 +71,44 @@ def _system_state_change(state, device):
return False
def main():
argument_spec = openstack_full_argument_spec(
class ServerVolumeModule(OpenStackModule):
argument_spec = dict(
server=dict(required=True),
volume=dict(required=True),
device=dict(default=None), # None == auto choose device name
state=dict(default='present', choices=['absent', 'present']),
)
module_kwargs = openstack_module_kwargs()
module = AnsibleModule(argument_spec,
supports_check_mode=True,
**module_kwargs)
def run(self):
state = module.params['state']
wait = module.params['wait']
timeout = module.params['timeout']
state = self.params['state']
wait = self.params['wait']
timeout = self.params['timeout']
sdk, cloud = openstack_cloud_from_module(module)
try:
server = cloud.get_server(module.params['server'])
volume = cloud.get_volume(module.params['volume'])
server = self.conn.get_server(self.params['server'])
volume = self.conn.get_volume(self.params['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:
module.exit_json(changed=_system_state_change(state, dev))
if self.ansible.check_mode:
self.exit(changed=_system_state_change(state, dev))
if state == 'present':
changed = False
if not dev:
changed = True
cloud.attach_volume(server, volume, module.params['device'],
wait=wait, timeout=timeout)
self.conn.attach_volume(server, volume, self.params['device'],
wait=wait, timeout=timeout)
server = cloud.get_server(module.params['server']) # refresh
volume = cloud.get_volume(module.params['volume']) # refresh
hostvars = cloud.get_openstack_vars(server)
server = self.conn.get_server(self.params['server']) # refresh
volume = self.conn.get_volume(self.params['volume']) # refresh
hostvars = self.conn.get_openstack_vars(server)
module.exit_json(
self.exit(
changed=changed,
id=volume['id'],
attachments=volume['attachments'],
@ -125,16 +118,18 @@ def main():
elif state == 'absent':
if not dev:
# 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)
module.exit_json(
self.conn.detach_volume(server, volume, wait=wait, timeout=timeout)
self.exit(
changed=True,
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__':