Merge changes from topic "OOM-2042"
[oom/offline-installer.git] / tools / cicdansible / library / os_floating_ips_facts.py
1 #!/usr/bin/python
2 ANSIBLE_METADATA = {
3     'METADATA_VERSION': '1.1',
4     'supported_by': 'community',
5     'status': 'preview'
6 }
7
8 DOCUMENTATION = '''
9 ---
10 module: "os_floating_ips_facts"
11 short_description: "Retrieves facts about floating ips"
12 description:
13   - "This module retrieves facts about one or more floating ips allocated to project."
14 version_added: "2.7"
15 author:
16   - "Michal Zegan"
17 requirements:
18   - "python => 2.7"
19   - "openstacksdk"
20 options:
21   floating_ip:
22     description:
23       - "The floating ip to retrieve facts for"
24     type: "str"
25   network:
26     description:
27       - "Name or id of the floating ip network to query."
28     required: true
29     type: "str"
30 notes:
31   - "Registers facts starting with openstack_floating_ips"
32 extends_documentation_fragment: openstack
33 '''
34
35 from ansible.module_utils.basic import AnsibleModule
36 from ansible.module_utils.openstack import openstack_full_argument_spec, openstack_module_kwargs, openstack_cloud_from_module
37
38 def run_module():
39     args=openstack_module_kwargs()
40     argspec=openstack_full_argument_spec(
41       floating_ip=dict(type=str),
42       network=dict(type=str, required=True))
43     module=AnsibleModule(argument_spec=argspec, **args)
44     sdk, cloud = openstack_cloud_from_module(module)
45     try:
46         fip_network=cloud.network.find_network(module.params['network'])
47         filter=dict(
48           project_id=cloud.current_project_id,
49           floating_network_id=fip_network.id)
50         if not (module.params['floating_ip'] is None):
51             filter['floating_ip_address'] = module.params['floating_ip']
52         ips=[dict(x) for x in cloud.network.ips(**filter)]
53         module.exit_json(
54           changed=False,
55           ansible_facts=dict(openstack_floating_ips=ips)
56         )
57     except sdk.exceptions.OpenStackCloudException as e:
58         module.fail_json(msg=str(e))
59
60 if __name__ == '__main__':
61     run_module()