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)
Esempio n. 2
0
def core(module):
    region = module.params["region"]
    state = module.params['state']
    name = module.params['name']
    organization = module.params['organization']
    project = module.params['project']
    size = module.params['size']
    volume_type = module.params['volume_type']
    module.params['api_url'] = SCALEWAY_LOCATION[region]["api_endpoint"]

    account_api = Scaleway(module)
    response = account_api.get('volumes')
    status_code = response.status_code
    volumes_json = response.json

    if project is None:
        project = organization

    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['project'] == project and volume['name'] == name:
            volumeByName = volume

    if state in ('present', ):
        if volumeByName is not None:
            module.exit_json(changed=False)

        payload = {
            'name': name,
            'project': project,
            '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))
Esempio n. 3
0
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))
Esempio n. 4
0
def core(module):
    api = Scaleway(module=module)

    security_group_rule = {
        'protocol': module.params['protocol'],
        'dest_port_from': module.params['port'],
        'ip_range': module.params['ip_range'],
        'direction': module.params['direction'],
        'action': module.params['action'],
    }

    region = module.params['region']
    module.params['api_url'] = SCALEWAY_LOCATION[region]['api_endpoint']

    if module.params['state'] == 'present':
        summary = present_strategy(
            api=api,
            security_group_id=module.params['security_group'],
            security_group_rule=security_group_rule)
    else:
        summary = absent_strategy(
            api=api,
            security_group_id=module.params['security_group'],
            security_group_rule=security_group_rule)
    module.exit_json(**summary)
Esempio n. 5
0
def core(module):
    region = module.params["region"]
    wished_server = {
        "state": module.params["state"],
        "image": module.params["image"],
        "name": module.params["name"],
        "commercial_type": module.params["commercial_type"],
        "enable_ipv6": module.params["enable_ipv6"],
        "tags": module.params["tags"],
        "organization": module.params["organization"],
        "security_group": module.params["security_group"]
    }
    module.params['api_url'] = SCALEWAY_LOCATION[region]["api_endpoint"]

    compute_api = Scaleway(module=module)

    check_image_id(compute_api, wished_server["image"])

    # IP parameters of the wished server depends on the configuration
    ip_payload = public_ip_payload(compute_api=compute_api,
                                   public_ip=module.params["public_ip"])
    wished_server.update(ip_payload)

    changed, summary = state_strategy[wished_server["state"]](
        compute_api=compute_api, wished_server=wished_server)
    module.exit_json(changed=changed, msg=summary)
Esempio n. 6
0
def core(module):
    region = module.params["region"]
    wished_load_balancer = {
        "state": module.params["state"],
        "name": module.params["name"],
        "description": module.params["description"],
        "tags": module.params["tags"],
        "organization_id": module.params["organization_id"]
    }
    module.params['api_url'] = SCALEWAY_ENDPOINT
    api = Scaleway(module=module)
    api.api_path = "lb/v1/regions/%s/lbs" % region

    changed, summary = state_strategy[wished_load_balancer["state"]](
        api=api, wished_lb=wished_load_balancer)
    module.exit_json(changed=changed, scaleway_lb=summary)
Esempio n. 7
0
def core(module):
    state = module.params['state']
    backup_id = module.params['id']

    account_api = Scaleway(module)

    if backup_id is None:
        backup_by_id = None
    else:
        response = account_api.get('/rdb/v1/regions/%s/backups/%s' % (module.params.get('region'), backup_id))
        status_code = response.status_code
        backup_json = response.json
        backup_by_id = None
        if status_code == 404:
            backup_by_id = None
        elif response.ok:
            backup_by_id = backup_json
        else:
            module.fail_json(msg='Error getting backup [{0}: {1}]'.format(status_code, response.json['message']))

    state_strategy[state](module, account_api, backup_by_id)
def core(module):
    wished_ip = {
        "organization": module.params['organization'],
        "reverse": module.params["reverse"],
        "id": module.params["id"],
        "server": module.params["server"]
    }

    region = module.params["region"]
    module.params['api_url'] = SCALEWAY_LOCATION[region]["api_endpoint"]

    api = Scaleway(module=module)
    if module.params["state"] == "absent":
        changed, summary = absent_strategy(api=api, wished_ip=wished_ip)
    else:
        changed, summary = present_strategy(api=api, wished_ip=wished_ip)
    module.exit_json(changed=changed, scaleway_ip=summary)
def core(module):

    wished_private_network = {
        "project": module.params['project'],
        "tags": module.params['tags'],
        "name": module.params['name']
    }

    region = module.params["region"]
    module.params['api_url'] = SCALEWAY_LOCATION[region]["api_endpoint_vpc"]

    api = Scaleway(module=module)
    if module.params["state"] == "absent":
        changed, summary = absent_strategy(
            api=api, wished_private_network=wished_private_network)
    else:
        changed, summary = present_strategy(
            api=api, wished_private_network=wished_private_network)
    module.exit_json(changed=changed, scaleway_private_network=summary)
Esempio n. 10
0
def core(module):
    security_group = {
        'organization': module.params['organization'],
        'name': module.params['name'],
        'description': module.params['description'],
        'stateful': module.params['stateful'],
        'inbound_default_policy': module.params['inbound_default_policy'],
        'outbound_default_policy': module.params['outbound_default_policy'],
        'organization_default': module.params['organization_default'],
    }

    region = module.params['region']
    module.params['api_url'] = SCALEWAY_LOCATION[region]['api_endpoint']

    api = Scaleway(module=module)
    if module.params['state'] == 'present':
        summary = present_strategy(api=api, security_group=security_group)
    else:
        summary = absent_strategy(api=api, security_group=security_group)
    module.exit_json(**summary)