You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
322 lines
12 KiB
322 lines
12 KiB
#!/usr/bin/python
|
|
|
|
DOCUMENTATION = r'''
|
|
---
|
|
module: decort_group
|
|
|
|
description: See L(Module Documentation,https://repository.basistech.ru/BASIS/decort-ansible/wiki/Home).
|
|
'''
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
from ansible.module_utils.basic import env_fallback
|
|
from ansible.module_utils.decort_utils import *
|
|
|
|
|
|
class decort_group(DecortController):
|
|
def __init__(self):
|
|
super(decort_group, self).__init__(AnsibleModule(**self.amodule_init_args))
|
|
arg_amodule = self.amodule
|
|
|
|
self.group_should_exist = False
|
|
validated_bservice_id = None
|
|
#find and validate B-Service
|
|
|
|
validated_bservice_id, bservice_info = self.bservice_get_by_id(arg_amodule.params['bservice_id'])
|
|
if not validated_bservice_id:
|
|
self.result['failed'] = True
|
|
self.result['changed'] = False
|
|
self.result['msg'] = ("Cannot find B-service ID {}.").format(arg_amodule.params['bservice_id'])
|
|
self.amodule.fail_json(**self.result)
|
|
#find group
|
|
self.bservice_id = validated_bservice_id
|
|
self.bservice_info = bservice_info
|
|
self.group_id,self.group_info = self.group_find(
|
|
bs_id=validated_bservice_id,
|
|
bs_info=bservice_info,
|
|
group_id=arg_amodule.params['id'],
|
|
group_name=arg_amodule.params['name'],
|
|
)
|
|
|
|
if self.group_id:
|
|
self.group_should_exist = True
|
|
|
|
return
|
|
def nop(self):
|
|
"""No operation (NOP) handler for B-service.
|
|
This function is intended to be called from the main switch construct of the module
|
|
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.group_id:
|
|
self.result['msg'] = ("No state change required for B-service ID {} because of its "
|
|
"current status '{}'.").format(self.group_id, self.group_info['status'])
|
|
else:
|
|
self.result['msg'] = ("No state change to '{}' can be done for "
|
|
"non-existent B-service instance.").format(self.amodule.params['state'])
|
|
return
|
|
|
|
def error(self):
|
|
self.result['failed'] = True
|
|
self.result['changed'] = False
|
|
if self.group_id:
|
|
self.result['msg'] = ("Invalid target state '{}' requested for Group ID {} in the "
|
|
"current status '{}'.").format(self.group_id,
|
|
self.amodule.params['state'],
|
|
self.group_info['status'])
|
|
else:
|
|
self.result['msg'] = ("Invalid target state '{}' requested for non-existent Group name '{}' "
|
|
"in B-service {}").format(self.amodule.params['state'],
|
|
self.amodule.params['name'],
|
|
self.amodule.params['bservice_id'],
|
|
)
|
|
return
|
|
|
|
def create(self):
|
|
self.group_id=self.group_provision(
|
|
self.bservice_id,
|
|
self.amodule.params['name'],
|
|
self.amodule.params['count'],
|
|
self.amodule.params['cpu'],
|
|
self.amodule.params['ram'],
|
|
self.amodule.params['boot_disk'],
|
|
self.amodule.params['image_id'],
|
|
self.amodule.params['driver'],
|
|
self.amodule.params['role'],
|
|
self.amodule.params['networks'],
|
|
self.amodule.params['timeoutStart'],
|
|
)
|
|
|
|
if self.amodule.params['state'] in ('started','present'):
|
|
self.group_state(self.bservice_id,self.group_id,self.amodule.params['state'])
|
|
return
|
|
|
|
def action(self):
|
|
#change desired state
|
|
if (
|
|
self.group_info['techStatus'] == 'STARTED' and self.amodule.params['state'] == 'stopped') or (
|
|
self.group_info['techStatus'] == 'STOPPED' and self.amodule.params['state'] in ('started','present')
|
|
):
|
|
self.group_state(self.bservice_id,self.group_id,self.amodule.params['state'])
|
|
|
|
if self.aparams['count'] != None:
|
|
self.group_resize_count(
|
|
bs_id=self.bservice_id,
|
|
gr_dict=self.group_info,
|
|
desired_count=self.aparams['count'],
|
|
)
|
|
|
|
for aparam_name, info_key in {'cpu': 'cpu',
|
|
'boot_disk': 'disk',
|
|
'role': 'role',
|
|
'ram': 'ram',
|
|
'name': 'name',
|
|
}.items():
|
|
aparam_value = self.aparams[aparam_name]
|
|
group_info_value = self.group_info[info_key]
|
|
if aparam_value != None and aparam_value != group_info_value:
|
|
self.group_update(
|
|
bs_id=self.bservice_id,
|
|
gr_dict=self.group_info,
|
|
arg_cpu=self.aparams['cpu'],
|
|
arg_disk=self.aparams['boot_disk'],
|
|
arg_name=self.aparams['name'],
|
|
arg_role=self.aparams['role'],
|
|
arg_ram=self.aparams['ram'],
|
|
)
|
|
break
|
|
|
|
if self.aparams['networks'] != None:
|
|
self.group_update_net(
|
|
bs_id=self.bservice_id,
|
|
gr_dict=self.group_info,
|
|
arg_net=self.aparams['networks'],
|
|
)
|
|
|
|
def destroy(self):
|
|
|
|
self.group_delete(
|
|
self.bservice_id,
|
|
self.group_id
|
|
)
|
|
self.group_should_exist = False
|
|
|
|
return
|
|
|
|
def package_facts(self,check_mode=False):
|
|
|
|
ret_dict = dict(
|
|
name="",
|
|
state="CHECK_MODE",
|
|
account_id=0,
|
|
rg_id=0,
|
|
)
|
|
|
|
if check_mode:
|
|
# in check mode return immediately with the default values
|
|
return ret_dict
|
|
if self.result['changed'] == True:
|
|
self.group_id,self.group_info = self.group_find(
|
|
self.bservice_id,
|
|
self.bservice_info,
|
|
self.group_id
|
|
)
|
|
|
|
ret_dict['account_id'] = self.group_info['accountId']
|
|
ret_dict['rg_id'] = self.group_info['rgId']
|
|
ret_dict['id'] = self.group_info['id']
|
|
ret_dict['name'] = self.group_info['name']
|
|
ret_dict['techStatus'] = self.group_info['techStatus']
|
|
ret_dict['state'] = self.group_info['status']
|
|
ret_dict['Computes'] = self.group_info['computes']
|
|
return ret_dict
|
|
|
|
@property
|
|
def amodule_init_args(self) -> dict:
|
|
return self.pack_amodule_init_args(
|
|
argument_spec=dict(
|
|
account_id=dict(
|
|
type='int',
|
|
),
|
|
account_name=dict(
|
|
type='str',
|
|
default='',
|
|
),
|
|
state=dict(
|
|
type='str',
|
|
default='present',
|
|
choices=[
|
|
'absent',
|
|
'started',
|
|
'stopped',
|
|
'present',
|
|
'check',
|
|
],
|
|
),
|
|
name=dict(
|
|
type='str',
|
|
),
|
|
id=dict(
|
|
type='int',
|
|
),
|
|
image_id=dict(
|
|
type='int',
|
|
),
|
|
image_name=dict(
|
|
type='str',
|
|
),
|
|
driver=dict(
|
|
type='str',
|
|
choices=[
|
|
'KVM_X86',
|
|
'SVA_KVM_X86',
|
|
],
|
|
default='KVM_X86',
|
|
),
|
|
boot_disk=dict(
|
|
type='int',
|
|
),
|
|
bservice_id=dict(
|
|
type='int',
|
|
required=True,
|
|
),
|
|
count=dict(
|
|
type='int',
|
|
),
|
|
timeoutStart=dict(
|
|
type='int',
|
|
),
|
|
role=dict(
|
|
type='str',
|
|
),
|
|
cpu=dict(
|
|
type='int',
|
|
),
|
|
ram=dict(
|
|
type='int',
|
|
),
|
|
networks=dict(
|
|
type='list',
|
|
elements='dict',
|
|
options=dict(
|
|
type=dict(
|
|
type='str',
|
|
required=True,
|
|
choices=[
|
|
'VINS',
|
|
'EXTNET',
|
|
]
|
|
),
|
|
id=dict(
|
|
type='int',
|
|
required=True,
|
|
)
|
|
)
|
|
),
|
|
),
|
|
supports_check_mode=True,
|
|
required_one_of=[
|
|
('id', 'name'),
|
|
('id', 'networks'),
|
|
('id', 'count'),
|
|
('id', 'cpu'),
|
|
('id', 'ram'),
|
|
('id', 'boot_disk'),
|
|
('id', 'image_id'),
|
|
('id', 'driver'),
|
|
],
|
|
)
|
|
|
|
def main():
|
|
subj = decort_group()
|
|
amodule = subj.amodule
|
|
|
|
if amodule.params['state'] == 'check':
|
|
subj.result['changed'] = False
|
|
if subj.group_id:
|
|
# cluster is found - package facts and report success to Ansible
|
|
subj.result['failed'] = False
|
|
subj.result['facts'] = subj.package_facts(amodule.check_mode)
|
|
amodule.exit_json(**subj.result)
|
|
# we exit the module at this point
|
|
else:
|
|
subj.result['failed'] = True
|
|
subj.result['msg'] = ("Cannot locate Group name '{}'. "
|
|
"B-service ID {}").format(amodule.params['name'],
|
|
amodule.params['bservice_id'],)
|
|
amodule.fail_json(**subj.result)
|
|
|
|
if subj.group_id:
|
|
if subj.group_info['status'] in ("DELETING","DESTROYNG","CREATING","DESTROYING",
|
|
"ENABLING","DISABLING","RESTORING","MODELED",
|
|
"DISABLED","DESTROYED"):
|
|
subj.error()
|
|
elif subj.group_info['status'] in ("DELETED","DESTROYED"):
|
|
if amodule.params['state'] == 'absent':
|
|
subj.nop()
|
|
if amodule.params['state'] in ('present','started','stopped'):
|
|
subj.create()
|
|
elif subj.group_info['techStatus'] in ("STARTED","STOPPED"):
|
|
if amodule.params['state'] == 'absent':
|
|
subj.destroy()
|
|
else:
|
|
subj.action()
|
|
|
|
else:
|
|
if amodule.params['state'] == 'absent':
|
|
subj.nop()
|
|
if amodule.params['state'] in ('present','started','stopped'):
|
|
subj.create()
|
|
|
|
if subj.result['failed']:
|
|
amodule.fail_json(**subj.result)
|
|
else:
|
|
if subj.group_should_exist:
|
|
subj.result['facts'] = subj.package_facts(amodule.check_mode)
|
|
amodule.exit_json(**subj.result)
|
|
else:
|
|
amodule.exit_json(**subj.result)
|
|
|
|
if __name__ == "__main__":
|
|
main() |