Skip to content

Commit

Permalink
Adding build command for site
Browse files Browse the repository at this point in the history
  • Loading branch information
rkaraddi committed May 9, 2024
1 parent 5d7589e commit 1ddb88c
Show file tree
Hide file tree
Showing 8 changed files with 402 additions and 0 deletions.
144 changes: 144 additions & 0 deletions src/aosm/azext_aosm/cli_handlers/onboarding_sns_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
from pathlib import Path
from azure.mgmt.resource import ResourceManagementClient
from azext_aosm.cli_handlers.onboarding_nfd_base_handler import OnboardingBaseCLIHandler
from azext_aosm.common.command_context import CommandContext
from azext_aosm.configuration_models.common_parameters_config import (
BaseCommonParametersConfig,
CoreVNFCommonParametersConfig,
NFDCommonParametersConfig,
NSDCommonParametersConfig
)
from azext_aosm.definition_folder.reader.base_definition import \
BaseDefinitionElement
from typing import Literal, Optional
from azext_aosm.configuration_models.onboarding_nsd_input_config import (
OnboardingNSDInputConfig,
)
from azext_aosm.definition_folder.builder.bicep_builder import (
BicepDefinitionElementBuilder,
)
from azext_aosm.definition_folder.builder.deploy_input_builder import DeploymentInputDefinitionElementBuilder
from azext_aosm.vendored_sdks.models import (
NetworkServiceDesignVersion,
)
from azext_aosm.configuration_models.sns_parameters_config import SNSCommonParametersConfig

from azext_aosm.configuration_models.onboarding_sns_input_config import (
OnboardingSNSInputConfig,
NsdReference,
)
from azext_aosm.common.constants import (
SNS_OUTPUT_FOLDER_FILENAME,
SNS_INPUT_FILENAME,
SNS_OUTPUT_FOLDER_FILENAME,
SNS_DEFINITION_FOLDER_NAME,
SNS_TEMPLATE_FOLDER_NAME,
SNS_DEFINITION_TEMPLATE_FILENAME,
)
from azext_aosm.vendored_sdks import HybridNetworkManagementClient
from azext_aosm.common.utils import render_bicep_contents_from_j2, get_template_path

class OnboardingSNSCLIHandler(OnboardingBaseCLIHandler):
"""CLI handler for deploying SNSs."""

config: OnboardingSNSInputConfig | SNSCommonParametersConfig

@property
def default_config_file_name(self) -> str:
"""Get the default configuration file name."""
return SNS_INPUT_FILENAME

@property
def output_folder_file_name(self) -> str:
"""Get the output folder file name."""
return SNS_OUTPUT_FOLDER_FILENAME

def build(self):
"""Build the definition."""
self.definition_folder_builder.add_element(self.build_deploy_input())
self.definition_folder_builder.add_element(self.build_resource_bicep())
self.definition_folder_builder.write()

def _get_processor_list(self) -> list:
return []

def _get_input_config(self, input_config: Optional[dict] = None) -> OnboardingSNSInputConfig:
"""Get the configuration for the command."""
if input_config is None:
input_config = {}
return OnboardingSNSInputConfig(**input_config)

