From bf401597f3780a769663552119d429cd2eac98d1 Mon Sep 17 00:00:00 2001 From: Jordan Date: Thu, 7 Mar 2024 11:53:19 +0000 Subject: [PATCH] refactor from day of learning; needs testing --- .../onboarding_core_vnf_handler.py | 50 ++++---------- .../onboarding_nexus_vnf_handler.py | 50 ++++---------- .../cli_handlers/onboarding_vnf_handler.py | 69 ++++++++++++++++--- 3 files changed, 87 insertions(+), 82 deletions(-) diff --git a/src/aosm/azext_aosm/cli_handlers/onboarding_core_vnf_handler.py b/src/aosm/azext_aosm/cli_handlers/onboarding_core_vnf_handler.py index 89ab6d9ff2b..f14a3bb30ca 100644 --- a/src/aosm/azext_aosm/cli_handlers/onboarding_core_vnf_handler.py +++ b/src/aosm/azext_aosm/cli_handlers/onboarding_core_vnf_handler.py @@ -46,23 +46,17 @@ class OnboardingCoreVNFCLIHandler(OnboardingVNFCLIHandler): config: OnboardingCoreVNFInputConfig - def _get_input_config( - self, input_config: Optional[dict] = None - ) -> OnboardingCoreVNFInputConfig: - """Get the configuration for the command.""" - if input_config is None: - input_config = {} - return OnboardingCoreVNFInputConfig(**input_config) - - def _get_params_config( - self, config_file: Path - ) -> CoreVNFCommonParametersConfig: - """Get the configuration for the command.""" - with open(config_file, "r", encoding="utf-8") as _file: - params_dict = json.load(_file) - if params_dict is None: - params_dict = {} - return CoreVNFCommonParametersConfig(**params_dict) + @property + def input_config(self): + return OnboardingCoreVNFInputConfig + + @property + def params_config(self): + return CoreVNFCommonParametersConfig + + @property + def base_template_filename(self): + return VNF_CORE_BASE_TEMPLATE_FILENAME def _get_processor_list(self) -> List[AzureCoreArmBuildProcessor | VHDProcessor]: """Get the list of processors.""" @@ -95,22 +89,8 @@ def _get_processor_list(self) -> List[AzureCoreArmBuildProcessor | VHDProcessor] processor_list.append(vhd_processor) return processor_list - def build_base_bicep(self) -> BicepDefinitionElementBuilder: - """Build the base bicep file.""" - # Build manifest bicep contents, with j2 template - template_path = get_template_path( - VNF_TEMPLATE_FOLDER_NAME, VNF_CORE_BASE_TEMPLATE_FILENAME - ) - bicep_contents = render_bicep_contents_from_j2(template_path, {}) - # Create Bicep element with manifest contents - bicep_file = BicepDefinitionElementBuilder( - Path(VNF_OUTPUT_FOLDER_FILENAME, BASE_FOLDER_NAME), bicep_contents - ) - return bicep_file - - def build_all_parameters_json(self) -> JSONDefinitionElementBuilder: - """Create object for all_parameters.json.""" - params_content = { + def get_params_content(self): + return { "location": self.config.location, "publisherName": self.config.publisher_name, "publisherResourceGroupName": self.config.publisher_resource_group_name, @@ -121,10 +101,6 @@ def build_all_parameters_json(self) -> JSONDefinitionElementBuilder: "nfDefinitionGroup": self.config.nf_name, "nfDefinitionVersion": self.config.version } - base_file = JSONDefinitionElementBuilder( - Path(VNF_OUTPUT_FOLDER_FILENAME), json.dumps(params_content, indent=4) - ) - return base_file def _get_default_config(self, vhd) -> Dict[str, Any]: """Get default VHD config for Azure Core VNF.""" diff --git a/src/aosm/azext_aosm/cli_handlers/onboarding_nexus_vnf_handler.py b/src/aosm/azext_aosm/cli_handlers/onboarding_nexus_vnf_handler.py index 9be8928cb87..86ab3f51116 100644 --- a/src/aosm/azext_aosm/cli_handlers/onboarding_nexus_vnf_handler.py +++ b/src/aosm/azext_aosm/cli_handlers/onboarding_nexus_vnf_handler.py @@ -46,23 +46,17 @@ class OnboardingNexusVNFCLIHandler(OnboardingVNFCLIHandler): config: OnboardingNexusVNFInputConfig - def _get_input_config( - self, input_config: Optional[dict] = None - ) -> OnboardingNexusVNFInputConfig: - """Get the configuration for the command.""" - if input_config is None: - input_config = {} - return OnboardingNexusVNFInputConfig(**input_config) - - def _get_params_config( - self, config_file: Path - ) -> NexusVNFCommonParametersConfig: - """Get the configuration for the command.""" - with open(config_file, "r", encoding="utf-8") as _file: - params_dict = json.load(_file) - if params_dict is None: - params_dict = {} - return NexusVNFCommonParametersConfig(**params_dict) + @property + def input_config(self): + return OnboardingNexusVNFInputConfig + + @property + def params_config(self): + return NexusVNFCommonParametersConfig + + @property + def base_template_filename(self): + return VNF_NEXUS_BASE_TEMPLATE_FILENAME def _get_processor_list(self) -> List[NexusArmBuildProcessor | NexusImageProcessor]: processor_list: List[NexusArmBuildProcessor | NexusImageProcessor] = [] @@ -92,22 +86,8 @@ def _get_processor_list(self) -> List[NexusArmBuildProcessor | NexusImageProcess return processor_list - def build_base_bicep(self) -> BicepDefinitionElementBuilder: - """Build the base bicep file.""" - # Build manifest bicep contents, with j2 template - template_path = get_template_path( - VNF_TEMPLATE_FOLDER_NAME, VNF_NEXUS_BASE_TEMPLATE_FILENAME - ) - bicep_contents = render_bicep_contents_from_j2(template_path, {}) - # Create Bicep element with manifest contents - bicep_file = BicepDefinitionElementBuilder( - Path(VNF_OUTPUT_FOLDER_FILENAME, BASE_FOLDER_NAME), bicep_contents - ) - return bicep_file - - def build_all_parameters_json(self) -> JSONDefinitionElementBuilder: - """Build the all parameters json file.""" - params_content = { + def get_params_content(self): + return { "location": self.config.location, "publisherName": self.config.publisher_name, "publisherResourceGroupName": self.config.publisher_resource_group_name, @@ -116,10 +96,6 @@ def build_all_parameters_json(self) -> JSONDefinitionElementBuilder: "nfDefinitionGroup": self.config.nf_name, "nfDefinitionVersion": self.config.version } - base_file = JSONDefinitionElementBuilder( - Path(VNF_OUTPUT_FOLDER_FILENAME), json.dumps(params_content, indent=4) - ) - return base_file def _generate_type_specific_nf_application(self, processor) -> "tuple[list, list]": """Generate the type specific nf application.""" diff --git a/src/aosm/azext_aosm/cli_handlers/onboarding_vnf_handler.py b/src/aosm/azext_aosm/cli_handlers/onboarding_vnf_handler.py index 5ed33c14336..6dac8f05467 100644 --- a/src/aosm/azext_aosm/cli_handlers/onboarding_vnf_handler.py +++ b/src/aosm/azext_aosm/cli_handlers/onboarding_vnf_handler.py @@ -3,17 +3,18 @@ # Licensed under the MIT License. See License.txt in the project root for license information. # -------------------------------------------------------------------------------------------- from __future__ import annotations - +import json from pathlib import Path from abc import abstractmethod - +from typing import Optional from .onboarding_nfd_base_handler import OnboardingNFDBaseCLIHandler from knack.log import get_logger -# from azext_aosm.configuration_models.onboarding_vnf_input_config import ( -# OnboardingBaseVNFInputConfig, -# ) +from azext_aosm.definition_folder.builder.json_builder import ( + JSONDefinitionElementBuilder, +) from azext_aosm.common.utils import render_bicep_contents_from_j2, get_template_path -from azext_aosm.configuration_models.onboarding_vnf_input_config import (OnboardingCoreVNFInputConfig, OnboardingNexusVNFInputConfig) +from azext_aosm.configuration_models.onboarding_vnf_input_config import ( + OnboardingNFDBaseInputConfig) from azext_aosm.definition_folder.builder.bicep_builder import ( BicepDefinitionElementBuilder, ) @@ -27,7 +28,8 @@ VNF_OUTPUT_FOLDER_FILENAME, VNF_TEMPLATE_FOLDER_NAME, MANIFEST_FOLDER_NAME, - VNF_MANIFEST_TEMPLATE_FILENAME + VNF_MANIFEST_TEMPLATE_FILENAME, + BASE_FOLDER_NAME ) logger = get_logger(__name__) @@ -36,7 +38,7 @@ class OnboardingVNFCLIHandler(OnboardingNFDBaseCLIHandler): """CLI handler for publishing NFDs.""" - config: OnboardingCoreVNFInputConfig | OnboardingNexusVNFInputConfig + config: OnboardingNFDBaseInputConfig @property def default_config_file_name(self) -> str: @@ -48,6 +50,35 @@ def output_folder_file_name(self) -> str: """Get the output folder file name.""" return VNF_OUTPUT_FOLDER_FILENAME + def _get_input_config( + self, input_config_dict: Optional[dict] = None + ) -> OnboardingNFDBaseInputConfig: + """Get the configuration for the command.""" + if input_config_dict is None: + input_config_dict = {} + return self.input_config(**input_config_dict) + + def _get_params_config( + self, config_file: Path + ) -> OnboardingNFDBaseInputConfig: + """Get the configuration for the command.""" + with open(config_file, "r", encoding="utf-8") as _file: + params_dict = json.load(_file) + if params_dict is None: + params_dict = {} + return self.params_config(**params_dict) + + def build_base_bicep(self) -> BicepDefinitionElementBuilder: + """Build the base bicep file.""" + template_path = get_template_path( + VNF_TEMPLATE_FOLDER_NAME, self.base_template_filename + ) + bicep_contents = render_bicep_contents_from_j2(template_path, {}) + bicep_file = BicepDefinitionElementBuilder( + Path(VNF_OUTPUT_FOLDER_FILENAME, BASE_FOLDER_NAME), bicep_contents + ) + return bicep_file + def build_artifact_list(self) -> ArtifactDefinitionElementBuilder: """Build the artifact list. @@ -165,6 +196,13 @@ def build_manifest_bicep(self) -> BicepDefinitionElementBuilder: logger.info("Created artifact manifest bicep element") return bicep_file + def build_all_parameters_json(self) -> JSONDefinitionElementBuilder: + """Build the all parameters json file.""" + params_content = self.get_params_content() + base_file = JSONDefinitionElementBuilder( + Path(VNF_OUTPUT_FOLDER_FILENAME), json.dumps(params_content, indent=4) + ) + return base_file @abstractmethod def _get_nfd_template_params(self, arm_nf_application_list, image_nf_application_list): return NotImplementedError @@ -176,3 +214,18 @@ def _generate_type_specific_nf_application(self, processor): @abstractmethod def _generate_type_specific_artifact_manifest(self, processor): return NotImplementedError + + @property + def input_config(self): + raise NotImplementedError + + @property + def params_config(self): + raise NotImplementedError + + @property + def base_template_filename(self): + raise NotImplementedError + + def get_params_content(self): + raise NotImplementedError