Skip to content

Commit

Permalink
refactor from day of learning; needs testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan committed Mar 7, 2024
1 parent 262fcc5 commit bf40159
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 82 deletions.
50 changes: 13 additions & 37 deletions src/aosm/azext_aosm/cli_handlers/onboarding_core_vnf_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -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,
Expand All @@ -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."""
Expand Down
50 changes: 13 additions & 37 deletions src/aosm/azext_aosm/cli_handlers/onboarding_nexus_vnf_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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] = []
Expand Down Expand Up @@ -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,
Expand All @@ -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."""
Expand Down
69 changes: 61 additions & 8 deletions src/aosm/azext_aosm/cli_handlers/onboarding_vnf_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand All @@ -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__)
Expand All @@ -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:
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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

0 comments on commit bf40159

Please sign in to comment.