Skip to content

Commit

Permalink
refactor: changed some regular modules
Browse files Browse the repository at this point in the history
  • Loading branch information
rmocanu-ionos committed Jan 11, 2024
1 parent fcb4aac commit ceb54d8
Show file tree
Hide file tree
Showing 54 changed files with 2,257 additions and 7,751 deletions.
12 changes: 7 additions & 5 deletions plugins/module_utils/common_ionos_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,24 +122,26 @@ def get_sdk_config(module, sdk):
return sdk.Configuration(**conf)


def check_required_arguments(module, object_name, options):
def check_required_arguments(module, state, object_name, options):
# manually checking if token or username & password provided
if (
not module.params.get("token")
and not (module.params.get("username") and module.params.get("password"))
):
module.fail_json(
msg='Token or username & password are required for {object_name}'.format(
msg='Token or username & password are required for {object_name} state {state}'.format(
object_name=object_name,
state=state,
),
)

for option_name, option in options.items():
if 'info' in option.get('required', []) and not module.params.get(option_name):
if state in option.get('required', []) and not module.params.get(option_name):
module.fail_json(
msg='{option_name} parameter is required for retrieving {object_name}'.format(
msg='{option_name} parameter is required for {object_name} state {state}'.format(
option_name=option_name,
object_name=object_name,
state=state,
),
)

Expand Down Expand Up @@ -213,7 +215,7 @@ def default_main_info(ionos_module, ionos_module_name, user_agent, has_sdk, opti
state = module.params.get('state')
with ionos_module.ApiClient(get_sdk_config(module, ionos_module)) as api_client:
api_client.user_agent = user_agent
check_required_arguments(module, object_name, options)
check_required_arguments(module, 'info', object_name, options)

try:
try:
Expand Down
26 changes: 20 additions & 6 deletions plugins/module_utils/common_ionos_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class CommonIonosModule():
def __init__(self):
pass
self.object_identity_paths = None

def _should_replace_object(self, existing_object):
pass
Expand Down Expand Up @@ -62,7 +62,10 @@ def update_replace_object(self, existing_object, clients):


def present_object(self, clients):
existing_object = get_resource(self.module, self._get_object_list(clients), self._get_object_name())
existing_object = get_resource(
self.module, self._get_object_list(clients),
self._get_object_name(), self.object_identity_paths,
)

if existing_object:
return self.update_replace_object(existing_object, clients)
Expand All @@ -79,13 +82,20 @@ def update_object(self, clients):
object_name = self._get_object_name()
object_list = self._get_object_list(clients)

existing_object = get_resource(self.module, object_list, self._get_object_identifier())
existing_object = get_resource(
self.module, object_list,
self._get_object_identifier(),
self.object_identity_paths,
)

if existing_object is None:
self.module.exit_json(changed=False)
return

existing_object_id_by_new_name = get_resource_id(self.module, object_list, object_name)
existing_object_id_by_new_name = get_resource_id(
self.module, object_list,
object_name, self.object_identity_paths,
)

if (
existing_object.id is not None
Expand All @@ -102,7 +112,11 @@ def update_object(self, clients):


def absent_object(self, clients):
existing_object = get_resource(self.module, self._get_object_list(clients), self._get_object_identifier())
existing_object = get_resource(
self.module, self._get_object_list(clients),
self._get_object_identifier(),
self.object_identity_paths,
)

if existing_object is None:
self.module.exit_json(changed=False)
Expand All @@ -122,7 +136,7 @@ def main(self):
clients = [sdk.ApiClient(get_sdk_config(self.module, sdk)) for sdk in self.sdks]
for client in clients:
client.user_agent = self.user_agent
check_required_arguments(self.module, state, self.options)
check_required_arguments(self.module, state, self.object_name, self.options)

try:
self.module.exit_json(**getattr(self, state + '_object')(clients))
Expand Down
12 changes: 12 additions & 0 deletions plugins/module_utils/common_ionos_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@ def get_default_options(states):
}


def get_default_options_with_replace(states):
return {
'allow_replace': {
'description': ['Boolean indicating if the resource should be recreated when the state cannot be reached in another way. This may be used to prevent resources from being deleted from specifying a different value to an immutable property. An error will be thrown instead'],
'available': ['present', 'update'],
'default': False,
'type': 'bool',
},
**get_default_options(states),
}


def get_info_default_options_with_depth(states):
return {
'depth': {
Expand Down
5 changes: 3 additions & 2 deletions plugins/modules/application_load_balancer.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
OBJECT_NAME = 'Application Load Balancer'
RETURNED_KEY = 'application_load_balancer'

OPTIONS = { **{
OPTIONS = {
'name': {
'description': ['The Application Load Balancer name.'],
'available': STATES,
Expand Down Expand Up @@ -85,7 +85,8 @@
'required': ['update', 'absent'],
'type': 'str',
},
}, **get_default_options(STATES) }
**get_default_options(STATES),
}


DOCUMENTATION = '''
Expand Down
5 changes: 3 additions & 2 deletions plugins/modules/application_load_balancer_flowlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
OBJECT_NAME = 'Flowlog'
RETURNED_KEY = 'flowlog'

OPTIONS = { **{
OPTIONS = {
'name': {
'description': ['The resource name.'],
'available': STATES,
Expand Down Expand Up @@ -82,7 +82,8 @@
'required': ['update', 'absent'],
'type': 'str',
},
}, **get_default_options(STATES) }
**get_default_options(STATES),
}


DOCUMENTATION = '''
Expand Down
5 changes: 3 additions & 2 deletions plugins/modules/application_load_balancer_forwardingrule.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
RETURNED_KEY = 'forwarding_rule'


OPTIONS = { **{
OPTIONS = {
'name': {
'description': ['The name of the Application Load Balancer forwarding rule.'],
'available': STATES,
Expand Down Expand Up @@ -119,7 +119,8 @@
'required': ['update', 'absent'],
'type': 'str',
},
}, **get_default_options(STATES) }
**get_default_options(STATES),
}


DOCUMENTATION = '''
Expand Down
Loading

0 comments on commit ceb54d8

Please sign in to comment.