def core(module): state = module.params['state'] name = module.params['name'] organization = module.params['organization'] size = module.params['size'] volume_type = module.params['volume_type'] account_api = Scaleway(module) response = account_api.get('volumes') status_code = response.status_code volumes_json = response.json if not response.ok: module.fail_json(msg='Error getting volume [{0}: {1}]'.format( status_code, response.json['message'])) volumeByName = None for volume in volumes_json['volumes']: if volume['organization'] == organization and volume['name'] == name: volumeByName = volume if state in ('present',): if volumeByName is not None: module.exit_json(changed=False) payload = {'name': name, 'organization': organization, 'size': size, 'volume_type': volume_type} response = account_api.post('/volumes', payload) if response.ok: module.exit_json(changed=True, data=response.json) module.fail_json(msg='Error creating volume [{0}: {1}]'.format( response.status_code, response.json)) elif state in ('absent',): if volumeByName is None: module.exit_json(changed=False) if module.check_mode: module.exit_json(changed=True) response = account_api.delete('/volumes/' + volumeByName['id']) if response.status_code == 204: module.exit_json(changed=True, data=response.json) module.fail_json(msg='Error deleting volume [{0}: {1}]'.format( response.status_code, response.json))
def core(module): id_ = module.params['id'] account_api = Scaleway(module) if module.check_mode: module.exit_json(changed=True) response = account_api.delete('/volumes/' + id_) if response.status_code == 204: module.exit_json(changed=True, data=response.json) elif response.status_code == 404: module.exit_json(changed=False) module.fail_json(msg='Error deleting volume [{0}: {1}]'.format( response.status_code, response.json))