def core(module): region = module.params["region"] server_id = module.params["server_id"] user_data = module.params["user_data"] changed = False module.params['api_url'] = SCALEWAY_LOCATION[region]["api_endpoint"] compute_api = Scaleway(module=module) user_data_list = compute_api.get(path="servers/%s/user_data" % server_id) if not user_data_list.ok: msg = 'Error during user_data fetching: %s %s' % user_data_list.status_code, user_data_list.body compute_api.module.fail_json(msg=msg) present_user_data_keys = user_data_list.json["user_data"] present_user_data = dict( (key, get_user_data(compute_api=compute_api, server_id=server_id, key=key)) for key in present_user_data_keys) if present_user_data == user_data: module.exit_json(changed=changed, msg=user_data_list.json) # First we remove keys that are not defined in the wished user_data for key in present_user_data: if key not in user_data: changed = True if compute_api.module.check_mode: module.exit_json(changed=changed, msg={ "status": "User-data of %s would be patched." % server_id }) delete_user_data(compute_api=compute_api, server_id=server_id, key=key) # Then we patch keys that are different for key, value in user_data.items(): if key not in present_user_data or user_data[key] != present_user_data[ key]: changed = True if compute_api.module.check_mode: module.exit_json(changed=changed, msg={ "status": "User-data of %s would be patched." % server_id }) patch_user_data(compute_api=compute_api, server_id=server_id, key=key, value=value) module.exit_json(changed=changed, msg=user_data)
def core(module): ssh_pub_key = module.params['ssh_pub_key'] state = module.params["state"] account_api = Scaleway(module) response = account_api.get('organizations') status_code = response.status_code organization_json = response.json if not response.ok: module.fail_json(msg='Error getting ssh key [{0}: {1}]'.format( status_code, response.json['message'])) user_id = extract_user_id(organization_json) present_sshkeys = [] try: present_sshkeys = extract_present_sshkeys(organization_json) except (KeyError, IndexError) as e: module.fail_json( changed=False, data="Error while extracting present SSH keys from API") if state in ('present', ): if ssh_pub_key in present_sshkeys: module.exit_json(changed=False) # If key not found create it! if module.check_mode: module.exit_json(changed=True) present_sshkeys.append(ssh_pub_key) payload = sshkey_user_patch(present_sshkeys) response = account_api.patch('/users/%s' % user_id, data=payload) if response.ok: module.exit_json(changed=True, data=response.json) module.fail_json(msg='Error creating ssh key [{0}: {1}]'.format( response.status_code, response.json)) elif state in ('absent', ): if ssh_pub_key not in present_sshkeys: module.exit_json(changed=False) if module.check_mode: module.exit_json(changed=True) present_sshkeys.remove(ssh_pub_key) payload = sshkey_user_patch(present_sshkeys) response = account_api.patch('/users/%s' % user_id, data=payload) if response.ok: module.exit_json(changed=True, data=response.json) module.fail_json(msg='Error deleting ssh key [{0}: {1}]'.format( response.status_code, response.json))
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))