mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-03-26 21:33:12 +00:00
Reformat everything.
This commit is contained in:
@@ -13,13 +13,13 @@ try:
|
||||
from keystoneauth1.adapter import Adapter
|
||||
from keystoneauth1.identity import v3
|
||||
from keystoneauth1 import session
|
||||
|
||||
HAS_THIRD_LIBRARIES = True
|
||||
except ImportError:
|
||||
THIRD_LIBRARIES_IMP_ERR = traceback.format_exc()
|
||||
HAS_THIRD_LIBRARIES = False
|
||||
|
||||
from ansible.module_utils.basic import (AnsibleModule, env_fallback,
|
||||
missing_required_lib)
|
||||
from ansible.module_utils.basic import AnsibleModule, env_fallback, missing_required_lib
|
||||
from ansible.module_utils.common.text.converters import to_text
|
||||
|
||||
|
||||
@@ -59,21 +59,19 @@ def session_method_wrapper(f):
|
||||
url = self.endpoint + url
|
||||
r = f(self, url, *args, **kwargs)
|
||||
except Exception as ex:
|
||||
raise HwcClientException(
|
||||
0, f"Sending request failed, error={ex}")
|
||||
raise HwcClientException(0, f"Sending request failed, error={ex}")
|
||||
|
||||
result = None
|
||||
if r.content:
|
||||
try:
|
||||
result = r.json()
|
||||
except Exception as ex:
|
||||
raise HwcClientException(
|
||||
0, f"Parsing response to json failed, error: {ex}")
|
||||
raise HwcClientException(0, f"Parsing response to json failed, error: {ex}")
|
||||
|
||||
code = r.status_code
|
||||
if code not in [200, 201, 202, 203, 204, 205, 206, 207, 208, 226]:
|
||||
msg = ""
|
||||
for i in ['message', 'error.message']:
|
||||
for i in ["message", "error.message"]:
|
||||
try:
|
||||
msg = navigate_value(result, i)
|
||||
break
|
||||
@@ -97,8 +95,8 @@ class _ServiceClient:
|
||||
self._client = client
|
||||
self._endpoint = endpoint
|
||||
self._default_header = {
|
||||
'User-Agent': f"Huawei-Ansible-MM-{product}",
|
||||
'Accept': 'application/json',
|
||||
"User-Agent": f"Huawei-Ansible-MM-{product}",
|
||||
"Accept": "application/json",
|
||||
}
|
||||
|
||||
@property
|
||||
@@ -111,23 +109,19 @@ class _ServiceClient:
|
||||
|
||||
@session_method_wrapper
|
||||
def get(self, url, body=None, header=None, timeout=None):
|
||||
return self._client.get(url, json=body, timeout=timeout,
|
||||
headers=self._header(header))
|
||||
return self._client.get(url, json=body, timeout=timeout, headers=self._header(header))
|
||||
|
||||
@session_method_wrapper
|
||||
def post(self, url, body=None, header=None, timeout=None):
|
||||
return self._client.post(url, json=body, timeout=timeout,
|
||||
headers=self._header(header))
|
||||
return self._client.post(url, json=body, timeout=timeout, headers=self._header(header))
|
||||
|
||||
@session_method_wrapper
|
||||
def delete(self, url, body=None, header=None, timeout=None):
|
||||
return self._client.delete(url, json=body, timeout=timeout,
|
||||
headers=self._header(header))
|
||||
return self._client.delete(url, json=body, timeout=timeout, headers=self._header(header))
|
||||
|
||||
@session_method_wrapper
|
||||
def put(self, url, body=None, header=None, timeout=None):
|
||||
return self._client.put(url, json=body, timeout=timeout,
|
||||
headers=self._header(header))
|
||||
return self._client.put(url, json=body, timeout=timeout, headers=self._header(header))
|
||||
|
||||
def _header(self, header):
|
||||
if header and isinstance(header, dict):
|
||||
@@ -167,22 +161,18 @@ class Config:
|
||||
def _gen_provider_client(self):
|
||||
m = self._module
|
||||
p = {
|
||||
"auth_url": m.params['identity_endpoint'],
|
||||
"password": m.params['password'],
|
||||
"username": m.params['user'],
|
||||
"project_name": m.params['project'],
|
||||
"user_domain_name": m.params['domain'],
|
||||
"reauthenticate": True
|
||||
"auth_url": m.params["identity_endpoint"],
|
||||
"password": m.params["password"],
|
||||
"username": m.params["user"],
|
||||
"project_name": m.params["project"],
|
||||
"user_domain_name": m.params["domain"],
|
||||
"reauthenticate": True,
|
||||
}
|
||||
|
||||
self._project_client = Adapter(
|
||||
session.Session(auth=v3.Password(**p)),
|
||||
raise_exc=False)
|
||||
self._project_client = Adapter(session.Session(auth=v3.Password(**p)), raise_exc=False)
|
||||
|
||||
p.pop("project_name")
|
||||
self._domain_client = Adapter(
|
||||
session.Session(auth=v3.Password(**p)),
|
||||
raise_exc=False)
|
||||
self._domain_client = Adapter(session.Session(auth=v3.Password(**p)), raise_exc=False)
|
||||
|
||||
def _get_service_endpoint(self, client, service_type, region):
|
||||
k = f"{service_type}.{region if region else ''}"
|
||||
@@ -192,15 +182,12 @@ class Config:
|
||||
|
||||
url = None
|
||||
try:
|
||||
url = client.get_endpoint(service_type=service_type,
|
||||
region_name=region, interface="public")
|
||||
url = client.get_endpoint(service_type=service_type, region_name=region, interface="public")
|
||||
except Exception as ex:
|
||||
raise HwcClientException(
|
||||
0, f"Getting endpoint failed, error={ex}")
|
||||
raise HwcClientException(0, f"Getting endpoint failed, error={ex}")
|
||||
|
||||
if url == "":
|
||||
raise HwcClientException(
|
||||
0, f"Cannot find the endpoint for {service_type}")
|
||||
raise HwcClientException(0, f"Cannot find the endpoint for {service_type}")
|
||||
|
||||
if url[-1] != "/":
|
||||
url += "/"
|
||||
@@ -210,42 +197,46 @@ class Config:
|
||||
|
||||
def _validate(self):
|
||||
if not HAS_THIRD_LIBRARIES:
|
||||
self.module.fail_json(
|
||||
msg=missing_required_lib('keystoneauth1'),
|
||||
exception=THIRD_LIBRARIES_IMP_ERR)
|
||||
self.module.fail_json(msg=missing_required_lib("keystoneauth1"), exception=THIRD_LIBRARIES_IMP_ERR)
|
||||
|
||||
|
||||
class HwcModule(AnsibleModule):
|
||||
def __init__(self, *args, **kwargs):
|
||||
arg_spec = kwargs.setdefault('argument_spec', {})
|
||||
arg_spec = kwargs.setdefault("argument_spec", {})
|
||||
|
||||
arg_spec.update(
|
||||
dict(
|
||||
identity_endpoint=dict(
|
||||
required=True, type='str',
|
||||
fallback=(env_fallback, ['ANSIBLE_HWC_IDENTITY_ENDPOINT']),
|
||||
required=True,
|
||||
type="str",
|
||||
fallback=(env_fallback, ["ANSIBLE_HWC_IDENTITY_ENDPOINT"]),
|
||||
),
|
||||
user=dict(
|
||||
required=True, type='str',
|
||||
fallback=(env_fallback, ['ANSIBLE_HWC_USER']),
|
||||
required=True,
|
||||
type="str",
|
||||
fallback=(env_fallback, ["ANSIBLE_HWC_USER"]),
|
||||
),
|
||||
password=dict(
|
||||
required=True, type='str', no_log=True,
|
||||
fallback=(env_fallback, ['ANSIBLE_HWC_PASSWORD']),
|
||||
required=True,
|
||||
type="str",
|
||||
no_log=True,
|
||||
fallback=(env_fallback, ["ANSIBLE_HWC_PASSWORD"]),
|
||||
),
|
||||
domain=dict(
|
||||
required=True, type='str',
|
||||
fallback=(env_fallback, ['ANSIBLE_HWC_DOMAIN']),
|
||||
required=True,
|
||||
type="str",
|
||||
fallback=(env_fallback, ["ANSIBLE_HWC_DOMAIN"]),
|
||||
),
|
||||
project=dict(
|
||||
required=True, type='str',
|
||||
fallback=(env_fallback, ['ANSIBLE_HWC_PROJECT']),
|
||||
required=True,
|
||||
type="str",
|
||||
fallback=(env_fallback, ["ANSIBLE_HWC_PROJECT"]),
|
||||
),
|
||||
region=dict(
|
||||
type='str',
|
||||
fallback=(env_fallback, ['ANSIBLE_HWC_REGION']),
|
||||
type="str",
|
||||
fallback=(env_fallback, ["ANSIBLE_HWC_REGION"]),
|
||||
),
|
||||
id=dict(type='str')
|
||||
id=dict(type="str"),
|
||||
)
|
||||
)
|
||||
|
||||
@@ -253,14 +244,14 @@ class HwcModule(AnsibleModule):
|
||||
|
||||
|
||||
class _DictComparison:
|
||||
''' This class takes in two dictionaries `a` and `b`.
|
||||
These are dictionaries of arbitrary depth, but made up of standard
|
||||
Python types only.
|
||||
This differ will compare all values in `a` to those in `b`.
|
||||
If value in `a` is None, always returns True, indicating
|
||||
this value is no need to compare.
|
||||
Note: On all lists, order does matter.
|
||||
'''
|
||||
"""This class takes in two dictionaries `a` and `b`.
|
||||
These are dictionaries of arbitrary depth, but made up of standard
|
||||
Python types only.
|
||||
This differ will compare all values in `a` to those in `b`.
|
||||
If value in `a` is None, always returns True, indicating
|
||||
this value is no need to compare.
|
||||
Note: On all lists, order does matter.
|
||||
"""
|
||||
|
||||
def __init__(self, request):
|
||||
self.request = request
|
||||
@@ -316,8 +307,7 @@ class _DictComparison:
|
||||
return self._compare_dicts(value1, value2)
|
||||
|
||||
# Always use to_text values to avoid unicode issues.
|
||||
return (to_text(value1, errors='surrogate_or_strict') == to_text(
|
||||
value2, errors='surrogate_or_strict'))
|
||||
return to_text(value1, errors="surrogate_or_strict") == to_text(value2, errors="surrogate_or_strict")
|
||||
|
||||
|
||||
def wait_to_finish(target, pending, refresh, timeout, min_interval=1, delay=3):
|
||||
@@ -338,8 +328,7 @@ def wait_to_finish(target, pending, refresh, timeout, min_interval=1, delay=3):
|
||||
not_found_times += 1
|
||||
|
||||
if not_found_times > 10:
|
||||
raise HwcModuleException(
|
||||
f"not found the object for {not_found_times} times")
|
||||
raise HwcModuleException(f"not found the object for {not_found_times} times")
|
||||
else:
|
||||
not_found_times = 0
|
||||
|
||||
@@ -347,8 +336,7 @@ def wait_to_finish(target, pending, refresh, timeout, min_interval=1, delay=3):
|
||||
return obj
|
||||
|
||||
if pending and status not in pending:
|
||||
raise HwcModuleException(
|
||||
f"unexpected status({status}) occurred")
|
||||
raise HwcModuleException(f"unexpected status({status}) occurred")
|
||||
|
||||
if not is_last_time:
|
||||
wait *= 2
|
||||
@@ -372,13 +360,11 @@ def navigate_value(data, index, array_index=None):
|
||||
return None
|
||||
|
||||
if not isinstance(d, dict):
|
||||
raise HwcModuleException(
|
||||
"can't navigate value from a non-dict object")
|
||||
raise HwcModuleException("can't navigate value from a non-dict object")
|
||||
|
||||
i = index[n]
|
||||
if i not in d:
|
||||
raise HwcModuleException(
|
||||
f"navigate value failed: key({i}) is not exist in dict")
|
||||
raise HwcModuleException(f"navigate value failed: key({i}) is not exist in dict")
|
||||
d = d[i]
|
||||
|
||||
if not array_index:
|
||||
@@ -392,13 +378,11 @@ def navigate_value(data, index, array_index=None):
|
||||
return None
|
||||
|
||||
if not isinstance(d, list):
|
||||
raise HwcModuleException(
|
||||
"can't navigate value from a non-list object")
|
||||
raise HwcModuleException("can't navigate value from a non-list object")
|
||||
|
||||
j = array_index.get(k)
|
||||
if j >= len(d):
|
||||
raise HwcModuleException(
|
||||
"navigate value failed: the index is out of list")
|
||||
raise HwcModuleException("navigate value failed: the index is out of list")
|
||||
d = d[j]
|
||||
|
||||
return d
|
||||
@@ -425,14 +409,14 @@ def build_path(module, path, kv=None):
|
||||
|
||||
|
||||
def get_region(module):
|
||||
if module.params['region']:
|
||||
return module.params['region']
|
||||
if module.params["region"]:
|
||||
return module.params["region"]
|
||||
|
||||
return module.params['project'].split("_")[0]
|
||||
return module.params["project"].split("_")[0]
|
||||
|
||||
|
||||
def is_empty_value(v):
|
||||
return (not v)
|
||||
return not v
|
||||
|
||||
|
||||
def are_different_dicts(dict1, dict2):
|
||||
|
||||
Reference in New Issue
Block a user