Switch coe_cluster module to OpenStackModule

Change-Id: Idb316136840001e406f76c08d99654d32e14d168
This commit is contained in:
Artem Goncharov 2021-05-20 18:36:47 +02:00 committed by Artem Goncharov
parent 6b3bf3bba0
commit 7e190d12b4

View File

@ -206,26 +206,11 @@ EXAMPLES = '''
node_count: 5 node_count: 5
''' '''
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 _parse_labels(labels): class CoeClusterModule(OpenStackModule):
if isinstance(labels, str): argument_spec = dict(
labels_dict = {}
for kv_str in labels.split(","):
k, v = kv_str.split("=")
labels_dict[k] = v
return labels_dict
if not labels:
return {}
return labels
def main():
argument_spec = openstack_full_argument_spec(
cluster_template_id=dict(required=True), cluster_template_id=dict(required=True),
discovery_url=dict(default=None), discovery_url=dict(default=None),
docker_volume_size=dict(type='int'), docker_volume_size=dict(type='int'),
@ -239,35 +224,46 @@ def main():
state=dict(default='present', choices=['absent', 'present']), state=dict(default='present', choices=['absent', 'present']),
timeout=dict(type='int', default=60), timeout=dict(type='int', default=60),
) )
module_kwargs = openstack_module_kwargs() module_kwargs = dict()
module = AnsibleModule(argument_spec, **module_kwargs)
params = module.params.copy() def _parse_labels(self, labels):
if isinstance(labels, str):
labels_dict = {}
for kv_str in labels.split(","):
k, v = kv_str.split("=")
labels_dict[k] = v
return labels_dict
if not labels:
return {}
return labels
state = module.params['state'] def run(self):
name = module.params['name'] params = self.params.copy()
cluster_template_id = module.params['cluster_template_id']
kwargs = dict( state = self.params['state']
discovery_url=module.params['discovery_url'], name = self.params['name']
docker_volume_size=module.params['docker_volume_size'], cluster_template_id = self.params['cluster_template_id']
flavor_id=module.params['flavor_id'],
keypair=module.params['keypair'], kwargs = dict(
labels=_parse_labels(params['labels']), discovery_url=self.params['discovery_url'],
master_count=module.params['master_count'], docker_volume_size=self.params['docker_volume_size'],
master_flavor_id=module.params['master_flavor_id'], flavor_id=self.params['flavor_id'],
node_count=module.params['node_count'], keypair=self.params['keypair'],
create_timeout=module.params['timeout'], labels=self._parse_labels(params['labels']),
) master_count=self.params['master_count'],
master_flavor_id=self.params['master_flavor_id'],
node_count=self.params['node_count'],
create_timeout=self.params['timeout'],
)
sdk, cloud = openstack_cloud_from_module(module)
try:
changed = False changed = False
cluster = cloud.get_coe_cluster(name_or_id=name, filters={'cluster_template_id': cluster_template_id}) cluster = self.conn.get_coe_cluster(
name_or_id=name, filters={'cluster_template_id': cluster_template_id})
if state == 'present': if state == 'present':
if not cluster: if not cluster:
cluster = cloud.create_coe_cluster(name, cluster_template_id=cluster_template_id, **kwargs) cluster = self.conn.create_coe_cluster(
name, cluster_template_id=cluster_template_id, **kwargs)
changed = True changed = True
else: else:
changed = False changed = False
@ -278,15 +274,18 @@ def main():
# therefore try `id` first then `uuid`. # therefore try `id` first then `uuid`.
cluster_id = cluster.get('id', cluster.get('uuid')) cluster_id = cluster.get('id', cluster.get('uuid'))
cluster['id'] = cluster['uuid'] = cluster_id cluster['id'] = cluster['uuid'] = cluster_id
module.exit_json(changed=changed, cluster=cluster, id=cluster_id) self.exit_json(changed=changed, cluster=cluster, id=cluster_id)
elif state == 'absent': elif state == 'absent':
if not cluster: if not cluster:
module.exit_json(changed=False) self.exit_json(changed=False)
else: else:
cloud.delete_coe_cluster(name) self.conn.delete_coe_cluster(name)
module.exit_json(changed=True) self.exit_json(changed=True)
except sdk.exceptions.OpenStackCloudException as e:
module.fail_json(msg=str(e), extra_data=e.extra_data)
def main():
module = CoeClusterModule()
module()
if __name__ == "__main__": if __name__ == "__main__":