|
27 | 27 | description:
|
28 | 28 | - Size of the block storage volume in GB.
|
29 | 29 | - Required if I(state) is present.
|
| 30 | + - If it's larger than the volume's current size, the volume will be resized. |
30 | 31 | type: int
|
31 | 32 | region:
|
32 | 33 | description:
|
|
37 | 38 | description:
|
38 | 39 | - State of the block storage volume.
|
39 | 40 | default: present
|
40 |
| - choices: [ present, absent ] |
| 41 | + choices: [ present, absent, attached, detached ] |
41 | 42 | type: str
|
| 43 | + attached_to_SUBID: |
| 44 | + description: |
| 45 | + - The ID of the server the volume is attached to. |
| 46 | + - Required if I(state) is attached. |
| 47 | + aliases: [ attached_to_id ] |
| 48 | + type: int |
| 49 | + live_attachment: |
| 50 | + description: |
| 51 | + - Whether the volume should be attached/detached, even if the server not stopped. |
| 52 | + type: bool |
| 53 | + default: True |
42 | 54 | extends_documentation_fragment:
|
43 | 55 | - ngine_io.vultr.vultr
|
44 | 56 |
|
|
55 | 67 | ngine_io.vultr.vultr_block_storage:
|
56 | 68 | name: myvolume
|
57 | 69 | state: absent
|
| 70 | +
|
| 71 | +- name: Ensure a block storage volume exists and is attached to server 114 |
| 72 | + ngine_io.vultr.vultr_block_storage: |
| 73 | + name: myvolume |
| 74 | + state: attached |
| 75 | + attached_to_id: 114 |
| 76 | + size: 10 |
| 77 | +
|
| 78 | +- name: Ensure a block storage volume exists and is not attached to any server |
| 79 | + ngine_io.vultr.vultr_block_storage: |
| 80 | + name: myvolume |
| 81 | + state: detached |
| 82 | + size: 10 |
58 | 83 | '''
|
59 | 84 |
|
60 | 85 | RETURN = '''
|
|
136 | 161 | sample: "active"
|
137 | 162 |
|
138 | 163 | '''
|
139 |
| - |
140 | 164 | from ansible.module_utils.basic import AnsibleModule
|
141 | 165 | from ..module_utils.vultr import (
|
142 | 166 | Vultr,
|
@@ -216,28 +240,140 @@ def absent_block_storage_volume(self):
|
216 | 240 | )
|
217 | 241 | return volume
|
218 | 242 |
|
| 243 | + def detached_block_storage_volume(self): |
| 244 | + volume = self.present_block_storage_volume() |
| 245 | + if volume.get('attached_to_SUBID') is None: |
| 246 | + return volume |
| 247 | + |
| 248 | + self.result['changed'] = True |
| 249 | + |
| 250 | + if not self.module.check_mode: |
| 251 | + data = { |
| 252 | + 'SUBID': volume['SUBID'], |
| 253 | + 'live': self.get_yes_or_no('live_attachment') |
| 254 | + } |
| 255 | + self.api_query( |
| 256 | + path='/v1/block/detach', |
| 257 | + method='POST', |
| 258 | + data=data |
| 259 | + ) |
| 260 | + |
| 261 | + volume = self.get_block_storage_volumes() |
| 262 | + else: |
| 263 | + volume['attached_to_SUBID'] = None |
| 264 | + |
| 265 | + self.result['diff']['after'] = volume |
| 266 | + |
| 267 | + return volume |
| 268 | + |
| 269 | + def attached_block_storage_volume(self): |
| 270 | + expected_server = self.module.params.get('attached_to_SUBID') |
| 271 | + volume = self.present_block_storage_volume() |
| 272 | + server = volume.get('attached_to_SUBID') |
| 273 | + if server == expected_server: |
| 274 | + return volume |
| 275 | + |
| 276 | + if server is not None: |
| 277 | + self.module.fail_json( |
| 278 | + msg='Volume already attached to server %s' % server |
| 279 | + ) |
| 280 | + |
| 281 | + self.result['changed'] = True |
| 282 | + |
| 283 | + if not self.module.check_mode: |
| 284 | + data = { |
| 285 | + 'SUBID': volume['SUBID'], |
| 286 | + # This API call expects a param called attach_to_SUBID, |
| 287 | + # but all the BlockStorage API response payloads call |
| 288 | + # this parameter attached_to_SUBID. So we'll standardize |
| 289 | + # to the latter and attached_to_id, but we'll pass the |
| 290 | + # expected attach_to_SUBID to this API call. |
| 291 | + 'attach_to_SUBID': expected_server, |
| 292 | + 'live': self.get_yes_or_no('live_attachment'), |
| 293 | + } |
| 294 | + self.api_query( |
| 295 | + path='/v1/block/attach', |
| 296 | + method='POST', |
| 297 | + data=data |
| 298 | + ) |
| 299 | + volume = self.get_block_storage_volumes() |
| 300 | + else: |
| 301 | + volume['attached_to_SUBID'] = expected_server |
| 302 | + |
| 303 | + self.result['diff']['after'] = volume |
| 304 | + |
| 305 | + return volume |
| 306 | + |
| 307 | + def ensure_volume_size(self, volume, expected_size): |
| 308 | + curr_size = volume.get('size_gb') |
| 309 | + # When creating, attaching, or detaching a volume in check_mode, |
| 310 | + # sadly, size_gb doesn't exist, because those methods return the |
| 311 | + # result of get_block_storage_volumes, which is {} on check_mode. |
| 312 | + if curr_size is None or curr_size >= expected_size: |
| 313 | + # we only resize volumes that are smaller than |
| 314 | + # expected. There's no shrinking operation. |
| 315 | + return volume |
| 316 | + |
| 317 | + self.result['changed'] = True |
| 318 | + |
| 319 | + volume['size_gb'] = expected_size |
| 320 | + self.result['diff']['after'] = volume |
| 321 | + |
| 322 | + if not self.module.check_mode: |
| 323 | + data = {'SUBID': volume['SUBID'], 'size_gb': expected_size} |
| 324 | + self.api_query( |
| 325 | + path='/v1/block/resize', |
| 326 | + method='POST', |
| 327 | + data=data, |
| 328 | + ) |
| 329 | + |
| 330 | + return volume |
| 331 | + |
219 | 332 |
|
220 | 333 | def main():
|
221 | 334 | argument_spec = vultr_argument_spec()
|
222 | 335 | argument_spec.update(dict(
|
223 | 336 | name=dict(type='str', required=True, aliases=['description', 'label']),
|
224 | 337 | size=dict(type='int'),
|
225 | 338 | region=dict(type='str'),
|
226 |
| - state=dict(type='str', choices=['present', 'absent'], default='present'), |
| 339 | + state=dict( |
| 340 | + type='str', |
| 341 | + choices=['present', 'absent', 'attached', 'detached'], |
| 342 | + default='present' |
| 343 | + ), |
| 344 | + attached_to_SUBID=dict(type='int', aliases=['attached_to_id']), |
| 345 | + live_attachment=dict(type='bool', default=True) |
227 | 346 | ))
|
228 | 347 |
|
229 | 348 | module = AnsibleModule(
|
230 | 349 | argument_spec=argument_spec,
|
231 | 350 | supports_check_mode=True,
|
232 |
| - required_if=[['state', 'present', ['size', 'region']]] |
| 351 | + required_if=[ |
| 352 | + ['state', 'present', ['size', 'region']], |
| 353 | + ['state', 'detached', ['size', 'region']], |
| 354 | + ['state', 'attached', ['size', 'region', 'attached_to_SUBID']], |
| 355 | + ] |
233 | 356 | )
|
234 | 357 |
|
235 | 358 | vultr_block_storage = AnsibleVultrBlockStorage(module)
|
236 |
| - if module.params.get('state') == "absent": |
| 359 | + |
| 360 | + desired_state = module.params.get('state') |
| 361 | + if desired_state == "absent": |
237 | 362 | volume = vultr_block_storage.absent_block_storage_volume()
|
| 363 | + elif desired_state == 'attached': |
| 364 | + volume = vultr_block_storage.attached_block_storage_volume() |
| 365 | + elif desired_state == 'detached': |
| 366 | + volume = vultr_block_storage.detached_block_storage_volume() |
238 | 367 | else:
|
239 | 368 | volume = vultr_block_storage.present_block_storage_volume()
|
240 | 369 |
|
| 370 | + expected_size = module.params.get('size') |
| 371 | + if expected_size and desired_state != 'absent': |
| 372 | + volume = vultr_block_storage.ensure_volume_size( |
| 373 | + volume, |
| 374 | + expected_size |
| 375 | + ) |
| 376 | + |
241 | 377 | result = vultr_block_storage.get_result(volume)
|
242 | 378 | module.exit_json(**result)
|
243 | 379 |
|
|
0 commit comments