def _get_params_config(self, config_file: Path) -> SNSCommonParametersConfig:
"""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 SNSCommonParametersConfig(**params_dict)

def build_deploy_input(self) -> DeploymentInputDefinitionElementBuilder:
"""Pre-validate the build."""
nsdv = self._get_nsdv()
deployment_input_file = DeploymentInputDefinitionElementBuilder(
Path(SNS_OUTPUT_FOLDER_FILENAME), nsdv.properties.nfvis_from_site
)
return deployment_input_file

def _get_nsdv(self) -> NetworkServiceDesignVersion:
"""Get the existing NSDV resource object."""
print(
f"Reading existing NSDV resource object {self.config.nsd_reference.nsd_version} from group {self.config.nsd_reference.nsd_name}"
)
assert isinstance(self.aosm_client, HybridNetworkManagementClient)
nsdv_object = self.aosm_client.network_service_design_versions.get(
resource_group_name=self.config.nsd_reference.publisher_resource_group_name,
publisher_name=self.config.nsd_reference.publisher_name,
network_service_design_group_name=self.config.nsd_reference.nsd_name,
network_service_design_version_name=self.config.nsd_reference.nsd_version,
)
return nsdv_object

def build_resource_bicep(self) -> BicepDefinitionElementBuilder:
"""Build the resource bicep file."""
template_path = get_template_path(
SNS_TEMPLATE_FOLDER_NAME, SNS_DEFINITION_TEMPLATE_FILENAME
)
params = {}

bicep_contents = render_bicep_contents_from_j2(
template_path, params
)
# Generate the nsd bicep file
bicep_file = BicepDefinitionElementBuilder(
Path(SNS_OUTPUT_FOLDER_FILENAME, SNS_DEFINITION_FOLDER_NAME), bicep_contents
)

return bicep_file

def build_base_bicep(self):
# TODO: Implement
raise NotImplementedError

def deploy(self):
# TODO: Implement this method
raise NotImplementedError

def build_all_parameters_json(self):
# TODO: Implement this method
raise NotImplementedError

def build_artifact_list(self):
# TODO: Implement this method
raise NotImplementedError

def build_manifest_bicep(self) -> BicepDefinitionElementBuilder:
# TODO: Implement this method
raise NotImplementedError

def build_resource_bicep(self) -> BicepDefinitionElementBuilder:
# TODO: Implement this method
raise NotImplementedError

def build_base_bicep(self) -> BicepDefinitionElementBuilder:
# TODO: Implement this method
raise NotImplementedError
4 changes: 4 additions & 0 deletions src/aosm/azext_aosm/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,9 @@ def load_command_table(self: AzCommandsLoader, _):
g.custom_command("publish", "onboard_nsd_publish")
# g.custom_command("delete", "onboard_nsd_delete")

with self.command_group("aosm sns") as g:
# Add each command and bind it to a function in custom.py
g.custom_command("build", "onboard_sns_build")

with self.command_group("aosm", is_preview=True):
pass
6 changes: 6 additions & 0 deletions src/aosm/azext_aosm/common/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ class ManifestsExist(str, Enum):
CNF_VALUES_SCHEMA_FILENAME = "values.schema.json"
CNF_TEMPLATE_FOLDER_NAME = "cnf"

SNS_OUTPUT_FOLDER_FILENAME = "sns-cli-output"
SNS_INPUT_FILENAME = "sns-input.jsonc"
SNS_DEFINITION_TEMPLATE_FILENAME = "snsdefinition.bicep.j2"
SNS_TEMPLATE_FOLDER_NAME = "sns"
SNS_DEFINITION_FOLDER_NAME = "snsDefinition"

NEXUS_IMAGE_REGEX = r"^[\~]?(\d+)\.(\d+)\.(\d+)$"

VALID_VNF_TEMPLATE_RESOURCE_TYPES = [
Expand Down
19 changes: 19 additions & 0 deletions src/aosm/azext_aosm/common/templates/sns/snsdefinition.bicep.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Highly Confidential Material
//
// Bicep template to create a Site.
//
// Requires an existing NSDV from which the values will be populated.
param location string
@description('The site name where the NFVI is deployed')
param siteName string
@description('The nfvi details for the site')
param nfviList array = []

resource site 'Microsoft.HybridNetwork/sites@2023-09-01' = {
name: siteName
location: location
properties: {
nfvis: nfviList
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

from __future__ import annotations

from abc import ABC
from dataclasses import dataclass, field

from azure.cli.core.azclierror import ValidationError

@dataclass
class PreexistingCgv:
name: str = field(
default="",
metadata={
"comment": (
"Name of the cgv."
)
},
)
resource_group: str = field(
default="",
metadata={
"comment": (
"If left blank, resource group is assumed to be same as operator_resource_group"
)
},
)
configuration_group_schema_name: str = field(
default="",
metadata={
"comment": (
"Name of the configuration group schema."
)
},
)

def validate(self):
"""Validate the configuration."""
if not self.name:
raise ValidationError("CGV name must be set")
if not self.configuration_group_schema_name:
raise ValidationError("Configuration group schema name must be set")

@dataclass
class NsdReference:
publisher_name: str = field(
default="",
metadata={
"comment": (
"Name of the Publisher resource you want your definition published to.\n"
"Will be created if it does not exist."
)
},
)
publisher_resource_group_name: str = field(
default="",
metadata={
"comment": (
"Resource group for the Publisher resource.\n"
"Will be created if it does not exist."
)
},
)
nsd_name: str = field(
default="",
metadata={
"comment": (
"Network Service Design (NSD) name. "
"This is the collection of Network Service Design Versions. Will be created if it does not exist."
)
},
)
nsd_version: str = field(
default="",
metadata={
"comment": "Version of the NSD to be created. This should be in the format A.B.C"
},
)

def validate(self):
"""Validate the configuration."""
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")
if not self.nsd_name:
raise ValidationError("NSD group name must be set")
if not self.nsd_version:
raise ValidationError("NSD version must be set")

@dataclass
class OnboardingSNSInputConfig(ABC):
"""Base input configuration for onboarding commands."""

location: str = field(
default="",
metadata={
"comment": "Azure location to use when creating resources e.g uksouth"
},
)
operator_resource_group_name: str = field(
default="",
metadata={
"comment": (
"Resource group for the operator resources.\n"
"Will be created if it does not exist."
)
},
)
sns_name: str = field(
default="",
metadata={
"comment": "Name of the sns."}
)
site_name: str = field(
default="",
metadata={
"comment": "Name of the site."}
)
nsd_reference: NsdReference = (
field(
default_factory=NsdReference,
metadata={
"comment": (
"Reference to the NSD to be used for the SNS."
)
},
)
)
# preexisting_cgvs: List[PreexistingCgv] = field(default_factory=list)# # TODO: Add detailed comment for this

def validate(self):
"""Validate the configuration."""
if not self.location:
raise ValidationError("Location must be set")
if not self.operator_resource_group_name:
raise ValidationError("Operator resource group name must be set")
if not self.site_name:
raise ValidationError("Site name must be set")
if not self.nsd_reference:
raise ValidationError("NSD reference must be set")

def __post_init__(self):
if self.nsd_reference and isinstance(self.nsd_reference, dict):
self.nsd_reference = NsdReference(**self.nsd_reference)
20 changes: 20 additions & 0 deletions src/aosm/azext_aosm/configuration_models/sns_parameters_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

from __future__ import annotations

from abc import ABC
from dataclasses import dataclass


@dataclass(frozen=True)
class SNSCommonParametersConfig(ABC):
"""Base common parameters configuration."""

location: str
operatorResourceGroupName: str
siteName: str
sns_name: str
nsd_reference: NsdReference
8 changes: 8 additions & 0 deletions src/aosm/azext_aosm/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from azext_aosm.cli_handlers.onboarding_vnf_handler import OnboardingVNFCLIHandler
from azext_aosm.cli_handlers.onboarding_core_vnf_handler import OnboardingCoreVNFCLIHandler
from azext_aosm.cli_handlers.onboarding_nexus_vnf_handler import OnboardingNexusVNFCLIHandler
from azext_aosm.cli_handlers.onboarding_sns_handler import OnboardingSNSCLIHandler
from azext_aosm.cli_handlers.onboarding_nsd_handler import OnboardingNSDCLIHandler
from azext_aosm.common.command_context import CommandContext
from azext_aosm.common.constants import ALL_PARAMETERS_FILE_NAME, CNF, VNF, VNF_NEXUS
Expand Down Expand Up @@ -135,3 +136,10 @@ def onboard_nsd_publish(
# command_context = CommandContext(cmd.cli_ctx)
# handler = OnboardingNSDCLIHandler(config_file)
# handler.delete(command_context=command_context)

def onboard_sns_build(config_file: Path, cmd: AzCliCommand):
"""Build the SNS definition."""
command_context = CommandContext(cli_ctx=cmd.cli_ctx)
handler = OnboardingSNSCLIHandler(config_file_path=Path(config_file),
aosm_client=command_context.aosm_client)
handler.build()
Loading

0 comments on commit 1ddb88c

Please sign in to comment.