|
|
@ -14,105 +14,247 @@ ANSIBLE_METADATA = {'metadata_version': '1.1',
|
|
|
|
'status': ['preview'],
|
|
|
|
'status': ['preview'],
|
|
|
|
'supported_by': 'community'}
|
|
|
|
'supported_by': 'community'}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
from ansible.module_utils.basic import env_fallback
|
|
|
|
from ansible.module_utils.basic import env_fallback
|
|
|
|
|
|
|
|
|
|
|
|
from ansible.module_utils.decort_utils import *
|
|
|
|
from ansible.module_utils.decort_utils import *
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class decort_k8s(DecortController):
|
|
|
|
|
|
|
|
def __init__(self,arg_amodule):
|
|
|
|
|
|
|
|
super(decort_k8s, self).__init__(arg_amodule)
|
|
|
|
|
|
|
|
|
|
|
|
def decort_k8s_package_facts(arg_k8s_facts, arg_check_mode=False):
|
|
|
|
validated_acc_id = 0
|
|
|
|
"""Package a dictionary of k8s facts according to the decort_k8s module specification. This dictionary will
|
|
|
|
validated_rg_id = 0
|
|
|
|
be returned to the upstream Ansible engine at the completion of the module run.
|
|
|
|
validated_rg_facts = None
|
|
|
|
|
|
|
|
validated_k8ci_id = 0
|
|
|
|
|
|
|
|
|
|
|
|
@param arg_k8s_facts: dictionary with k8s facts as returned by API call to .../k8s/get
|
|
|
|
if arg_amodule.params['name'] == "" and arg_amodule.params['id'] == 0:
|
|
|
|
@param arg_check_mode: boolean that tells if this Ansible module is run in check mode
|
|
|
|
self.result['failed'] = True
|
|
|
|
"""
|
|
|
|
self.result['changed'] = False
|
|
|
|
|
|
|
|
self.result['msg'] = "Cannot manage k8s cluster when its ID is 0 and name is empty."
|
|
|
|
|
|
|
|
self.fail_json(**self.result)
|
|
|
|
|
|
|
|
|
|
|
|
ret_dict = dict(id=0,
|
|
|
|
|
|
|
|
name="none",
|
|
|
|
|
|
|
|
state="CHECK_MODE",
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if arg_check_mode:
|
|
|
|
if not arg_amodule.params['id']:
|
|
|
|
# in check mode return immediately with the default values
|
|
|
|
if not arg_amodule.params['rg_id']: # RG ID is not set -> locate RG by name -> need account ID
|
|
|
|
return ret_dict
|
|
|
|
validated_acc_id, _ = self.account_find(arg_amodule.params['account_name'],
|
|
|
|
|
|
|
|
arg_amodule.params['account_id'])
|
|
|
|
|
|
|
|
if not validated_acc_id:
|
|
|
|
|
|
|
|
self.result['failed'] = True
|
|
|
|
|
|
|
|
self.result['changed'] = False
|
|
|
|
|
|
|
|
self.result['msg'] = ("Current user does not have access to the account ID {} / "
|
|
|
|
|
|
|
|
"name '{}' or non-existent account specified.").format(arg_amodule.params['account_id'],
|
|
|
|
|
|
|
|
arg_amodule.params['account_name'])
|
|
|
|
|
|
|
|
self.fail_json(**self.result)
|
|
|
|
|
|
|
|
# fail the module -> exit
|
|
|
|
|
|
|
|
# now validate RG
|
|
|
|
|
|
|
|
validated_rg_id, validated_rg_facts = self.rg_find(validated_acc_id,
|
|
|
|
|
|
|
|
arg_amodule.params['rg_id'],)
|
|
|
|
|
|
|
|
if not validated_rg_id:
|
|
|
|
|
|
|
|
self.result['failed'] = True
|
|
|
|
|
|
|
|
self.result['changed'] = False
|
|
|
|
|
|
|
|
self.result['msg'] = "Cannot find RG ID {} / name '{}'.".format(arg_amodule.params['rg_id'],
|
|
|
|
|
|
|
|
arg_amodule.params['rg_name'])
|
|
|
|
|
|
|
|
self.fail_json(**self.result)
|
|
|
|
|
|
|
|
# fail the module - exit
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#validate k8ci ID
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
validated_k8ci_id = self.k8s_k8ci_find(arg_amodule.params['k8ci_id'])
|
|
|
|
|
|
|
|
if not validated_k8ci_id:
|
|
|
|
|
|
|
|
self.result['failed'] = True
|
|
|
|
|
|
|
|
self.result['changed'] = False
|
|
|
|
|
|
|
|
self.result['msg'] = "Cannot find K8CI ID {}.".format(arg_amodule.params['k8ci_id'])
|
|
|
|
|
|
|
|
self.fail_json(**self.result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.rg_id = validated_rg_id
|
|
|
|
|
|
|
|
arg_amodule.params['rg_id'] = validated_rg_id
|
|
|
|
|
|
|
|
arg_amodule.params['rg_name'] = validated_rg_facts['name']
|
|
|
|
|
|
|
|
self.acc_id = validated_rg_facts['accountId']
|
|
|
|
|
|
|
|
arg_amodule.params['k8ci_id'] = validated_k8ci_id
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.k8s_id,self.k8s_info = self.k8s_find(k8s_id=arg_amodule.params['id'],
|
|
|
|
|
|
|
|
k8s_name=arg_amodule.params['name'],
|
|
|
|
|
|
|
|
rg_id=validated_rg_id,
|
|
|
|
|
|
|
|
check_state=False)
|
|
|
|
|
|
|
|
if self.k8s_id:
|
|
|
|
|
|
|
|
self.k8s_should_exist = True
|
|
|
|
|
|
|
|
self.acc_id = self.k8s_info['accountId']
|
|
|
|
|
|
|
|
# check workers and groups for add or remove
|
|
|
|
|
|
|
|
|
|
|
|
if arg_k8s_facts is None:
|
|
|
|
return
|
|
|
|
# if void facts provided - change state value to ABSENT and return
|
|
|
|
|
|
|
|
ret_dict['state'] = "ABSENT"
|
|
|
|
def package_facts(self,check_mode=False):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ret_dict = dict(
|
|
|
|
|
|
|
|
name="",
|
|
|
|
|
|
|
|
state="CHECK_MODE",
|
|
|
|
|
|
|
|
account_id=0,
|
|
|
|
|
|
|
|
rg_id=0,
|
|
|
|
|
|
|
|
config=None,
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if check_mode:
|
|
|
|
|
|
|
|
# in check mode return immediately with the default values
|
|
|
|
|
|
|
|
return ret_dict
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if self.k8s_facts is None:
|
|
|
|
|
|
|
|
# #if void facts provided - change state value to ABSENT and return
|
|
|
|
|
|
|
|
# ret_dict['state'] = "ABSENT"
|
|
|
|
|
|
|
|
# return ret_dict
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ret_dict['id'] = self.k8s_info['id']
|
|
|
|
|
|
|
|
ret_dict['name'] = self.k8s_info['name']
|
|
|
|
|
|
|
|
ret_dict['techStatus'] = self.k8s_info['techStatus']
|
|
|
|
|
|
|
|
ret_dict['state'] = self.k8s_info['status']
|
|
|
|
|
|
|
|
ret_dict['rg_id'] = self.rg_id
|
|
|
|
|
|
|
|
ret_dict['account_id'] = self.acc_id
|
|
|
|
|
|
|
|
if self.amodule.params['getConfig'] and self.k8s_info['techStatus'] == "STARTED":
|
|
|
|
|
|
|
|
ret_dict['config'] = self.k8s_getConfig()
|
|
|
|
return ret_dict
|
|
|
|
return ret_dict
|
|
|
|
|
|
|
|
|
|
|
|
ret_dict['id'] = arg_k8s_facts['id']
|
|
|
|
def nop(self):
|
|
|
|
ret_dict['name'] = arg_k8s_facts['name']
|
|
|
|
"""No operation (NOP) handler for Compute management by decort_kvmvm module.
|
|
|
|
ret_dict['techStatus'] = arg_k8s_facts['techStatus']
|
|
|
|
This function is intended to be called from the main switch construct of the module
|
|
|
|
ret_dict['state'] = arg_k8s_facts['status']
|
|
|
|
when current state -> desired state change logic does not require any changes to
|
|
|
|
|
|
|
|
the actual Compute state.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
self.result['failed'] = False
|
|
|
|
|
|
|
|
self.result['changed'] = False
|
|
|
|
|
|
|
|
if self.k8s_id:
|
|
|
|
|
|
|
|
self.result['msg'] = ("No state change required for K8s ID {} because of its "
|
|
|
|
|
|
|
|
"current status '{}'.").format(self.k8s_id, self.k8s_info['status'])
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
self.result['msg'] = ("No state change to '{}' can be done for "
|
|
|
|
|
|
|
|
"non-existent K8s instance.").format(self.amodule.params['state'])
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def error(self):
|
|
|
|
|
|
|
|
self.result['failed'] = True
|
|
|
|
|
|
|
|
self.result['changed'] = False
|
|
|
|
|
|
|
|
if self.k8s_id:
|
|
|
|
|
|
|
|
self.result['msg'] = ("Invalid target state '{}' requested for K8s cluster ID {} in the "
|
|
|
|
|
|
|
|
"current status '{}'.").format(self.k8s_id,
|
|
|
|
|
|
|
|
self.amodule.params['state'],
|
|
|
|
|
|
|
|
self.k8s_info['status'])
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
self.result['msg'] = ("Invalid target state '{}' requested for non-existent K8s Cluster name '{}' "
|
|
|
|
|
|
|
|
"in RG ID {} / name '{}'").format(self.amodule.params['state'],
|
|
|
|
|
|
|
|
self.amodule.params['name'],
|
|
|
|
|
|
|
|
self.amodule.params['rg_id'],
|
|
|
|
|
|
|
|
self.amodule.params['rg_name'])
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def create(self):
|
|
|
|
|
|
|
|
self.k8s_provision(self.amodule.params['name'],
|
|
|
|
|
|
|
|
self.amodule.params['workers'][0]['name'],
|
|
|
|
|
|
|
|
self.amodule.params['k8ci_id'],
|
|
|
|
|
|
|
|
self.amodule.params['rg_id'],
|
|
|
|
|
|
|
|
self.amodule.params['master_count'],
|
|
|
|
|
|
|
|
self.amodule.params['master_cpu'],
|
|
|
|
|
|
|
|
self.amodule.params['master_ram_mb'],
|
|
|
|
|
|
|
|
self.amodule.params['master_disk_gb'],
|
|
|
|
|
|
|
|
self.amodule.params['workers'][0]['num'],
|
|
|
|
|
|
|
|
self.amodule.params['workers'][0]['cpu'],
|
|
|
|
|
|
|
|
self.amodule.params['workers'][0]['ram'],
|
|
|
|
|
|
|
|
self.amodule.params['workers'][0]['disk'],
|
|
|
|
|
|
|
|
self.amodule.params['extnet_id'],
|
|
|
|
|
|
|
|
self.amodule.params['with_lb'],
|
|
|
|
|
|
|
|
self.amodule.params['description'],)
|
|
|
|
|
|
|
|
|
|
|
|
return ret_dict
|
|
|
|
self.k8s_id,self.k8s_info = self.k8s_find(k8s_id=self.amodule.params['id'],
|
|
|
|
|
|
|
|
k8s_name=self.amodule.params['name'],
|
|
|
|
|
|
|
|
rg_id=self.rg_id,
|
|
|
|
|
|
|
|
check_state=False)
|
|
|
|
|
|
|
|
|
|
|
|
def decort_k8s_parameters():
|
|
|
|
if self.k8s_id:
|
|
|
|
"""Build and return a dictionary of parameters expected by decort_k8s module in a form accepted
|
|
|
|
self.k8s_should_exist = True
|
|
|
|
by AnsibleModule utility class."""
|
|
|
|
if self.k8s_id and self.amodule.params['workers'][1]:
|
|
|
|
|
|
|
|
self.k8s_workers_modify(self.k8s_info,self.amodule.params['workers'])
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
return dict(
|
|
|
|
def destroy(self):
|
|
|
|
account_id=dict(type='int', required=False),
|
|
|
|
self.k8s_delete(self.k8s_id)
|
|
|
|
account_name=dict(type='str', required=False, default=''),
|
|
|
|
self.k8s_info['status'] = 'DELETED'
|
|
|
|
annotation=dict(type='str', required=False, default=''),
|
|
|
|
self.k8s_should_exist = False
|
|
|
|
app_id=dict(type='str',
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def action(self,disared_state,started=True):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.k8s_state(self.k8s_info, disared_state,started)
|
|
|
|
|
|
|
|
self.k8s_id,self.k8s_info = self.k8s_find(k8s_id=self.amodule.params['id'],
|
|
|
|
|
|
|
|
k8s_name=self.amodule.params['name'],
|
|
|
|
|
|
|
|
rg_id=self.rg_id,
|
|
|
|
|
|
|
|
check_state=False)
|
|
|
|
|
|
|
|
if started == True and self.k8s_info['techStatus'] == "STOPPED":
|
|
|
|
|
|
|
|
self.k8s_state(self.k8s_info, disared_state,started)
|
|
|
|
|
|
|
|
self.k8s_info['techStatus'] == "STARTED"
|
|
|
|
|
|
|
|
self.k8s_workers_modify(self.k8s_info,self.amodule.params['workers'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
|
|
|
def build_parameters():
|
|
|
|
|
|
|
|
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,
|
|
|
|
required=False,
|
|
|
|
fallback=(env_fallback, ['DECORT_APP_ID'])),
|
|
|
|
fallback=(env_fallback, ['DECORT_JWT']),
|
|
|
|
app_secret=dict(type='str',
|
|
|
|
no_log=True),
|
|
|
|
|
|
|
|
oauth2_url=dict(type='str',
|
|
|
|
|
|
|
|
required=False,
|
|
|
|
|
|
|
|
fallback=(env_fallback, ['DECORT_OAUTH2_URL'])),
|
|
|
|
|
|
|
|
password=dict(type='str',
|
|
|
|
required=False,
|
|
|
|
required=False,
|
|
|
|
fallback=(env_fallback, ['DECORT_APP_SECRET']),
|
|
|
|
fallback=(env_fallback, ['DECORT_PASSWORD']),
|
|
|
|
no_log=True),
|
|
|
|
no_log=True),
|
|
|
|
authenticator=dict(type='str',
|
|
|
|
quotas=dict(type='dict', required=False),
|
|
|
|
required=True,
|
|
|
|
state=dict(type='str',
|
|
|
|
choices=['legacy', 'oauth2', 'jwt']),
|
|
|
|
default='present',
|
|
|
|
controller_url=dict(type='str', required=True),
|
|
|
|
choices=['absent', 'disabled', 'enabled', 'present','check']),
|
|
|
|
# datacenter=dict(type='str', required=False, default=''),
|
|
|
|
permanent=dict(type='bool', default=False),
|
|
|
|
jwt=dict(type='str',
|
|
|
|
started=dict(type='bool', default=True),
|
|
|
|
required=False,
|
|
|
|
user=dict(type='str',
|
|
|
|
fallback=(env_fallback, ['DECORT_JWT']),
|
|
|
|
required=False,
|
|
|
|
no_log=True),
|
|
|
|
fallback=(env_fallback, ['DECORT_USER'])),
|
|
|
|
oauth2_url=dict(type='str',
|
|
|
|
name=dict(type='str', required=True),
|
|
|
|
required=False,
|
|
|
|
id=dict(type='int', required=False, default=0),
|
|
|
|
fallback=(env_fallback, ['DECORT_OAUTH2_URL'])),
|
|
|
|
getConfig=dict(type='bool',required=False, default=False),
|
|
|
|
password=dict(type='str',
|
|
|
|
rg_id=dict(type='int', default=0),
|
|
|
|
required=False,
|
|
|
|
rg_name=dict(type='str',default=""),
|
|
|
|
fallback=(env_fallback, ['DECORT_PASSWORD']),
|
|
|
|
k8ci_id=dict(type='int', required=True),
|
|
|
|
no_log=True),
|
|
|
|
wg_name=dict(type='str', required=False),
|
|
|
|
quotas=dict(type='dict', required=False),
|
|
|
|
master_count=dict(type='int', default=1),
|
|
|
|
state=dict(type='str',
|
|
|
|
master_cpu=dict(type='int', default=2),
|
|
|
|
default='present',
|
|
|
|
master_ram_mb=dict(type='int', default=2048),
|
|
|
|
choices=['absent', 'disabled', 'enabled', 'present']),
|
|
|
|
master_disk_gb=dict(type='int', default=10),
|
|
|
|
permanent=dict(type='bool', default=False),
|
|
|
|
worker_count=dict(type='int', default=1),
|
|
|
|
started=dict(type='bool', default=True),
|
|
|
|
worker_cpu=dict(type='int', default=1),
|
|
|
|
user=dict(type='str',
|
|
|
|
worker_ram_mb=dict(type='int', default=1024),
|
|
|
|
required=False,
|
|
|
|
worker_disk_gb=dict(type='int', default=10),
|
|
|
|
fallback=(env_fallback, ['DECORT_USER'])),
|
|
|
|
workers=dict(type='list'),
|
|
|
|
k8s_name=dict(type='str', required=True),
|
|
|
|
extnet_id=dict(type='int', default=0),
|
|
|
|
rg_id=dict(type='int', required=True),
|
|
|
|
description=dict(type='str', default="Created by decort ansible module"),
|
|
|
|
k8ci_id=dict(type='int', required=True),
|
|
|
|
with_lb=dict(type='bool', default=True),
|
|
|
|
wg_name=dict(type='str', required=True),
|
|
|
|
verify_ssl=dict(type='bool', required=False, default=True),
|
|
|
|
master_count=dict(type='int', default=1),
|
|
|
|
workflow_callback=dict(type='str', required=False),
|
|
|
|
master_cpu=dict(type='int', default=2),
|
|
|
|
workflow_context=dict(type='str', required=False),)
|
|
|
|
master_ram_mb=dict(type='int', default=2048),
|
|
|
|
|
|
|
|
master_disk_gb=dict(type='int', default=10),
|
|
|
|
|
|
|
|
worker_count=dict(type='int', default=1),
|
|
|
|
|
|
|
|
worker_cpu=dict(type='int', default=1),
|
|
|
|
|
|
|
|
worker_ram_mb=dict(type='int', default=1024),
|
|
|
|
|
|
|
|
worker_disk_gb=dict(type='int', default=0),
|
|
|
|
|
|
|
|
extnet_id=dict(type='int', default=0),
|
|
|
|
|
|
|
|
description=dict(type='str', default="Created by decort ansible module"),
|
|
|
|
|
|
|
|
with_lb=dict(type='bool', default=True),
|
|
|
|
|
|
|
|
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():
|
|
|
|
def main():
|
|
|
|
module_parameters = decort_k8s_parameters()
|
|
|
|
module_parameters = decort_k8s.build_parameters()
|
|
|
|
|
|
|
|
|
|
|
|
amodule = AnsibleModule(argument_spec=module_parameters,
|
|
|
|
amodule = AnsibleModule(argument_spec=module_parameters,
|
|
|
|
supports_check_mode=True,
|
|
|
|
supports_check_mode=True,
|
|
|
@ -125,112 +267,72 @@ def main():
|
|
|
|
['app_id', 'app_secret'],
|
|
|
|
['app_id', 'app_secret'],
|
|
|
|
['user', 'password'],
|
|
|
|
['user', 'password'],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
|
|
|
|
required_one_of=[
|
|
|
|
|
|
|
|
['id', 'name'],
|
|
|
|
|
|
|
|
['rg_id','rg_name']
|
|
|
|
|
|
|
|
],
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
decon = DecortController(amodule)
|
|
|
|
subj = decort_k8s(amodule)
|
|
|
|
k8s_id, k8s_facts = decon.k8s_find(arg_k8s_name=amodule.params['k8s_name'],
|
|
|
|
|
|
|
|
arg_check_state=False)
|
|
|
|
if amodule.params['state'] == 'check':
|
|
|
|
k8s_should_exist = True
|
|
|
|
subj.result['changed'] = False
|
|
|
|
|
|
|
|
if subj.k8s_id:
|
|
|
|
if k8s_id:
|
|
|
|
# cluster is found - package facts and report success to Ansible
|
|
|
|
if k8s_facts['status'] in ["MODELED", "DISABLING", "ENABLING", "DELETING", "DESTROYING", "CREATING",
|
|
|
|
subj.result['failed'] = False
|
|
|
|
"RESTORING"] and amodule.params['state'] != "present":
|
|
|
|
subj.result['facts'] = subj.package_facts(amodule.check_mode)
|
|
|
|
decon.result['failed'] = True
|
|
|
|
amodule.exit_json(**subj.result)
|
|
|
|
decon.result['changed'] = False
|
|
|
|
# we exit the module at this point
|
|
|
|
decon.result['msg'] = ("No change can be done for existing k8s ID {} because of its current "
|
|
|
|
else:
|
|
|
|
"status '{}'").format(k8s_id, k8s_facts['status'])
|
|
|
|
subj.result['failed'] = True
|
|
|
|
elif k8s_facts['status'] in ["DISABLED", "ENABLED", "CREATED", "DELETED"] and amodule.params['state'] == "absent":
|
|
|
|
subj.result['msg'] = ("Cannot locate K8s cluster name '{}'. "
|
|
|
|
if amodule.params['permanent'] is True:
|
|
|
|
"RG ID {}").format(amodule.params['name'],
|
|
|
|
decon.k8s_delete(k8s_id, True)
|
|
|
|
amodule.params['rg_id'],)
|
|
|
|
k8s_facts['status'] = 'DESTROYED'
|
|
|
|
amodule.fail_json(**subj.result)
|
|
|
|
k8s_should_exist = False
|
|
|
|
|
|
|
|
else:
|
|
|
|
if subj.k8s_id:
|
|
|
|
decon.k8s_delete(k8s_id)
|
|
|
|
if subj.k8s_info['status'] in ("DELETING","DESTROYNG","CREATING","DESTROYING",
|
|
|
|
k8s_facts['status'] = 'DELETED'
|
|
|
|
"ENABLING","DISABLING","RESTORING","MODELED"):
|
|
|
|
k8s_should_exist = True
|
|
|
|
subj.error()
|
|
|
|
elif k8s_facts['status'] == "ENABLED" and amodule.params['started'] is True:
|
|
|
|
elif subj.k8s_info['status'] == "DELETED":
|
|
|
|
decon.k8s_state(k8s_facts, amodule.params['state'], amodule.params['started'])
|
|
|
|
if amodule.params['state'] in ('disabled', 'enabled', 'present'):
|
|
|
|
elif k8s_facts['status'] == amodule.params['state'].upper():
|
|
|
|
subj.k8s_restore(subj.k8s_id)
|
|
|
|
decon.k8s_state(k8s_facts, amodule.params['state'])
|
|
|
|
subj.action(amodule.params['state'])
|
|
|
|
elif k8s_facts['status'] in ["ENABLED", "CREATED"] and amodule.params['state'] == "disabled":
|
|
|
|
if amodule.params['state'] == 'absent':
|
|
|
|
decon.k8s_state(k8s_facts, 'disabled')
|
|
|
|
subj.nop()
|
|
|
|
elif k8s_facts['status'] in ["DISABLED", "CREATED"]:
|
|
|
|
elif subj.k8s_info['techStatus'] in ("STARTED","STOPPED"):
|
|
|
|
if amodule.params['state'] == 'enabled':
|
|
|
|
if amodule.params['state'] == 'disabled':
|
|
|
|
decon.k8s_state(k8s_facts, 'enabled', amodule.params['started'])
|
|
|
|
subj.action(amodule.params['state'])
|
|
|
|
elif amodule.params['state'] == "disabled":
|
|
|
|
|
|
|
|
decon.k8s_state(k8s_facts, 'disabled')
|
|
|
|
|
|
|
|
k8s_should_exist = True
|
|
|
|
|
|
|
|
elif k8s_facts['status'] == "DELETED":
|
|
|
|
|
|
|
|
if amodule.params['state'] in ('enabled', 'present'):
|
|
|
|
|
|
|
|
decon.k8s_restore(k8s_id)
|
|
|
|
|
|
|
|
k8s_should_exist = True
|
|
|
|
|
|
|
|
elif amodule.params['state'] == 'disabled':
|
|
|
|
|
|
|
|
decon.result['failed'] = True
|
|
|
|
|
|
|
|
decon.result['changed'] = False
|
|
|
|
|
|
|
|
decon.result['msg'] = ("Invalid target state '{}' requested for k8s ID {} in the "
|
|
|
|
|
|
|
|
"current status '{}'").format(k8s_id,
|
|
|
|
|
|
|
|
amodule.params['state'],
|
|
|
|
|
|
|
|
k8s_facts['status'])
|
|
|
|
|
|
|
|
k8s_should_exist = False
|
|
|
|
|
|
|
|
elif k8s_facts['status'] == "DESTROYED":
|
|
|
|
|
|
|
|
if amodule.params['state'] in ('present', 'enabled'):
|
|
|
|
|
|
|
|
k8s_should_exist = True
|
|
|
|
|
|
|
|
elif amodule.params['state'] == 'absent':
|
|
|
|
elif amodule.params['state'] == 'absent':
|
|
|
|
# nop
|
|
|
|
subj.destroy()
|
|
|
|
decon.result['failed'] = False
|
|
|
|
else:
|
|
|
|
decon.result['changed'] = False
|
|
|
|
subj.action(amodule.params['state'],amodule.params['started'])
|
|
|
|
decon.result['msg'] = ("No state change required for k8s ID {} because of its "
|
|
|
|
elif subj.k8s_info['status'] == "DISABLED":
|
|
|
|
"current status '{}'").format(k8s_id,
|
|
|
|
if amodule.params['state'] == 'absent':
|
|
|
|
k8s_facts['status'])
|
|
|
|
subj.destroy()
|
|
|
|
k8s_should_exist = False
|
|
|
|
elif amodule.params['state'] in ('present','enabled'):
|
|
|
|
elif amodule.params['state'] == 'disabled':
|
|
|
|
subj.action(amodule.params['state'],amodule.params['started'])
|
|
|
|
# error
|
|
|
|
else:
|
|
|
|
decon.result['failed'] = True
|
|
|
|
subj.nop()
|
|
|
|
decon.result['changed'] = False
|
|
|
|
elif subj.k8s_info['status'] == "DESTROED":
|
|
|
|
decon.result['msg'] = ("Invalid target state '{}' requested for k8s ID {} in the "
|
|
|
|
if amodule.params['state'] in ('present','enabled'):
|
|
|
|
"current status '{}'").format(k8s_id,
|
|
|
|
subj.create()
|
|
|
|
amodule.params['state'],
|
|
|
|
if amodule.params['state'] == 'absent':
|
|
|
|
k8s_facts['status'])
|
|
|
|
subj.nop()
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
k8s_should_exist = False
|
|
|
|
|
|
|
|
if amodule.params['state'] == 'absent':
|
|
|
|
if amodule.params['state'] == 'absent':
|
|
|
|
decon.result['failed'] = False
|
|
|
|
subj.nop()
|
|
|
|
decon.result['changed'] = False
|
|
|
|
if amodule.params['state'] in ('present','started'):
|
|
|
|
decon.result['msg'] = ("Nothing to do as target state 'absent' was requested for "
|
|
|
|
subj.create()
|
|
|
|
"non-existent k8s name '{}'").format(amodule.params['k8s_name'])
|
|
|
|
elif amodule.params['state'] in ('stopped', 'disabled','enabled'):
|
|
|
|
elif amodule.params['state'] in ('present', 'enabled'):
|
|
|
|
subj.error()
|
|
|
|
decon.check_amodule_argument('k8s_name')
|
|
|
|
|
|
|
|
k8s_id = decon.k8s_provision(amodule.params['k8s_name'],
|
|
|
|
if subj.result['failed']:
|
|
|
|
amodule.params['wg_name'],
|
|
|
|
amodule.fail_json(**subj.result)
|
|
|
|
amodule.params['k8ci_id'],
|
|
|
|
|
|
|
|
amodule.params['rg_id'],
|
|
|
|
|
|
|
|
amodule.params['master_count'],
|
|
|
|
|
|
|
|
amodule.params['master_cpu'],
|
|
|
|
|
|
|
|
amodule.params['master_ram_mb'],
|
|
|
|
|
|
|
|
amodule.params['master_disk_gb'],
|
|
|
|
|
|
|
|
amodule.params['worker_count'],
|
|
|
|
|
|
|
|
amodule.params['worker_cpu'],
|
|
|
|
|
|
|
|
amodule.params['worker_ram_mb'],
|
|
|
|
|
|
|
|
amodule.params['worker_disk_gb'],
|
|
|
|
|
|
|
|
amodule.params['extnet_id'],
|
|
|
|
|
|
|
|
amodule.params['with_lb'],
|
|
|
|
|
|
|
|
amodule.params['description'],
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
k8s_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 "
|
|
|
|
|
|
|
|
"k8s name '{}' ").format(amodule.params['state'],
|
|
|
|
|
|
|
|
amodule.params['k8s_name'])
|
|
|
|
|
|
|
|
if decon.result['failed']:
|
|
|
|
|
|
|
|
amodule.fail_json(**decon.result)
|
|
|
|
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
if k8s_should_exist:
|
|
|
|
if subj.k8s_should_exist:
|
|
|
|
if decon.result['changed']:
|
|
|
|
subj.result['facts'] = subj.package_facts(amodule.check_mode)
|
|
|
|
_, k8s_facts = decon.k8s_find(arg_k8s_id=k8s_id)
|
|
|
|
amodule.exit_json(**subj.result)
|
|
|
|
decon.result['facts'] = decort_k8s_package_facts(k8s_facts, amodule.check_mode)
|
|
|
|
|
|
|
|
amodule.exit_json(**decon.result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|
|
|
|
main()
|
|
|
|