Debian Codegraph @

ansible_collections/community/general/plugins/modules/scaleway_volume.py

raw
1 #!/usr/bin/python
2 #
3 # Scaleway volumes management module
4 #
5 # Copyright (C) 2018 Henryk Konsek Consulting ([email protected]).
6 #
7 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
8 # SPDX-License-Identifier: GPL-3.0-or-later
9
10 from __future__ import annotations
11
12 DOCUMENTATION = r"""
13 module: scaleway_volume
14 short_description: Scaleway volumes management module
15 author: Henryk Konsek (@hekonsek)
16 description:
17 - This module manages volumes on Scaleway account U(https://developer.scaleway.com).
18 extends_documentation_fragment:
19 - community.general._scaleway
20 - community.general._attributes
21 - community.general._scaleway.actiongroup_scaleway
22
23 attributes:
24 check_mode:
25 support: full
26 diff_mode:
27 support: none
28 action_group:
29 version_added: 11.3.0
30
31 options:
32 state:
33 type: str
34 description:
35 - Indicate desired state of the volume.
36 default: present
37 choices:
38 - present
39 - absent
40 region:
41 type: str
42 description:
43 - Scaleway region to use (for example par1).
44 required: true
45 choices:
46 - ams1
47 - EMEA-NL-EVS
48 - ams2
49 - ams3
50 - par1
51 - EMEA-FR-PAR1
52 - par2
53 - EMEA-FR-PAR2
54 - par3
55 - waw1
56 - EMEA-PL-WAW1
57 - waw2
58 - waw3
59 name:
60 type: str
61 description:
62 - Name used to identify the volume.
63 required: true
64 project:
65 type: str
66 description:
67 - Scaleway project ID to which volume belongs.
68 version_added: 4.3.0
69 organization:
70 type: str
71 description:
72 - ScaleWay organization ID to which volume belongs.
73 size:
74 type: int
75 description:
76 - Size of the volume in bytes.
77 volume_type:
78 type: str
79 description:
80 - Type of the volume (for example 'l_ssd').
81 """
82
83 EXAMPLES = r"""
84 - name: Create 10GB volume
85 community.general.scaleway_volume:
86 name: my-volume
87 state: present
88 region: par1
89 project: "{{ scw_org }}"
90 "size": 10000000000
91 volume_type: l_ssd
92 register: server_creation_check_task
93
94 - name: Make sure volume deleted
95 community.general.scaleway_volume:
96 name: my-volume
97 state: absent
98 region: par1
99 """
100
101 RETURN = r"""
102 data:
103 description: This is only present when O(state=present).
104 returned: when O(state=present)
105 type: dict
106 sample:
107 {
108 "volume": {
109 "export_uri": null,
110 "id": "c675f420-cfeb-48ff-ba2a-9d2a4dbe3fcd",
111 "name": "volume-0-3",
112 "project": "000a115d-2852-4b0a-9ce8-47f1134ba95a",
113 "server": null,
114 "size": 10000000000,
115 "volume_type": "l_ssd"
116 }
117 }
118 """
119
120 from ansible.module_utils.basic import AnsibleModule
121
122 from ansible_collections.community.general.plugins.module_utils._scaleway import (
123 SCALEWAY_LOCATION,
124 Scaleway,
125 scaleway_argument_spec,
126 )
127
128
129 def core(module):
130 region = module.params["region"]
131 state = module.params["state"]
132 name = module.params["name"]
133 organization = module.params["organization"]
134 project = module.params["project"]
135 size = module.params["size"]
136 volume_type = module.params["volume_type"]
137 module.params["api_url"] = SCALEWAY_LOCATION[region]["api_endpoint"]
138
139 account_api = Scaleway(module)
140 response = account_api.get("volumes")
141 status_code = response.status_code
142 volumes_json = response.json
143
144 if project is None:
145 project = organization
146
147 if not response.ok:
148 module.fail_json(msg=f"Error getting volume [{status_code}: {response.json['message']}]")
149
150 volumeByName = None
151 for volume in volumes_json["volumes"]:
152 if volume["project"] == project and volume["name"] == name:
153 volumeByName = volume
154
155 if state in ("present",):
156 if volumeByName is not None:
157 module.exit_json(changed=False)
158
159 payload = {"name": name, "project": project, "size": size, "volume_type": volume_type}
160
161 response = account_api.post("/volumes", payload)
162
163 if response.ok:
164 module.exit_json(changed=True, data=response.json)
165
166 module.fail_json(msg=f"Error creating volume [{response.status_code}: {response.json}]")
167
168 elif state in ("absent",):
169 if volumeByName is None:
170 module.exit_json(changed=False)
171
172 if module.check_mode:
173 module.exit_json(changed=True)
174
175 response = account_api.delete(f"/volumes/{volumeByName['id']}")
176 if response.status_code == 204:
177 module.exit_json(changed=True, data=response.json)
178
179 module.fail_json(msg=f"Error deleting volume [{response.status_code}: {response.json}]")
180
181
182 def main():
183 argument_spec = scaleway_argument_spec()
184 argument_spec.update(
185 dict(
186 state=dict(default="present", choices=["absent", "present"]),
187 name=dict(required=True),
188 size=dict(type="int"),
189 project=dict(),
190 organization=dict(),
191 volume_type=dict(),
192 region=dict(required=True, choices=list(SCALEWAY_LOCATION.keys())),
193 )
194 )
195 module = AnsibleModule(
196 argument_spec=argument_spec,
197 supports_check_mode=True,
198 mutually_exclusive=[
199 ("organization", "project"),
200 ],
201 required_one_of=[
202 ("organization", "project"),
203 ],
204 )
205
206 core(module)
207
208
209 if __name__ == "__main__":
210 main()