-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathwaldur_marketplace_os_volume.py
202 lines (185 loc) · 5.59 KB
/
waldur_marketplace_os_volume.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/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 (
MultipleObjectsReturned,
ObjectDoesNotExist,
WaldurClientException,
waldur_client_from_module,
waldur_resource_argument_spec,
)
ANSIBLE_METADATA = {
"metadata_version": "1.1",
"status": ["preview"],
"supported_by": "OpenNode",
}
DOCUMENTATION = """
---
module: waldur_marketplace_os_volume
short_description: Create/Update/Delete OpenStack volume via marketplace
version_added: 0.8
description:
- "Create/Update/Delete OpenStack volume"
requirements:
- "python = 3.8"
- "requests"
- "python-waldur-client"
options:
access_token:
description:
- An access token which has permissions to create a volume.
required: true
api_url:
description:
- Fully qualified URL to the Waldur.
required: true
description:
description:
- A description of the volume.
interval:
default: 20
description:
- An interval of the volume state polling.
name:
description:
- The name of the volume.
required: true
project:
description:
- The name or id of the project to add volume to.
It is required if is state is 'present'.
offering:
description:
- The name or id of the marketplace offering.
It is required if is state is 'present'.
size:
description:
- The size of the volume in GBs.
It is required if is state is 'present'.
type:
description:
- UUID or name of volume type.
state:
choices:
- present
- absent
default: present
description:
- Should the resource be present or absent.
tags:
description:
- List of tags that will be added to the volume on provisioning.
timeout:
default: 600
description:
- The maximum amount of seconds to wait until the volume provisioning is finished.
wait:
default: true
description:
- A boolean value that defines whether client has to wait until the volume is provisioned.
"""
EXAMPLES = """
- name: add volume
hosts: localhost
tasks:
- name: create volume
waldur_marketplace_os_volume:
access_token: b83557fd8e2066e98f27dee8f3b3433cdc4183ce
api_url: https://waldur.example.com:8000/api
name: test volume
project: OpenStack Project
offering: Volume in Tenant
size: 40
type: lvm
state: present
- name: remove volume
hosts: localhost
tasks:
- name: remove existing volume
waldur_marketplace_os_volume:
access_token: b83557fd8e2066e98f27dee8f3b3433cdc4183ce
api_url: https://waldur.example.com:8000/api
name: test volume
project: OpenStack Project
state: absent
- name: update volume
hosts: localhost
tasks:
- name: update volume description
waldur_marketplace_os_volume:
access_token: b83557fd8e2066e98f27dee8f3b3433cdc4183ce
api_url: https://waldur.example.com:8000/api
name: test volume
project: OpenStack Project
description: do not delete this volume
"""
def send_request_to_waldur(client, module):
has_changed = False
name = module.params["name"]
project = module.params["project"]
offering = module.params["offering"]
size = module.params["size"]
volume_type = module.params["type"]
try:
volume = client.get_volume_via_marketplace(name, project)
except (ObjectDoesNotExist, MultipleObjectsReturned):
volume = None
pass
present = module.params["state"] == "present"
if volume:
if present:
if volume["description"] != module.params.get("description"):
client.update_volume(
volume, description=module.params.get("description")
)
has_changed = True
else:
client.delete_volume_via_marketplace(volume["uuid"])
has_changed = True
elif present:
client.create_volume_via_marketplace(
name=module.params["name"],
project=project,
offering=offering,
size=size,
volume_type=volume_type,
description=module.params.get("description"),
tags=module.params.get("tags"),
wait=module.params["wait"],
interval=module.params["interval"],
timeout=module.params["timeout"],
)
has_changed = True
return has_changed
def main():
fields = waldur_resource_argument_spec(
project=dict(type="str", default=None),
offering=dict(type="str", default=None),
size=dict(type="int", default=None),
type=dict(type="str", default=None),
)
module = AnsibleModule(argument_spec=fields)
state = module.params["state"]
project = module.params["project"]
offering = module.params["offering"]
size = module.params["size"]
if state == "present":
if not project:
module.fail_json(
msg="Parameter 'project' is required if state == 'present'"
)
if not offering:
module.fail_json(
msg="Parameter 'offering' is required if state == 'present'"
)
if not size:
module.fail_json(msg="Parameter 'size' is required if state == 'present'")
client = waldur_client_from_module(module)
try:
has_changed = send_request_to_waldur(client, module)
except WaldurClientException as e:
module.fail_json(msg=str(e))
else:
module.exit_json(changed=has_changed)
if __name__ == "__main__":
main()