Skip to content

Commit c33ea0f

Browse files
Merge pull request #552 from netscaler/cleanup
Code cleanup
2 parents 3aa26a5 + af7df88 commit c33ea0f

6 files changed

Lines changed: 123 additions & 97 deletions

File tree

Makefile

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
fmt:
2-
autoflake plugins/modules/*.py
3-
autoflake plugins/module_utils/*.py
4-
autoflake --recursive tests/
5-
autoflake migrationtool/*py
2+
autoflake plugins/modules/*.py
3+
autoflake plugins/module_utils/*.py
4+
autoflake --recursive tests/
5+
autoflake tools/migrationtool/*py
66

7-
black plugins/modules/*.py
8-
black plugins/module_utils/*.py
9-
black tests/
10-
black migrationtool/*.py
7+
black plugins/modules/*.py
8+
black plugins/module_utils/*.py
9+
black tests/
10+
black tools/migrationtool/*.py
1111

12-
isort plugins/modules/*.py
13-
isort plugins/module_utils/*.py
14-
isort tests/
15-
isort migrationtool/*.py
12+
isort plugins/modules/*.py
13+
isort plugins/module_utils/*.py
14+
isort tests/
15+
isort tools/migrationtool/*.py
1616

17-
yamlfmt .
17+
yamlfmt $(shell find . -name '*.yml' -o -name '*.yaml')
1818

1919
install:
2020
ansible-galaxy collection install . --force

plugins/module_utils/client.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -62,25 +62,25 @@ def __init__(self, module, resource_name):
6262
)
6363
if netscaler_console_as_proxy:
6464
if self._module.params.get("managed_netscaler_instance_name"):
65-
self._headers[
66-
"_MPS_API_PROXY_MANAGED_INSTANCE_NAME"
67-
] = self._module.params.get("managed_netscaler_instance_name")
65+
self._headers["_MPS_API_PROXY_MANAGED_INSTANCE_NAME"] = (
66+
self._module.params.get("managed_netscaler_instance_name")
67+
)
6868
if self._module.params.get("managed_netscaler_instance_ip"):
69-
self._headers[
70-
"_MPS_API_PROXY_MANAGED_INSTANCE_IP"
71-
] = self._module.params.get("managed_netscaler_instance_ip")
69+
self._headers["_MPS_API_PROXY_MANAGED_INSTANCE_IP"] = (
70+
self._module.params.get("managed_netscaler_instance_ip")
71+
)
7272
if self._module.params.get("managed_netscaler_instance_id"):
73-
self._headers[
74-
"_MPS_API_PROXY_MANAGED_INSTANCE_ID"
75-
] = self._module.params.get("managed_netscaler_instance_id")
73+
self._headers["_MPS_API_PROXY_MANAGED_INSTANCE_ID"] = (
74+
self._module.params.get("managed_netscaler_instance_id")
75+
)
7676
if self._module.params.get("managed_netscaler_instance_username"):
77-
self._headers[
78-
"_MPS_API_PROXY_MANAGED_INSTANCE_USERNAME"
79-
] = self._module.params.get("managed_netscaler_instance_username")
77+
self._headers["_MPS_API_PROXY_MANAGED_INSTANCE_USERNAME"] = (
78+
self._module.params.get("managed_netscaler_instance_username")
79+
)
8080
if self._module.params.get("managed_netscaler_instance_password"):
81-
self._headers[
82-
"_MPS_API_PROXY_MANAGED_INSTANCE_PASSWORD"
83-
] = self._module.params.get("managed_netscaler_instance_password")
81+
self._headers["_MPS_API_PROXY_MANAGED_INSTANCE_PASSWORD"] = (
82+
self._module.params.get("managed_netscaler_instance_password")
83+
)
8484

8585
@trace
8686
def url_builder(

plugins/module_utils/common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
import re
1111

1212
from .constants import (
13+
GLOBAL_BINDING_ARG_LIST,
1314
HTTP_RESOURCE_ALREADY_EXISTS,
1415
HTTP_RESOURCE_NOT_FOUND,
1516
HTTP_SUCCESS_CODES,
16-
GLOBAL_BINDING_ARG_LIST
1717
)
1818
from .decorators import trace
1919
from .logger import log

plugins/module_utils/constants.py

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -117,37 +117,37 @@
117117

118118
# this list contains globalbindings whose GET call must have query params args=type and filter=get_arg_keys (check in nitro_resource_map)
119119
GLOBAL_BINDING_ARG_LIST = [
120-
'dnsglobal_dnspolicy_binding',
121-
'responderglobal_responderpolicy_binding',
122-
'contentinspectionglobal_contentinspectionpolicy_binding',
123-
'appflowglobal_appflowpolicy_binding',
124-
'appfwglobal_auditnslogpolicy_binding',
125-
'appfwglobal_auditsyslogpolicy_binding',
126-
'appfwglobal_appfwpolicy_binding',
127-
'rewriteglobal_rewritepolicy_binding',
128-
'transformglobal_transformpolicy_binding',
129-
'sslglobal_sslpolicy_binding',
130-
'tunnelglobal_tunneltrafficpolicy_binding',
131-
'cmpglobal_cmppolicy_binding',
132-
'feoglobal_feopolicy_binding',
133-
'icaglobal_icapolicy_binding',
134-
'lbglobal_lbpolicy_binding',
135-
'cacheglobal_cachepolicy_binding',
136-
'botglobal_botpolicy_binding',
120+
"dnsglobal_dnspolicy_binding",
121+
"responderglobal_responderpolicy_binding",
122+
"contentinspectionglobal_contentinspectionpolicy_binding",
123+
"appflowglobal_appflowpolicy_binding",
124+
"appfwglobal_auditnslogpolicy_binding",
125+
"appfwglobal_auditsyslogpolicy_binding",
126+
"appfwglobal_appfwpolicy_binding",
127+
"rewriteglobal_rewritepolicy_binding",
128+
"transformglobal_transformpolicy_binding",
129+
"sslglobal_sslpolicy_binding",
130+
"tunnelglobal_tunneltrafficpolicy_binding",
131+
"cmpglobal_cmppolicy_binding",
132+
"feoglobal_feopolicy_binding",
133+
"icaglobal_icapolicy_binding",
134+
"lbglobal_lbpolicy_binding",
135+
"cacheglobal_cachepolicy_binding",
136+
"botglobal_botpolicy_binding",
137137
]
138138

139139
GETALL_ONLY_RESOURCES = [
140-
'appfwlearningdata',
141-
'application',
142-
'bridgetable',
143-
'gslbldnsentry',
144-
'locationfile',
145-
'locationfile6',
146-
'routerdynamicrouting',
147-
'sslcertbundle',
148-
'sslcertfile',
149-
'sslcrlfile',
150-
'ssldhfile',
151-
'sslkeyfile',
152-
'systementitydata'
140+
"appfwlearningdata",
141+
"application",
142+
"bridgetable",
143+
"gslbldnsentry",
144+
"locationfile",
145+
"locationfile6",
146+
"routerdynamicrouting",
147+
"sslcertbundle",
148+
"sslcertfile",
149+
"sslcrlfile",
150+
"ssldhfile",
151+
"sslkeyfile",
152+
"systementitydata",
153153
]

plugins/module_utils/module_executor.py

Lines changed: 63 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@
3535
)
3636
from .constants import (
3737
ATTRIBUTES_NOT_PRESENT_IN_GET_RESPONSE,
38+
GETALL_ONLY_RESOURCES,
3839
HTTP_RESOURCE_ALREADY_EXISTS,
3940
NETSCALER_COMMON_ARGUMENTS,
4041
NITRO_ATTRIBUTES_ALIASES,
41-
GETALL_ONLY_RESOURCES,
4242
)
4343
from .decorators import trace
4444
from .logger import log, loglines
@@ -91,9 +91,7 @@ def __init__(self, resource_name, supports_check_mode=True):
9191
argument_spec=argument_spec,
9292
supports_check_mode=supports_check_mode,
9393
mutually_exclusive=[
94-
(
95-
"nitro_pass", "nitro_auth_token"
96-
),
94+
("nitro_pass", "nitro_auth_token"),
9795
(
9896
"managed_netscaler_instance_name",
9997
"managed_netscaler_instance_ip",
@@ -148,10 +146,9 @@ def __init__(self, resource_name, supports_check_mode=True):
148146
self.module.params["api_path"] = "nitro/v2/config"
149147
self.have_token = self.module.params.get("nitro_auth_token", None)
150148
self.client = NitroAPIClient(self.module, self.resource_name)
151-
have_userpass = all([
152-
self.module.params.get("nitro_user"),
153-
self.module.params.get("nitro_pass")
154-
])
149+
have_userpass = all(
150+
[self.module.params.get("nitro_user"), self.module.params.get("nitro_pass")]
151+
)
155152
if have_userpass and not self.module.check_mode:
156153
self.client._headers.pop("X-NITRO-USER", None)
157154
self.client._headers.pop("X-NITRO-PASS", None)
@@ -167,13 +164,22 @@ def __init__(self, resource_name, supports_check_mode=True):
167164

168165
else:
169166
if self.netscaler_console_as_proxy_server:
170-
self.module.params["nitro_auth_token"] = response["login"][0].get("sessionid", None)
167+
self.module.params["nitro_auth_token"] = response["login"][0].get(
168+
"sessionid", None
169+
)
171170
else:
172-
self.module.params["nitro_auth_token"] = response.get("sessionid", None)
171+
self.module.params["nitro_auth_token"] = response.get(
172+
"sessionid", None
173+
)
173174

174175
if not self.module.params["nitro_auth_token"]:
175-
self.return_failure("ERROR: Login failed. No sessionid returned from the NetScaler ADC")
176-
log("INFO: Login successful. Session ID: %s" % self.module.params["nitro_auth_token"])
176+
self.return_failure(
177+
"ERROR: Login failed. No sessionid returned from the NetScaler ADC"
178+
)
179+
log(
180+
"INFO: Login successful. Session ID: %s"
181+
% self.module.params["nitro_auth_token"]
182+
)
177183

178184
self.client._headers["Cookie"] = (
179185
"NITRO_AUTH_TOKEN=%s" % self.module.params["nitro_auth_token"]
@@ -222,7 +228,11 @@ def return_success(self):
222228
# }
223229
if self.resource_name == "login":
224230
self.module_result["sessionid"] = self.sessionid
225-
if self.client._headers.get("Cookie", None) not in (None, "") and not self.module.check_mode and not self.have_token:
231+
if (
232+
self.client._headers.get("Cookie", None) not in (None, "")
233+
and not self.module.check_mode
234+
and not self.have_token
235+
):
226236
ok, response = adc_logout(self.client)
227237
if not ok:
228238
log("ERROR: Logout failed: %s" % response)
@@ -252,7 +262,11 @@ def update_diff_list(self, existing=None, desired=None, delete=False, **kwargs):
252262

253263
@trace
254264
def return_failure(self, msg):
255-
if self.client._headers.get("Cookie", None) not in (None, "") and not self.module.check_mode and not self.have_token:
265+
if (
266+
self.client._headers.get("Cookie", None) not in (None, "")
267+
and not self.module.check_mode
268+
and not self.have_token
269+
):
256270
ok, response = adc_logout(self.client)
257271
if not ok:
258272
log("ERROR: Logout failed: %s" % response)
@@ -445,21 +459,23 @@ def install(self):
445459
@trace
446460
def create_or_update(self):
447461
desired_state = self.module.params["state"]
448-
remove_non_updatable_params = self.module.params.get("remove_non_updatable_params", "no")
462+
remove_non_updatable_params = self.module.params.get(
463+
"remove_non_updatable_params", "no"
464+
)
449465
if (
450-
desired_state == "present" and
451-
self.resource_name.endswith("_binding") and
452-
"state" in self.resource_module_params
466+
desired_state == "present"
467+
and self.resource_name.endswith("_binding")
468+
and "state" in self.resource_module_params
453469
):
454470
self.resource_module_params.pop("state")
455471
self.update_diff_list(
456472
existing=self.existing_resource, desired=self.resource_module_params
457473
)
458474
if not self.existing_resource and "add" in self.supported_operations:
459-
if (
460-
self.resource_name.endswith("_binding") and
461-
desired_state in {"enabled", "disabled"}
462-
):
475+
if self.resource_name.endswith("_binding") and desired_state in {
476+
"enabled",
477+
"disabled",
478+
}:
463479
self.resource_module_params["state"] = desired_state.upper()
464480

465481
self.module_result["changed"] = True
@@ -556,9 +572,8 @@ def create_or_update(self):
556572
% (self.resource_name, self.resource_id)
557573
)
558574
self.delete()
559-
if (
560-
self.module.params["state"] == "present" and
561-
any(x in self.supported_operations for x in ("enabled", "disabled"))
575+
if self.module.params["state"] == "present" and any(
576+
x in self.supported_operations for x in ("enabled", "disabled")
562577
):
563578
# We want to keep the previous state of the binding
564579
self.resource_module_params["state"] = (
@@ -569,7 +584,9 @@ def create_or_update(self):
569584
)
570585
# Here we are checking if resource has immutable keys
571586
# if yes, we will check if the user wants to keep the non-updatable params (which in turn will return error)
572-
elif immutable_keys_list is None or (immutable_keys_list and remove_non_updatable_params == "no"):
587+
elif immutable_keys_list is None or (
588+
immutable_keys_list and remove_non_updatable_params == "no"
589+
):
573590
self.module_result["changed"] = True
574591
log(
575592
"INFO: Resource %s:%s exists and is different. Will be UPDATED."
@@ -597,18 +614,21 @@ def create_or_update(self):
597614
self.module_result["changed"] = False
598615
self.module.exit_json(**self.module_result)
599616
else:
600-
if (
601-
self.resource_name.endswith("_binding") and
602-
self.module.params["state"] in {"enabled", "disabled"}
603-
):
604-
existing_state = self.existing_resource.get("state", "").upper()
617+
if self.resource_name.endswith(
618+
"_binding"
619+
) and self.module.params["state"] in {"enabled", "disabled"}:
620+
existing_state = self.existing_resource.get(
621+
"state", ""
622+
).upper()
605623
if existing_state != desired_state:
606624
# Create the resource in desired state
607625
self.module_result["changed"] = True
608626
self.delete()
609627
self.resource_module_params["state"] = desired_state
610628
ok, err = create_resource(
611-
self.client, self.resource_name, self.resource_module_params
629+
self.client,
630+
self.resource_name,
631+
self.resource_module_params,
612632
)
613633
if not ok:
614634
self.return_failure(err)
@@ -847,8 +867,10 @@ def sync_single_binding(self, binding_name):
847867
self.add_bindings(
848868
binding_name=binding_name,
849869
desired_bindings=[
850-
x for x in desired_binding_members
851-
if x[get_bindprimary_key(binding_name, x)] in to_be_added_bindprimary_keys
870+
x
871+
for x in desired_binding_members
872+
if x[get_bindprimary_key(binding_name, x)]
873+
in to_be_added_bindprimary_keys
852874
],
853875
)
854876

@@ -967,9 +989,9 @@ def change_password(self):
967989
)
968990
else:
969991
# set system user USERNAME -password NEW_PASSWORD
970-
self.resource_module_params[
971-
"password"
972-
] = self.resource_module_params["new_password"]
992+
self.resource_module_params["password"] = (
993+
self.resource_module_params["new_password"]
994+
)
973995
ok, err = update_resource(
974996
self.client, "systemuser", self.resource_module_params
975997
)
@@ -1096,7 +1118,10 @@ def main(self):
10961118
if "bindings" in NITRO_RESOURCE_MAP[self.resource_name].keys():
10971119
self.sync_all_bindings()
10981120

1099-
elif self.resource_name == "install" and self.module.params["state"] == "installed":
1121+
elif (
1122+
self.resource_name == "install"
1123+
and self.module.params["state"] == "installed"
1124+
):
11001125
self.install()
11011126

11021127
elif self.module.params["state"] in {

tools/migrationtool/convert_yaml.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# MIT License (see LICENSE or https://opensource.org/licenses/MIT)
55

66
import argparse
7+
78
import yaml
89
from utils import (
910
netscaler_login_specifics,

0 commit comments

Comments
 (0)