Skip to content

Commit d16c15f

Browse files
authored
cs_network_offering: add tags, zones and domains support (#82)
1 parent 18a7b26 commit d16c15f

File tree

5 files changed

+125
-44
lines changed

5 files changed

+125
-44
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
minor_changes:
2+
- cs_network_offering - implemented support for ``tags``, ``zones`` and ``domains`` (https://github.com/ngine-io/ansible-collection-cloudstack/pull/82).

plugins/module_utils/cloudstack.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ def __init__(self, module):
7070
'displaytext': 'display_text',
7171
'displayname': 'display_name',
7272
'description': 'description',
73+
'tags': 'tags',
7374
}
7475

7576
# Init returns dict for use in subclasses
@@ -158,10 +159,13 @@ def has_changed(self, want_dict, current_dict, only_keys=None, skip_diff_for_key
158159
# ensure we compare the same type
159160
if isinstance(value, int):
160161
current_dict[key] = int(current_dict[key])
162+
161163
elif isinstance(value, float):
162164
current_dict[key] = float(current_dict[key])
165+
163166
elif isinstance(value, long):
164167
current_dict[key] = long(current_dict[key])
168+
165169
elif isinstance(value, complex):
166170
current_dict[key] = complex(current_dict[key])
167171

@@ -650,8 +654,6 @@ def update_result(self, resource, result=None):
650654
if search_key in resource:
651655
result[return_key] = int(resource[search_key])
652656

653-
if 'tags' in resource:
654-
result['tags'] = resource['tags']
655657
return result
656658

657659
def get_result(self, resource):

plugins/modules/cs_network_offering.py

Lines changed: 77 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,30 @@
119119
description:
120120
- Whether the offering is meant to be used for VPC or not.
121121
type: bool
122+
tags:
123+
description:
124+
- List of tags. Tags are a list of strings.
125+
- "To delete all tags, set an empty list e.g. I(tags: [])."
126+
type: list
127+
elements: str
128+
aliases: [ tag ]
129+
version_added: 2.2.0
130+
domains:
131+
description:
132+
- List of domains the network offering is related to.
133+
- Use C(public) for public offerings.
134+
type: list
135+
elements: str
136+
aliases: [ domain ]
137+
version_added: 2.2.0
138+
zones:
139+
description:
140+
- List of zones the network offering is related to.
141+
- Use C(all) for all zones offering.
142+
type: list
143+
elements: str
144+
aliases: [ zone ]
145+
version_added: 2.2.0
122146
extends_documentation_fragment:
123147
- ngine_io.cloudstack.cloudstack
124148
'''
@@ -213,6 +237,24 @@
213237
returned: success
214238
type: bool
215239
sample: false
240+
tags:
241+
description: List of tags associated with the network offering.
242+
returned: success
243+
type: list
244+
sample: [ tag1, tag2 ]
245+
version_added: 2.2.0
246+
domains:
247+
description: List of domains associated with the network offering.
248+
returned: success
249+
type: list
250+
sample: [ public ]
251+
version_added: 2.2.0
252+
zones:
253+
description: List of zones associated with the network offering.
254+
returned: success
255+
type: list
256+
sample: [ all ]
257+
version_added: 2.2.0
216258
'''
217259

218260
from ansible.module_utils.basic import AnsibleModule
@@ -270,13 +312,16 @@ def get_network_offering(self):
270312

271313
return self.network_offering
272314

273-
def create_or_update(self):
315+
def present(self):
274316
network_offering = self.get_network_offering()
275317

276318
if not network_offering:
277319
network_offering = self.create_network_offering()
278320

279-
return self.update_network_offering(network_offering=network_offering)
321+
if network_offering:
322+
network_offering = self.update_network_offering(network_offering=network_offering)
323+
324+
return network_offering
280325

281326
def create_network_offering(self):
282327
network_offering = None
@@ -303,6 +348,10 @@ def create_network_offering(self):
303348
'specifyipranges': self.module.params.get('specify_ip_ranges'),
304349
'specifyvlan': self.module.params.get('specify_vlan'),
305350
'forvpc': self.module.params.get('for_vpc'),
351+
# Tags are comma separated strings in network offerings
352+
'tags': self.module.params.get('tags'),
353+
'domainid': self.module.params.get('domains'),
354+
'zoneid': self.module.params.get('zones'),
306355
}
307356

308357
required_params = [
@@ -320,7 +369,7 @@ def create_network_offering(self):
320369

321370
return network_offering
322371

323-
def delete_network_offering(self):
372+
def absent(self):
324373
network_offering = self.get_network_offering()
325374

326375
if network_offering:
@@ -331,8 +380,10 @@ def delete_network_offering(self):
331380
return network_offering
332381

333382
def update_network_offering(self, network_offering):
334-
if not network_offering:
335-
return network_offering
383+
384+
tags = self.module.params.get('tags')
385+
domains = self.module.params.get('domains')
386+
zones = self.module.params.get('zones')
336387

337388
args = {
338389
'id': network_offering['id'],
@@ -341,6 +392,9 @@ def update_network_offering(self, network_offering):
341392
'name': self.module.params.get('name'),
342393
'availability': self.module.params.get('availability'),
343394
'maxconnections': self.module.params.get('max_connections'),
395+
'tags': ','.join(tags) if tags else None,
396+
'domainid': ','.join(domains) if domains else None,
397+
'zoneid': ','.join(zones) if zones else None,
344398
}
345399

346400
if args['state'] in ['enabled', 'disabled']:
@@ -361,6 +415,17 @@ def get_result(self, network_offering):
361415
super(AnsibleCloudStackNetworkOffering, self).get_result(network_offering)
362416
if network_offering:
363417
self.result['egress_default_policy'] = 'allow' if network_offering.get('egressdefaultpolicy') else 'deny'
418+
419+
# Return a list of comma separated network offering tags
420+
tags = network_offering.get('tags')
421+
self.result['tags'] = tags.split(',') if tags else []
422+
423+
zone_id = network_offering.get('zoneid')
424+
self.result['zones'] = zone_id.split(',') if zone_id else []
425+
426+
domain_id = network_offering.get('domainid')
427+
self.result['domains'] = zone_id.split(',') if domain_id else []
428+
364429
return self.result
365430

366431

@@ -397,6 +462,10 @@ def main():
397462
specify_ip_ranges=dict(type='bool'),
398463
specify_vlan=dict(type='bool'),
399464
for_vpc=dict(type='bool'),
465+
# Tags are comma separated strings in network offerings
466+
tags=dict(type='list', elements='str', aliases=['tag']),
467+
domains=dict(type='list', elements='str', aliases=['domain']),
468+
zones=dict(type='list', elements='str', aliases=['zone']),
400469
))
401470

402471
module = AnsibleModule(
@@ -408,10 +477,10 @@ def main():
408477
acs_network_offering = AnsibleCloudStackNetworkOffering(module)
409478

410479
state = module.params.get('state')
411-
if state in ['absent']:
412-
network_offering = acs_network_offering.delete_network_offering()
480+
if state == "absent":
481+
network_offering = acs_network_offering.absent()
413482
else:
414-
network_offering = acs_network_offering.create_or_update()
483+
network_offering = acs_network_offering.present()
415484

416485
result = acs_network_offering.get_result(network_offering)
417486

0 commit comments

Comments
 (0)