12.0.0
This commit is contained in:
213
library/decort_sdn_access_group.py
Normal file
213
library/decort_sdn_access_group.py
Normal file
@@ -0,0 +1,213 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
DOCUMENTATION = r'''
|
||||
---
|
||||
module: decort_sdn_access_group
|
||||
|
||||
description: See L(Module Documentation,https://repository.basistech.ru/BASIS/decort-ansible/wiki/Home). # noqa: E501
|
||||
'''
|
||||
|
||||
from typing import Any
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
from ansible.module_utils.decort_utils import DecortController
|
||||
import requests
|
||||
|
||||
|
||||
class DecortSDNAccessGroups(DecortController):
|
||||
access_group_id: str | None = None
|
||||
_access_group_info: dict[str, Any] | None = None
|
||||
need_final_get: bool = True
|
||||
|
||||
def __init__(self):
|
||||
super().__init__(AnsibleModule(**self.amodule_init_args))
|
||||
|
||||
@property
|
||||
def amodule_init_args(self) -> dict:
|
||||
return self.pack_amodule_init_args(
|
||||
argument_spec=dict(
|
||||
access_group_id=dict(
|
||||
type='str',
|
||||
),
|
||||
comment=dict(
|
||||
type='str',
|
||||
),
|
||||
display_name=dict(
|
||||
type='str',
|
||||
),
|
||||
state=dict(
|
||||
type='str',
|
||||
choices=['present', 'absent'],
|
||||
),
|
||||
),
|
||||
supports_check_mode=True,
|
||||
)
|
||||
|
||||
@property
|
||||
def access_group_info(self) -> dict[str, Any]:
|
||||
if self._access_group_info is None:
|
||||
if not isinstance(self.access_group_id, str):
|
||||
raise TypeError
|
||||
access_group_info = self.get()
|
||||
if access_group_info is None:
|
||||
raise TypeError
|
||||
self._access_group_info = access_group_info
|
||||
return self._access_group_info
|
||||
|
||||
def check_amodule_args(self):
|
||||
check_errors = False
|
||||
|
||||
if (
|
||||
self.aparams['state'] == 'absent'
|
||||
and self.aparams['access_group_id'] is None
|
||||
):
|
||||
check_errors = True
|
||||
self.message(
|
||||
'Check for parameter "access_group_id" failed: '
|
||||
'access_group_id must be specified when state is "absent".'
|
||||
)
|
||||
|
||||
if check_errors:
|
||||
self.exit(fail=True)
|
||||
|
||||
def check_amodule_args_for_create(self):
|
||||
check_errors = False
|
||||
|
||||
if self.aparams['comment'] is None:
|
||||
check_errors = True
|
||||
self.message(
|
||||
'Check for parameter "comment" failed: parameter must '
|
||||
'be specified for creating an access_group.'
|
||||
)
|
||||
if self.aparams['display_name'] is None:
|
||||
check_errors = True
|
||||
self.message(
|
||||
'Check for parameter "display_name" failed: parameter must '
|
||||
'be specified for creating an access_group.'
|
||||
)
|
||||
|
||||
if check_errors:
|
||||
self.exit(fail=True)
|
||||
|
||||
@DecortController.waypoint
|
||||
@DecortController.checkmode
|
||||
def get(self) -> dict[str, Any] | None:
|
||||
params = {'access_group_id': self.access_group_id}
|
||||
response = self.decort_api_call(
|
||||
arg_req_function=requests.get,
|
||||
arg_api_name='/restmachine/sdn/access_group/get',
|
||||
arg_params=params,
|
||||
not_fail_codes=[404],
|
||||
accept_json_response=True,
|
||||
)
|
||||
if response.status_code == 404:
|
||||
self.message(
|
||||
self.MESSAGES.obj_not_found(
|
||||
obj='access_group',
|
||||
id=self.access_group_id,
|
||||
)
|
||||
)
|
||||
self.exit(fail=True)
|
||||
return response.json()
|
||||
|
||||
@DecortController.waypoint
|
||||
@DecortController.checkmode
|
||||
def access_group_find(self, display_name: str) -> dict[str, Any] | None:
|
||||
params = {'display_name': display_name}
|
||||
response = self.decort_api_call(
|
||||
arg_req_function=requests.get,
|
||||
arg_api_name='/restmachine/sdn/access_group/list',
|
||||
arg_params=params,
|
||||
accept_json_response=True,
|
||||
)
|
||||
for access_group in response.json() or []:
|
||||
if access_group['display_name'] == display_name:
|
||||
return access_group
|
||||
return None
|
||||
|
||||
@DecortController.waypoint
|
||||
@DecortController.checkmode
|
||||
def create(self):
|
||||
params = {
|
||||
'comment': self.aparams['comment'],
|
||||
'display_name': self.aparams['display_name'],
|
||||
}
|
||||
response = self.decort_api_call(
|
||||
arg_req_function=requests.post,
|
||||
arg_api_name='/restmachine/sdn/access_group/create',
|
||||
arg_params=params,
|
||||
accept_json_response=True,
|
||||
)
|
||||
self.access_group_id = response.json()['id']
|
||||
self.set_changed()
|
||||
|
||||
@DecortController.waypoint
|
||||
@DecortController.checkmode
|
||||
def delete(self):
|
||||
params = {'access_group_id': self.access_group_id}
|
||||
response = self.decort_api_call(
|
||||
arg_req_function=requests.delete,
|
||||
arg_api_name='/restmachine/sdn/access_group/delete',
|
||||
arg_params=params,
|
||||
not_fail_codes=[204, 404]
|
||||
)
|
||||
self.need_final_get = False
|
||||
if response.status_code == 204:
|
||||
self.set_changed()
|
||||
self.message(
|
||||
self.MESSAGES.obj_deleted(
|
||||
obj='access_group',
|
||||
id=self.access_group_id,
|
||||
)
|
||||
)
|
||||
else:
|
||||
self.message(
|
||||
self.MESSAGES.obj_not_found(
|
||||
obj='access_group',
|
||||
id=self.access_group_id,
|
||||
)
|
||||
)
|
||||
self.facts = {}
|
||||
|
||||
def run(self):
|
||||
self.check_amodule_args()
|
||||
|
||||
if self.aparams['access_group_id']:
|
||||
self.access_group_id = self.aparams['access_group_id']
|
||||
elif self.aparams['state'] != 'absent':
|
||||
self.check_amodule_args_for_create()
|
||||
access_group_info = self.access_group_find(
|
||||
display_name=self.aparams['display_name'],
|
||||
)
|
||||
if access_group_info:
|
||||
self.access_group_id = access_group_info['id']
|
||||
self._access_group_info = access_group_info
|
||||
|
||||
if self.access_group_id:
|
||||
if self.aparams['state'] == 'absent':
|
||||
self.delete()
|
||||
else:
|
||||
state = self.aparams['state']
|
||||
if state is None:
|
||||
state = 'present'
|
||||
self.message(
|
||||
msg=self.MESSAGES.default_value_used(
|
||||
param_name='state',
|
||||
default_value=state,
|
||||
),
|
||||
warning=True,
|
||||
)
|
||||
|
||||
if state != 'absent':
|
||||
self.create()
|
||||
|
||||
if self.need_final_get:
|
||||
self.facts = self.get()
|
||||
self.exit()
|
||||
|
||||
|
||||
def main():
|
||||
DecortSDNAccessGroups().run()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user