def main()

in src/ansible_collections/alibaba/apsarastack/plugins/modules/ali_eip.py [0:0]


def main():
    argument_spec = common_argument_spec()
    argument_spec.update(
        dict(
            state=dict(type='str', default='present', choices=['present', 'absent']),
            ip_address=dict(type='str', aliases=['ip']),
            instance_id=dict(type='str', aliases=['device_id']),
            allocation_id=dict(type='str', aliases=['id']),
            internet_charge_type=dict(type='str', default='PayByTraffic', choices=['PayByTraffic', 'PayByBandwidth']),
            netmode=dict(type='str', default='public', choices=['public', 'hybrid']),
            isp=dict(type='str', default='BGP', choices=['CMCC_AZ1', 'CUMCC_AZ1', 'CTC_AZ1', 'PrivateEIP_AZ1', 'BGP']),
            bandwidth=dict(type='int', default=5),
            reuse_existing_ip_allowed=dict(type='bool', default=False),
            release_on_disassociation=dict(type='bool', default=False),
            allow_reassociation=dict(type='bool', default=False),
            name=dict(type='str'),
            description=dict(type='str'),
            tags=dict(type='dict'),
            purge_tags=dict(type='bool', default=False)
        )
    )
    module = AnsibleModule(argument_spec=argument_spec)

    if not HAS_FOOTMARK:
        module.fail_json(msg='footmark is required for the module ali_eip.')

    vpc = vpc_connect(module)

    # set values
    state = module.params['state']
    instance_id = module.params['instance_id']
    ip_address = module.params['ip_address']
    allocation_id = module.params['allocation_id']

    eip, eips = find_eip(vpc, module, ip_address, instance_id, allocation_id)
    changed = False

    if state == 'absent':
        if not eip:
            module.exit_json(changed=changed, eip={})

        if ip_address and instance_id and eip.ip_address != ip_address:
            module.exit_json(changed=changed, eip=eip.get.read())

        release = module.params['release_on_disassociation']
        if instance_id:
            try:
                if unassociate_eip(eip, module, instance_id):
                    changed = True
                if not release:
                    module.exit_json(changed=changed, eip=eip.get().read())
            except Exception as e:
                module.fail_json(msg="Disassociate EIP with instance {0} failed. Error: {1}".format(instance_id, e))
        else:
            release = True

        if release:
            try:
                changed = eip.release()
                module.exit_json(changed=changed, eip={})
            except Exception as e:
                module.fail_json(msg="{0}".format(e))

    # state == present
    if not eip:
        if module.params['reuse_existing_ip_allowed'] and len(eips) > 0:
            for e in eips:
                if str(e.status).lower() == "available":
                    eip = e
                    break
    if not eip:
        try:
            params = module.params
            params['client_token'] = "Ansible-Apsarastack-%s-%s" % (hash(str(module.params)), str(time.time()))
            eip = vpc.allocate_eip_address(**params)
            changed = True
        except VPCResponseError as e:
            module.fail_json(msg='Unable to allocate an eip address, error: {0}'.format(e))

    # Modify EIP attribute
    name = module.params['name']
    description = module.params['description']
    bandwidth = module.params['bandwidth']
    if not name:
        name = eip.name
    if not description:
        description = eip.description
    if not bandwidth:
        bandwidth = eip.bandwidth

    try:
        if eip.modify(bandwidth, name, description):
            changed = True
    except VPCResponseError as e:
        module.fail_json(msg='Modify EIP attribute with an error {0}.'.format(e))

    # Associate instance
    if instance_id:
        if eip.instance_id and eip.instance_id != instance_id:
            if not module.params['allow_reassociation']:
                module.fail_json(msg='Target EIP {0} has been associated. Please set allow_reassociation to ture to '
                                     'associate the target instance {1}'. format(eip.ip_address, instance_id))
            try:
                if unassociate_eip(eip, module, instance_id):
                    changed = True
            except Exception as e:
                module.fail_json(msg="Unassociate EIP from instance {0} failed. Error: {1}".format(instance_id, e))
        try:
            if eip.get().associate(instance_id=instance_id):
                changed = True
        except Exception as e:
            module.fail_json(msg="Associate EIP with instance {0} failed. Error: {1}".format(instance_id, e))

    tags = module.params['tags']
    if module.params['purge_tags']:
        if not tags:
            tags = eip.tags
        try:
            if eip.remove_tags(tags):
                changed = True
            module.exit_json(changed=changed, eip=eip.get().read())
        except Exception as e:
            module.fail_json(msg="{0}".format(e))

    if tags:
        try:
            if eip.add_tags(tags):
                changed = True
        except Exception as e:
            module.fail_json(msg="{0}".format(e))
    module.exit_json(changed=changed, eip=eip.get().read())