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.
347 lines
13 KiB
347 lines
13 KiB
#!/usr/bin/python
|
|
|
|
DOCUMENTATION = r'''
|
|
---
|
|
module: decort_user_info
|
|
|
|
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.decort_utils import DecortController
|
|
|
|
|
|
class DecortUserInfo(DecortController):
|
|
def __init__(self):
|
|
super().__init__(AnsibleModule(**self.amodule_init_args))
|
|
self.check_amodule_args()
|
|
|
|
@property
|
|
def amodule_init_args(self) -> dict:
|
|
return self.pack_amodule_init_args(
|
|
argument_spec=dict(
|
|
accounts=dict(
|
|
type='dict',
|
|
options=dict(
|
|
deleted=dict(
|
|
type='bool',
|
|
default=False,
|
|
),
|
|
filter=dict(
|
|
type='dict',
|
|
options=dict(
|
|
rights=dict(
|
|
type='str',
|
|
choices=[
|
|
e.value
|
|
for e in self.AccountUserRights
|
|
],
|
|
),
|
|
id=dict(
|
|
type='int',
|
|
),
|
|
name=dict(
|
|
type='str',
|
|
),
|
|
status=dict(
|
|
type='str',
|
|
choices=[
|
|
e.value for e in self.AccountStatus
|
|
],
|
|
),
|
|
),
|
|
),
|
|
pagination=dict(
|
|
type='dict',
|
|
options=dict(
|
|
number=dict(
|
|
type='int',
|
|
default=1,
|
|
),
|
|
size=dict(
|
|
type='int',
|
|
required=True,
|
|
),
|
|
),
|
|
),
|
|
resource_consumption=dict(
|
|
type='bool',
|
|
default=False,
|
|
),
|
|
sorting=dict(
|
|
type='dict',
|
|
options=dict(
|
|
asc=dict(
|
|
type='bool',
|
|
default=True,
|
|
),
|
|
field=dict(
|
|
type='str',
|
|
choices=[
|
|
e.value
|
|
for e in self.AccountSortableField
|
|
],
|
|
required=True,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
api_methods=dict(
|
|
type='bool',
|
|
default=False,
|
|
),
|
|
audits=dict(
|
|
type='dict',
|
|
options=dict(
|
|
filter=dict(
|
|
type='dict',
|
|
options=dict(
|
|
api_method=dict(
|
|
type='str',
|
|
),
|
|
status_code=dict(
|
|
type='dict',
|
|
options=dict(
|
|
min=dict(
|
|
type='int',
|
|
),
|
|
max=dict(
|
|
type='int',
|
|
),
|
|
),
|
|
),
|
|
time=dict(
|
|
type='dict',
|
|
options=dict(
|
|
start=dict(
|
|
type='dict',
|
|
options=dict(
|
|
unix=dict(
|
|
type='int',
|
|
),
|
|
date_time=dict(
|
|
type='str',
|
|
),
|
|
),
|
|
mutually_exclusive=[
|
|
('unix', 'date_time'),
|
|
],
|
|
),
|
|
end=dict(
|
|
type='dict',
|
|
options=dict(
|
|
unix=dict(
|
|
type='int',
|
|
),
|
|
date_time=dict(
|
|
type='str',
|
|
),
|
|
),
|
|
mutually_exclusive=[
|
|
('unix', 'date_time'),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
pagination=dict(
|
|
type='dict',
|
|
apply_defaults=True,
|
|
options=dict(
|
|
number=dict(
|
|
type='int',
|
|
default=1,
|
|
),
|
|
size=dict(
|
|
type='int',
|
|
default=50,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
objects_search=dict(
|
|
type='str',
|
|
),
|
|
resource_consumption=dict(
|
|
type='bool',
|
|
default=False,
|
|
),
|
|
),
|
|
supports_check_mode=True,
|
|
)
|
|
|
|
def check_amodule_args(self):
|
|
"""
|
|
Additional validation of Ansible Module arguments.
|
|
This validation cannot be implemented using
|
|
Ansible Argument spec.
|
|
"""
|
|
|
|
check_error = False
|
|
|
|
match self.aparams['audits']:
|
|
case {'filter': {'time':
|
|
{'start': {'date_time': str() as dt_str}}
|
|
}
|
|
}:
|
|
if self.dt_str_to_sec(dt_str=dt_str) is None:
|
|
self.message(self.MESSAGES.str_not_parsed(string=dt_str))
|
|
check_error = True
|
|
match self.aparams['audits']:
|
|
case {'filter': {'time':
|
|
{'end': {'date_time': str() as dt_str}}
|
|
}
|
|
}:
|
|
if self.dt_str_to_sec(dt_str=dt_str) is None:
|
|
self.message(self.MESSAGES.str_not_parsed(string=dt_str))
|
|
check_error = True
|
|
|
|
if check_error:
|
|
self.exit(fail=True)
|
|
|
|
@property
|
|
def mapped_accounts_args(self) -> None | dict:
|
|
"""
|
|
Map the module argument `accounts` to
|
|
arguments dictionary for the method
|
|
`DecortController.user_accounts`.
|
|
"""
|
|
|
|
input_args = self.aparams['accounts']
|
|
if not input_args:
|
|
return input_args
|
|
|
|
mapped_args = {}
|
|
|
|
mapped_args['deleted'] = input_args['deleted']
|
|
|
|
mapped_args['resource_consumption'] = (
|
|
input_args['resource_consumption']
|
|
)
|
|
|
|
input_args_filter = input_args['filter']
|
|
if input_args_filter:
|
|
input_args_filter_rights = input_args_filter['rights']
|
|
if input_args_filter_rights:
|
|
mapped_args['account_user_rights'] = (
|
|
self.AccountUserRights(input_args_filter_rights)
|
|
)
|
|
|
|
mapped_args['account_id'] = input_args_filter['id']
|
|
|
|
mapped_args['account_name'] = input_args_filter['name']
|
|
|
|
input_args_filter_status = input_args_filter['status']
|
|
if input_args_filter_status:
|
|
mapped_args['account_status'] = (
|
|
self.AccountStatus(input_args_filter_status)
|
|
)
|
|
|
|
input_args_pagination = input_args['pagination']
|
|
if input_args_pagination:
|
|
mapped_args['page_number'] = input_args_pagination['number']
|
|
mapped_args['page_size'] = input_args_pagination['size']
|
|
|
|
input_args_sorting = input_args['sorting']
|
|
if input_args_sorting:
|
|
mapped_args['sort_by_asc'] = input_args_sorting['asc']
|
|
|
|
input_args_sorting_field = input_args_sorting['field']
|
|
if input_args_sorting_field:
|
|
mapped_args['sort_by_field'] = (
|
|
self.AccountSortableField(input_args_sorting_field)
|
|
)
|
|
|
|
return mapped_args
|
|
|
|
@property
|
|
def mapped_audits_args(self):
|
|
"""
|
|
Map the module argument `audits` to
|
|
arguments dictionary for the method
|
|
`DecortController.user_audits`.
|
|
"""
|
|
|
|
input_args = self.aparams['audits']
|
|
if not input_args:
|
|
return input_args
|
|
|
|
mapped_args = {}
|
|
|
|
input_args_filter = input_args['filter']
|
|
if input_args_filter:
|
|
mapped_args['api_method'] = input_args_filter['api_method']
|
|
|
|
match input_args_filter['status_code']:
|
|
case {'min': int() as min_status_code}:
|
|
mapped_args['min_status_code'] = min_status_code
|
|
match input_args_filter['status_code']:
|
|
case {'max': int() as max_status_code}:
|
|
mapped_args['max_status_code'] = max_status_code
|
|
|
|
match input_args_filter['time']:
|
|
case {'start': {'unix': int() as start_unix_time}}:
|
|
mapped_args['start_unix_time'] = start_unix_time
|
|
case {'start': {'date_time': str() as start_dt_str}}:
|
|
mapped_args['start_unix_time'] = self.dt_str_to_sec(
|
|
dt_str=start_dt_str
|
|
)
|
|
match input_args_filter['time']:
|
|
case {'end': {'unix': int() as end_unix_time}}:
|
|
mapped_args['end_unix_time'] = end_unix_time
|
|
case {'end': {'date_time': str() as end_dt_str}}:
|
|
mapped_args['end_unix_time'] = self.dt_str_to_sec(
|
|
dt_str=end_dt_str
|
|
)
|
|
|
|
input_args_pagination = input_args['pagination']
|
|
if input_args_pagination:
|
|
mapped_args['page_number'] = input_args_pagination['number']
|
|
mapped_args['page_size'] = input_args_pagination['size']
|
|
|
|
return mapped_args
|
|
|
|
def run(self):
|
|
self.get_info()
|
|
self.exit()
|
|
|
|
def get_info(self):
|
|
self.facts = self.user_whoami()
|
|
self.id = self.facts['name']
|
|
|
|
user_get = self.user_get(id=self.id)
|
|
for key in ['emailaddresses', 'data']:
|
|
self.facts[key] = user_get[key]
|
|
|
|
if self.aparams['accounts']:
|
|
self.facts['accounts'] = self.user_accounts(
|
|
**self.mapped_accounts_args,
|
|
)
|
|
|
|
if self.aparams['resource_consumption']:
|
|
self.facts.update(self.user_resource_consumption())
|
|
|
|
if self.aparams['audits']:
|
|
self.facts['audits'] = self.user_audits(**self.mapped_audits_args)
|
|
|
|
if self.aparams['api_methods']:
|
|
self.facts['api_methods'] = self.user_api_methods(id=self.id)
|
|
|
|
|
|
search_string = self.aparams['objects_search']
|
|
if search_string:
|
|
self.facts['objects_search'] = self.user_objects_search(
|
|
search_string=search_string,
|
|
)
|
|
|
|
|
|
def main():
|
|
DecortUserInfo().run()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|