Switch hostaggregate module to OpenStackModule
Change-Id: I86ebbcc52b6558f4110fc439ed13fb590dddb590
This commit is contained in:
parent
4171ee52b7
commit
fa7526a0ec
@ -77,69 +77,11 @@ RETURN = '''
|
|||||||
|
|
||||||
'''
|
'''
|
||||||
|
|
||||||
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 _needs_update(module, aggregate):
|
class ComputeHostAggregateModule(OpenStackModule):
|
||||||
new_metadata = (module.params['metadata'] or {})
|
argument_spec = dict(
|
||||||
|
|
||||||
if module.params['availability_zone'] is not None:
|
|
||||||
new_metadata['availability_zone'] = module.params['availability_zone']
|
|
||||||
|
|
||||||
if module.params['name'] != aggregate.name:
|
|
||||||
return True
|
|
||||||
if module.params['hosts'] is not None:
|
|
||||||
if module.params['purge_hosts']:
|
|
||||||
if set(module.params['hosts']) != set(aggregate.hosts):
|
|
||||||
return True
|
|
||||||
else:
|
|
||||||
intersection = set(module.params['hosts']).intersection(set(aggregate.hosts))
|
|
||||||
if set(module.params['hosts']) != intersection:
|
|
||||||
return True
|
|
||||||
if module.params['availability_zone'] is not None:
|
|
||||||
if module.params['availability_zone'] != aggregate.availability_zone:
|
|
||||||
return True
|
|
||||||
if module.params['metadata'] is not None:
|
|
||||||
if new_metadata != aggregate.metadata:
|
|
||||||
return True
|
|
||||||
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
def _system_state_change(module, aggregate):
|
|
||||||
state = module.params['state']
|
|
||||||
if state == 'absent' and aggregate:
|
|
||||||
return True
|
|
||||||
|
|
||||||
if state == 'present':
|
|
||||||
if aggregate is None:
|
|
||||||
return True
|
|
||||||
return _needs_update(module, aggregate)
|
|
||||||
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
def _update_hosts(cloud, aggregate, hosts, purge_hosts):
|
|
||||||
if hosts is None:
|
|
||||||
return
|
|
||||||
|
|
||||||
hosts_to_add = set(hosts) - set(aggregate.get("hosts", []))
|
|
||||||
for i in hosts_to_add:
|
|
||||||
cloud.add_host_to_aggregate(aggregate.id, i)
|
|
||||||
|
|
||||||
if not purge_hosts:
|
|
||||||
return
|
|
||||||
|
|
||||||
hosts_to_remove = set(aggregate.get("hosts", [])) - set(hosts)
|
|
||||||
for i in hosts_to_remove:
|
|
||||||
cloud.remove_host_from_aggregate(aggregate.id, i)
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
|
||||||
argument_spec = openstack_full_argument_spec(
|
|
||||||
name=dict(required=True),
|
name=dict(required=True),
|
||||||
metadata=dict(required=False, default=None, type='dict'),
|
metadata=dict(required=False, default=None, type='dict'),
|
||||||
availability_zone=dict(required=False, default=None),
|
availability_zone=dict(required=False, default=None),
|
||||||
@ -148,24 +90,74 @@ def main():
|
|||||||
state=dict(default='present', choices=['absent', 'present']),
|
state=dict(default='present', choices=['absent', 'present']),
|
||||||
)
|
)
|
||||||
|
|
||||||
module_kwargs = openstack_module_kwargs()
|
module_kwargs = dict(
|
||||||
module = AnsibleModule(argument_spec,
|
supports_check_mode=True
|
||||||
supports_check_mode=True,
|
)
|
||||||
**module_kwargs)
|
|
||||||
|
|
||||||
name = module.params['name']
|
def _needs_update(self, aggregate):
|
||||||
metadata = module.params['metadata']
|
new_metadata = (self.params['metadata'] or {})
|
||||||
availability_zone = module.params['availability_zone']
|
|
||||||
hosts = module.params['hosts']
|
if self.params['availability_zone'] is not None:
|
||||||
purge_hosts = module.params['purge_hosts']
|
new_metadata['availability_zone'] = self.params['availability_zone']
|
||||||
state = module.params['state']
|
|
||||||
|
if self.params['name'] != aggregate.name:
|
||||||
|
return True
|
||||||
|
if self.params['hosts'] is not None:
|
||||||
|
if self.params['purge_hosts']:
|
||||||
|
if set(self.params['hosts']) != set(aggregate.hosts):
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
intersection = set(self.params['hosts']).intersection(set(aggregate.hosts))
|
||||||
|
if set(self.params['hosts']) != intersection:
|
||||||
|
return True
|
||||||
|
if self.params['availability_zone'] is not None:
|
||||||
|
if self.params['availability_zone'] != aggregate.availability_zone:
|
||||||
|
return True
|
||||||
|
if self.params['metadata'] is not None:
|
||||||
|
if new_metadata != aggregate.metadata:
|
||||||
|
return True
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
def _system_state_change(self, aggregate):
|
||||||
|
state = self.params['state']
|
||||||
|
if state == 'absent' and aggregate:
|
||||||
|
return True
|
||||||
|
|
||||||
|
if state == 'present':
|
||||||
|
if aggregate is None:
|
||||||
|
return True
|
||||||
|
return self._needs_update(aggregate)
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
def _update_hosts(self, aggregate, hosts, purge_hosts):
|
||||||
|
if hosts is None:
|
||||||
|
return
|
||||||
|
|
||||||
|
hosts_to_add = set(hosts) - set(aggregate.get("hosts", []))
|
||||||
|
for i in hosts_to_add:
|
||||||
|
self.conn.add_host_to_aggregate(aggregate.id, i)
|
||||||
|
|
||||||
|
if not purge_hosts:
|
||||||
|
return
|
||||||
|
|
||||||
|
hosts_to_remove = set(aggregate.get("hosts", [])) - set(hosts)
|
||||||
|
for i in hosts_to_remove:
|
||||||
|
self.conn.remove_host_from_aggregate(aggregate.id, i)
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
name = self.params['name']
|
||||||
|
metadata = self.params['metadata']
|
||||||
|
availability_zone = self.params['availability_zone']
|
||||||
|
hosts = self.params['hosts']
|
||||||
|
purge_hosts = self.params['purge_hosts']
|
||||||
|
state = self.params['state']
|
||||||
|
|
||||||
if metadata is not None:
|
if metadata is not None:
|
||||||
metadata.pop('availability_zone', None)
|
metadata.pop('availability_zone', None)
|
||||||
|
|
||||||
sdk, cloud = openstack_cloud_from_module(module)
|
aggregates = self.conn.search_aggregates(name_or_id=name)
|
||||||
try:
|
|
||||||
aggregates = cloud.search_aggregates(name_or_id=name)
|
|
||||||
|
|
||||||
if len(aggregates) == 1:
|
if len(aggregates) == 1:
|
||||||
aggregate = aggregates[0]
|
aggregate = aggregates[0]
|
||||||
@ -174,45 +166,48 @@ def main():
|
|||||||
else:
|
else:
|
||||||
raise Exception("Should not happen")
|
raise Exception("Should not happen")
|
||||||
|
|
||||||
if module.check_mode:
|
if self.ansible.check_mode:
|
||||||
module.exit_json(changed=_system_state_change(module, aggregate))
|
self.exit_json(changed=self._system_state_change(aggregate))
|
||||||
|
|
||||||
if state == 'present':
|
if state == 'present':
|
||||||
if aggregate is None:
|
if aggregate is None:
|
||||||
aggregate = cloud.create_aggregate(name=name,
|
aggregate = self.conn.create_aggregate(
|
||||||
availability_zone=availability_zone)
|
name=name, availability_zone=availability_zone)
|
||||||
_update_hosts(cloud, aggregate, hosts, False)
|
self._update_hosts(aggregate, hosts, False)
|
||||||
if metadata:
|
if metadata:
|
||||||
cloud.set_aggregate_metadata(aggregate.id, metadata)
|
self.conn.set_aggregate_metadata(aggregate.id, metadata)
|
||||||
changed = True
|
changed = True
|
||||||
else:
|
else:
|
||||||
if _needs_update(module, aggregate):
|
if self._needs_update(aggregate):
|
||||||
if availability_zone is not None:
|
if availability_zone is not None:
|
||||||
aggregate = cloud.update_aggregate(aggregate.id, name=name,
|
aggregate = self.conn.update_aggregate(
|
||||||
|
aggregate.id, name=name,
|
||||||
availability_zone=availability_zone)
|
availability_zone=availability_zone)
|
||||||
if metadata is not None:
|
if metadata is not None:
|
||||||
metas = metadata
|
metas = metadata
|
||||||
for i in (set(aggregate.metadata.keys()) - set(metadata.keys())):
|
for i in (set(aggregate.metadata.keys()) - set(metadata.keys())):
|
||||||
if i != 'availability_zone':
|
if i != 'availability_zone':
|
||||||
metas[i] = None
|
metas[i] = None
|
||||||
cloud.set_aggregate_metadata(aggregate.id, metas)
|
self.conn.set_aggregate_metadata(aggregate.id, metas)
|
||||||
_update_hosts(cloud, aggregate, hosts, purge_hosts)
|
self._update_hosts(aggregate, hosts, purge_hosts)
|
||||||
changed = True
|
changed = True
|
||||||
else:
|
else:
|
||||||
changed = False
|
changed = False
|
||||||
module.exit_json(changed=changed)
|
self.exit_json(changed=changed)
|
||||||
|
|
||||||
elif state == 'absent':
|
elif state == 'absent':
|
||||||
if aggregate is None:
|
if aggregate is None:
|
||||||
changed = False
|
changed = False
|
||||||
else:
|
else:
|
||||||
_update_hosts(cloud, aggregate, [], True)
|
self._update_hosts(aggregate, [], True)
|
||||||
cloud.delete_aggregate(aggregate.id)
|
self.conn.delete_aggregate(aggregate.id)
|
||||||
changed = True
|
changed = True
|
||||||
module.exit_json(changed=changed)
|
self.exit_json(changed=changed)
|
||||||
|
|
||||||
except sdk.exceptions.OpenStackCloudException as e:
|
|
||||||
module.fail_json(msg=str(e))
|
def main():
|
||||||
|
module = ComputeHostAggregateModule()
|
||||||
|
module()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
Loading…
x
Reference in New Issue
Block a user