Skip to content

Commit dd53481

Browse files
committed
remove attributes default values
1 parent 5b5c0b1 commit dd53481

116 files changed

Lines changed: 856 additions & 642 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
44

55
[project]
66
name = "mistapi"
7-
version = "0.59.3"
7+
version = "0.55.15"
88
authors = [{ name = "Thomas Munzer", email = "tmunzer@juniper.net" }]
99
description = "Python package to simplify the Mist System APIs usage"
1010
keywords = ["Mist", "Juniper", "API"]

scripts/generate_from_openapi.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -346,20 +346,21 @@ def _gen_code_params_default_value(param: dict) -> str:
346346
"""
347347
code_default = ""
348348
if not param["required"]:
349-
if param.get("default"):
350-
if param["type"] == "string":
351-
code_default = f'="{param["default"]}"'
352-
elif param["type"] == "boolean":
353-
if param["default"] == "true":
354-
code_default = "=True"
355-
else:
356-
code_default = "=False"
357-
else:
358-
code_default = f"={param['default']}"
359-
else:
360-
# TODO: Fix union type syntax for Python 3.9 compatibility
361-
code_default = "|None=None"
362-
# code_default = "=None"
349+
code_default = "|None=None"
350+
# if param.get("default"):
351+
# if param["type"] == "string":
352+
# code_default = f'="{param["default"]}"'
353+
# elif param["type"] == "boolean":
354+
# if param["default"] == "true":
355+
# code_default = "=True"
356+
# else:
357+
# code_default = "=False"
358+
# else:
359+
# code_default = f"={param['default']}"
360+
# else:
361+
# # TODO: Fix union type syntax for Python 3.9 compatibility
362+
# code_default = "|None=None"
363+
# # code_default = "=None"
363364
return code_default
364365

365366

src/mistapi/__api_session.py

Lines changed: 76 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ def __init__(
9292
Path to the secret in Vault
9393
vault_token : str
9494
Token for authenticating with Vault
95+
keyring_service : str
96+
keyring service name to load Mist API settings from system keyring
9597
console_log_level : int, default: 20
9698
Log level for the console output. Values are:
9799
50 -> CRITICAL
@@ -332,6 +334,9 @@ def _load_env(self, env_file=None) -> None:
332334
if os.getenv("MIST_VAULT_TOKEN") and not self.vault_token:
333335
self.vault_token = os.getenv("MIST_VAULT_TOKEN")
334336

337+
if os.getenv("MIST_KEYRING_SERVICE"):
338+
self.keyring_service = os.getenv("MIST_KEYRING_SERVICE")
339+
335340
if os.getenv("HTTPS_PROXY"):
336341
self._proxies["https"] = os.getenv("HTTPS_PROXY")
337342

@@ -477,14 +482,19 @@ def set_api_token(self, apitoken: str) -> None:
477482
if token and token not in apitokens_out:
478483
apitokens_out.append(token)
479484
LOGGER.info("apisession:set_api_token:found %s API Tokens", len(apitokens_out))
480-
if self._check_api_tokens(apitokens_out):
481-
self._apitoken = apitokens_out
485+
486+
valid_api_tokens = self._check_api_tokens(apitokens_out)
487+
if valid_api_tokens:
488+
self._apitoken = valid_api_tokens
482489
self._apitoken_index = 0
483490
self._session.headers.update(
484491
{"Authorization": "Token " + self._apitoken[self._apitoken_index]}
485492
)
486493
LOGGER.info("apisession:set_api_token:API Token configured")
487494
CONSOLE.debug("API Token configured")
495+
else:
496+
LOGGER.error("apisession:set_api_token:No valid API Token provided")
497+
CONSOLE.error("No valid API Token provided")
488498

489499
def _get_api_token_data(self, apitoken) -> tuple[str | None, list | None]:
490500
token_privileges = []
@@ -568,30 +578,48 @@ def _get_api_token_data(self, apitoken) -> tuple[str | None, list | None]:
568578
)
569579
return (token_type, token_privileges)
570580

571-
def _check_api_tokens(self, apitokens) -> bool:
581+
def _check_api_tokens(self, apitokens) -> list[str]:
572582
"""
573583
Function used when multiple API tokens are provided, to validate they
574584
have same privileges
575585
"""
576586
LOGGER.debug("apisession:_check_api_tokens")
587+
valid_api_tokens: list[str] = []
577588
if len(apitokens) == 0:
578589
LOGGER.error("apisession:_check_api_tokens:there is not API token to check")
579-
elif (len(apitokens)) == 1:
580-
LOGGER.info(
581-
"apisession:_check_api_tokens:there is only 1 API token. No check required"
582-
)
583590
else:
584591
primary_token_privileges: list[str] = []
585592
primary_token_type: str | None = ""
586593
primary_token_value: str = ""
587594
for token in apitokens:
588595
token_value = f"{token[:4]}...{token[-4:]}"
596+
if token in valid_api_tokens:
597+
LOGGER.info(
598+
"apisession:_check_api_tokens:API Token %s is already valid",
599+
token_value,
600+
)
601+
continue
589602
(token_type, token_privileges) = self._get_api_token_data(token)
590-
if len(primary_token_privileges) == 0 and token_privileges:
603+
if token_type is None or token_privileges is None:
604+
LOGGER.error(
605+
"apisession:_check_api_tokens:API Token %s is not valid",
606+
token_value,
607+
)
608+
LOGGER.error(
609+
"API Token %s is not valid and will not be used", token_value
610+
)
611+
elif len(primary_token_privileges) == 0 and token_privileges:
591612
primary_token_privileges = token_privileges
592613
primary_token_type = token_type
593614
primary_token_value = token_value
615+
valid_api_tokens.append(token)
616+
LOGGER.info(
617+
"apisession:_check_api_tokens:"
618+
"API Token %s set as primary for comparison",
619+
token_value,
620+
)
594621
elif primary_token_privileges == token_privileges:
622+
valid_api_tokens.append(token)
595623
LOGGER.info(
596624
"apisession:_check_api_tokens:"
597625
"%s API Token %s has same privileges as "
@@ -602,7 +630,7 @@ def _check_api_tokens(self, apitokens) -> bool:
602630
primary_token_value,
603631
)
604632
else:
605-
LOGGER.critical(
633+
LOGGER.error(
606634
"apisession:_check_api_tokens:"
607635
"%s API Token %s has different privileges "
608636
"than the %s API Token %s",
@@ -611,14 +639,11 @@ def _check_api_tokens(self, apitokens) -> bool:
611639
primary_token_type,
612640
primary_token_value,
613641
)
614-
LOGGER.critical(" /!\\ API TOKEN CRITICAL ERROR /!\\")
615-
LOGGER.critical(
616-
" When using multiple API Tokens, be sure they are all linked"
617-
" to the same Org/User, and all have the same privileges"
642+
LOGGER.error(
643+
"API Token %s has different privileges and will not be used",
644+
token_value,
618645
)
619-
LOGGER.critical(" Exiting...")
620-
sys.exit(255)
621-
return True
646+
return valid_api_tokens
622647

623648
def _process_login(self, retry: bool = True) -> str | None:
624649
"""
@@ -643,26 +668,42 @@ def _process_login(self, retry: bool = True) -> str | None:
643668
if not self._password:
644669
self.set_password()
645670

