-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathwaldur_os_floating_ip.py
151 lines (138 loc) · 4.24 KB
/
waldur_os_floating_ip.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/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,
waldur_full_argument_spec,
)
ANSIBLE_METADATA = {
"metadata_version": "1.1",
"status": ["preview"],
"supported_by": "OpenNode",
}
DOCUMENTATION = """
---
module: waldur_os_floating_ip
short_description: Assign floating IPs
version_added: 0.1
requirements:
- "python = 3.8"
- "requests"
- "python-waldur-client"
options:
access_token:
description:
- An access token which has permissions to create an OpenStack instances.
required: true
address:
description:
- IP address of the floating IP to be assigned to the instance.
It is required if 'floating_ips' are not provided.
api_url:
description:
- Fully qualified url to the Waldur.
required: true
floating_ips:
description:
- A list of floating IPs to be assigned to the instance.
A floating IP consists of 'subnet' and 'address'.
It is required if 'floating_ips' are not provided.
subnet:
description:
- A subnet to be assigned to the instance.
It is required if 'floating_ips' are not provided.
instance:
description:
- The name of the virtual machine to assign floating IPs to.
required: true
interval:
default: 20
description:
- An interval of the instance state polling.
timeout:
default: 600
description:
- The maximum amount of seconds to wait until the floating IP is assigned to instance.
wait:
default: true
description:
- A boolean value that defines whether client has to wait until the floating IP is assigned to instance.
"""
EXAMPLES = """
- name: assign multiple floating IPs
hosts: localhost
tasks:
- name: assign single floating IP
waldur_os_floating_ip:
access_token: b83557fd8e2066e98f27dee8f3b3433cdc4183ce
api_url: https://waldur.example.com:8000
instance: VM #1
floating_ips:
- address: 10.30.201.18
subnet: vpc-1-tm-sub-net
- address: 10.30.201.177
subnet: vpc-2-tm-sub-net
- name: assign floating IP
hosts: localhost
tasks:
- name: assign single floating IP
waldur_os_floating_ip:
access_token: b83557fd8e2066e98f27dee8f3b3433cdc4183ce
api_url: https://waldur.example.com:8000
instance: VM #3
address: 10.30.201.19
subnet: vpc-3-tm-sub-net
- name: detach floating IP
hosts: localhost
tasks:
- name: detach floating IP
waldur_os_floating_ip:
access_token: b83557fd8e2066e98f27dee8f3b3433cdc4183ce
api_url: https://waldur.example.com:8000
instance: VM #3
state: absent
"""
def main():
fields = waldur_full_argument_spec(
instance=dict(type="str"),
floating_ips=dict(type="list"),
address=dict(type="str"),
subnet=dict(type="str"),
state=dict(default="present", choices=["absent", "present"]),
)
required_together = [["address", "subnet"]]
mutually_exclusive = [["floating_ips", "subnet"], ["floating_ips", "address"]]
required_if = [("state", "present", ("floating_ips", "subnet"))]
module = AnsibleModule(
argument_spec=fields,
required_together=required_together,
mutually_exclusive=mutually_exclusive,
required_if=required_if,
)
present = module.params["state"] == "present"
client = waldur_client_from_module(module)
if present:
floating_ips = module.params.get("floating_ips") or [
{
"address": module.params["address"],
"subnet": module.params["subnet"],
}
]
else:
floating_ips = []
instance = module.params["instance"]
try:
response = client.assign_floating_ips(
instance=instance,
floating_ips=floating_ips,
wait=module.params["wait"],
timeout=module.params["timeout"],
interval=module.params["interval"],
)
except WaldurClientException as e:
module.fail_json(msg=str(e))
else:
module.exit_json(meta=response)
if __name__ == "__main__":
main()