-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathwaldur_os_security_group_gather_facts.py
93 lines (81 loc) · 2.4 KB
/
waldur_os_security_group_gather_facts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/python
# has to be a full import due to Ansible 2.0 compatibility
from ansible.module_utils.basic import AnsibleModule
from waldur_client import WaldurClientException, waldur_client_from_module
ANSIBLE_METADATA = {
"metadata_version": "1.1",
"status": ["preview"],
"supported_by": "OpenNode",
}
DOCUMENTATION = """
---
module: waldur_os_security_group_gather_facts
short_description: Get OpenStack tenant security group
version_added: 0.1
description:
- "Get OpenStack tenant security group"
requirements:
- "python = 3.8"
- "requests"
- "python-waldur-client"
options:
access_token:
description:
- An access token which has permission to read a security group.
required: true
api_url:
description:
- Fully qualified URL to the Waldur.
required: true
name:
description:
- The name of the security group.
required: false
tenant:
description:
- The name of the tenant.
required: true
"""
EXAMPLES = """
- name: get security group
hosts: localhost
tasks:
- name: get security group
waldur_os_security_group_gather_facts:
access_token: b83557fd8e2066e98f27dee8f3b3433cdc4183ce
api_url: https://waldur.example.com:8000/api
tenant: VPC #1
name: classic-web
- name: list tenant security groups
hosts: localhost
tasks:
- name: list all security groups belonging to the tenant
waldur_os_security_group_gather_facts:
access_token: b83557fd8e2066e98f27dee8f3b3433cdc4183ce
api_url: https://waldur.example.com:8000/api
tenant: VPC #1
"""
def send_request_to_waldur(client, module):
tenant = module.params["tenant"]
name = module.params["name"]
if name:
return [client.get_security_group(tenant, name)]
else:
return client.list_security_group(tenant)
def main():
fields = dict(
api_url=dict(required=True, type="str"),
access_token=dict(required=True, type="str", no_log=True),
name=dict(type="str", required=False),
tenant=dict(type="str", required=True),
)
module = AnsibleModule(argument_spec=fields)
client = waldur_client_from_module(module)
try:
security_groups = send_request_to_waldur(client, module)
except WaldurClientException as e:
module.fail_json(msg=str(e))
else:
module.exit_json(security_groups=security_groups)
if __name__ == "__main__":
main()