This commit is contained in:
2026-06-01 18:27:15 +03:00
parent ae986fa9e6
commit 1ab446e05d
34 changed files with 5135 additions and 2113 deletions

View File

@@ -18,11 +18,9 @@ class decort_lb(DecortController):
arg_amodule = self.amodule
self.lb_id = 0
self.lb_facts = None
self.vins_id = 0
self.vins_facts = None
self.rg_id = 0
self.rg_facts = None
self.rg_model = None
self.default_server_check = "enabled"
self.default_alg = "roundrobin"
self.default_settings = {
@@ -38,22 +36,34 @@ class decort_lb(DecortController):
self.is_lb_stopped_or_will_be_stopped: None | bool = None
if arg_amodule.params['lb_id']:
self.lb_id, self.lb_facts = self.lb_find(arg_amodule.params['lb_id'])
if not self.lb_id:
self.result['failed'] = True
self.result['msg'] = "Specified LB ID {} not found."\
.format(arg_amodule.params['lb_id'])
self.amodule.fail_json(**self.result)
self.rg_id = self.lb_facts['rgId']
self.vins_id = self.lb_facts['vinsId']
_, self._lb_info = self.lb_find(lb_id=arg_amodule.params['lb_id'])
if self._lb_info is None:
if arg_amodule.params['state'] == 'absent':
self.nop()
self.exit()
else:
self.message(
self.MESSAGES.obj_not_found(
obj='lb',
id=arg_amodule.params['lb_id'],
)
)
self.exit(fail=True)
self.lb_id = self._lb_info.id
self.rg_id = self._lb_info.rg_id
self.vins_id = self._lb_info.vins_id
elif arg_amodule.params['rg_id']:
self.rg_id, self.rg_facts = self.rg_find(0,arg_amodule.params['rg_id'], arg_rg_name="")
if not self.rg_id:
self.rg_id, self.rg_model = self.rg_find(
0,
arg_amodule.params['rg_id'],
arg_rg_name="",
)
if not self.rg_id or not self.rg_model:
self.result['failed'] = True
self.result['msg'] = "Specified RG ID {} not found.".format(arg_amodule.params['rg_id'])
self.amodule.fail_json(**self.result)
self.acc_id = self.rg_facts['accountId']
self.acc_id = self.rg_model.account_id
elif arg_amodule.params['account_id'] or arg_amodule.params['account_name'] != "":
if not arg_amodule.params['rg_name']:
@@ -67,24 +77,22 @@ class decort_lb(DecortController):
self.result['msg'] = ("Current user does not have access to the requested account "
"or non-existent account specified.")
self.amodule.fail_json(**self.result)
self.rg_id, self.rg_facts = self.rg_find(self.acc_id,0, arg_rg_name=arg_amodule.params['rg_name'])
self.rg_id, self.rg_model = self.rg_find(
self.acc_id,
0,
arg_rg_name=arg_amodule.params['rg_name'],
)
if arg_amodule.params['vins_id']:
self.vins_id, self.vins_facts = self.vins_find(
self.vins_id = self._vins_get_by_id(
vins_id=arg_amodule.params['vins_id']
)
if not self.vins_id:
self.result['failed'] = True
self.result['msg'] = (
f'Specified ViNS ID {arg_amodule.params["vins_id"]}'
f' not found'
)
self.amodule.fail_json(**self.result)
).id
elif arg_amodule.params['vins_name']:
self.vins_id, self.vins_facts = self.vins_find(
self.vins_id, _ = self.vins_find(
vins_id=arg_amodule.params['vins_id'],
vins_name=arg_amodule.params['vins_name'],
rg_id=self.rg_id)
rg_id=self.rg_id,
)
if not self.vins_id:
self.result['failed'] = True
self.result['msg'] = (
@@ -94,10 +102,17 @@ class decort_lb(DecortController):
self.amodule.fail_json(**self.result)
if self.rg_id and arg_amodule.params['lb_name']:
self.lb_id, self.lb_facts = self.lb_find(0,arg_amodule.params['lb_name'],self.rg_id)
self.lb_id, self._lb_info = self.lb_find(
0,
arg_amodule.params['lb_name'],
self.rg_id,
)
if self.lb_id and self.lb_facts['status'] != 'DESTROYED':
self.acc_id = self.lb_facts['accountId']
if (
self._lb_info
and self._lb_info.status != sdk_types.LBStatus.DESTROYED
):
self.acc_id = self._lb_info.account_id
self.check_amodule_args_for_change()
else:
self.check_amodule_args_for_create()
@@ -115,54 +130,73 @@ class decort_lb(DecortController):
zone_id=self.aparams['zone_id'],
start=start_after_create,
)
if self.lb_id and (self.amodule.params['backends'] or
self.amodule.params['frontends']):
self.lb_id, self.lb_facts = self.lb_find(0,self.amodule.params['lb_name'],self.rg_id)
if (
self.lb_id and (
self.amodule.params['backends']
or self.amodule.params['frontends']
)
):
self._lb_info = self._lb_get_by_id(lb_id=self.lb_id)
self.lb_update(
lb_facts=self.lb_facts,
lb_model=self.lb_info,
aparam_backends=self.amodule.params['backends'],
aparam_frontends=self.amodule.params['frontends'],
aparam_servers=self.amodule.params['servers'],
)
return
def action(self,d_state='',restore=False):
if restore == True:
self.lb_restore(lb_id=self.lb_id)
_, self.lb_facts = self._lb_get_by_id(lb_id=self.lb_id)
self.lb_state(self.lb_facts, 'enabled')
_, self.lb_facts = self._lb_get_by_id(lb_id=self.lb_id)
def action(
self,
d_state='',
restore=False,
):
if restore:
self.sdk_checkmode(self.api.ca.lb.restore)(
lb_id=self.lb_id,
)
self._lb_info = self._lb_get_by_id(lb_id=self.lb_id)
self.lb_state(self.lb_info, 'enabled')
self._lb_info = self._lb_get_by_id(lb_id=self.lb_id)
self.lb_update(
lb_facts=self.lb_facts,
lb_model=self.lb_info,
aparam_backends=self.amodule.params['backends'],
aparam_frontends=self.amodule.params['frontends'],
aparam_servers=self.amodule.params['servers'],
aparam_sysctl=self.aparams['sysctl'],
)
self._lb_info = self._lb_get_by_id(lb_id=self.lb_id)
if d_state != '':
self.lb_state(self.lb_facts, d_state)
_, self.lb_facts = self._lb_get_by_id(lb_id=self.lb_id)
self.lb_state(self.lb_info, d_state)
self._lb_info = self._lb_get_by_id(lb_id=self.lb_id)
if (d_state == 'enabled' and
self.lb_facts.get('status') == 'ENABLED' and
self.lb_facts.get('techStatus') == 'STOPPED'):
self.lb_state(self.lb_facts, 'started')
_, self.lb_facts = self._lb_get_by_id(lb_id=self.lb_id)
if (
d_state == 'enabled'
and self.lb_info.status == sdk_types.LBStatus.ENABLED
and self.lb_info.tech_status == sdk_types.LBTechStatus.STOPPED
):
self.lb_state(self.lb_info, 'started')
self._lb_info = self._lb_get_by_id(lb_id=self.lb_id)
aparam_zone_id = self.aparams['zone_id']
if aparam_zone_id is not None and aparam_zone_id != self.lb_facts['zoneId']:
if (
aparam_zone_id is not None
and aparam_zone_id != self.lb_info.zone_id
):
self.lb_migrate_to_zone(
lb_id=self.lb_id,
lb_id=self.lb_info.id,
zone_id=aparam_zone_id,
)
return
def delete(self):
self.lb_delete(self.lb_id, self.amodule.params['permanently'])
self.lb_id, self.lb_facts = self._lb_get_by_id(self.lb_id)
self.sdk_checkmode(self.api.ca.lb.delete)(
lb_id=self.lb_id,
permanently=self.amodule.params['permanently'],
)
self._lb_info = self._lb_get_by_id(lb_id=self.lb_id)
return
def nop(self):
@@ -173,30 +207,39 @@ class decort_lb(DecortController):
"""
self.result['failed'] = False
self.result['changed'] = False
if self.lb_id:
self.result['msg'] = ("No state change required for LB ID {} because of its "
"current status '{}'.").format(self.lb_id, self.lb_facts['status'])
if self._lb_info:
self.result['msg'] = (
f'No state change required for LB ID {self._lb_info.id} '
f'because of its current status "{self._lb_info.status}".'
)
else:
self.result['msg'] = ("No state change to '{}' can be done for "
"non-existent LB instance.").format(self.amodule.params['state'])
self.result['msg'] = (
f'No state change to "{self.amodule.params['state']}" '
f'can be done for non-existent LB instance.'
)
return
def error(self):
self.result['failed'] = True
self.result['changed'] = False
if self.vins_id:
if self._lb_info:
self.result['failed'] = True
self.result['changed'] = False
self.result['msg'] = ("Invalid target state '{}' requested for LB ID {} in the "
"current status '{}'").format(self.lb_id,
self.amodule.params['state'],
self.lb_facts['status'])
self.result['msg'] = (
f'Invalid target state "{self.amodule.params['state']}" '
f'requested for LB ID {self._lb_info.id} in the current status '
f'"{self._lb_info.status}".'
)
else:
self.result['failed'] = True
self.result['changed'] = False
self.result['msg'] = ("Invalid target state '{}' requested for non-existent "
"LB name '{}'").format(self.amodule.params['state'],
self.amodule.params['lb_name'])
self.result['msg'] = (
f'Invalid target state "{self.amodule.params['state']}" '
f'requested for non-existent LB name '
f'"{self.amodule.params['lb_name']}".'
)
return
def package_facts(self, arg_check_mode=False):
"""Package a dictionary of LB facts according to the decort_lb module specification.
This dictionary will be returned to the upstream Ansible engine at the completion of
@@ -205,34 +248,16 @@ class decort_lb(DecortController):
@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",
sysctl={},
)
ret_dict = {}
if arg_check_mode:
# in check mode return immediately with the default values
return ret_dict
if self.lb_facts is None:
# if void facts provided - change state value to ABSENT and return
ret_dict['state'] = "ABSENT"
if self._lb_info is None:
return ret_dict
ret_dict['id'] = self.lb_facts['id']
ret_dict['name'] = self.lb_facts['name']
ret_dict['state'] = self.lb_facts['status']
ret_dict['account_id'] = self.lb_facts['accountId']
ret_dict['rg_id'] = self.lb_facts['rgId']
ret_dict['gid'] = self.lb_facts['gid']
if self.amodule.params['state']!="absent":
ret_dict['backends'] = self.lb_facts['backends']
ret_dict['frontends'] = self.lb_facts['frontends']
ret_dict['sysctl'] = self.lb_facts['sysctlParams']
ret_dict['zone_id'] = self.lb_facts['zoneId']
ret_dict['tech_status'] = self.lb_facts['techStatus']
return ret_dict
return self._lb_info.model_dump()
@property
def amodule_init_args(self) -> dict:
@@ -321,18 +346,16 @@ class decort_lb(DecortController):
def check_amodule_args_for_change(self):
check_errors = False
lb_info: dict = self.lb_facts
self.is_lb_stopped_or_will_be_stopped = (
self.is_lb_stopped_or_will_be_stopped = (
(
lb_info['techStatus'] == 'STOPPED'
self.lb_info.tech_status == sdk_types.LBTechStatus.STOPPED
and (
self.aparams['state'] is None
or self.aparams['state'] in ('present', 'stopped')
)
)
or (
lb_info['techStatus'] != 'STOPPED'
self.lb_info.tech_status != sdk_types.LBTechStatus.STOPPED
and self.aparams['state'] == 'stopped'
)
)
@@ -341,7 +364,7 @@ class decort_lb(DecortController):
check_errors = True
if (
self.aparams['zone_id'] is not None
and self.aparams['zone_id'] != lb_info['zoneId']
and self.aparams['zone_id'] != self.lb_info.zone_id
and not self.is_lb_stopped_or_will_be_stopped
):
check_errors = True
@@ -364,18 +387,32 @@ class decort_lb(DecortController):
@DecortController.handle_sdk_exceptions
def run(self):
amodule = self.amodule
if self.lb_id:
if self.lb_facts['status'] in ["MODELED", "DISABLING", "ENABLING", "DELETING","DESTROYING","RESTORING"]:
if self._lb_info:
if self._lb_info.status in [
sdk_types.LBStatus.MODELED,
sdk_types.LBStatus.DISABLING,
sdk_types.LBStatus.ENABLING,
sdk_types.LBStatus.DELETING,
sdk_types.LBStatus.DESTROYING,
sdk_types.LBStatus.RESTORING,
]:
self.result['failed'] = True
self.result['changed'] = False
self.result['msg'] = ("No change can be done for existing LB ID {} because of its current "
"status '{}'").format(self.lb_id, self.lb_facts['status'])
elif self.lb_facts['status'] in ('DISABLED', 'ENABLED', 'CREATED'):
self.result['msg'] = (
f'No change can be done for existing LB ID '
f'{self._lb_info.id} because of its current status '
f'"{self._lb_info.status.name}".'
)
elif self._lb_info.status in [
sdk_types.LBStatus.DISABLED,
sdk_types.LBStatus.ENABLED,
sdk_types.LBStatus.CREATED,
]:
if amodule.params['state'] == 'absent':
self.delete()
else:
self.action(d_state=amodule.params['state'])
elif self.lb_facts['status'] == "DELETED":
elif self._lb_info.status == sdk_types.LBStatus.DELETED:
if amodule.params['state'] == 'present':
self.action(restore=True)
elif amodule.params['state'] == 'enabled':
@@ -385,7 +422,7 @@ class decort_lb(DecortController):
self.delete()
elif amodule.params['state'] == 'disabled':
self.error()
elif self.lb_facts['status'] == "DESTROYED":
elif self._lb_info.status == sdk_types.LBStatus.DESTROYED:
if amodule.params['state'] in ('present', 'enabled'):
self.create()
elif amodule.params['state'] == 'absent':
@@ -407,7 +444,7 @@ class decort_lb(DecortController):
amodule.fail_json(**self.result)
else:
if self.result['changed']:
_, self.lb_facts = self.lb_find(lb_id=self.lb_id)
self._lb_info = self._lb_get_by_id(lb_id=self.lb_id)
self.result['facts'] = self.package_facts(amodule.check_mode)
amodule.exit_json(**self.result)