From 889f84ff786849d25d6cc2d28463beb9ab9ed811 Mon Sep 17 00:00:00 2001 From: Martin Kudlej Date: Fri, 29 Nov 2024 11:14:38 +0100 Subject: [PATCH] small pylint fixes --- tests/integration/conftest.py | 165 ++++++++++++++++++-------------- threescale_api_crd/client.py | 4 +- threescale_api_crd/resources.py | 69 +++++++------ 3 files changed, 126 insertions(+), 112 deletions(-) diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index 8d2c136..4071ded 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -121,7 +121,7 @@ def apicast_http_client(application, proxy, ssl_verify): @pytest.fixture(scope="module") def service_params(): suffix = get_suffix() - return dict(name=f"test-{suffix}") + return {"name": f"test-{suffix}"} @pytest.fixture(scope="module") @@ -135,14 +135,14 @@ def service(service_params, api) -> Service: def account_params(): suffix = get_suffix() name = f"testacc{suffix}" - return dict( - name=name, - username=name, - org_name=name, - monthly_billing_enabled=False, - monthly_charging_enabled=False, - email=f"{name}@name.none", - ) + return { + "name": name, + "username": name, + "org_name": name, + "monthly_billing_enabled": False, + "monthly_charging_enabled": False, + "email": f"{name}@name.none", + } @pytest.fixture(scope="module") @@ -161,12 +161,12 @@ def acc_user(account): @pytest.fixture(scope="module") def acc_user2_params(account, acc_user): name = acc_user["username"] + "2" - return dict( - username=name, - email=f"{name}@name.none", - role="member", - account_name=account["name"], - ) + return { + "username": name, + "email": f"{name}@name.none", + "role": "member", + "account_name": account["name"], + } @pytest.fixture(scope="module") @@ -177,12 +177,12 @@ def acc_user2(account, acc_user, acc_user2_params): @pytest.fixture(scope="module") def application_plan_params(service) -> dict: suffix = get_suffix() - return dict( - name=f"test-{suffix}", - setup_fee="1.00", - state_event="publish", - cost_per_month="3.00", - ) + return { + "name": f"test-{suffix}", + "setup_fee": "1.00", + "state_event": "publish", + "cost_per_month": "3.00", + } @pytest.fixture(scope="module") @@ -196,13 +196,13 @@ def application_plan(api, service, application_plan_params) -> ApplicationPlan: def application_params(application_plan, service, account): suffix = get_suffix() name = f"test-{suffix}" - return dict( - name=name, - description=name, - plan_id=application_plan["id"], - service_id=service["id"], - account_id=account["id"], - ) + return { + "name": name, + "description": name, + "plan_id": application_plan["id"], + "service_id": service["id"], + "account_id": account["id"], + } @pytest.fixture(scope="module") @@ -270,7 +270,7 @@ def metric_params(service): suffix = get_suffix() friendly_name = f"test-metric-{suffix}" name = f"{friendly_name}".replace("-", "_") - return dict(friendly_name=friendly_name, system_name=name, unit="count") + return {"friendly_name": friendly_name, "system_name": name, "unit": "count"} @pytest.fixture(scope="module") @@ -278,7 +278,7 @@ def backend_metric_params(): suffix = get_suffix() friendly_name = f"test-metric-{suffix}" name = f"{friendly_name}".replace("-", "") - return dict(friendly_name=friendly_name, system_name=name, unit="count") + return {"friendly_name": friendly_name, "system_name": name, "unit": "count"} @pytest.fixture(scope="module") @@ -310,7 +310,7 @@ def method_params(): suffix = get_suffix() friendly_name = f"test-method-{suffix}" system_name = f"{friendly_name}".replace("-", "_") - return dict(friendly_name=friendly_name, system_name=system_name) + return {"friendly_name": friendly_name, "system_name": system_name} # 'friendly_name' is id in CRD for methods @@ -347,7 +347,12 @@ def mapping_rule_params(service): Fixture for getting paramteres for mapping rule for product/service. """ hits_metric = service.metrics.read_by_name("hits") - return dict(http_method="GET", pattern="/get", metric_id=hits_metric["id"], delta=1) + return { + "http_method": "GET", + "pattern": "/get", + "metric_id": hits_metric["id"], + "delta": 1, + } @pytest.fixture(scope="module") @@ -356,7 +361,12 @@ def backend_mapping_rule_params(backend, backend_metric): Fixture for getting paramteres for mapping rule for backend. """ back = backend_metric["id"] - return dict(http_method="GET", pattern="/anything/get/ida", metric_id=back, delta=1) + return { + "http_method": "GET", + "pattern": "/anything/get/ida", + "metric_id": back, + "delta": 1, + } @pytest.fixture(scope="module") @@ -414,13 +424,13 @@ def create_mapping_rule(service): rules = [] def _create(metric, http_method, path): - params = dict( - service_id=service["id"], - http_method=http_method, - pattern=f"/anything{path}", - delta=1, - metric_id=metric["id"], - ) + params = { + "service_id": service["id"], + "http_method": http_method, + "pattern": f"/anything{path}", + "delta": 1, + "metric_id": metric["id"], + } rule = service.mapping_rules.create(params=params) rules.append(rule) return rule @@ -441,13 +451,13 @@ def create_backend_mapping_rule(backend): rules = [] def _create(backend_metric, http_method, path): - params = dict( - backend_id=backend["id"], - http_method=http_method, - pattern=f"/anything{path}", - delta=1, - metric_id=backend_metric["id"], - ) + params = { + "backend_id": backend["id"], + "http_method": http_method, + "pattern": f"/anything{path}", + "delta": 1, + "metric_id": backend_metric["id"], + } rule = backend.mapping_rules.create(params=params) rules.append(rule) return rule @@ -465,9 +475,11 @@ def backend_params(api_backend): Fixture for getting backend parameters. """ suffix = get_suffix() - return dict( - name=f"test-backend-{suffix}", private_endpoint=api_backend, description="111" - ) + return { + "name": f"test-backend-{suffix}", + "private_endpoint": api_backend, + "description": "111", + } @pytest.fixture(scope="module") @@ -505,12 +517,12 @@ def tenant_params(): """ Params for custom tenant """ - return dict( - username=f"tenant{get_suffix()}", - admin_password="123456", - email=f"e{get_suffix()}@invalid.invalid", - org_name="org", - ) + return { + "username": f"tenant{get_suffix()}", + "admin_password": "123456", + "email": f"e{get_suffix()}@invalid.invalid", + "org_name": "org", + } @pytest.fixture(scope="module") @@ -525,7 +537,7 @@ def active_docs_params(active_docs_body): suffix = get_suffix() name = f"test-{suffix}" des = f"description-{suffix}" - return dict(name=name, body=active_docs_body, description=des) + return {"name": name, "body": active_docs_body, "description": des} @pytest.fixture(scope="module") @@ -544,17 +556,17 @@ def active_doc(api, service, active_docs_params) -> ActiveDoc: def openapi_params(active_docs_body): suffix = get_suffix() name = f"test-{suffix}" - params = dict( - name=name, - productionPublicBaseURL="http://productionPublicBaseURL", - stagingPublicBaseURL="http://stagingPublicBaseURL", - productSystemName="PrOdUcTsYsTeMnAmE", - privateBaseURL="http://privateBaseURL", - prefixMatching=True, - privateAPIHostHeader="privateAPIHostHeader", - privateAPISecretToken="privateAPISecretToken", - body=active_docs_body, - ) + params = { + "name": name, + "productionPublicBaseURL": "http://productionPublicBaseURL", + "stagingPublicBaseURL": "http://stagingPublicBaseURL", + "productSystemName": "PrOdUcTsYsTeMnAmE", + "privateBaseURL": "http://privateBaseURL", + "prefixMatching": True, + "privateAPIHostHeader": "privateAPIHostHeader", + "privateAPISecretToken": "privateAPISecretToken", + "body": active_docs_body, + } return params @@ -610,7 +622,7 @@ def policy_registry_params(policy_registry_schema): """Params for policy registry.""" suffix = get_suffix() name = f"test-{suffix}" - return dict(name=name, version="0.1", schema=policy_registry_schema) + return {"name": name, "version": "0.1", "schema": policy_registry_schema} @pytest.fixture(scope="module") @@ -627,7 +639,7 @@ def policy_registry(api, policy_registry_params) -> PolicyRegistry: @pytest.fixture(scope="module") def limit_params(metric): """Params for limit.""" - return dict(metric_id=metric["id"], period="minute", value=10) + return {"metric_id": metric["id"], "period": "minute", "value": 10} @pytest.fixture(scope="module") @@ -643,7 +655,7 @@ def limit(service, application, application_plan, metric, limit_params) -> Limit @pytest.fixture(scope="module") def backend_limit_params(backend_metric): """Params for limit.""" - return dict(metric_id=backend_metric["id"], period="minute", value=10) + return {"metric_id": backend_metric["id"], "period": "minute", "value": 10} @pytest.fixture(scope="module") @@ -666,7 +678,7 @@ def backend_limit( @pytest.fixture(scope="module") def prule_params(metric): """Params for prule.""" - return dict(metric_id=metric["id"], min=10, max=100, cost_per_unit="10") + return {"metric_id": metric["id"], "min": 10, "max": 100, "cost_per_unit": "10"} @pytest.fixture(scope="module") @@ -682,7 +694,12 @@ def prule(service, application, application_plan, metric, prule_params) -> Prici @pytest.fixture(scope="module") def backend_prule_params(backend_metric): """Params for prule.""" - return dict(metric_id=backend_metric["id"], min=10, max=100, cost_per_unit=10) + return { + "metric_id": backend_metric["id"], + "min": 10, + "max": 100, + "cost_per_unit": 10, + } @pytest.fixture(scope="module") @@ -709,7 +726,7 @@ def promote_params(api, service): """ Promote params for service. """ - return dict(productCRName=service.crd.as_dict()["metadata"]["name"]) + return {"productCRName": service.crd.as_dict()["metadata"]["name"]} @pytest.fixture(scope="module") diff --git a/threescale_api_crd/client.py b/threescale_api_crd/client.py index 4f4d8c2..8fe14dd 100644 --- a/threescale_api_crd/client.py +++ b/threescale_api_crd/client.py @@ -13,7 +13,9 @@ class ThreeScaleClientCRD(threescale_api.client.ThreeScaleClient): Threescale client for CRD. """ - def __init__(self, url, token, ocp_provider_ref=None, ocp_namespace=None, *args, **kwargs): + def __init__( + self, url, token, ocp_provider_ref=None, ocp_namespace=None, *args, **kwargs + ): super().__init__(url, token, *args, **kwargs) self._ocp_provider_ref = ocp_provider_ref self._ocp_namespace = ThreeScaleClientCRD.get_namespace(ocp_namespace) diff --git a/threescale_api_crd/resources.py b/threescale_api_crd/resources.py index 6144c52..b6342b2 100644 --- a/threescale_api_crd/resources.py +++ b/threescale_api_crd/resources.py @@ -180,7 +180,9 @@ def update(self, *args, **kwargs): def delete(self): """This functions is not implemented for Proxies.""" - raise threescale_api.errors.ThreeScaleApiError("Delete not implemented for Proxies") + raise threescale_api.errors.ThreeScaleApiError( + "Delete not implemented for Proxies" + ) def deploy(self): """ @@ -368,8 +370,7 @@ def trans_item(self, key, value, obj): return obj[key][0] met = self.parent.metrics.read(int(obj[key])) return met["system_name"].split(".")[0] - else: - return obj[key] + return obj[key] @staticmethod def insert_into_position(maps, params, spec): @@ -395,7 +396,7 @@ def get_from_position(maps, params): if "last" in params.keys(): return maps[-1] for mapi in maps: - if all([params[key] == mapi[key] for key in params.keys()]): + if all(params[key] == mapi[key] for key in params.keys()): return mapi return None @@ -597,7 +598,7 @@ def in_create(self, maps, params, spec): self.parent.read() self.parent.update({"metrics": maps}) for mapi in self.get_list(): - if all([params[key] == mapi[key] for key in params.keys()]): + if all(params[key] == mapi[key] for key in params.keys()): return mapi return None @@ -716,7 +717,7 @@ def in_create(self, maps, params, spec): self.parent.update({"backend_usages": maps}) params.pop("name", None) for mapi in self.get_list(): - if all([params[key] == mapi[key] for key in params.keys()]): + if all(params[key] == mapi[key] for key in params.keys()): return mapi return None @@ -926,7 +927,7 @@ def in_create(self, maps, params, spec): self.parent.read() self.parent.update({"application_plans": maps}) for mapi in self.get_list(): - if all([params[key] == mapi[key] for key in params.keys()]): + if all(params[key] == mapi[key] for key in params.keys()): return mapi return None @@ -1141,6 +1142,7 @@ class Promotes(DefaultClientCRD, threescale_api.defaults.DefaultClient): SELECTOR = "ProxyConfigPromote" ID_NAME = "productId" # flake8: noqa E501 + # pylint: disable=line-too-long ERROR_MSG = '[]: Invalid value: "": cannot promote to staging as no product changes detected. Delete this proxyConfigPromote CR, then introduce changes to configuration, and then create a new proxyConfigPromote CR' def __init__( @@ -1508,15 +1510,13 @@ def _create_instance_trans(self, instance): if self.metric[BackendMetrics.ID_NAME] == obj["metric_name"] and self.metric.parent["system_name"] == obj["backend_name"] ] - else: - return [ - obj - for obj in instance - if self.metric[Metrics.ID_NAME] == obj["metric_name"] - and "backend_name" not in obj.entity - ] - else: - return [obj for obj in instance] + return [ + obj + for obj in instance + if self.metric[Metrics.ID_NAME] == obj["metric_name"] + and "backend_name" not in obj.entity + ] + return [obj for obj in instance] class PricingRules(DefaultClientNestedCRD, threescale_api.resources.PricingRules): @@ -1707,15 +1707,13 @@ def _create_instance_trans(self, instance): if self.metric[Metrics.ID_NAME] == obj["metric_name"] and self.metric.parent["system_name"] == obj["backend_name"] ] - else: - return [ - obj - for obj in instance - if self.metric[Metrics.ID_NAME] == obj["metric_name"] - and "backend_name" not in obj.entity - ] - else: - return [obj for obj in instance] + return [ + obj + for obj in instance + if self.metric[Metrics.ID_NAME] == obj["metric_name"] + and "backend_name" not in obj.entity + ] + return [obj for obj in instance] class Applications(DefaultClientCRD, threescale_api.resources.Applications): @@ -1915,7 +1913,7 @@ def in_create(self, maps, params, spec): self.topmost_parent().read() self.topmost_parent().update({"methods": maps}) for mapi in self.get_list(): - if all([params[key] == mapi[key] for key in params.keys()]): + if all(params[key] == mapi[key] for key in params.keys()): return mapi return None @@ -2129,8 +2127,7 @@ def deploy(self): prom.delete() if ide and int(ide) == self.parent["id"]: return True - else: - return False + return False def promote(self, **kwargs): """ @@ -2150,8 +2147,7 @@ def promote(self, **kwargs): prom.delete() if ide and int(ide) == self.parent["id"]: return True - else: - return False + return False @property def service(self) -> "Service": @@ -2288,7 +2284,7 @@ def load_openapi(entity, spec): if "url" in spec: url = spec["url"] entity["url"] = url - res = requests.get(url) + res = requests.get(urli, timeout=60) if url.endswith(".yaml") or url.endswith(".yml"): entity["body"] = json.dumps( yaml.load(res.content, Loader=yaml.SafeLoader) @@ -3156,8 +3152,7 @@ def service(self) -> "Service": def account(self) -> "Account": if self.client.account: return self.client.account - else: - return self.parent.accounts.read_by_name(self.entity["account_name"]) + return self.parent.accounts.read_by_name(self.entity["account_name"]) def set_state(self, state: str): """Sets the state for the resource @@ -3217,10 +3212,10 @@ def __init__(self, entity_name="system_name", **kwargs): self.entity_id = entity["id"] super().__init__(crd=crd, entity=entity, entity_name=entity_name, **kwargs) - else: - # this is not here because of some backup, but because we need to have option - # to creater empty object without any data. This is related to "lazy load" - super().__init__(entity_name=entity_name, **kwargs) + + # this is not here because of some backup, but because we need to have option + # to creater empty object without any data. This is related to "lazy load" + super().__init__(entity_name=entity_name, **kwargs) @property def metric(self) -> "Metric":