forked from Azure/azure-cli-extensions
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
402 additions
and
0 deletions.
There are no files selected for viewing
144 changes: 144 additions & 0 deletions
144
src/aosm/azext_aosm/cli_handlers/onboarding_sns_handler.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/aosm/azext_aosm/common/templates/sns/snsdefinition.bicep.j2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
148 changes: 148 additions & 0 deletions
148
src/aosm/azext_aosm/configuration_models/onboarding_sns_input_config.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
20
src/aosm/azext_aosm/configuration_models/sns_parameters_config.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.