scaleway: Refactor modules

This commit is contained in:
Rémy Léone 2018-08-20 10:50:28 +02:00 committed by Michael Scherer
commit 8f9d55529d
13 changed files with 544 additions and 652 deletions

View file

@ -25,6 +25,8 @@ version_added: "2.6"
author: Remy Leone (@sieben)
description:
- "This module manages compute instances on Scaleway."
extends_documentation_fragment: scaleway
options:
enable_ipv6:
@ -64,11 +66,6 @@ options:
required: false
default: []
oauth_token:
description:
- Scaleway OAuth token.
required: true
region:
description:
- Scaleway compute zone
@ -104,12 +101,6 @@ options:
- X64-60GB
- X64-120GB
timeout:
description:
- Timeout for API calls
required: false
default: 30
wait:
description:
- Wait for the instance to reach its desired state before returning.
@ -159,9 +150,8 @@ import datetime
import time
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.basic import env_fallback
from ansible.module_utils.six.moves.urllib.parse import quote as urlquote
from ansible.module_utils.scaleway import ScalewayAPI, SCALEWAY_LOCATION
from ansible.module_utils.scaleway import SCALEWAY_LOCATION, scaleway_argument_spec, Scaleway
SCALEWAY_COMMERCIAL_TYPES = [
@ -573,7 +563,6 @@ def server_change_attributes(compute_api, target_server, wished_server):
def core(module):
api_token = module.params['oauth_token']
region = module.params["region"]
wished_server = {
"state": module.params["state"],
@ -584,37 +573,31 @@ def core(module):
"tags": module.params["tags"],
"organization": module.params["organization"]
}
module.params['api_url'] = SCALEWAY_LOCATION[region]["api_endpoint"]
compute_api = ScalewayAPI(module=module,
headers={'X-Auth-Token': api_token},
base_url=SCALEWAY_LOCATION[region]["api_endpoint"])
compute_api = Scaleway(module=module)
changed, summary = state_strategy[wished_server["state"]](compute_api=compute_api, wished_server=wished_server)
module.exit_json(changed=changed, msg=summary)
def main():
argument_spec = scaleway_argument_spec()
argument_spec.update(dict(
image=dict(required=True),
name=dict(),
region=dict(required=True, choices=SCALEWAY_LOCATION.keys()),
commercial_type=dict(required=True, choices=SCALEWAY_COMMERCIAL_TYPES),
enable_ipv6=dict(default=False, type="bool"),
state=dict(choices=state_strategy.keys(), default='present'),
tags=dict(type="list", default=[]),
organization=dict(required=True),
wait=dict(type="bool", default=False),
wait_timeout=dict(type="int", default=300),
wait_sleep_time=dict(type="int", default=3),
))
module = AnsibleModule(
argument_spec=dict(
oauth_token=dict(
no_log=True,
# Support environment variable for Scaleway OAuth Token
fallback=(env_fallback, ['SCW_TOKEN', 'SCW_API_KEY', 'SCW_OAUTH_TOKEN']),
required=True,
),
image=dict(required=True),
name=dict(),
region=dict(required=True, choices=SCALEWAY_LOCATION.keys()),
commercial_type=dict(required=True, choices=SCALEWAY_COMMERCIAL_TYPES),
enable_ipv6=dict(default=False, type="bool"),
state=dict(choices=state_strategy.keys(), default='present'),
tags=dict(type="list", default=[]),
organization=dict(required=True),
timeout=dict(type="int", default=30),
wait=dict(type="bool", default=False),
wait_timeout=dict(type="int", default=300),
wait_sleep_time=dict(type="int", default=3),
),
argument_spec=argument_spec,
supports_check_mode=True,
)

View file

@ -26,12 +26,13 @@ author: Remy Leone (@sieben)
description:
- This module manages SSH keys on Scaleway account
U(https://developer.scaleway.com)
extends_documentation_fragment: scaleway
options:
state:
description:
- Indicate desired state of the SSH key.
required: true
default: present
choices:
- present
- absent
@ -39,18 +40,11 @@ options:
description:
- The public SSH key as a string to add.
required: true
oauth_token:
api_url:
description:
- Scaleway OAuth token.
required: true
timeout:
description:
- Timeout for API calls
default: 30
base_url:
description:
- Base URL for account API
default: "https://account.scaleway.com"
- Scaleway API URL
default: 'https://account.scaleway.com'
aliases: ['base_url']
'''
EXAMPLES = '''
@ -83,9 +77,8 @@ data:
}
'''
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.basic import env_fallback
from ansible.module_utils.scaleway import ScalewayAPI
from ansible.module_utils.basic import AnsibleModule, env_fallback
from ansible.module_utils.scaleway import scaleway_argument_spec, Scaleway
def extract_present_sshkeys(raw_organization_dict):
@ -105,12 +98,9 @@ def sshkey_user_patch(ssh_lookup):
def core(module):
api_token = module.params['oauth_token']
ssh_pub_key = module.params['ssh_pub_key']
state = module.params["state"]
account_api = ScalewayAPI(module,
headers={'X-Auth-Token': api_token},
base_url=module.params["base_url"])
account_api = Scaleway(module)
response = account_api.get('organizations')
status_code = response.status_code
@ -166,19 +156,14 @@ def core(module):
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=dict(
base_url=dict(default='https://account.scaleway.com'),
oauth_token=dict(
no_log=True,
# Support environment variable for Scaleway OAuth Token
fallback=(env_fallback, ['SCW_TOKEN', 'SCW_API_KEY', 'SCW_OAUTH_TOKEN']),
required=True,
),
state=dict(choices=['present', 'absent'], required=True),
ssh_pub_key=dict(required=True),
timeout=dict(type='int', default=30),
),
argument_spec=argument_spec,
supports_check_mode=True,
)

View file

@ -25,19 +25,16 @@ author: Henryk Konsek (@hekonsek)
description:
- This module manages volumes on Scaleway account
U(https://developer.scaleway.com)
extends_documentation_fragment: scaleway
options:
state:
description:
- Indicate desired state of the volume.
required: true
default: present
choices:
- present
- absent
oauth_token:
description:
- Scaleway OAuth token.
required: true
region:
description:
- Scaleway region to use (for example par1).
@ -60,10 +57,6 @@ options:
volume_type:
description:
- Type of the volume (for example 'l_ssd').
timeout:
description:
- Timeout for API calls
default: 30
'''
EXAMPLES = '''
@ -103,24 +96,18 @@ data:
}
'''
from ansible.module_utils.scaleway import ScalewayAPI, SCALEWAY_LOCATION
from ansible.module_utils.scaleway import SCALEWAY_LOCATION, scaleway_argument_spec, Scaleway
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.basic import env_fallback
from ansible.module_utils.scaleway import ScalewayAPI
def core(module):
api_token = module.params['oauth_token']
region = module.params['region']
state = module.params['state']
name = module.params['name']
organization = module.params['organization']
size = module.params['size']
volume_type = module.params['volume_type']
account_api = ScalewayAPI(module,
headers={'X-Auth-Token': api_token},
base_url=SCALEWAY_LOCATION[region]['api_endpoint'])
account_api = Scaleway(module)
response = account_api.get('volumes')
status_code = response.status_code
volumes_json = response.json
@ -164,22 +151,17 @@ def core(module):
def main():
argument_spec = scaleway_argument_spec()
argument_spec.update(dict(
state=dict(default='present', choices=['absent', 'present']),
name=dict(required=True),
size=dict(type='int'),
organization=dict(),
volume_type=dict(),
region=dict(required=True, choices=SCALEWAY_LOCATION.keys()),
))
module = AnsibleModule(
argument_spec=dict(
region=dict(required=True, choices=SCALEWAY_LOCATION.keys()),
oauth_token=dict(
no_log=True,
# Support environment variable for Scaleway OAuth Token
fallback=(env_fallback, ['SCW_TOKEN', 'SCW_API_KEY', 'SCW_OAUTH_TOKEN']),
required=True,
),
state=dict(choices=['present', 'absent'], required=True),
name=dict(required=True),
size=dict(type='int'),
organization=dict(),
volume_type=dict(),
timeout=dict(type='int', default=30),
),
argument_spec=argument_spec,
supports_check_mode=True,
)