Skip to content

Commit

Permalink
Merge pull request #13 from jddarby/jl/first-round-feedback
Browse files Browse the repository at this point in the history
Improve CLI based on Feedback
  • Loading branch information
jordlay authored May 26, 2023
2 parents f43f313 + bb6302a commit 276d3ca
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/aosm/azext_aosm/_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
),
"nf_name": "Name of NF definition",
"version": "Version of the NF definition",
"acr_artifact_store_name": "Name of the ACR Artifact Store resource",
"acr_artifact_store_name": "Name of the ACR Artifact Store resource. Will be created if it does not exist.",
"location": "Azure location to use when creating resources",
"blob_artifact_store_name": "Name of the storage account Artifact Store resource",
"blob_artifact_store_name": "Name of the storage account Artifact Store resource. Will be created if it does not exist.",
"artifact_name": "Name of the artifact",
"file_path": (
"Optional. File path of the artifact you wish to upload from your "
Expand Down
9 changes: 8 additions & 1 deletion src/aosm/azext_aosm/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,14 @@ def _generate_config(definition_type: str, output_file: str = "input.json"):
"""
config = get_configuration(definition_type)
config_as_dict = json.dumps(asdict(config), indent=4)


if os.path.exists(output_file):
carry_on = input(
f"The file {output_file} already exists - do you want to overwrite it? (y/n)"
)
if carry_on != "y":
raise UnclassifiedUserFault("User aborted!")

with open(output_file, "w", encoding="utf-8") as f:
f.write(config_as_dict)
if definition_type == CNF or definition_type == VNF:
Expand Down
24 changes: 23 additions & 1 deletion src/aosm/azext_aosm/generate_nfd/cnf_nfd_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ def generate_nfd(self) -> None:
self._tmp_folder_name = tmpdirname
try:
for helm_package in self.config.helm_packages:

# Turn Any type into HelmPackageConfig, to access properties on the object
helm_package = HelmPackageConfig(**helm_package)

# Unpack the chart into the tmp folder
self._extract_chart(helm_package.path_to_chart)

Expand Down Expand Up @@ -147,6 +150,9 @@ def _extract_chart(self, path: str) -> None:
:param path: The path to helm package
"""

logger.debug("Extracting helm package %s", path)

(_, ext) = os.path.splitext(path)
if ext == ".gz" or ext == ".tgz":
tar = tarfile.open(path, "r:gz")
Expand Down Expand Up @@ -177,6 +183,8 @@ def write_manifest_bicep_file(self) -> None:
path = os.path.join(self._tmp_folder_name, CNF_MANIFEST_BICEP_TEMPLATE)
with open(path, "w", encoding="utf-8") as f:
f.write(bicep_contents)

logger.info("Created artifact manifest bicep template: %s", path)

def write_nfd_bicep_file(self) -> None:
"""Write the bicep file for the NFD."""
Expand All @@ -194,19 +202,26 @@ def write_nfd_bicep_file(self) -> None:
path = os.path.join(self._tmp_folder_name, CNF_DEFINITION_BICEP_TEMPLATE)
with open(path, "w", encoding="utf-8") as f:
f.write(bicep_contents)

logger.info("Created NFD bicep template: %s", path)

def write_schema_to_file(self) -> None:
"""Write the schema to file deploymentParameters.json."""

logger.debug("Create deploymentParameters.json")

full_schema = os.path.join(self._tmp_folder_name, DEPLOYMENT_PARAMETERS)
with open(full_schema, "w", encoding="UTF-8") as f:
json.dump(self.deployment_parameter_schema, f, indent=4)

logger.debug(f"{full_schema} created")

def copy_to_output_folder(self) -> None:
"""Copy the config mappings, schema and bicep templates (artifact manifest and NFDV) to the output folder."""

logger.info("Create NFD bicep %s", self.output_folder_name)
os.mkdir(self.output_folder_name)

os.mkdir(self.output_folder_name)
os.mkdir(os.path.join(self.output_folder_name, SCHEMAS))

tmp_nfd_bicep_path = os.path.join(
Expand All @@ -233,6 +248,8 @@ def copy_to_output_folder(self) -> None:
tmp_schema_path,
output_schema_path,
)

logger.info("Copied files to %s", self.output_folder_name)

def generate_nf_application_config(
self,
Expand Down Expand Up @@ -323,6 +340,9 @@ def get_chart_mapping_schema(
param helm_package: The helm package config.
"""

logger.debug("Get chart mapping schema for %s", helm_package.name)

non_def_values = os.path.join(
self._tmp_folder_name, helm_package.name, "values.mappings.yaml"
)
Expand All @@ -349,6 +369,7 @@ def get_chart_mapping_schema(
f"ERROR: Your schema and values for the helm package '{helm_package.name}' do not match. Please fix this and run the command again."
) from e

logger.debug("Generated chart mapping schema for %s", helm_package.name)
return final_schema

def find_deploy_params(
Expand Down Expand Up @@ -422,4 +443,5 @@ def generate_parameter_mappings(self, helm_package: HelmPackageConfig) -> str:
with open(mapping_file_path, "w", encoding="utf-8") as file:
json.dump(data, file, indent=4)

logger.debug("Generated parameter mappings for %s", helm_package.name)
return os.path.join(CONFIG_MAPPINGS, mappings_filename)
28 changes: 22 additions & 6 deletions src/aosm/azext_aosm/generate_nfd/vnf_nfd_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,13 @@ def manifest_path(self) -> Optional[str]:
def vm_parameters(self) -> Dict[str, Any]:
"""The parameters from the VM ARM template."""
with open(self.arm_template_path, "r") as _file:
parameters: Dict[str, Any] = json.load(_file)["parameters"]

data = json.load(_file)
if "parameters" in data:
parameters: Dict[str, Any] = data["parameters"]
else:
print("No parameters found in the template provided. Your schema will have no properties")
parameters = {}

return parameters

def create_parameter_files(self) -> None:
Expand All @@ -120,9 +125,18 @@ def write_deployment_parameters(self, folder_path: str) -> None:
"""
logger.debug("Create deploymentParameters.json")

nfd_parameters: Dict[str, Any] = {
key: {"type": self.vm_parameters[key]["type"]} for key in self.vm_parameters
}
nfd_parameters = {}

for key in self.vm_parameters:
# ARM templates allow int and secureString but we do not currently accept them in AOSM
# This may change, but for now we should change them to accepted types integer and string
if self.vm_parameters[key]["type"] == "int":
nfd_parameters[key] = {"type": "integer"}
elif self.vm_parameters[key]["type"] == "secureString":
nfd_parameters[key] = {"type": "string"}
else:
nfd_parameters[key] = {"type": self.vm_parameters[key]["type"]}


deployment_parameters_path = os.path.join(
folder_path, DEPLOYMENT_PARAMETERS
Expand Down Expand Up @@ -208,4 +222,6 @@ def copy_to_output_folder(self) -> None:
tmp_config_mappings_path,
output_config_mappings_path,
dirs_exist_ok=True,
)
)

logger.info("Copied files to %s", self.output_folder_name)

0 comments on commit 276d3ca

Please sign in to comment.