in src/ansible_collections/alibaba/apsarastack/plugins/modules/ali_vswitch.py [0:0]
def main():
argument_spec = common_argument_spec()
argument_spec.update(dict(
state=dict(default='present', choices=['present', 'absent']),
cidr_block=dict(type='str', required=True),
description=dict(type='str'),
zone_id=dict(type='str', aliases=['availability_zone', 'apsarastack_zone'],
fallback=(env_fallback, ['APSARASTACK_ZONE', 'APSARASTACK_ZONE_ID'])),
vpc_id=dict(type='str', required=True),
name=dict(type='str', aliases=['vswitch_name', 'subnet_name']),
vswitch_id=dict(type='str', aliases=['subnet_id', 'id']),
tags=dict(type='dict'),
purge_tags=dict(type='bool', default=False)
))
module = AnsibleModule(argument_spec=argument_spec)
if HAS_FOOTMARK is False:
module.fail_json(msg='footmark required for the module ali_vswitch.')
vpc = vpc_connect(module)
# Get values of variable
state = module.params['state']
vswitch_id = module.params['vswitch_id']
changed = False
vswitch = vswitch_exists(vpc, module, vswitch_id, module.params['vpc_id'], module.params['cidr_block'])
if state == 'absent':
if not vswitch:
module.exit_json(changed=changed, vswitch={})
try:
changed = vswitch.delete()
module.exit_json(changed=changed, vswitch={})
except VPCResponseError as ex:
module.fail_json(msg='Unable to delete vswitch: {0}, error: {1}'.format(vswitch.id, ex))
vswitch_name = module.params['name']
description = module.params['description']
if str(description).startswith('http://') or str(description).startswith('https://'):
module.fail_json(msg='description can not start with http:// or https://')
if str(vswitch_name).startswith('http://') or str(vswitch_name).startswith('https://'):
module.fail_json(msg='vswitch_name can not start with http:// or https://')
if not vswitch:
try:
params = module.params
params['client_token'] = "Ansible-Apsarastack-{0}-{1}".format(hash(str(module.params)), str(time.time()))
params['vswitch_name'] = vswitch_name
vswitch = vpc.create_vswitch(**params)
module.exit_json(changed=True, vswitch=vswitch.get().read())
except VPCResponseError as e:
module.fail_json(msg='Unable to create VSwitch, error: {0}'.format(e))
if not vswitch_name:
vswitch_name = vswitch.vswitch_name
if not description:
description = vswitch.description
try:
if vswitch.modify(name=vswitch_name, description=description):
changed = True
except VPCResponseError as e:
module.fail_json(msg='Unable to modify vswitch attribute, error: {0}'.format(e))
tags = module.params['tags']
if module.params['purge_tags']:
if not tags:
tags = vswitch.tags
try:
if vswitch.remove_tags(tags):
changed = True
module.exit_json(changed=changed, vswitch=vswitch.get().read())
except Exception as e:
module.fail_json(msg="{0}".format(e))
if tags:
try:
if vswitch.add_tags(tags):
changed = True
except Exception as e:
module.fail_json(msg="{0}".format(e))
module.exit_json(changed=changed, vswitch=vswitch.get().read())