2020-03-09 10:11:07 +01:00
|
|
|
#!/usr/bin/python
|
2021-08-08 10:40:22 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2020-03-09 10:11:07 +01:00
|
|
|
#
|
|
|
|
# Scaleway SSH keys management module
|
|
|
|
#
|
|
|
|
# Copyright (C) 2018 Online SAS.
|
|
|
|
# https://www.scaleway.com
|
|
|
|
#
|
2022-08-05 13:17:19 +02:00
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: scaleway_sshkey
|
|
|
|
short_description: Scaleway SSH keys management module
|
2021-12-08 18:32:26 +01:00
|
|
|
author: Remy Leone (@remyleone)
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
2023-01-12 21:56:21 +01:00
|
|
|
- "This module manages SSH keys on Scaleway account U(https://developer.scaleway.com)."
|
2020-03-09 10:11:07 +01:00
|
|
|
extends_documentation_fragment:
|
|
|
|
- community.general.scaleway
|
|
|
|
|
|
|
|
|
|
|
|
options:
|
|
|
|
state:
|
2020-11-12 08:26:54 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- Indicate desired state of the SSH key.
|
|
|
|
default: present
|
|
|
|
choices:
|
|
|
|
- present
|
|
|
|
- absent
|
|
|
|
ssh_pub_key:
|
2020-11-12 08:26:54 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- The public SSH key as a string to add.
|
|
|
|
required: true
|
|
|
|
api_url:
|
2020-11-12 08:26:54 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
2023-01-12 21:56:21 +01:00
|
|
|
- Scaleway API URL.
|
2020-03-09 10:11:07 +01:00
|
|
|
default: 'https://account.scaleway.com'
|
|
|
|
aliases: ['base_url']
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
- name: "Add SSH key"
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.scaleway_sshkey:
|
2020-03-09 10:11:07 +01:00
|
|
|
ssh_pub_key: "ssh-rsa AAAA..."
|
|
|
|
state: "present"
|
|
|
|
|
|
|
|
- name: "Delete SSH key"
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.scaleway_sshkey:
|
2020-03-09 10:11:07 +01:00
|
|
|
ssh_pub_key: "ssh-rsa AAAA..."
|
|
|
|
state: "absent"
|
|
|
|
|
|
|
|
- name: "Add SSH key with explicit token"
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.scaleway_sshkey:
|
2020-03-09 10:11:07 +01:00
|
|
|
ssh_pub_key: "ssh-rsa AAAA..."
|
|
|
|
state: "present"
|
|
|
|
oauth_token: "6ecd2c9b-6f4f-44d4-a187-61a92078d08c"
|
|
|
|
'''
|
|
|
|
|
|
|
|
RETURN = '''
|
|
|
|
data:
|
2023-01-12 21:56:21 +01:00
|
|
|
description: This is only present when I(state=present).
|
|
|
|
returned: when I(state=present)
|
2020-03-09 10:11:07 +01:00
|
|
|
type: dict
|
|
|
|
sample: {
|
|
|
|
"ssh_public_keys": [
|
|
|
|
{"key": "ssh-rsa AAAA...."}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
'''
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule, env_fallback
|
|
|
|
from ansible_collections.community.general.plugins.module_utils.scaleway import scaleway_argument_spec, Scaleway
|
|
|
|
|
|
|
|
|
|
|
|
def extract_present_sshkeys(raw_organization_dict):
|
|
|
|
ssh_key_list = raw_organization_dict["organizations"][0]["users"][0]["ssh_public_keys"]
|
|
|
|
ssh_key_lookup = [ssh_key["key"] for ssh_key in ssh_key_list]
|
|
|
|
return ssh_key_lookup
|
|
|
|
|
|
|
|
|
|
|
|
def extract_user_id(raw_organization_dict):
|
|
|
|
return raw_organization_dict["organizations"][0]["users"][0]["id"]
|
|
|
|
|
|
|
|
|
|
|
|
def sshkey_user_patch(ssh_lookup):
|
|
|
|
ssh_list = {"ssh_public_keys": [{"key": key}
|
|
|
|
for key in ssh_lookup]}
|
|
|
|
return ssh_list
|
|
|
|
|
|
|
|
|
|
|
|
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 main():
|
|
|
|
argument_spec = scaleway_argument_spec()
|
|
|
|
argument_spec.update(dict(
|
|
|
|
state=dict(default='present', choices=['absent', 'present']),
|
|
|
|
ssh_pub_key=dict(required=True),
|
|
|
|
api_url=dict(fallback=(env_fallback, ['SCW_API_URL']), default='https://account.scaleway.com', aliases=['base_url']),
|
|
|
|
))
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=argument_spec,
|
|
|
|
supports_check_mode=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
core(module)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|