Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor input improvements #132

Merged
merged 4 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ def __init__(
):
"""Initialize the CLI handler."""
self.aosm_client = aosm_client
config_file = Path(config_file)
# If config file provided (for build, publish and delete)
if config_file:
config_file = Path(config_file)
# If config file is the input.jsonc for build command
if config_file.suffix == ".jsonc":
config_dict = self._read_input_config_from_file(config_file)
Expand All @@ -50,7 +50,6 @@ def __init__(
# If config file is the all parameters json file for publish/delete
elif config_file.suffix == ".json":
self.config = self._get_params_config(config_file)
print("cnf config", self.config)
else:
raise UnclassifiedUserFault("Invalid input")
# TODO: Change this to work with publish?
Expand Down
22 changes: 8 additions & 14 deletions src/aosm/azext_aosm/cli_handlers/onboarding_nsd_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,20 +235,14 @@ def build_resource_bicep(self):
def build_all_parameters_json(self):
# TODO: add common params for build resource bicep
params_content = {
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"location": {"value": self.config.location},
"publisherName": {"value": self.config.publisher_name},
"publisherResourceGroupName": {
"value": self.config.publisher_resource_group_name
},
"acrArtifactStoreName": {"value": self.config.acr_artifact_store_name},
"acrManifestName": {
"value": self.config.acr_artifact_store_name + "-manifest"
},
"nsDesignGroup": {"value": self.config.nsd_name},
},
"location": self.config.location,
"publisherName": self.config.publisher_name,
"publisherResourceGroupName": self.config.publisher_resource_group_name,
"acrArtifactStoreName": self.config.acr_artifact_store_name,
"acrManifestName": self.config.acr_artifact_store_name + "-manifest",
"nsDesignGroup":self.config.nsd_name,
"nsDesignVersion" : self.config.nsd_version,
"nfviSiteName": self.nfvi_site_name
}
base_file = JSONDefinitionElementBuilder(
Path(NSD_OUTPUT_FOLDER_FILENAME), json.dumps(params_content, indent=4)
Expand Down
28 changes: 9 additions & 19 deletions src/aosm/azext_aosm/cli_handlers/onboarding_vnf_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,25 +261,15 @@ def build_resource_bicep(self):

def build_all_parameters_json(self):
params_content = {
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"location": {"value": self.config.location},
"publisherName": {"value": self.config.publisher_name},
"publisherResourceGroupName": {
"value": self.config.publisher_resource_group_name
},
"acrArtifactStoreName": {"value": self.config.acr_artifact_store_name},
"saArtifactStoreName": {"value": self.config.blob_artifact_store_name},
"acrManifestName": {
"value": self.config.acr_artifact_store_name + "-manifest"
},
"saManifestName": {
"value": self.config.blob_artifact_store_name + "-manifest"
},
"nfDefinitionGroup": {"value": self.config.nf_name},
"nfDefinitionVersion": {"value": self.config.version},
},
"location": self.config.location,
"publisherName": self.config.publisher_name,
"publisherResourceGroupName": self.config.publisher_resource_group_name,
"acrArtifactStoreName": self.config.acr_artifact_store_name,
"saArtifactStoreName": self.config.blob_artifact_store_name,
"acrManifestName": self.config.acr_artifact_store_name + "-manifest",
"saManifestName": self.config.blob_artifact_store_name + "-manifest",
"nfDefinitionGroup": self.config.nf_name,
"nfDefinitionVersion": self.config.version
}
base_file = JSONDefinitionElementBuilder(
Path(VNF_OUTPUT_FOLDER_FILENAME), json.dumps(params_content, indent=4)
Expand Down
16 changes: 10 additions & 6 deletions src/aosm/azext_aosm/common/artifact.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,13 @@ def __init__(self, artifact_name, artifact_type, artifact_version, file_path: Pa
self.file_path = str(file_path) # TODO: Jordan cast this to str here, check output file isn't broken, and/or is it used as a Path elsewhere?
# TODO (WIBNI): check if the artifact name ends in .bicep and if so use utils.convert_bicep_to_arm()
# This way we can support in-place Bicep artifacts in the folder.
def upload(self, config: BaseCommonParametersConfig, command_context: CommandContext):

def upload(self, config: BaseCommonParametersConfig, command_context: CommandContext, oras_client: OrasClient = None):
"""Upload the artifact."""
logger.debug("LocalFileACRArtifact config: %s", config)
manifest_credentials = self._manifest_credentials(config=config, aosm_client=command_context.aosm_client)
oras_client = self._get_oras_client(manifest_credentials=manifest_credentials)
if not oras_client:
manifest_credentials = self._manifest_credentials(config=config, aosm_client=command_context.aosm_client)
oras_client = self._get_oras_client(manifest_credentials=manifest_credentials)
target_acr = self._get_acr(oras_client)
target = (
f"{target_acr}/{self.artifact_name}:{self.artifact_version}"
Expand All @@ -165,14 +167,16 @@ def upload(self, config: BaseCommonParametersConfig, command_context: CommandCon
try:
oras_client.push(files=[self.file_path], target=target)
break
except ValueError:
if retries < 20:
except ValueError as error:
if retries < 50:
logger.info("Retrying pushing local artifact to ACR. Retries so far: %s", retries)
retries += 1
sleep(3)
sleep(1)
continue
raise error
logger.info("LocalFileACRArtifact uploaded %s to %s", self.file_path, target)

return oras_client

class RemoteACRArtifact(BaseACRArtifact):
"""Class for ACR artifacts from a remote ACR image."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,6 @@ class CNFCommonParametersConfig(NFDCommonParametersConfig):
class NSDCommonParametersConfig(BaseCommonParametersConfig):
"""Common parameters configuration for NSDs."""

nsDefinitionGroup: str
nsDesignGroup: str
nsDesignVersion: str
nfviSiteName: str
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ class OnboardingBaseInputConfig(ABC):
)
},
)
publisher_resource_group_name: str | None = field(
publisher_resource_group_name: str = field(
default="",
metadata={
"comment": (
"Optional. Resource group for the Publisher resource.\n"
"Will be created if it does not exist (with a default name if none is supplied)."
"Resource group for the Publisher resource.\n"
"You should create this before running the publish command"
)
},
)
Expand All @@ -53,3 +53,5 @@ def validate(self):
raise ValidationError("Location must be set")
if not self.publisher_name:
raise ValidationError("Publisher name must be set")
if not self.publisher_resource_group_name:
raise ValidationError("Publisher resource group name must be set")
Original file line number Diff line number Diff line change
Expand Up @@ -21,51 +21,29 @@ class ImageSourceConfig:
default="",
metadata={
"comment": (
"Optional. login server of the source acr registry from which to pull the image(s).\n"
"Login server of the source acr registry from which to pull the image(s).\n"
"For example sourceacr.azurecr.io. "
"Leave blank if you have set source_local_docker_image."
)
},
)
source_registry_namespace: str = field(
source_registry_namespace: str | None = field(
default="",
metadata={
"comment": (
"Optional. namespace of the repository of the source acr registry from which to pull.\n"
"Optional. Namespace of the repository of the source acr registry from which to pull.\n"
"For example if your repository is samples/prod/nginx then set this to samples/prod.\n"
"Leave blank if the image is in the root namespace or you have set source_local_docker_image.\n"
"Leave blank if the image is in the root namespace.\n"
"See https://learn.microsoft.com/en-us/azure/container-registry/"
"container-registry-best-practices#repository-namespaces for further details."
)
},
)
source_local_docker_image: str = field(
default="",
metadata={
"comment": (
"Optional. The image name of the source docker image from your local machine.\n"
"For limited use case where the CNF only requires a single docker image "
"that exists in the local docker repository."
)
},
)

def validate(self):
"""Validate the image configuration."""
if self.source_registry_namespace and not self.source_registry:
raise ValidationError(
"Config validation error. The image source registry namespace should "
"only be configured if a source registry is configured."
)

if self.source_registry and self.source_local_docker_image:
raise ValidationError(
"Only one of source_registry and source_local_docker_image can be set."
)

if not (self.source_registry or self.source_local_docker_image):
if not self.source_registry:
raise ValidationError(
"One of source_registry or source_local_docker_image must be set."
"Source registry must be set"
)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class OnboardingNFDBaseInputConfig(OnboardingBaseInputConfig):
nf_name: str = field(default="", metadata={"comment": "Name of NF definition."})
version: str = field(
default="",
metadata={"comment": "Version of the NF definition in A.B.C format."},
metadata={"comment": "Version of the NF definition in 1.1.1 format."},
)

def validate(self):
Expand All @@ -28,3 +28,8 @@ def validate(self):
raise ValidationError("nf_name must be set")
if not self.version:
raise ValidationError("version must be set")
if "-" in self.version or not "." in self.version:
raise ValidationError(
"Config validation error. Version should be in"
" format 1.1.1"
)
Original file line number Diff line number Diff line change
Expand Up @@ -158,5 +158,4 @@ def validate(self):
raise ValidationError("You must include at least one arm template")
for arm_template in self.arm_templates:
arm_template.validate()
self.vhd.validate()

self.vhd.validate()
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,5 @@ def write(self):
artifacts_list = []
for artifact in self.artifacts:
artifacts_list.append(artifact.to_dict())
print("artifact list", artifacts_list)
(self.path / "artifacts.json").write_text(json.dumps(artifacts_list, indent=4))
self._write_supporting_files()
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ def create_artifact_object(self, artifact: dict) -> BaseArtifact:
Use the inspect module to identify the artifact class's required fields and
create an instance of the class using the supplied artifact dict.
"""
print("artifact: ", artifact)
if "type" not in artifact or artifact["type"] not in ARTIFACT_TYPE_TO_CLASS:
raise ValueError("Artifact type is missing or invalid")
class_sig = inspect.signature(ARTIFACT_TYPE_TO_CLASS[artifact["type"]].__init__)
Expand All @@ -57,9 +56,14 @@ def create_artifact_object(self, artifact: dict) -> BaseArtifact:

def deploy(self, config: BaseCommonParametersConfig, command_context: CommandContext):
"""Deploy the element."""
oras_client = None
for artifact in self.artifacts:
logger.info("Deploying artifact %s of type %s", artifact.artifact_name, type(artifact))
artifact.upload(config=config, command_context=command_context)
logger.info("Oras client id: %s", id(oras_client))
if oras_client:
artifact.upload(config=config, command_context=command_context, oras_client=oras_client)
else:
oras_client = artifact.upload(config=config, command_context=command_context)

def delete(self):
"""Delete the element."""
Expand Down
32 changes: 16 additions & 16 deletions src/aosm/azext_aosm/inputs/helm_chart_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,26 +103,26 @@ def from_chart_path(
"""
logger.info("Creating Helm chart input from chart path")
temp_dir = Path(tempfile.mkdtemp())
# TODO: raise useful error if no path given?
if chart_path.exists():
logger.debug("Unpacking Helm chart to %s", temp_dir)
if chart_path.is_dir():
unpacked_chart_path = Path(chart_path)
else:
unpacked_chart_path = extract_tarfile(chart_path, temp_dir)

logger.debug("Unpacking Helm chart to %s", temp_dir)
name, version = HelmChartInput._get_name_and_version(unpacked_chart_path)

if chart_path.is_dir():
unpacked_chart_path = Path(chart_path)
else:
unpacked_chart_path = extract_tarfile(chart_path, temp_dir)

name, version = HelmChartInput._get_name_and_version(unpacked_chart_path)
shutil.rmtree(temp_dir)

shutil.rmtree(temp_dir)
logger.debug("Deleted temporary directory %s", temp_dir)

logger.debug("Deleted temporary directory %s", temp_dir)

return HelmChartInput(
artifact_name=name,
artifact_version=version,
chart_path=chart_path,
default_config=default_config,
)
return HelmChartInput(
artifact_name=name,
artifact_version=version,
chart_path=chart_path,
default_config=default_config,
)

def get_defaults(self) -> Dict[str, Any]:
"""
Expand Down