diff --git a/.flake8 b/.flake8 index e44b810..a6f727b 100644 --- a/.flake8 +++ b/.flake8 @@ -1,2 +1,2 @@ [flake8] -ignore = E501 +ignore = E501, W503 diff --git a/.gitignore b/.gitignore index dacab84..9091912 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ devops/plugins/ node_modules .gx-credentials/ *.tar.gz +htmlcov # Make sure credentials not push to the repo os_secret k8s_secret diff --git a/LICENSE b/LICENSE index 5e3fe2e..e48e096 100644 --- a/LICENSE +++ b/LICENSE @@ -275,4 +275,3 @@ version(s), and exceptions or additional permissions here}." look for such a notice. You may add additional accurate notices of copyright ownership. - \ No newline at end of file diff --git a/generator/cli.py b/generator/cli.py index b37644a..c2e6726 100755 --- a/generator/cli.py +++ b/generator/cli.py @@ -64,7 +64,7 @@ def openstack(cloud, timeout, config): os_cloud = OsCloud(conn, Config(config_dict)) # run discovery - creds = os_cloud.discover() + creds = os_cloud.discover props = json_ld.get_json_ld_context() props["@graph"] = creds diff --git a/generator/common/config.py b/generator/common/config.py index 2b9c947..9f1be93 100644 --- a/generator/common/config.py +++ b/generator/common/config.py @@ -22,10 +22,9 @@ def __init__(self, config): def get_value(self, keys: List[str]): try: return _get_value(self.config, keys) - except KeyError as ke: + except KeyError: raise KeyError("Config file missing following keys: " + str(keys)) - def get_copyright_owner(self, software: str) -> List[str]: return self.get_value([const.CONFIG_SOFTWARE, software, const.CONFIG_COPYRIGHT]) diff --git a/generator/discovery/openstack/openstack_discovery.py b/generator/discovery/openstack/openstack_discovery.py index 57500fa..b96d53a 100644 --- a/generator/discovery/openstack/openstack_discovery.py +++ b/generator/discovery/openstack/openstack_discovery.py @@ -3,37 +3,33 @@ (c) Anja Strunk , 2/2024 SPDX-License-Identifier: EPL-2.0 """ -import json -import os -import uuid -from datetime import datetime + from hashlib import sha256 -from typing import List import requests -from jinja2 import Environment, FileSystemLoader, select_autoescape -from linkml_runtime.utils import yamlutils from openstack.connection import Connection from requests.exceptions import HTTPError from generator.common import const from generator.common.config import Config -from generator.common.gx_schema import DataAccountExport, TermsAndConditions, VirtualMachineServiceOffering -from generator.discovery.openstack.server_flavor_discovery import \ - ServerFlavorDiscovery +from generator.common.gx_schema import ( + DataAccountExport, + TermsAndConditions, + VirtualMachineServiceOffering, +) +from generator.discovery.openstack.server_flavor_discovery import ServerFlavorDiscovery from generator.discovery.openstack.vm_images_discovery import VmImageDiscovery - class OsCloud: """Abstraction for openStack cloud with all its services.""" def __init__(self, conn: Connection, config: Config) -> None: - # import copy self.conn = conn # self.regions = list(conn.identity.regions()) self.config = config + @property def discover(self) -> VirtualMachineServiceOffering: """ Discover all attributes of OS Cloud. @@ -48,33 +44,58 @@ def discover(self) -> VirtualMachineServiceOffering: # Create Virtual Service Offering object data_export_account = DataAccountExport( requestType=self.config.get_value( - [const.CONFIG_IAAS, const.CONFIG_IAAS_DATA_EXPORT, const.CONFIG_IAAS_DATA_EXPORT_REQ_TYPE]), + [ + const.CONFIG_IAAS, + const.CONFIG_IAAS_DATA_EXPORT, + const.CONFIG_IAAS_DATA_EXPORT_REQ_TYPE, + ] + ), accessType=self.config.get_value( - [const.CONFIG_IAAS, const.CONFIG_IAAS_DATA_EXPORT, const.CONFIG_IAAS_DATA_EXPORT_ACCESS_TYPE]), + [ + const.CONFIG_IAAS, + const.CONFIG_IAAS_DATA_EXPORT, + const.CONFIG_IAAS_DATA_EXPORT_ACCESS_TYPE, + ] + ), formatType=self.config.get_value( - [const.CONFIG_IAAS, const.CONFIG_IAAS_DATA_EXPORT, const.CONFIG_IAAS_DATA_EXPORT_FORMAT_TYPE]) + [ + const.CONFIG_IAAS, + const.CONFIG_IAAS_DATA_EXPORT, + const.CONFIG_IAAS_DATA_EXPORT_FORMAT_TYPE, + ] + ), ) - terms_and_conditions = [] - for url in self.config.get_value([const.CONFIG_IAAS, const.CONFIG_IAAS_T_AND_C]): + serivce_tac = [] + for url in self.config.get_value( + [const.CONFIG_IAAS, const.CONFIG_IAAS_T_AND_C] + ): httpResponse = requests.get(url) if httpResponse.status_code == 200: content = httpResponse.text - terms_and_conditions.append(TermsAndConditions(url=url, hash=sha256(content.encode("utf-8")).hexdigest())) + serivce_tac.append( + TermsAndConditions( + url=url, hash=sha256(content.encode("utf-8")).hexdigest() + ) + ) else: raise HTTPError( - "Cloud not retrieve terms and conditions from '" + url + "'. HTTP Status code: " + str( - httpResponse.status_code)) + "Cloud not retrieve terms and conditions from '" + + url + "'. HTTP Status code: " + str(httpResponse.status_code) + ) - if len(terms_and_conditions) == 0: + if len(serivce_tac) == 0: raise ValueError( - "Service offerings terms and conditions MUST not be empty. Please check config.yaml. " - + "There MUST be at least on entry in " + const.CONFIG_IAAS + "." + const.CONFIG_IAAS_T_AND_C) + "Service offerings terms and conditions MUST not be empty. Please check config.yaml. There MUST be at least on entry in " + + const.CONFIG_IAAS + "." + const.CONFIG_IAAS_T_AND_C + ) return VirtualMachineServiceOffering( providedBy=self.config.get_value([const.CONFIG_CSP, const.CONFIG_DID]), dataAccountExport=data_export_account, - servicePolicy=self.config.get_value([const.CONFIG_IAAS, const.CONFIG_IAAS_SERVICE_POLICY]), - serviceOfferingTermsAndConditions=terms_and_conditions, + servicePolicy=self.config.get_value( + [const.CONFIG_IAAS, const.CONFIG_IAAS_SERVICE_POLICY] + ), + serviceOfferingTermsAndConditions=serivce_tac, codeArtifact=images, - instantiationReq=flavors + instantiationReq=flavors, ) diff --git a/generator/discovery/openstack/server_flavor_discovery.py b/generator/discovery/openstack/server_flavor_discovery.py index 9e5e9f8..abb6bb3 100644 --- a/generator/discovery/openstack/server_flavor_discovery.py +++ b/generator/discovery/openstack/server_flavor_discovery.py @@ -16,7 +16,6 @@ from generator.common.gx_schema import (Disk, DiskType, Frequency, Hypervisor, HypervisorType, Memory, MemorySize) from generator.common.gx_schema import ServerFlavor as GX_Flavor -from generator.common.json_ld import JsonLdObject from generator.vendor.flavor_names import Flavorname, parser_v3 # map SCS hypervisor names to corresponding GX type and config key diff --git a/generator/discovery/openstack/vm_images_discovery.py b/generator/discovery/openstack/vm_images_discovery.py index 83bf081..f5250f0 100644 --- a/generator/discovery/openstack/vm_images_discovery.py +++ b/generator/discovery/openstack/vm_images_discovery.py @@ -28,7 +28,6 @@ VMDiskType) from generator.common.gx_schema import VMImage as GX_Image from generator.common.gx_schema import WatchDogActions -from generator.common.json_ld import JsonLdObject VALID_UNTIL_LOOKUP = { "none": Validity1.none.text, diff --git a/tests/test_openstack_discovery.py b/tests/test_openstack_discovery.py index b6a86f5..7fcb2ed 100644 --- a/tests/test_openstack_discovery.py +++ b/tests/test_openstack_discovery.py @@ -68,7 +68,7 @@ def setUp(self): conn=MockConnection(images=[OS_IMAGE_1], flavors=[OS_FLAVOR_1]), config=get_config()) def test_discovery(self): - claim = self.discovery.discover() + claim = self.discovery.discover pass