parent
d75763115b
commit
ce9fb0ceea
@ -0,0 +1,157 @@
|
|||||||
|
#!/usr/bin/python
|
||||||
|
#
|
||||||
|
# Digital Enegry Cloud Orchestration Technology (DECORT) modules for Ansible
|
||||||
|
# Copyright: (c) 2018-2020 Digital Energy Cloud Solutions LLC
|
||||||
|
#
|
||||||
|
# Apache License 2.0 (see http://www.apache.org/licenses/LICENSE-2.0.txt)
|
||||||
|
#
|
||||||
|
|
||||||
|
#
|
||||||
|
# Author: Sergey Shubin (sergey.shubin@digitalenergy.online)
|
||||||
|
#
|
||||||
|
|
||||||
|
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
||||||
|
'status': ['preview'],
|
||||||
|
'supported_by': 'community'}
|
||||||
|
|
||||||
|
DOCUMENTATION = '''
|
||||||
|
---
|
||||||
|
module: decort_jwt
|
||||||
|
short_description: Obtain access token to be used for authentication to DECORT cloud controller
|
||||||
|
description:
|
||||||
|
- Obtain JWT (JSON Web Token) from the specified Oauth2 provider. This JWT can be used in subsequent DECS modules'
|
||||||
|
invocations to authenticate them to the DECS cloud controller.
|
||||||
|
version_added: "2.4"
|
||||||
|
author: "Sergey Shubin (sergey.shubin@digitalenergy.online)"
|
||||||
|
notes:
|
||||||
|
- Environment variables can be used to pass parameters to the module (see options below for details).
|
||||||
|
- Specified Oauth2 provider must be trusted by the DECORT cloud controller on which JWT will be used.
|
||||||
|
- 'If you register module output as I(my_jwt), the JWT value is accessed as I(my_jwt.jwt)'
|
||||||
|
requirements:
|
||||||
|
- python >= 2.6
|
||||||
|
- PyJWT module
|
||||||
|
- requests module
|
||||||
|
- decort_utils utility library (module)
|
||||||
|
- DECORT cloud platform version 3.4.0 or higher
|
||||||
|
options:
|
||||||
|
app_id:
|
||||||
|
description:
|
||||||
|
- 'Application ID for authenticating to the Oauth2 provider specified in I(oauth2_url).'
|
||||||
|
- 'If not found in the playbook or command line arguments, the value will be taken from DECORT_APP_ID
|
||||||
|
environment variable.'
|
||||||
|
required: no
|
||||||
|
app_secret:
|
||||||
|
description:
|
||||||
|
- 'Application API secret used for authenticating to the Oauth2 provider specified in I(oauth2_url).'
|
||||||
|
- 'If not found in the playbook or command line arguments, the value will be taken from DECORT_APP_SECRET
|
||||||
|
environment variable.'
|
||||||
|
required: no
|
||||||
|
oauth2_url:
|
||||||
|
description:
|
||||||
|
- 'URL of the oauth2 authentication provider to obtain JWT from.'
|
||||||
|
- If not specified in the playbook, the value will be taken from DECORT_OAUTH2_URL environment variable.
|
||||||
|
validity:
|
||||||
|
description:
|
||||||
|
- Validity of the JWT in seconds. Default value is 3600 (one hour).
|
||||||
|
required: no
|
||||||
|
verify_ssl:
|
||||||
|
description:
|
||||||
|
- 'Controls SSL verification mode when making API calls to DECS controller. Set it to False if you
|
||||||
|
want to disable SSL certificate verification.'
|
||||||
|
- `Intended use case is when you run module in a trusted environment that uses self-signed certificates.
|
||||||
|
Note that disabling SSL verification in any other scenario can lead to security issues, so please use
|
||||||
|
with caution.'
|
||||||
|
default: True
|
||||||
|
required: no
|
||||||
|
'''
|
||||||
|
|
||||||
|
EXAMPLES = '''
|
||||||
|
- name: Obtain JWT and store it as my_jwt for authenticating subsequent task to DECORT cloud controller
|
||||||
|
decort_jwt:
|
||||||
|
app_id: "{{ my_app_id }}"
|
||||||
|
app_secret: "{{ my_app_secret }}"
|
||||||
|
oauth2_url: https://sso.decs.online
|
||||||
|
delegate_to: localhost
|
||||||
|
register: my_jwt
|
||||||
|
'''
|
||||||
|
|
||||||
|
RETURN = '''
|
||||||
|
jwt:
|
||||||
|
description: JSON Web Token that can be used to access DECS cloud controller
|
||||||
|
returned: always
|
||||||
|
type: string
|
||||||
|
sample: None
|
||||||
|
'''
|
||||||
|
|
||||||
|
import requests
|
||||||
|
|
||||||
|
from ansible.module_utils.basic import AnsibleModule
|
||||||
|
from ansible.module_utils.basic import env_fallback
|
||||||
|
|
||||||
|
def decort_jwt_parameters():
|
||||||
|
"""Build and return a dictionary of parameters expected by decort_jwt module in a form accepted
|
||||||
|
by AnsibleModule utility class"""
|
||||||
|
|
||||||
|
return dict(
|
||||||
|
app_id=dict(type='str',
|
||||||
|
required=True,
|
||||||
|
fallback=(env_fallback, ['DECORT_APP_ID'])),
|
||||||
|
app_secret=dict(type='str',
|
||||||
|
required=True,
|
||||||
|
fallback=(env_fallback, ['DECORT_APP_SECRET']),
|
||||||
|
no_log=True),
|
||||||
|
oauth2_url=dict(type='str',
|
||||||
|
required=True,
|
||||||
|
fallback=(env_fallback, ['DECORT_OAUTH2_URL'])),
|
||||||
|
validity=dict(type='int',
|
||||||
|
required=False,
|
||||||
|
default=3600),
|
||||||
|
verify_ssl=dict(type='bool', required=False, default=True),
|
||||||
|
workflow_callback=dict(type='str', required=False),
|
||||||
|
workflow_context=dict(type='str', required=False),
|
||||||
|
)
|
||||||
|
|
||||||
|
def main():
|
||||||
|
module_parameters = decort_jwt_parameters()
|
||||||
|
|
||||||
|
amodule = AnsibleModule(argument_spec=module_parameters,
|
||||||
|
supports_check_mode=True,)
|
||||||
|
|
||||||
|
result = {'failed': False, 'changed': False}
|
||||||
|
|
||||||
|
token_get_url = amodule.params['oauth2_url'] + "/v1/oauth/access_token"
|
||||||
|
req_data = dict(grant_type="client_credentials",
|
||||||
|
client_id=amodule.params['app_id'],
|
||||||
|
client_secret=amodule.params['app_secret'],
|
||||||
|
response_type="id_token",
|
||||||
|
validity=amodule.params['validity'],)
|
||||||
|
# TODO: Need standard code snippet to handle server timeouts gracefully
|
||||||
|
# Consider a few retries before giving up or use requests.Session & requests.HTTPAdapter
|
||||||
|
# see https://stackoverflow.com/questions/15431044/can-i-set-max-retries-for-requests-request
|
||||||
|
|
||||||
|
# catch requests.exceptions.ConnectionError to handle incorrect oauth2_url case
|
||||||
|
try:
|
||||||
|
token_get_resp = requests.post(token_get_url, data=req_data, verify=amodule.params['verify_ssl'])
|
||||||
|
except requests.exceptions.ConnectionError:
|
||||||
|
result.update(failed=True)
|
||||||
|
result['msg'] = "Failed to connect to {}".format(token_get_url)
|
||||||
|
amodule.fail_json(**result)
|
||||||
|
except requests.exceptions.Timeout:
|
||||||
|
result.update(failed=True)
|
||||||
|
result['msg'] = "Timeout when trying to connect to {}".format(token_get_url)
|
||||||
|
amodule.fail_json(**result)
|
||||||
|
|
||||||
|
# alternative -- if resp == requests.codes.ok
|
||||||
|
if token_get_resp.status_code != 200:
|
||||||
|
result.update(failed=True)
|
||||||
|
result['msg'] = "Failed to obtain JWT access token from oauth2_url {} for app_id {}: {} {}".format(
|
||||||
|
token_get_url, amodule.params['app_id'],
|
||||||
|
token_get_resp.status_code, token_get_resp.reason)
|
||||||
|
amodule.fail_json(**result)
|
||||||
|
|
||||||
|
# Common return values: https://docs.ansible.com/ansible/2.3/common_return_values.html
|
||||||
|
result['jwt'] = token_get_resp.content.decode('utf8')
|
||||||
|
amodule.exit_json(**result)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
@ -0,0 +1,299 @@
|
|||||||
|
#!/usr/bin/python
|
||||||
|
#
|
||||||
|
# Digital Enegry Cloud Orchestration Technology (DECORT) modules for Ansible
|
||||||
|
# Copyright: (c) 2018-2020 Digital Energy Cloud Solutions LLC
|
||||||
|
#
|
||||||
|
# Apache License 2.0 (see http://www.apache.org/licenses/LICENSE-2.0.txt)
|
||||||
|
#
|
||||||
|
|
||||||
|
#
|
||||||
|
# Author: Sergey Shubin (sergey.shubin@digitalenergy.online)
|
||||||
|
#
|
||||||
|
|
||||||
|
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
||||||
|
'status': ['preview'],
|
||||||
|
'supported_by': 'community'}
|
||||||
|
|
||||||
|
DOCUMENTATION = '''
|
||||||
|
---
|
||||||
|
module: decort_osimage
|
||||||
|
short_description: Locate OS image in DCORT cloud by its name and return image ID
|
||||||
|
description: >
|
||||||
|
This module can be used to obtain image ID of an OS image in DECORT cloud to use with subsequent calls to
|
||||||
|
decort_vm module for batch VM provisioning. It will speed up VM creation and save a bunch of extra calls to
|
||||||
|
DECORT cloud controller on each VM creation act.
|
||||||
|
Note that this module is effectively an information provisioner. It is not designed to and does not manage
|
||||||
|
nor change state of OS image (or any other) objects in DECORT cloud.
|
||||||
|
version_added: "2.2"
|
||||||
|
author:
|
||||||
|
- Sergey Shubin <sergey.shubin@digitalenergy.online>
|
||||||
|
requirements:
|
||||||
|
- python >= 2.6
|
||||||
|
- PyJWT module
|
||||||
|
- requests module
|
||||||
|
- decort_utils utility library (module)
|
||||||
|
- DECORT cloud platform version 3.4.0 or higher.
|
||||||
|
notes:
|
||||||
|
- Environment variables can be used to pass selected parameters to the module, see details below.
|
||||||
|
- Specified Oauth2 provider must be trusted by the DECORT cloud controller on which JWT will be used.
|
||||||
|
- 'Similarly, JWT supplied in I(authenticator=jwt) mode should be received from Oauth2 provider trusted by
|
||||||
|
the DECORT cloud controller on which this JWT will be used.'
|
||||||
|
options:
|
||||||
|
app_id:
|
||||||
|
description:
|
||||||
|
- 'Application ID for authenticating to the DECORT controller when I(authenticator=oauth2).'
|
||||||
|
- 'Required if I(authenticator=oauth2).'
|
||||||
|
- 'If not found in the playbook or command line arguments, the value will be taken from DECORT_APP_ID
|
||||||
|
environment variable.'
|
||||||
|
required: no
|
||||||
|
app_secret:
|
||||||
|
description:
|
||||||
|
- 'Application API secret used for authenticating to the DECORT controller when I(authenticator=oauth2).'
|
||||||
|
- This parameter is required when I(authenticator=oauth2) and ignored in other modes.
|
||||||
|
- 'If not found in the playbook or command line arguments, the value will be taken from DECORT_APP_SECRET
|
||||||
|
environment variable.'
|
||||||
|
required: no
|
||||||
|
authenticator:
|
||||||
|
description:
|
||||||
|
- Authentication mechanism to be used when accessing DECORT controller and authorizing API call.
|
||||||
|
default: jwt
|
||||||
|
choices: [ jwt, oauth2, legacy ]
|
||||||
|
required: yes
|
||||||
|
controller_url:
|
||||||
|
description:
|
||||||
|
- URL of the DECORT controller that will be contacted to obtain OS image details.
|
||||||
|
- 'This parameter is always required regardless of the specified I(authenticator) type.'
|
||||||
|
required: yes
|
||||||
|
image_name:
|
||||||
|
description:
|
||||||
|
- Name of the OS image to use. Module will return the ID of this image.
|
||||||
|
- 'The specified image name will be looked up in the target DECORT controller and error will be generated if
|
||||||
|
no matching image is found.'
|
||||||
|
required: yes
|
||||||
|
jwt:
|
||||||
|
description:
|
||||||
|
- 'JWT (access token) for authenticating to the DECORT controller when I(authenticator=jwt).'
|
||||||
|
- 'This parameter is required if I(authenticator=jwt) and ignored for other authentication modes.'
|
||||||
|
- If not specified in the playbook, the value will be taken from DECORT_JWT environment variable.
|
||||||
|
required: no
|
||||||
|
oauth2_url:
|
||||||
|
description:
|
||||||
|
- 'URL of the oauth2 authentication provider to use when I(authenticator=oauth2).'
|
||||||
|
- 'This parameter is required when when I(authenticator=oauth2).'
|
||||||
|
- If not specified in the playbook, the value will be taken from DECORT_OAUTH2_URL environment variable.
|
||||||
|
password:
|
||||||
|
description:
|
||||||
|
- 'Password for authenticating to the DECORT controller when I(authenticator=legacy).'
|
||||||
|
- 'This parameter is required if I(authenticator=legacy) and ignored in other authentication modes.'
|
||||||
|
- If not specified in the playbook, the value will be taken from DECORT_PASSWORD environment variable.
|
||||||
|
required: no
|
||||||
|
pool:
|
||||||
|
description:
|
||||||
|
- 'Name of the storage pool, where the image should be found.'
|
||||||
|
- 'Omit this option if no matching by pool name is required. The first matching image will be returned."
|
||||||
|
required: no
|
||||||
|
sep_id:
|
||||||
|
description:
|
||||||
|
- 'ID of the SEP (Storage End-point Provider), where the image should be found.'
|
||||||
|
- 'Omit this option if no matching by SEP ID is required. The first matching image will be returned."
|
||||||
|
required: no
|
||||||
|
account_name:
|
||||||
|
description:
|
||||||
|
- 'Name of the account for which the specified OS image will be looked up.'
|
||||||
|
- 'This parameter is required for listing OS images.'
|
||||||
|
required: yes
|
||||||
|
user:
|
||||||
|
description:
|
||||||
|
- 'Name of the legacy user for authenticating to the DECORT controller when I(authenticator=legacy).'
|
||||||
|
- 'This parameter is required when I(authenticator=legacy) and ignored for other authentication modes.'
|
||||||
|
- If not specified in the playbook, the value will be taken from DECORT_USER environment variable.
|
||||||
|
required: no
|
||||||
|
vdc_id:
|
||||||
|
description:
|
||||||
|
- ID of the VDC to limit the search of the OS image to.
|
||||||
|
required: no
|
||||||
|
verify_ssl:
|
||||||
|
description:
|
||||||
|
- 'Controls SSL verification mode when making API calls to DECORT controller. Set it to False if you
|
||||||
|
want to disable SSL certificate verification. Intended use case is when you run module in a trusted
|
||||||
|
environment that uses self-signed certificates. Note that disabling SSL verification in any other
|
||||||
|
scenario can lead to security issues, so please know what you are doing.'
|
||||||
|
default: True
|
||||||
|
required: no
|
||||||
|
workflow_callback:
|
||||||
|
description:
|
||||||
|
- 'Callback URL that represents an application, which invokes this module (e.g. up-level orchestrator or
|
||||||
|
end-user portal) and may except out-of-band updates on progress / exit status of the module run.'
|
||||||
|
- API call at this URL will be used to relay such information to the application.
|
||||||
|
- 'API call payload will include module-specific details about this module run and I(workflow_context).'
|
||||||
|
required: no
|
||||||
|
workflow_context:
|
||||||
|
description:
|
||||||
|
- 'Context data that will be included into the payload of the API call directed at I(workflow_callback) URL.'
|
||||||
|
- 'This context data is expected to uniquely identify the task carried out by this module invocation so
|
||||||
|
that up-level orchestrator could match returned information to the its internal entities.'
|
||||||
|
required: no
|
||||||
|
'''
|
||||||
|
|
||||||
|
EXAMPLES = '''
|
||||||
|
- name: locate OS image specified by its name, store result in image_to_use variable.
|
||||||
|
decort_osimage:
|
||||||
|
authenticator: oauth2
|
||||||
|
app_id: "{{ MY_APP_ID }}"
|
||||||
|
app_secret: "{{ MY_APP_SECRET }}"
|
||||||
|
controller_url: "https://ds1.digitalenergy.online"
|
||||||
|
image_name: "Ubuntu 18.04 v1.2.5"
|
||||||
|
account_name: "GreyseDevelopment"
|
||||||
|
delegate_to: localhost
|
||||||
|
register: image_to_use
|
||||||
|
'''
|
||||||
|
|
||||||
|
RETURN = '''
|
||||||
|
facts:
|
||||||
|
description: facts about the specified OS image
|
||||||
|
returned: always
|
||||||
|
type: dict
|
||||||
|
sample:
|
||||||
|
facts:
|
||||||
|
id: 100
|
||||||
|
name: "Ubuntu 16.04 v1.0"
|
||||||
|
size: 3
|
||||||
|
sep_id: 1
|
||||||
|
pool: "vmstore"
|
||||||
|
type: Linux
|
||||||
|
arch: x86_64
|
||||||
|
state: CREATED
|
||||||
|
'''
|
||||||
|
|
||||||
|
from ansible.module_utils.basic import AnsibleModule
|
||||||
|
from ansible.module_utils.basic import env_fallback
|
||||||
|
|
||||||
|
from ansible.module_utils.decort_utils import *
|
||||||
|
|
||||||
|
|
||||||
|
def decort_osimage_package_facts(arg_osimage_facts, arg_check_mode=False):
|
||||||
|
"""Package a dictionary of OS image according to the decort_osimage module specification. This
|
||||||
|
dictionary will be returned to the upstream Ansible engine at the completion of the module run.
|
||||||
|
|
||||||
|
@param arg_osimage_facts: dictionary with OS image facts as returned by API call to .../images/list
|
||||||
|
@param arg_check_mode: boolean that tells if this Ansible module is run in check mode.
|
||||||
|
|
||||||
|
@return: dictionary with OS image specs populated from arg_osimage_facts.
|
||||||
|
"""
|
||||||
|
|
||||||
|
ret_dict = dict(id=0,
|
||||||
|
name="none",
|
||||||
|
size=0,
|
||||||
|
type="none",
|
||||||
|
state="CHECK_MODE",
|
||||||
|
)
|
||||||
|
|
||||||
|
if arg_check_mode:
|
||||||
|
# in check mode return immediately with the default values
|
||||||
|
return ret_dict
|
||||||
|
|
||||||
|
if arg_osimage_facts is None:
|
||||||
|
# if void facts provided - change state value to ABSENT and return
|
||||||
|
ret_dict['state'] = "ABSENT"
|
||||||
|
return ret_dict
|
||||||
|
|
||||||
|
ret_dict['id'] = arg_osimage_facts['id']
|
||||||
|
ret_dict['name'] = arg_osimage_facts['name']
|
||||||
|
ret_dict['size'] = arg_osimage_facts['size']
|
||||||
|
ret_dict['type'] = arg_osimage_facts['type']
|
||||||
|
# ret_dict['arch'] = arg_osimage_facts['architecture']
|
||||||
|
ret_dict['sep_id'] = arg_osimage_facts['sepid']
|
||||||
|
ret_dict['pool'] = arg_osimage_facts['pool']
|
||||||
|
ret_dict['state'] = arg_osimage_facts['status']
|
||||||
|
|
||||||
|
return ret_dict
|
||||||
|
|
||||||
|
def decort_osimage_parameters():
|
||||||
|
"""Build and return a dictionary of parameters expected by decort_osimage module in a form accepted
|
||||||
|
by AnsibleModule utility class."""
|
||||||
|
|
||||||
|
return dict(
|
||||||
|
app_id=dict(type='str',
|
||||||
|
required=False,
|
||||||
|
fallback=(env_fallback, ['DECORT_APP_ID'])),
|
||||||
|
app_secret=dict(type='str',
|
||||||
|
required=False,
|
||||||
|
fallback=(env_fallback, ['DECORT_APP_SECRET']),
|
||||||
|
no_log=True),
|
||||||
|
authenticator=dict(type='str',
|
||||||
|
required=True,
|
||||||
|
choices=['legacy', 'oauth2', 'jwt']),
|
||||||
|
controller_url=dict(type='str', required=True),
|
||||||
|
image_name=dict(type='str', required=True),
|
||||||
|
jwt=dict(type='str',
|
||||||
|
required=False,
|
||||||
|
fallback=(env_fallback, ['DECORT_JWT']),
|
||||||
|
no_log=True),
|
||||||
|
oauth2_url=dict(type='str',
|
||||||
|
required=False,
|
||||||
|
fallback=(env_fallback, ['DECORT_OAUTH2_URL'])),
|
||||||
|
password=dict(type='str',
|
||||||
|
required=False,
|
||||||
|
fallback=(env_fallback, ['DECORT_PASSWORD']),
|
||||||
|
no_log=True),
|
||||||
|
pool=dict(type='str', required=False, default=""),
|
||||||
|
sep_id=dict(type='int', required=False, default=0),
|
||||||
|
account_name=dict(type='str', required=True),
|
||||||
|
user=dict(type='str',
|
||||||
|
required=False,
|
||||||
|
fallback=(env_fallback, ['DECORT_USER'])),
|
||||||
|
vdc_id=dict(type='int', required=False, default=0),
|
||||||
|
verify_ssl=dict(type='bool', required=False, default=True),
|
||||||
|
workflow_callback=dict(type='str', required=False),
|
||||||
|
workflow_context=dict(type='str', required=False),
|
||||||
|
)
|
||||||
|
|
||||||
|
# Workflow digest:
|
||||||
|
# 1) authenticate to DECORT controller & validate authentication by issuing API call - done when
|
||||||
|
# creating DecortController
|
||||||
|
# 2) obtain a list of OS images accessible to the specified account (and optionally - within
|
||||||
|
# the specified VDC)
|
||||||
|
# 3) match specified OS image by its name - if image is not found abort the module
|
||||||
|
# 5) report result to Ansible
|
||||||
|
|
||||||
|
def main():
|
||||||
|
module_parameters = decort_osimage_parameters()
|
||||||
|
|
||||||
|
amodule = AnsibleModule(argument_spec=module_parameters,
|
||||||
|
supports_check_mode=True,
|
||||||
|
mutually_exclusive=[
|
||||||
|
['oauth2', 'password'],
|
||||||
|
['password', 'jwt'],
|
||||||
|
['jwt', 'oauth2'],
|
||||||
|
],
|
||||||
|
required_together=[
|
||||||
|
['app_id', 'app_secret'],
|
||||||
|
['user', 'password'],
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
||||||
|
decon = DecortController(amodule)
|
||||||
|
|
||||||
|
# we need account ID to locate OS images - find the account by the specified name and get its ID
|
||||||
|
account_id, _ = decon.account_find(amodule.params['account_name'])
|
||||||
|
if account_id == 0:
|
||||||
|
# we failed either to find or access the specified account - fail the module
|
||||||
|
decon.result['failed'] = True
|
||||||
|
decon.result['changed'] = False
|
||||||
|
decon.result['msg'] = ("Cannot find account '{}'").format(amodule.params['account_name'])
|
||||||
|
amodule.fail_json(**decon.result)
|
||||||
|
|
||||||
|
osimage_facts = decon.image_find(amodule.params['image_name'], 0, account_id,
|
||||||
|
amodule.params['sep_id'], amodule.params['pool'])
|
||||||
|
if decon.result['failed'] == True:
|
||||||
|
# we failed to find the specified image - fail the module
|
||||||
|
decon.result['changed'] = False
|
||||||
|
amodule.fail_json(**decon.result)
|
||||||
|
|
||||||
|
decon.result['facts'] = decort_osimage_package_facts(osimage_facts, amodule.check_mode)
|
||||||
|
decon.result['changed'] = False # decort_osimage is a read-only module - make sure the 'changed' flag is set to False
|
||||||
|
amodule.exit_json(**decon.result)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
@ -0,0 +1,463 @@
|
|||||||
|
#!/usr/bin/python
|
||||||
|
#
|
||||||
|
# Digital Enegry Cloud Orchestration Technology (DECORT) modules for Ansible
|
||||||
|
# Copyright: (c) 2018-2020 Digital Energy Cloud Solutions LLC
|
||||||
|
#
|
||||||
|
# Apache License 2.0 (see http://www.apache.org/licenses/LICENSE-2.0.txt)
|
||||||
|
#
|
||||||
|
|
||||||
|
#
|
||||||
|
# Author: Sergey Shubin (sergey.shubin@digitalenergy.online)
|
||||||
|
#
|
||||||
|
|
||||||
|
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
||||||
|
'status': ['preview'],
|
||||||
|
'supported_by': 'community'}
|
||||||
|
|
||||||
|
DOCUMENTATION = '''
|
||||||
|
---
|
||||||
|
module: decort_rg
|
||||||
|
short_description: Manage resource groups (RGs) in DECORT cloud
|
||||||
|
description: >
|
||||||
|
This module can be used to create a new resource group in DECORT cloud platform, modify its
|
||||||
|
characteristics, and delete it.
|
||||||
|
version_added: "2.2"
|
||||||
|
author:
|
||||||
|
- Sergey Shubin <sergey.shubin@digitalenergy.online>
|
||||||
|
requirements:
|
||||||
|
- python >= 2.6
|
||||||
|
- PyJWT module
|
||||||
|
- requests module
|
||||||
|
- decort_utils utility library (module)
|
||||||
|
- DECORT cloud platform version 3.4.0 or higher
|
||||||
|
notes:
|
||||||
|
- Environment variables can be used to pass selected parameters to the module, see details below.
|
||||||
|
- Specified Oauth2 provider must be trusted by the DECORT cloud controller on which JWT will be used.
|
||||||
|
- 'Similarly, JWT supplied in I(authenticator=jwt) mode should be received from Oauth2 provider trusted by
|
||||||
|
the DECORT cloud controller on which this JWT will be used.'
|
||||||
|
- New RGs provisioned with this module will be deployed to the first location under specified DECORT
|
||||||
|
controller (if there is more than one location).
|
||||||
|
options:
|
||||||
|
account_id:
|
||||||
|
description:
|
||||||
|
- ID of the account under which this RG will be created. This is the alternative to I(account_name)
|
||||||
|
option. If both I(account_id) and I(account_name) specified, the latter is ignored.
|
||||||
|
account_name:
|
||||||
|
description:
|
||||||
|
- 'Name of the account under which this RG will be created (for new RGs) or is located.'
|
||||||
|
- 'This parameter is ignored if I(account_id) is specified.'
|
||||||
|
required: no
|
||||||
|
annotation:
|
||||||
|
description:
|
||||||
|
- Optional text description of this resource group.
|
||||||
|
default: empty string
|
||||||
|
required: no
|
||||||
|
app_id:
|
||||||
|
description:
|
||||||
|
- 'Application ID for authenticating to the DECORT controller when I(authenticator=oauth2).'
|
||||||
|
- 'Required if I(authenticator=oauth2).'
|
||||||
|
- 'If not found in the playbook or command line arguments, the value will be taken from DECORT_APP_ID
|
||||||
|
environment variable.'
|
||||||
|
required: no
|
||||||
|
app_secret:
|
||||||
|
description:
|
||||||
|
- 'Application API secret used for authenticating to the DECORT controller when I(authenticator=oauth2).'
|
||||||
|
- This parameter is required when I(authenticator=oauth2) and ignored in other modes.
|
||||||
|
- 'If not found in the playbook or command line arguments, the value will be taken from DECORT_APP_SECRET
|
||||||
|
environment variable.'
|
||||||
|
required: no
|
||||||
|
authenticator:
|
||||||
|
description:
|
||||||
|
- Authentication mechanism to be used when accessing DECORT controller and authorizing API call.
|
||||||
|
default: jwt
|
||||||
|
choices: [ jwt, oauth2, legacy ]
|
||||||
|
required: yes
|
||||||
|
controller_url:
|
||||||
|
description:
|
||||||
|
- URL of the DECORT controller that will be contacted to manage the RG according to the specification.
|
||||||
|
- 'This parameter is always required regardless of the specified I(authenticator) type.'
|
||||||
|
required: yes
|
||||||
|
jwt:
|
||||||
|
description:
|
||||||
|
- 'JWT (access token) for authenticating to the DECORT controller when I(authenticator=jwt).'
|
||||||
|
- 'This parameter is required if I(authenticator=jwt) and ignored for other authentication modes.'
|
||||||
|
- If not specified in the playbook, the value will be taken from DECORT_JWT environment variable.
|
||||||
|
required: no
|
||||||
|
oauth2_url:
|
||||||
|
description:
|
||||||
|
- 'URL of the oauth2 authentication provider to use when I(authenticator=oauth2).'
|
||||||
|
- 'This parameter is required when when I(authenticator=oauth2).'
|
||||||
|
- If not specified in the playbook, the value will be taken from DECORT_OAUTH2_URL environment variable.
|
||||||
|
password:
|
||||||
|
description:
|
||||||
|
- 'Password for authenticating to the DECORT controller when I(authenticator=legacy).'
|
||||||
|
- 'This parameter is required if I(authenticator=legacy) and ignored in other authentication modes.'
|
||||||
|
- If not specified in the playbook, the value will be taken from DECORT_PASSWORD environment variable.
|
||||||
|
required: no
|
||||||
|
quotas:
|
||||||
|
description:
|
||||||
|
- Dictionary that defines resource quotas to be set on a newly created RG.
|
||||||
|
- 'This parameter is optional and only used when creating new RG. It is ignored for any operations on an
|
||||||
|
existing RG.'
|
||||||
|
- 'The following keys are valid to set the resource quotas:'
|
||||||
|
- ' - I(cpu) (integer) - limit on the total number of CPUs that can be consumed by all compute instances
|
||||||
|
in this RG.'
|
||||||
|
- ' - I(ram) (integer) - limit on the total amount of RAM in GB that can be consumed by compute instances
|
||||||
|
in this RG.'
|
||||||
|
- ' - I(disk) (integer) - limit on the total volume of disk space in GB that can be consumed by all
|
||||||
|
compute instances in this RG.'
|
||||||
|
- ' - I(ext_ips) (integer) - maximum number of external IP addresses that can be allocated to the compute
|
||||||
|
instances and virtual network segments (ViNS) in this RG.'
|
||||||
|
- 'Each of the above keys is optional. For example, you may specify I(cpu) and I(ram) while omitting the
|
||||||
|
other two keys. Then the quotas will be set on RAM and CPU leaving disk volume and the number of external
|
||||||
|
IP addresses unlimited.'
|
||||||
|
required: no
|
||||||
|
rg_name:
|
||||||
|
description:
|
||||||
|
- Name of the RG to manage.
|
||||||
|
required: yes
|
||||||
|
state:
|
||||||
|
description:
|
||||||
|
- Specify the desired state of the resource group at the exit of the module.
|
||||||
|
- 'Regardless of I(state), if RG exists and is in one of [DEPLOYING, DESTROYING, MIGRATING, ] states,
|
||||||
|
do nothing.'
|
||||||
|
- 'If desired I(state=present):'
|
||||||
|
- ' - RG does not exist or is in DESTROYED state, create new RG according to the specifications.'
|
||||||
|
- ' - RG is in one of [CREATED, DISABLED] states, change quotas if necessary.'
|
||||||
|
- ' - RG is in DELETED state, restore it and change quotas if necessary. RG will be left in DISABLED state.'
|
||||||
|
- ' - RG in any other state, abort with an error.'
|
||||||
|
- 'If desired I(state=enabled):'
|
||||||
|
- ' - RG does not exist or is in DESTROYED state, create new RG according to the specifications.'
|
||||||
|
- ' - RG is in CREATED state, change quotas if necessary.'
|
||||||
|
- ' - RG is in DELETED state, restore it, change quotas if necessary and enable.'
|
||||||
|
- ' - RG is in any other state, abort with an error.'
|
||||||
|
- 'If desired I(state=absent):'
|
||||||
|
- ' - RG is in one of [CREATED, DISABLED, DELETED] states, destroy it.'
|
||||||
|
- ' - RG in DESTROYED state, do nothing.'
|
||||||
|
- ' - RG in any other state, abort with an error.'
|
||||||
|
- 'If desired I(state=disabled):'
|
||||||
|
- ' - RG does not exist or is in one of [ENABLING, DISABLING, DELETING, DELETED, DESTROYING, DESTROYED]
|
||||||
|
states, abort with an error.'
|
||||||
|
- ' - RG is DISABLED state, change quotas if necessary.'
|
||||||
|
- ' - RG is in CREATED state, change quotas if necessary and disable the RG.'
|
||||||
|
default: present
|
||||||
|
choices: [ absent, disabled, enabled, present ]
|
||||||
|
user:
|
||||||
|
description:
|
||||||
|
- 'Name of the legacy user for authenticating to the DECORT controller when I(authenticator=legacy).'
|
||||||
|
- 'This parameter is required when I(authenticator=legacy) and ignored for other authentication modes.'
|
||||||
|
- If not specified in the playbook, the value will be taken from DECORT_USER environment variable.
|
||||||
|
required: no
|
||||||
|
verify_ssl:
|
||||||
|
description:
|
||||||
|
- 'Controls SSL verification mode when making API calls to DECORT controller. Set it to False if you
|
||||||
|
want to disable SSL certificate verification. Intended use case is when you run module in a trusted
|
||||||
|
environment that uses self-signed certificates. Note that disabling SSL verification in any other
|
||||||
|
scenario can lead to security issues, so please know what you are doing.'
|
||||||
|
default: True
|
||||||
|
required: no
|
||||||
|
workflow_callback:
|
||||||
|
description:
|
||||||
|
- 'Callback URL that represents an application, which invokes this module (e.g. up-level orchestrator or
|
||||||
|
end-user portal) and may except out-of-band updates on progress / exit status of the module run.'
|
||||||
|
- API call at this URL will be used to relay such information to the application.
|
||||||
|
- 'API call payload will include module-specific details about this module run and I(workflow_context).'
|
||||||
|
required: no
|
||||||
|
workflow_context:
|
||||||
|
description:
|
||||||
|
- 'Context data that will be included into the payload of the API call directed at I(workflow_callback) URL.'
|
||||||
|
- 'This context data is expected to uniquely identify the task carried out by this module invocation so
|
||||||
|
that up-level orchestrator could match returned information to the its internal entities.'
|
||||||
|
required: no
|
||||||
|
'''
|
||||||
|
|
||||||
|
EXAMPLES = '''
|
||||||
|
- name: create a new RG named "MyFirstRG" if it does not exist yet, set quotas on CPU and the number of exteranl IPs.
|
||||||
|
decort_rg:
|
||||||
|
authenticator: oauth2
|
||||||
|
app_id: "{{ MY_APP_ID }}"
|
||||||
|
app_secret: "{{ MY_APP_SECRET }}"
|
||||||
|
controller_url: "https://cloud.digitalenergy.online"
|
||||||
|
rg_name: "MyFirstRG"
|
||||||
|
account_name: "MyMainAccount"
|
||||||
|
quotas:
|
||||||
|
cpu: 16
|
||||||
|
ext_ips: 4
|
||||||
|
annotation: "My first RG created with Ansible module"
|
||||||
|
state: present
|
||||||
|
delegate_to: localhost
|
||||||
|
register: my_rg
|
||||||
|
'''
|
||||||
|
|
||||||
|
RETURN = '''
|
||||||
|
facts:
|
||||||
|
description: facts about the resource group
|
||||||
|
returned: always
|
||||||
|
type: dict
|
||||||
|
sample:
|
||||||
|
facts:
|
||||||
|
id: 100
|
||||||
|
name: MyFirstRG
|
||||||
|
state: CREATED
|
||||||
|
account_id: 10
|
||||||
|
gid: 1001
|
||||||
|
'''
|
||||||
|
|
||||||
|
from ansible.module_utils.basic import AnsibleModule
|
||||||
|
from ansible.module_utils.basic import env_fallback
|
||||||
|
|
||||||
|
from ansible.module_utils.decort_utils import *
|
||||||
|
|
||||||
|
|
||||||
|
def decort_rg_package_facts(arg_rg_facts, arg_check_mode=False):
|
||||||
|
"""Package a dictionary of RG facts according to the decort_rg module specification. This dictionary will
|
||||||
|
be returned to the upstream Ansible engine at the completion of the module run.
|
||||||
|
|
||||||
|
@param arg_rg_facts: dictionary with RG facts as returned by API call to .../rg/get
|
||||||
|
@param arg_check_mode: boolean that tells if this Ansible module is run in check mode
|
||||||
|
"""
|
||||||
|
|
||||||
|
ret_dict = dict(id=0,
|
||||||
|
name="none",
|
||||||
|
state="CHECK_MODE",
|
||||||
|
)
|
||||||
|
|
||||||
|
if arg_check_mode:
|
||||||
|
# in check mode return immediately with the default values
|
||||||
|
return ret_dict
|
||||||
|
|
||||||
|
if arg_rg_facts is None:
|
||||||
|
# if void facts provided - change state value to ABSENT and return
|
||||||
|
ret_dict['state'] = "ABSENT"
|
||||||
|
return ret_dict
|
||||||
|
|
||||||
|
ret_dict['id'] = arg_rg_facts['id']
|
||||||
|
ret_dict['name'] = arg_rg_facts['name']
|
||||||
|
ret_dict['state'] = arg_rg_facts['status']
|
||||||
|
ret_dict['account_id'] = arg_rg_facts['accountId']
|
||||||
|
ret_dict['gid'] = arg_rg_facts['gid']
|
||||||
|
|
||||||
|
return ret_dict
|
||||||
|
|
||||||
|
def decort_rg_parameters():
|
||||||
|
"""Build and return a dictionary of parameters expected by decort_rg module in a form accepted
|
||||||
|
by AnsibleModule utility class."""
|
||||||
|
|
||||||
|
return dict(
|
||||||
|
account_id=dict(type='int', required=False),
|
||||||
|
account_name=dict(type='str', required=False, default=''),
|
||||||
|
annotation=dict(type='str', required=False, default=''),
|
||||||
|
app_id=dict(type='str',
|
||||||
|
required=False,
|
||||||
|
fallback=(env_fallback, ['DECORT_APP_ID'])),
|
||||||
|
app_secret=dict(type='str',
|
||||||
|
required=False,
|
||||||
|
fallback=(env_fallback, ['DECORT_APP_SECRET']),
|
||||||
|
no_log=True),
|
||||||
|
authenticator=dict(type='str',
|
||||||
|
required=True,
|
||||||
|
choices=['legacy', 'oauth2', 'jwt']),
|
||||||
|
controller_url=dict(type='str', required=True),
|
||||||
|
# datacenter=dict(type='str', required=False, default=''),
|
||||||
|
jwt=dict(type='str',
|
||||||
|
required=False,
|
||||||
|
fallback=(env_fallback, ['DECORT_JWT']),
|
||||||
|
no_log=True),
|
||||||
|
oauth2_url=dict(type='str',
|
||||||
|
required=False,
|
||||||
|
fallback=(env_fallback, ['DECORT_OAUTH2_URL'])),
|
||||||
|
password=dict(type='str',
|
||||||
|
required=False,
|
||||||
|
fallback=(env_fallback, ['DECORT_PASSWORD']),
|
||||||
|
no_log=True),
|
||||||
|
quotas=dict(type='dict', required=False),
|
||||||
|
state=dict(type='str',
|
||||||
|
default='present',
|
||||||
|
choices=['absent', 'disabled', 'enabled', 'present']),
|
||||||
|
user=dict(type='str',
|
||||||
|
required=False,
|
||||||
|
fallback=(env_fallback, ['DECORT_USER'])),
|
||||||
|
rg_name=dict(type='str', required=True,),
|
||||||
|
verify_ssl=dict(type='bool', required=False, default=True),
|
||||||
|
workflow_callback=dict(type='str', required=False),
|
||||||
|
workflow_context=dict(type='str', required=False),
|
||||||
|
)
|
||||||
|
|
||||||
|
# Workflow digest:
|
||||||
|
# 1) authenticate to DECORT controller & validate authentication by issuing API call - done when creating DECORTController
|
||||||
|
# 2) check if the RG with the specified id or rg_name:name exists
|
||||||
|
# 3) if RG does not exist -> deploy
|
||||||
|
# 4) if RG exists: check desired state, desired configuration -> initiate action accordingly
|
||||||
|
# 5) report result to Ansible
|
||||||
|
|
||||||
|
def main():
|
||||||
|
module_parameters = decort_rg_parameters()
|
||||||
|
|
||||||
|
amodule = AnsibleModule(argument_spec=module_parameters,
|
||||||
|
supports_check_mode=True,
|
||||||
|
mutually_exclusive=[
|
||||||
|
['oauth2', 'password'],
|
||||||
|
['password', 'jwt'],
|
||||||
|
['jwt', 'oauth2'],
|
||||||
|
],
|
||||||
|
required_together=[
|
||||||
|
['app_id', 'app_secret'],
|
||||||
|
['user', 'password'],
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
||||||
|
decon = DecortController(amodule)
|
||||||
|
|
||||||
|
# We need valid Account ID to manage RG.
|
||||||
|
# Account may be specified either by account_id or account_name. In both cases we
|
||||||
|
# have to validate account presence and accesibility by the current user.
|
||||||
|
validated_acc_id = 0
|
||||||
|
if decon.check_amodule_argument('account_id', False):
|
||||||
|
validated_acc_id, _ = decon.account_find("", amodule.params['account_id'])
|
||||||
|
else:
|
||||||
|
decon.check_amodule_argument('account_name') # if no account_name, this function will abort module
|
||||||
|
validated_acc_id, _ = decon.account_find(amodule.params['account_name'])
|
||||||
|
|
||||||
|
if not validated_acc_id:
|
||||||
|
# we failed to locate account by either name or ID - abort with an error
|
||||||
|
decon.result['failed'] = True
|
||||||
|
decon.result['msg'] = ("Current user does not have access to the requested account "
|
||||||
|
"or non-existent account specified.")
|
||||||
|
decon.fail_json(**decon.result)
|
||||||
|
|
||||||
|
# Check if the RG with the specified parameters already exists
|
||||||
|
rg_id, rg_facts = decon.rg_find(validated_acc_id,
|
||||||
|
0, arg_rg_name=amodule.params['rg_name'],
|
||||||
|
arg_check_state=False)
|
||||||
|
rg_should_exist = True
|
||||||
|
|
||||||
|
if rg_id:
|
||||||
|
if rg_facts['status'] in ["MODELED", "DISABLING", "ENABLING", "DELETING", "DESTROYING"]:
|
||||||
|
# error: nothing can be done to existing RG in the listed statii regardless of
|
||||||
|
# the requested state
|
||||||
|
decon.result['failed'] = True
|
||||||
|
decon.result['changed'] = False
|
||||||
|
decon.result['msg'] = ("No change can be done for existing RG ID {} because of its current "
|
||||||
|
"status '{}'").format(rg_id, rg_facts['status'])
|
||||||
|
elif rg_facts['status'] == "DISABLED":
|
||||||
|
if amodule.params['state'] == 'absent':
|
||||||
|
decon.rg_delete(arg_rg_id=rg_id, arg_permanently=True)
|
||||||
|
rg_facts['status'] = 'DESTROYED'
|
||||||
|
rg_should_exist = False
|
||||||
|
elif amodule.params['state'] in ('present', 'disabled'):
|
||||||
|
# update quotas
|
||||||
|
decon.rg_quotas(rg_facts, amodule.params['quotas'])
|
||||||
|
elif amodule.params['state'] == 'enabled':
|
||||||
|
# update quotas and enable
|
||||||
|
decon.rg_quotas(rg_facts, amodule.params['quotas'])
|
||||||
|
decon.rg_state(rg_facts, 'enabled')
|
||||||
|
elif rg_facts['status'] == "CREATED":
|
||||||
|
if amodule.params['state'] == 'absent':
|
||||||
|
decon.rg_delete(arg_rg_id=rg_id, arg_permanently=True)
|
||||||
|
rg_facts['status'] = 'DESTROYED'
|
||||||
|
rg_should_exist = False
|
||||||
|
elif amodule.params['state'] in ('present', 'enabled'):
|
||||||
|
# update quotas
|
||||||
|
decon.rg_quotas(rg_facts, amodule.params['quotas'])
|
||||||
|
elif amodule.params['state'] == 'disabled':
|
||||||
|
# disable and update quotas
|
||||||
|
decon.rg_state(rg_facts, 'disabled')
|
||||||
|
decon.rg_quotas(rg_facts, amodule.params['quotas'])
|
||||||
|
elif rg_facts['status'] == "DELETED":
|
||||||
|
if amodule.params['state'] in ['present', 'enabled']:
|
||||||
|
# restore and enable
|
||||||
|
# TODO: check if restore RG API returns the new RG ID of the restored RG instance.
|
||||||
|
decon.rg_restore(arg_rg_id=rg_id)
|
||||||
|
decon.rg_state(rg_facts, 'enabled')
|
||||||
|
# TODO: Not sure what to do with the quotas after RG is restored. May need to update rg_facts.
|
||||||
|
rg_should_exist = True
|
||||||
|
pass
|
||||||
|
elif amodule.params['state'] == 'absent':
|
||||||
|
# destroy permanently
|
||||||
|
decon.rg_delete(arg_rg_id=rg_id, arg_permanently=True)
|
||||||
|
rg_facts['status'] = 'DESTROYED'
|
||||||
|
rg_should_exist = False
|
||||||
|
elif amodule.params['state'] == 'disabled':
|
||||||
|
# error
|
||||||
|
decon.result['failed'] = True
|
||||||
|
decon.result['changed'] = False
|
||||||
|
decon.result['msg'] = ("Invalid target state '{}' requested for RG ID {} in the "
|
||||||
|
"current status '{}'").format(rg_id,
|
||||||
|
amodule.params['state'],
|
||||||
|
rg_facts['status'])
|
||||||
|
rg_should_exist = False
|
||||||
|
elif rg_facts['status'] == "DESTROYED":
|
||||||
|
if amodule.params['state'] in ('present', 'enabled'):
|
||||||
|
# need to re-provision RG
|
||||||
|
decon.check_amodule_argument('rg_name')
|
||||||
|
# As we alreafy have validated account ID we can create RG and get rg_id on success
|
||||||
|
# pass empty string for location code, rg_provision will select the 1st location
|
||||||
|
rg_id = decon.rg_provision(validated_acc_id,
|
||||||
|
amodule.params['rg_name'], decon.decort_username,
|
||||||
|
amodule.params['quotas'],
|
||||||
|
"", # this is location code. TODO: add module argument
|
||||||
|
amodule.params['annotation'])
|
||||||
|
rg_should_exist = True
|
||||||
|
elif amodule.params['state'] == 'absent':
|
||||||
|
# nop
|
||||||
|
decon.result['failed'] = False
|
||||||
|
decon.result['changed'] = False
|
||||||
|
decon.result['msg'] = ("No state change required for RG ID {} because of its "
|
||||||
|
"current status '{}'").format(rg_id,
|
||||||
|
rg_facts['status'])
|
||||||
|
rg_should_exist = False
|
||||||
|
elif amodule.params['state'] == 'disabled':
|
||||||
|
# error
|
||||||
|
decon.result['failed'] = True
|
||||||
|
decon.result['changed'] = False
|
||||||
|
decon.result['msg'] = ("Invalid target state '{}' requested for RG ID {} in the "
|
||||||
|
"current status '{}'").format(rg_id,
|
||||||
|
amodule.params['state'],
|
||||||
|
rg_facts['status'])
|
||||||
|
else:
|
||||||
|
# Preexisting RG was not found.
|
||||||
|
rg_should_exist = False # we will change it back to True if RG is explicitly created or restored
|
||||||
|
# If requested state is 'absent' - nothing to do
|
||||||
|
if amodule.params['state'] == 'absent':
|
||||||
|
decon.result['failed'] = False
|
||||||
|
decon.result['changed'] = False
|
||||||
|
decon.result['msg'] = ("Nothing to do as target state 'absent' was requested for "
|
||||||
|
"non-existent RG name '{}'").format(amodule.params['rg_name'])
|
||||||
|
elif amodule.params['state'] in ('present', 'enabled'):
|
||||||
|
# Target RG does not exist yet - create it and store the returned ID in rg_id variable for later use
|
||||||
|
# To create RG we need account name (or account ID) and RG name - check
|
||||||
|
# that these parameters are present and proceed.
|
||||||
|
decon.check_amodule_argument('rg_name')
|
||||||
|
# as we already have account ID we can create RG and get rg_id on success
|
||||||
|
# pass empty string for location code, rg_provision will select the 1st location
|
||||||
|
rg_id = decon.rg_provision(validated_acc_id,
|
||||||
|
amodule.params['rg_name'], decon.decort_username,
|
||||||
|
amodule.params['quotas'],
|
||||||
|
"", # this is location code. TODO: add module argument
|
||||||
|
amodule.params['annotation'])
|
||||||
|
rg_should_exist = True
|
||||||
|
elif amodule.params['state'] == 'disabled':
|
||||||
|
decon.result['failed'] = True
|
||||||
|
decon.result['changed'] = False
|
||||||
|
decon.result['msg'] = ("Invalid target state '{}' requested for non-existent "
|
||||||
|
"RG name '{}' ").format(amodule.params['state'],
|
||||||
|
amodule.params['rg_name'])
|
||||||
|
#
|
||||||
|
# conditional switch end - complete module run
|
||||||
|
if decon.result['failed']:
|
||||||
|
amodule.fail_json(**decon.result)
|
||||||
|
else:
|
||||||
|
# prepare RG facts to be returned as part of decon.result and then call exit_json(...)
|
||||||
|
# rg_facts = None
|
||||||
|
if rg_should_exist:
|
||||||
|
if decon.result['changed']:
|
||||||
|
# If we arrive here, there is a good chance that the RG is present - get fresh RG facts from
|
||||||
|
# the cloud by RG ID.
|
||||||
|
# Otherwise, RG facts from previous call (when the RG was still in existence) will be returned.
|
||||||
|
_, rg_facts = decon.rg_find(arg_account_id=0, arg_rg_id=rg_id)
|
||||||
|
decon.result['facts'] = decort_rg_package_facts(rg_facts, amodule.check_mode)
|
||||||
|
amodule.exit_json(**decon.result)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
@ -0,0 +1,485 @@
|
|||||||
|
#!/usr/bin/python
|
||||||
|
#
|
||||||
|
# Digital Enegry Cloud Orchestration Technology (DECORT) modules for Ansible
|
||||||
|
# Copyright: (c) 2018-2020 Digital Energy Cloud Solutions LLC
|
||||||
|
#
|
||||||
|
# Apache License 2.0 (see http://www.apache.org/licenses/LICENSE-2.0.txt)
|
||||||
|
#
|
||||||
|
|
||||||
|
#
|
||||||
|
# Author: Sergey Shubin (sergey.shubin@digitalenergy.online)
|
||||||
|
#
|
||||||
|
|
||||||
|
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
||||||
|
'status': ['preview'],
|
||||||
|
'supported_by': 'community'}
|
||||||
|
|
||||||
|
DOCUMENTATION = '''
|
||||||
|
---
|
||||||
|
module: decort_vins
|
||||||
|
short_description: Manage Virtual Network Segments (ViNS) in DECORT cloud
|
||||||
|
description: >
|
||||||
|
This module can be used to create new ViNS in DECORT cloud platform, obtain or
|
||||||
|
modify its characteristics, and delete it.
|
||||||
|
version_added: "2.2"
|
||||||
|
author:
|
||||||
|
- Sergey Shubin <sergey.shubin@digitalenergy.online>
|
||||||
|
requirements:
|
||||||
|
- python >= 2.6
|
||||||
|
- PyJWT module
|
||||||
|
- requests module
|
||||||
|
- decort_utils utility library (module)
|
||||||
|
- DECORT cloud platform version 3.4.0 or higher
|
||||||
|
notes:
|
||||||
|
- Environment variables can be used to pass selected parameters to the module, see details below.
|
||||||
|
- Specified Oauth2 provider must be trusted by the DECORT cloud controller on which JWT will be used.
|
||||||
|
- 'Similarly, JWT supplied in I(authenticator=jwt) mode should be received from Oauth2 provider trusted by
|
||||||
|
the DECORT cloud controller on which this JWT will be used.'
|
||||||
|
options:
|
||||||
|
account_id:
|
||||||
|
description:
|
||||||
|
- ID of the account under which this ViNS will be created (for new ViNS) or is located (for already
|
||||||
|
existing ViNS). This is the alternative to I(account_name) option.
|
||||||
|
- If both I(account_id) and I(account_name) specified, then I(account_name) is ignored.
|
||||||
|
required: no
|
||||||
|
account_name:
|
||||||
|
description:
|
||||||
|
- 'Name of the account under which this ViNS will be created (for new RGs) or is located (for already
|
||||||
|
existing ViNS).'
|
||||||
|
- 'This parameter is ignored if I(account_id) is specified.'
|
||||||
|
required: no
|
||||||
|
annotation:
|
||||||
|
description:
|
||||||
|
- Optional text description of this virtual network segment.
|
||||||
|
default: empty string
|
||||||
|
required: no
|
||||||
|
app_id:
|
||||||
|
description:
|
||||||
|
- 'Application ID for authenticating to the DECORT controller when I(authenticator=oauth2).'
|
||||||
|
- 'Required if I(authenticator=oauth2).'
|
||||||
|
- 'If not found in the playbook or command line arguments, the value will be taken from DECORT_APP_ID
|
||||||
|
environment variable.'
|
||||||
|
required: no
|
||||||
|
app_secret:
|
||||||
|
description:
|
||||||
|
- 'Application API secret used for authenticating to the DECORT controller when I(authenticator=oauth2).'
|
||||||
|
- This parameter is required when I(authenticator=oauth2) and ignored in other modes.
|
||||||
|
- 'If not found in the playbook or command line arguments, the value will be taken from DECORT_APP_SECRET
|
||||||
|
environment variable.'
|
||||||
|
required: no
|
||||||
|
authenticator:
|
||||||
|
description:
|
||||||
|
- Authentication mechanism to be used when accessing DECORT controller and authorizing API call.
|
||||||
|
default: jwt
|
||||||
|
choices: [ jwt, oauth2, legacy ]
|
||||||
|
required: yes
|
||||||
|
controller_url:
|
||||||
|
description:
|
||||||
|
- URL of the DECORT controller that will be contacted to manage the RG according to the specification.
|
||||||
|
- 'This parameter is always required regardless of the specified I(authenticator) type.'
|
||||||
|
required: yes
|
||||||
|
ext_net_id:
|
||||||
|
description:
|
||||||
|
- `Controls ViNS connection to an external network. This argument is optional with default value of -1,
|
||||||
|
which means no external connection.`
|
||||||
|
- Specify 0 to connect ViNS to external network and let platform select external network Id automatically.
|
||||||
|
- Specify positive value to request ViNS connection to the external network with corresponding ID.
|
||||||
|
- You may also control external IP address selection with I(ext_ip_addr) argument.
|
||||||
|
default: -1
|
||||||
|
required: no
|
||||||
|
ext_ip_addr:
|
||||||
|
description:
|
||||||
|
- IP address to assign to the external interface of this ViNS when connecting to the external net.
|
||||||
|
- If empty string is passed, the platform will assign free IP address automatically.
|
||||||
|
- `Note that if invalid IP address or an address already occupied by another client is specified,
|
||||||
|
the module will abort with an error.`
|
||||||
|
- `This argument is used only for new connection to the specified network. You cannot select another
|
||||||
|
external IP address without changing external network ID.'
|
||||||
|
- ViNS connection to the external network is controlled by I(ext_net_id) argument.
|
||||||
|
default: empty string
|
||||||
|
required: no
|
||||||
|
jwt:
|
||||||
|
description:
|
||||||
|
- 'JWT (access token) for authenticating to the DECORT controller when I(authenticator=jwt).'
|
||||||
|
- 'This parameter is required if I(authenticator=jwt) and ignored for other authentication modes.'
|
||||||
|
- If not specified in the playbook, the value will be taken from DECORT_JWT environment variable.
|
||||||
|
required: no
|
||||||
|
oauth2_url:
|
||||||
|
description:
|
||||||
|
- 'URL of the oauth2 authentication provider to use when I(authenticator=oauth2).'
|
||||||
|
- 'This parameter is required when when I(authenticator=oauth2).'
|
||||||
|
- 'If not specified in the playbook, the value will be taken from DECORT_OAUTH2_URL environment variable.'
|
||||||
|
password:
|
||||||
|
description:
|
||||||
|
- 'Password for authenticating to the DECORT controller when I(authenticator=legacy).'
|
||||||
|
- 'This parameter is required if I(authenticator=legacy) and ignored in other authentication modes.'
|
||||||
|
- If not specified in the playbook, the value will be taken from DECORT_PASSWORD environment variable.
|
||||||
|
required: no
|
||||||
|
rg_id:
|
||||||
|
description:
|
||||||
|
- 'ID of the resource group (RG), where this ViNS will be created (for a new ViNS) or located
|
||||||
|
(for already existing ViNS).'
|
||||||
|
- If ViNS is created at the account level, I(rg_id) should be omitted or set to 0.
|
||||||
|
- If both I(rg_id) and I(rg_name) are specified, then I(rg_name) is ignored.
|
||||||
|
default: 0
|
||||||
|
required: no
|
||||||
|
rg_name:
|
||||||
|
description:
|
||||||
|
- 'Name of the resource group (RG), where this ViNS will be created (for new ViNS) or
|
||||||
|
located (for already existing ViNS).'
|
||||||
|
- If ViNS is created at the account level, I(rg_name) should be omitted or set to emtpy string.
|
||||||
|
- If both I(rg_name) and I(rg_id) are specified, then I(rg_name) is ignored.
|
||||||
|
default: empty string
|
||||||
|
required: no
|
||||||
|
state:
|
||||||
|
description:
|
||||||
|
- Specify the desired state of the ViNS at the exit of the module.
|
||||||
|
- 'Regardless of I(state), if ViNS exists and is in one of [DEPLOYING, DESTROYING, MIGRATING] states,
|
||||||
|
do nothing.'
|
||||||
|
- 'If desired I(state=present):'
|
||||||
|
- ' - ViNS does not exist or is in DESTROYED state, create new ViNS according to the specifications.'
|
||||||
|
- ' - ViNS is in DELETED state, restore it and change quotas if necessary. Note that on successful
|
||||||
|
restore ViNS will be left in DISABLED state.'
|
||||||
|
- ' - ViNS is in one of [CREATED, ENABLED, DISABLED] states, do nothing.'
|
||||||
|
- ' - ViNS in any other state, abort with an error.'
|
||||||
|
- 'If desired I(state=enabled):'
|
||||||
|
- ' - ViNS does not exist or is in DESTROYED state, create new ViNS according to the specifications.'
|
||||||
|
- ' - ViNS is in DELETED state, restore and enable it.'
|
||||||
|
- ' - ViNS is in one of [CREATED, ENABLED] states, do nothing.'
|
||||||
|
- ' - viNS is in any other state, abort with an error.'
|
||||||
|
- 'If desired I(state=absent):'
|
||||||
|
- ' - ViNS is in one of [CREATED, ENABLED, DISABLED, DELETED] states, destroy it.'
|
||||||
|
- ' - ViNS in DESTROYED state, do nothing.'
|
||||||
|
- ' - ViNS in any other state, abort with an error.'
|
||||||
|
- 'If desired I(state=disabled):'
|
||||||
|
- ' - ViNS is in one of [CREATED, ENABLED] states, disable it.'
|
||||||
|
- ' - ViNS is DISABLED state, do nothing.'
|
||||||
|
- ' - ViNS does not exist or is in one of [ENABLING, DISABLING, DELETING, DELETED, DESTROYING, DESTROYED]
|
||||||
|
states, abort with an error.'
|
||||||
|
default: present
|
||||||
|
choices: [ absent, disabled, enabled, present ]
|
||||||
|
user:
|
||||||
|
description:
|
||||||
|
- 'Name of the legacy user for authenticating to the DECORT controller when I(authenticator=legacy).'
|
||||||
|
- 'This parameter is required when I(authenticator=legacy) and ignored for other authentication modes.'
|
||||||
|
- If not specified in the playbook, the value will be taken from DECORT_USER environment variable.
|
||||||
|
required: no
|
||||||
|
verify_ssl:
|
||||||
|
description:
|
||||||
|
- 'Controls SSL verification mode when making API calls to DECORT controller. Set it to False if you
|
||||||
|
want to disable SSL certificate verification. Intended use case is when you run module in a trusted
|
||||||
|
environment that uses self-signed certificates. Note that disabling SSL verification in any other
|
||||||
|
scenario can lead to security issues, so please know what you are doing.'
|
||||||
|
default: True
|
||||||
|
required: no
|
||||||
|
vins_name:
|
||||||
|
description:
|
||||||
|
- Name of the ViNS.
|
||||||
|
- ViNS can exist at either account or resource group level.
|
||||||
|
- ViNS name is unique only within its parent (i.e. account or resource group).
|
||||||
|
- 'To create ViNS at account level omit both I(rg_id) and I(rg_name), or set them to 0 and empty
|
||||||
|
string respectively.'
|
||||||
|
required: yes
|
||||||
|
workflow_callback:
|
||||||
|
description:
|
||||||
|
- 'Callback URL that represents an application, which invokes this module (e.g. up-level orchestrator or
|
||||||
|
end-user portal) and may except out-of-band updates on progress / exit status of the module run.'
|
||||||
|
- API call at this URL will be used to relay such information to the application.
|
||||||
|
- 'API call payload will include module-specific details about this module run and I(workflow_context).'
|
||||||
|
required: no
|
||||||
|
workflow_context:
|
||||||
|
description:
|
||||||
|
- 'Context data that will be included into the payload of the API call directed at I(workflow_callback) URL.'
|
||||||
|
- 'This context data is expected to uniquely identify the task carried out by this module invocation so
|
||||||
|
that up-level orchestrator could match returned information to the its internal entities.'
|
||||||
|
required: no
|
||||||
|
'''
|
||||||
|
|
||||||
|
EXAMPLES = '''
|
||||||
|
- name: create a new ViNS named "MyViNS" if it does not exist yet under RG "MyRG" in the account "MyAccount".
|
||||||
|
decort_vins:
|
||||||
|
authenticator: oauth2
|
||||||
|
app_id: "{{ MY_APP_ID }}"
|
||||||
|
app_secret: "{{ MY_APP_SECRET }}"
|
||||||
|
controller_url: "https://cloud.digitalenergy.online"
|
||||||
|
vins_name: "MyViNS"
|
||||||
|
rg_name: "MyRG"
|
||||||
|
account_name: "MyAccount"
|
||||||
|
state: present
|
||||||
|
delegate_to: localhost
|
||||||
|
register: my_vins
|
||||||
|
'''
|
||||||
|
|
||||||
|
RETURN = '''
|
||||||
|
facts:
|
||||||
|
description: facts about the virtual network segment
|
||||||
|
returned: always
|
||||||
|
type: dict
|
||||||
|
sample:
|
||||||
|
facts:
|
||||||
|
id: 5
|
||||||
|
name: MyViNS
|
||||||
|
state: CREATED
|
||||||
|
account_id: 7
|
||||||
|
rg_id: 19
|
||||||
|
gid: 1001
|
||||||
|
'''
|
||||||
|
|
||||||
|
from ansible.module_utils.basic import AnsibleModule
|
||||||
|
from ansible.module_utils.basic import env_fallback
|
||||||
|
|
||||||
|
from ansible.module_utils.decort_utils import *
|
||||||
|
|
||||||
|
|
||||||
|
def decort_vins_package_facts(arg_vins_facts, arg_check_mode=False):
|
||||||
|
"""Package a dictionary of RG facts according to the decort_vins module specification. This dictionary will
|
||||||
|
be returned to the upstream Ansible engine at the completion of the module run.
|
||||||
|
|
||||||
|
@param arg_vins_facts: dictionary with RG facts as returned by API call to .../rg/get
|
||||||
|
@param arg_check_mode: boolean that tells if this Ansible module is run in check mode
|
||||||
|
"""
|
||||||
|
|
||||||
|
ret_dict = dict(id=0,
|
||||||
|
name="none",
|
||||||
|
state="CHECK_MODE",
|
||||||
|
)
|
||||||
|
|
||||||
|
if arg_check_mode:
|
||||||
|
# in check mode return immediately with the default values
|
||||||
|
return ret_dict
|
||||||
|
|
||||||
|
if arg_vins_facts is None:
|
||||||
|
# if void facts provided - change state value to ABSENT and return
|
||||||
|
ret_dict['state'] = "ABSENT"
|
||||||
|
return ret_dict
|
||||||
|
|
||||||
|
ret_dict['id'] = arg_rg_facts['id']
|
||||||
|
ret_dict['name'] = arg_rg_facts['name']
|
||||||
|
ret_dict['state'] = arg_rg_facts['status']
|
||||||
|
ret_dict['account_id'] = arg_rg_facts['accountId']
|
||||||
|
ret_dict['gid'] = arg_rg_facts['gid']
|
||||||
|
|
||||||
|
return ret_dict
|
||||||
|
|
||||||
|
def decort_vins_parameters():
|
||||||
|
"""Build and return a dictionary of parameters expected by decort_vins module in a form accepted
|
||||||
|
by AnsibleModule utility class."""
|
||||||
|
|
||||||
|
return dict(
|
||||||
|
account_id=dict(type='int', required=False),
|
||||||
|
account_name=dict(type='str', required=False, default=''),
|
||||||
|
annotation=dict(type='str', required=False, default=''),
|
||||||
|
app_id=dict(type='str',
|
||||||
|
required=False,
|
||||||
|
fallback=(env_fallback, ['DECORT_APP_ID'])),
|
||||||
|
app_secret=dict(type='str',
|
||||||
|
required=False,
|
||||||
|
fallback=(env_fallback, ['DECORT_APP_SECRET']),
|
||||||
|
no_log=True),
|
||||||
|
authenticator=dict(type='str',
|
||||||
|
required=True,
|
||||||
|
choices=['legacy', 'oauth2', 'jwt']),
|
||||||
|
controller_url=dict(type='str', required=True),
|
||||||
|
# datacenter=dict(type='str', required=False, default=''),
|
||||||
|
ext_net_id=dict(type='int', required=False, default=-1),
|
||||||
|
ext_ip_addr=dict(type='str', required=False, default=''),
|
||||||
|
jwt=dict(type='str',
|
||||||
|
required=False,
|
||||||
|
fallback=(env_fallback, ['DECORT_JWT']),
|
||||||
|
no_log=True),
|
||||||
|
oauth2_url=dict(type='str',
|
||||||
|
required=False,
|
||||||
|
fallback=(env_fallback, ['DECORT_OAUTH2_URL'])),
|
||||||
|
password=dict(type='str',
|
||||||
|
required=False,
|
||||||
|
fallback=(env_fallback, ['DECORT_PASSWORD']),
|
||||||
|
no_log=True),
|
||||||
|
quotas=dict(type='dict', required=False),
|
||||||
|
state=dict(type='str',
|
||||||
|
default='present',
|
||||||
|
choices=['absent', 'disabled', 'enabled', 'present']),
|
||||||
|
user=dict(type='str',
|
||||||
|
required=False,
|
||||||
|
fallback=(env_fallback, ['DECORT_USER'])),
|
||||||
|
rg_id=dict(type='int', required=False, default=0),
|
||||||
|
rg_name=dict(type='str', required=False, default=''),
|
||||||
|
verify_ssl=dict(type='bool', required=False, default=True),
|
||||||
|
vins_name=dict(type='str', required=True),
|
||||||
|
workflow_callback=dict(type='str', required=False),
|
||||||
|
workflow_context=dict(type='str', required=False),
|
||||||
|
)
|
||||||
|
|
||||||
|
# Workflow digest:
|
||||||
|
# 1) authenticate to DECORT controller & validate authentication by issuing API call - done when creating DECORTController
|
||||||
|
# 2) check if the ViNS with this id or name exists under specified account / resource group
|
||||||
|
# 3) if ViNS does not exist -> deploy
|
||||||
|
# 4) if ViNS exists: check desired state, desired configuration -> initiate action(s) accordingly
|
||||||
|
# 5) report result to Ansible
|
||||||
|
|
||||||
|
def main():
|
||||||
|
module_parameters = decort_vins_parameters()
|
||||||
|
|
||||||
|
amodule = AnsibleModule(argument_spec=module_parameters,
|
||||||
|
supports_check_mode=True,
|
||||||
|
mutually_exclusive=[
|
||||||
|
['oauth2', 'password'],
|
||||||
|
['password', 'jwt'],
|
||||||
|
['jwt', 'oauth2'],
|
||||||
|
],
|
||||||
|
required_together=[
|
||||||
|
['app_id', 'app_secret'],
|
||||||
|
['user', 'password'],
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
||||||
|
decon = DecortController(amodule)
|
||||||
|
|
||||||
|
# We need valid Account ID to manage RG.
|
||||||
|
# Account may be specified either by account_id or account_name. In both cases we
|
||||||
|
# have to validate account presence and accesibility by the current user.
|
||||||
|
validated_acc_id = 0
|
||||||
|
if decon.check_amodule_argument('account_id', False):
|
||||||
|
validated_acc_id, _ = decon.account_find("", amodule.params['account_id'])
|
||||||
|
else:
|
||||||
|
decon.check_amodule_argument('account_name') # if no account_name, this function will abort module
|
||||||
|
validated_acc_id, _ = decon.account_find(amodule.params['account_name'])
|
||||||
|
|
||||||
|
if not validated_acc_id:
|
||||||
|
# we failed to locate account by either name or ID - abort with an error
|
||||||
|
decon.result['failed'] = True
|
||||||
|
decon.result['msg'] = ("Current user does not have access to the requested account "
|
||||||
|
"or non-existent account specified.")
|
||||||
|
decon.fail_json(**decon.result)
|
||||||
|
|
||||||
|
# Check if the RG with the specified parameters already exists
|
||||||
|
rg_id, rg_facts = decon.rg_find(validated_acc_id,
|
||||||
|
0, arg_rg_name=amodule.params['rg_name'],
|
||||||
|
arg_check_state=False)
|
||||||
|
rg_should_exist = True
|
||||||
|
|
||||||
|
if rg_id:
|
||||||
|
if rg_facts['status'] in ["MODELED", "DISABLING", "ENABLING", "DELETING", "DESTROYING"]:
|
||||||
|
# error: nothing can be done to existing RG in the listed statii regardless of
|
||||||
|
# the requested state
|
||||||
|
decon.result['failed'] = True
|
||||||
|
decon.result['changed'] = False
|
||||||
|
decon.result['msg'] = ("No change can be done for existing RG ID {} because of its current "
|
||||||
|
"status '{}'").format(rg_id, rg_facts['status'])
|
||||||
|
elif rg_facts['status'] == "DISABLED":
|
||||||
|
if amodule.params['state'] == 'absent':
|
||||||
|
decon.rg_delete(arg_rg_id=rg_id, arg_permanently=True)
|
||||||
|
rg_facts['status'] = 'DESTROYED'
|
||||||
|
rg_should_exist = False
|
||||||
|
elif amodule.params['state'] in ('present', 'disabled'):
|
||||||
|
# update quotas
|
||||||
|
decon.rg_quotas(rg_facts, amodule.params['quotas'])
|
||||||
|
elif amodule.params['state'] == 'enabled':
|
||||||
|
# update quotas and enable
|
||||||
|
decon.rg_quotas(rg_facts, amodule.params['quotas'])
|
||||||
|
decon.rg_state(rg_facts, 'enabled')
|
||||||
|
elif rg_facts['status'] == "CREATED":
|
||||||
|
if amodule.params['state'] == 'absent':
|
||||||
|
decon.rg_delete(arg_rg_id=rg_id, arg_permanently=True)
|
||||||
|
rg_facts['status'] = 'DESTROYED'
|
||||||
|
rg_should_exist = False
|
||||||
|
elif amodule.params['state'] in ('present', 'enabled'):
|
||||||
|
# update quotas
|
||||||
|
decon.rg_quotas(rg_facts, amodule.params['quotas'])
|
||||||
|
elif amodule.params['state'] == 'disabled':
|
||||||
|
# disable and update quotas
|
||||||
|
decon.rg_state(rg_facts, 'disabled')
|
||||||
|
decon.rg_quotas(rg_facts, amodule.params['quotas'])
|
||||||
|
elif rg_facts['status'] == "DELETED":
|
||||||
|
if amodule.params['state'] in ['present', 'enabled']:
|
||||||
|
# restore and enable
|
||||||
|
# TODO: check if restore RG API returns the new RG ID of the restored RG instance.
|
||||||
|
decon.rg_restore(arg_rg_id=rg_id)
|
||||||
|
decon.rg_state(rg_facts, 'enabled')
|
||||||
|
# TODO: Not sure what to do with the quotas after RG is restored. May need to update rg_facts.
|
||||||
|
rg_should_exist = True
|
||||||
|
pass
|
||||||
|
elif amodule.params['state'] == 'absent':
|
||||||
|
# destroy permanently
|
||||||
|
decon.rg_delete(arg_rg_id=rg_id, arg_permanently=True)
|
||||||
|
rg_facts['status'] = 'DESTROYED'
|
||||||
|
rg_should_exist = False
|
||||||
|
elif amodule.params['state'] == 'disabled':
|
||||||
|
# error
|
||||||
|
decon.result['failed'] = True
|
||||||
|
decon.result['changed'] = False
|
||||||
|
decon.result['msg'] = ("Invalid target state '{}' requested for RG ID {} in the "
|
||||||
|
"current status '{}'").format(rg_id,
|
||||||
|
amodule.params['state'],
|
||||||
|
rg_facts['status'])
|
||||||
|
rg_should_exist = False
|
||||||
|
elif rg_facts['status'] == "DESTROYED":
|
||||||
|
if amodule.params['state'] in ('present', 'enabled'):
|
||||||
|
# need to re-provision RG
|
||||||
|
decon.check_amodule_argument('rg_name')
|
||||||
|
# As we alreafy have validated account ID we can create RG and get rg_id on success
|
||||||
|
# pass empty string for location code, rg_provision will select the 1st location
|
||||||
|
rg_id = decon.rg_provision(validated_acc_id,
|
||||||
|
amodule.params['rg_name'], decon.decort_username,
|
||||||
|
amodule.params['quotas'])
|
||||||
|
rg_should_exist = True
|
||||||
|
elif amodule.params['state'] == 'absent':
|
||||||
|
# nop
|
||||||
|
decon.result['failed'] = False
|
||||||
|
decon.result['changed'] = False
|
||||||
|
decon.result['msg'] = ("No state change required for RG ID {} because of its "
|
||||||
|
"current status '{}'").format(rg_id,
|
||||||
|
rg_facts['status'])
|
||||||
|
rg_should_exist = False
|
||||||
|
elif amodule.params['state'] == 'disabled':
|
||||||
|
# error
|
||||||
|
decon.result['failed'] = True
|
||||||
|
decon.result['changed'] = False
|
||||||
|
decon.result['msg'] = ("Invalid target state '{}' requested for RG ID {} in the "
|
||||||
|
"current status '{}'").format(rg_id,
|
||||||
|
amodule.params['state'],
|
||||||
|
rg_facts['status'])
|
||||||
|
else:
|
||||||
|
# Preexisting RG was not found.
|
||||||
|
rg_should_exist = False # we will change it back to True if RG is explicitly created or restored
|
||||||
|
# If requested state is 'absent' - nothing to do
|
||||||
|
if amodule.params['state'] == 'absent':
|
||||||
|
decon.result['failed'] = False
|
||||||
|
decon.result['changed'] = False
|
||||||
|
decon.result['msg'] = ("Nothing to do as target state 'absent' was requested for "
|
||||||
|
"non-existent RG name '{}'").format(amodule.params['rg_name'])
|
||||||
|
elif amodule.params['state'] in ('present', 'enabled'):
|
||||||
|
# Target RG does not exist yet - create it and store the returned ID in rg_id variable for later use
|
||||||
|
# To create RG we need account name (or account ID) and RG name - check
|
||||||
|
# that these parameters are present and proceed.
|
||||||
|
decon.check_amodule_argument('rg_name')
|
||||||
|
# as we already have account ID we can create RG and get rg_id on success
|
||||||
|
# pass empty string for location code, rg_provision will select the 1st location
|
||||||
|
rg_id = decon.rg_provision(validated_acc_id,
|
||||||
|
amodule.params['rg_name'], decon.decort_username,
|
||||||
|
amodule.params['quotas'])
|
||||||
|
rg_should_exist = True
|
||||||
|
elif amodule.params['state'] == 'disabled':
|
||||||
|
decon.result['failed'] = True
|
||||||
|
decon.result['changed'] = False
|
||||||
|
decon.result['msg'] = ("Invalid target state '{}' requested for non-existent "
|
||||||
|
"RG name '{}' ").format(amodule.params['state'],
|
||||||
|
amodule.params['rg_name'])
|
||||||
|
#
|
||||||
|
# conditional switch end - complete module run
|
||||||
|
if decon.result['failed']:
|
||||||
|
amodule.fail_json(**decon.result)
|
||||||
|
else:
|
||||||
|
# prepare RG facts to be returned as part of decon.result and then call exit_json(...)
|
||||||
|
# rg_facts = None
|
||||||
|
if rg_should_exist:
|
||||||
|
if decon.result['changed']:
|
||||||
|
# If we arrive here, there is a good chance that the RG is present - get fresh RG facts from
|
||||||
|
# the cloud by RG ID.
|
||||||
|
# Otherwise, RG facts from previous call (when the RG was still in existence) will be returned.
|
||||||
|
_, rg_facts = decon.rg_find(arg_account_id=0, arg_rg_id=rg_id)
|
||||||
|
decon.result['facts'] = decort_rg_package_facts(rg_facts, amodule.check_mode)
|
||||||
|
amodule.exit_json(**decon.result)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in new issue