mso_st_bd_subnet: Fix various issues (#53240)

This PR includes:
- Rename 'ip' to 'subnet' as that's what we're managing
- Small improvement in logic
pull/53255/head
Dag Wieers 6 years ago committed by GitHub
parent 12a949229d
commit 8c659c35ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -36,11 +36,12 @@ options:
- The name of the BD to manage. - The name of the BD to manage.
type: str type: str
required: yes required: yes
ip: subnet:
description: description:
- The IP range in CIDR notation. - The IP range in CIDR notation.
type: str type: str
required: true required: true
aliases: [ ip ]
description: description:
description: description:
- The description of this subnet. - The description of this subnet.
@ -79,7 +80,7 @@ EXAMPLES = r'''
schema: Schema 1 schema: Schema 1
template: Template 1 template: Template 1
bd: BD 1 bd: BD 1
ip: 10.0.0.0/24 subnet: 10.0.0.0/24
state: present state: present
delegate_to: localhost delegate_to: localhost
@ -91,7 +92,7 @@ EXAMPLES = r'''
schema: Schema 1 schema: Schema 1
template: Template 1 template: Template 1
bd: BD 1 bd: BD 1
ip: 10.0.0.0/24 subnet: 10.0.0.0/24
state: absent state: absent
delegate_to: localhost delegate_to: localhost
@ -103,7 +104,7 @@ EXAMPLES = r'''
schema: Schema 1 schema: Schema 1
template: Template 1 template: Template 1
bd: BD 1 bd: BD 1
ip: 10.0.0.0/24 subnet: 10.0.0.0/24
state: query state: query
delegate_to: localhost delegate_to: localhost
register: query_result register: query_result
@ -142,15 +143,15 @@ def main():
argument_spec=argument_spec, argument_spec=argument_spec,
supports_check_mode=True, supports_check_mode=True,
required_if=[ required_if=[
['state', 'absent', ['ip']], ['state', 'absent', ['subnet']],
['state', 'present', ['ip']], ['state', 'present', ['subnet']],
], ],
) )
schema = module.params['schema'] schema = module.params['schema']
template = module.params['template'] template = module.params['template']
bd = module.params['bd'] bd = module.params['bd']
ip = module.params['ip'] subnet = module.params['subnet']
description = module.params['description'] description = module.params['description']
scope = module.params['scope'] scope = module.params['scope']
shared = module.params['shared'] shared = module.params['shared']
@ -172,7 +173,7 @@ def main():
mso.fail_json(msg="Provided template '{0}' does not exist. Existing templates: {1}".format(template, ', '.join(templates))) mso.fail_json(msg="Provided template '{0}' does not exist. Existing templates: {1}".format(template, ', '.join(templates)))
template_idx = templates.index(template) template_idx = templates.index(template)
# Get EPG # Get BD
bds = [b['name'] for b in schema_obj['templates'][template_idx]['bds']] bds = [b['name'] for b in schema_obj['templates'][template_idx]['bds']]
if bd not in bds: if bd not in bds:
mso.fail_json(msg="Provided BD '{0}' does not exist. Existing BDs: {1}".format(bd, ', '.join(bds))) mso.fail_json(msg="Provided BD '{0}' does not exist. Existing BDs: {1}".format(bd, ', '.join(bds)))
@ -180,17 +181,17 @@ def main():
# Get Subnet # Get Subnet
subnets = [s['ip'] for s in schema_obj['templates'][template_idx]['bds'][bd_idx]['subnets']] subnets = [s['ip'] for s in schema_obj['templates'][template_idx]['bds'][bd_idx]['subnets']]
if ip in subnets: if subnet in subnets:
ip_idx = subnets.index(ip) subnet_idx = subnets.index(subnet)
# FIXME: Changes based on index are DANGEROUS # FIXME: Changes based on index are DANGEROUS
subnet_path = '/templates/{0}/bds/{1}/subnets/{2}'.format(template, bd, ip_idx) subnet_path = '/templates/{0}/bds/{1}/subnets/{2}'.format(template, bd, subnet_idx)
mso.existing = schema_obj['templates'][template_idx]['bds'][bd_idx]['subnets'][ip_idx] mso.existing = schema_obj['templates'][template_idx]['bds'][bd_idx]['subnets'][subnet_idx]
if state == 'query': if state == 'query':
if ip is None: if subnet is None:
mso.existing = schema_obj['templates'][template_idx]['bds'][bd_idx]['subnets'] mso.existing = schema_obj['templates'][template_idx]['bds'][bd_idx]['subnets']
elif not mso.existing: elif not mso.existing:
mso.fail_json(msg="Subnet '{ip}' not found".format(ip=ip)) mso.fail_json(msg="Subnet IP '{subnet}' not found".format(subnet=subnet))
mso.exit_json() mso.exit_json()
subnets_path = '/templates/{0}/bds/{1}/subnets'.format(template, bd) subnets_path = '/templates/{0}/bds/{1}/subnets'.format(template, bd)
@ -203,17 +204,18 @@ def main():
ops.append(dict(op='remove', path=subnet_path)) ops.append(dict(op='remove', path=subnet_path))
elif state == 'present': elif state == 'present':
if description is None and not mso.existing: if not mso.existing:
description = ip if description is None:
if scope is None and not mso.existing: description = subnet
if scope is None:
scope = 'private' scope = 'private'
if shared is None and not mso.existing: if shared is None:
shared = False shared = False
if no_default_gateway is None and not mso.existing: if no_default_gateway is None:
no_default_gateway = False no_default_gateway = False
payload = dict( payload = dict(
ip=ip, ip=subnet,
description=description, description=description,
scope=scope, scope=scope,
shared=shared, shared=shared,

Loading…
Cancel
Save