def main()

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


def main():
    argument_spec = common_argument_spec()
    argument_spec.update(dict(
        vswitch_name=dict(type='str', aliases=['name', 'subnet_name']),
        cidr_block=dict(type='str'),
        name_prefix=dict(type='str'),
        cidr_prefix=dict(type='str'),
        vswitch_ids=dict(type='list', elements='str', aliases=['ids', 'subnet_ids']),
        filters=dict(type='dict'),
        tags=dict(type='dict')
    )
    )

    module = AnsibleModule(argument_spec=argument_spec)

    if HAS_FOOTMARK is False:
        module.fail_json(msg="Package 'footmark' required for this module.")

    filters = module.params['filters']
    if not filters:
        filters = {}

    vswitch_ids = module.params['vswitch_ids']
    if not vswitch_ids:
        vswitch_ids = []
    for key, value in list(filters.items()):
        if key in ["VSwitchId", "vswitch_id", "vswitch-id"] and value not in vswitch_ids:
            vswitch_ids.append(value)

    name = module.params['vswitch_name']
    cidr_block = module.params['cidr_block']
    name_prefix = module.params['name_prefix']
    cidr_prefix = module.params['cidr_prefix']

    try:
        vswitches = []
        ids = []
        while True:
            if vswitch_ids:
                filters['vswitch_id'] = vswitch_ids[0]
                vswitch_ids.pop(0)
            for vsw in vpc_connect(module).describe_vswitches(**filters):
                if name and vsw.vswitch_name != name:
                    continue
                if cidr_block and vsw.cidr_block != cidr_block:
                    continue
                if name_prefix and not str(vsw.vswitch_name).startswith(name_prefix):
                    continue
                if cidr_prefix and not str(vsw.cidr_block).startswith(cidr_prefix):
                    continue
                vswitches.append(vsw.read())
                ids.append(vsw.id)
            if not vswitch_ids:
                break

        module.exit_json(changed=False, ids=ids, vswitches=vswitches)
    except Exception as e:
        module.fail_json(msg=str("Unable to get vswitches, error:{0}".format(e)))