646-
LOGGER.debug("apisession:_process_login:email/password configured")
647-
uri = "/api/v1/login"
648-
body = {"email": self.email, "password": self._password}
649-
resp = self._session.post(self._url(uri), json=body)
650-
if resp.status_code == 200:
651-
LOGGER.info("apisession:_process_login:authentication successful!")
652-
CONSOLE.info("Authentication successful!")
653-
self._set_authenticated(True)
654-
else:
655-
error = resp.json().get("detail")
656-
LOGGER.error("apisession:_process_login:authentication failed:%s", error)
657-
CONSOLE.error(f"Authentication failed: {error}\r\n")
658-
self.email = None
659-
self._password = None
660-
LOGGER.info(
661-
"apisession:_process_login:"
662-
"email/password cleaned up. Restarting authentication function"
671+
try:
672+
LOGGER.debug("apisession:_process_login:email/password configured")
673+
uri = "/api/v1/login"
674+
body = {"email": self.email, "password": self._password}
675+
resp = self._session.post(self._url(uri), json=body)
676+
if resp.status_code == 200:
677+
LOGGER.info("apisession:_process_login:authentication successful!")
678+
CONSOLE.info("Authentication successful!")
679+
self._set_authenticated(True)
680+
else:
681+
error = resp.json().get("detail")
682+
LOGGER.error(
683+
"apisession:_process_login:authentication failed:%s", error
684+
)
685+
CONSOLE.error(f"Authentication failed: {error}\r\n")
686+
self.email = None
687+
self._password = None
688+
LOGGER.info(
689+
"apisession:_process_login:"
690+
"email/password cleaned up. Restarting authentication function"
691+
)
692+
if retry:
693+
return self._process_login(retry)
694+
except requests.exceptions.ProxyError:
695+
LOGGER.critical("apisession:_process_login:proxy not valid...")
696+
CONSOLE.critical("Proxy not valid...\r\n")
697+
sys.exit(0)
698+
except requests.exceptions.ConnectionError as connexion_error:
699+
LOGGER.critical(
700+
"apirequest:mist_post:Connection Error: %s", connexion_error
663701
)
664-
if retry:
665-
return self._process_login(retry)
702+
CONSOLE.critical("Connexion error...\r\n")
703+
sys.exit(0)
704+
except Exception:
705+
LOGGER.error("apisession:_process_login:Exception occurred", exc_info=True)
706+
error = "Exception occurred during authentication"
666707

667708
return error
668709

src/mistapi/__version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
__version__ = "0.59.3"
1+
__version__ = "0.55.15"
22
__author__ = "Thomas Munzer <tmunzer@juniper.net>"

src/mistapi/api/v1/installer/orgs/deviceprofiles.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616

1717
def listInstallerDeviceProfiles(
18-
mist_session: _APISession, org_id: str, type: str = "ap"
18+
mist_session: _APISession, org_id: str, type: str | None = None
1919
) -> _APIResponse:
2020
"""
2121
API doc: https://www.juniper.net/documentation/us/en/software/mist/api/http/api/installer/list-installer-device-profiles

src/mistapi/api/v1/installer/orgs/devices.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ def listInstallerListOfRecentlyClaimedDevices(
2020
model: str | None = None,
2121
site_name: str | None = None,
2222
site_id: str | None = None,
23-
limit: int = 100,
24-
page: int = 1,
23+
limit: int | None = None,
24+
page: int | None = None,
2525
) -> _APIResponse:
2626
"""
2727
API doc: https://www.juniper.net/documentation/us/en/software/mist/api/http/api/installer/list-installer-list-of-recently-claimed-devices

src/mistapi/api/v1/msps/insights.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def getMspSle(
1919
msp_id: str,
2020
metric: str,
2121
sle: str | None = None,
22-
duration: str = "1d",
22+
duration: str | None = None,
2323
interval: str | None = None,
2424
start: str | None = None,
2525
end: str | None = None,

src/mistapi/api/v1/msps/logs.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ def listMspAuditLogs(
2323
sort: str | None = None,
2424
start: str | None = None,
2525
end: str | None = None,
26-
duration: str = "1d",
27-
limit: int = 100,
28-
page: int = 1,
26+
duration: str | None = None,
27+
limit: int | None = None,
28+
page: int | None = None,
2929
) -> _APIResponse:
3030
"""
3131
API doc: https://www.juniper.net/documentation/us/en/software/mist/api/http/api/msps/logs/list-msp-audit-logs
@@ -85,8 +85,8 @@ def listMspAuditLogs(
8585
def countMspAuditLogs(
8686
mist_session: _APISession,
8787
msp_id: str,
88-
distinct: str = "admin_name",
89-
limit: int = 100,
88+
distinct: str | None = None,
89+
limit: int | None = None,
9090
) -> _APIResponse:
9191
"""
9292
API doc: https://www.juniper.net/documentation/us/en/software/mist/api/http/api/msps/logs/count-msp-audit-logs

src/mistapi/api/v1/msps/orgs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ def searchMspOrgs(
107107
sub_insufficient: bool | None = None,
108108
trial_enabled: bool | None = None,
109109
usage_types: list | None = None,
110-
limit: int = 100,
111-
sort: str = "timestamp",
110+
limit: int | None = None,
111+
sort: str | None = None,
112112
start: str | None = None,
113113
end: str | None = None,
114114
search_after: str | None = None,

0 commit comments

Comments
 (0)