Skip to content

Commit

Permalink
Cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
patrykkulik-microsoft committed Jul 26, 2023
1 parent 7546a5d commit cd8599d
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 119 deletions.
43 changes: 25 additions & 18 deletions src/aosm/azext_aosm/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def build_definition(
config_file: str,
order_params: bool = False,
interactive: bool = False,
force: bool = False,
):
"""
Build a definition.
Expand All @@ -66,6 +67,7 @@ def build_definition(
config=config,
order_params=order_params,
interactive=interactive,
force=force,
)


Expand Down Expand Up @@ -100,7 +102,7 @@ def _get_config_from_file(config_file: str, configuration_type: str) -> Configur


def _generate_nfd(
definition_type: str, config: NFConfiguration, order_params: bool, interactive: bool
definition_type: str, config: NFConfiguration, order_params: bool, interactive: bool, force: bool = False
):
"""Generate a Network Function Definition for the given type and config."""
nfd_generator: NFDGenerator
Expand All @@ -116,12 +118,13 @@ def _generate_nfd(
" have been implemented."
)
if nfd_generator.nfd_bicep_path:
carry_on = input(
f"The {nfd_generator.nfd_bicep_path.parent} directory already exists -"
" delete it and continue? (y/n)"
)
if carry_on != "y":
raise UnclassifiedUserFault("User aborted!")
if not force:
carry_on = input(
f"The {nfd_generator.nfd_bicep_path.parent} directory already exists -"
" delete it and continue? (y/n)"
)
if carry_on != "y":
raise UnclassifiedUserFault("User aborted!")

shutil.rmtree(nfd_generator.nfd_bicep_path.parent)
nfd_generator.generate_nfd()
Expand Down Expand Up @@ -201,6 +204,7 @@ def delete_published_definition(
definition_type,
config_file,
clean=False,
force=False
):
"""
Delete a published definition.
Expand All @@ -221,9 +225,9 @@ def delete_published_definition(

delly = ResourceDeleter(api_clients, config)
if definition_type == VNF:
delly.delete_nfd(clean=clean)
delly.delete_nfd(clean=clean, force=force)
elif definition_type == CNF:
delly.delete_nfd(clean=clean)
delly.delete_nfd(clean=clean, force=force)
else:
raise ValueError(
"Definition type must be either 'vnf' or 'cnf'. Definition type"
Expand Down Expand Up @@ -277,7 +281,7 @@ def _generate_config(configuration_type: str, output_file: str = "input.json"):
)


def build_design(cmd, client: HybridNetworkManagementClient, config_file: str):
def build_design(cmd, client: HybridNetworkManagementClient, config_file: str, force: bool = False):
"""
Build a Network Service Design.
Expand All @@ -302,13 +306,15 @@ def build_design(cmd, client: HybridNetworkManagementClient, config_file: str):
_generate_nsd(
config=config,
api_clients=api_clients,
force=force,
)


def delete_published_design(
cmd,
client: HybridNetworkManagementClient,
config_file,
force=False,
):
"""
Delete a published NSD.
Expand All @@ -325,7 +331,7 @@ def delete_published_design(
)

destroyer = ResourceDeleter(api_clients, config)
destroyer.delete_nsd()
destroyer.delete_nsd(force=force)


def publish_design(
Expand Down Expand Up @@ -377,15 +383,16 @@ def publish_design(
)


def _generate_nsd(config: NSConfiguration, api_clients: ApiClients):
def _generate_nsd(config: NSConfiguration, api_clients: ApiClients, force: bool = False):
"""Generate a Network Service Design for the given config."""
if os.path.exists(config.output_directory_for_build):
carry_on = input(
f"The folder {config.output_directory_for_build} already exists - delete it"
" and continue? (y/n)"
)
if carry_on != "y":
raise UnclassifiedUserFault("User aborted! ")
if not force:
carry_on = input(
f"The folder {config.output_directory_for_build} already exists - delete it"
" and continue? (y/n)"
)
if carry_on != "y":
raise UnclassifiedUserFault("User aborted! ")

shutil.rmtree(config.output_directory_for_build)
nsd_generator = NSDGenerator(api_clients, config)
Expand Down
80 changes: 41 additions & 39 deletions src/aosm/azext_aosm/delete/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def __init__(
self.api_clients = api_clients
self.config = config

def delete_nfd(self, clean: bool = False):
def delete_nfd(self, clean: bool = False, force: bool = False) -> None:
"""
Delete the NFDV and manifests. If they don't exist it still reports them as deleted.
Expand All @@ -44,33 +44,34 @@ def delete_nfd(self, clean: bool = False):
"""
assert isinstance(self.config, NFConfiguration)

if clean:
print(
"Are you sure you want to delete all resources associated with NFD"
f" {self.config.nf_name} including the artifact stores and publisher"
f" {self.config.publisher_name}?"
)
logger.warning(
"This command will fail if other NFD versions exist in the NFD group."
)
logger.warning(
"Only do this if you are SURE you are not sharing the publisher and"
" artifact stores with other NFDs"
)
print("There is no undo. Type the publisher name to confirm.")
if not input_ack(self.config.publisher_name.lower(), "Confirm delete:"):
print("Not proceeding with delete")
return
else:
print(
"Are you sure you want to delete the NFD Version"
f" {self.config.version} and associated manifests from group"
f" {self.config.nfdg_name} and publisher {self.config.publisher_name}?"
)
print("There is no undo. Type 'delete' to confirm")
if not input_ack("delete", "Confirm delete:"):
print("Not proceeding with delete")
return
if not force:
if clean:
print(
"Are you sure you want to delete all resources associated with NFD"
f" {self.config.nf_name} including the artifact stores and publisher"
f" {self.config.publisher_name}?"
)
logger.warning(
"This command will fail if other NFD versions exist in the NFD group."
)
logger.warning(
"Only do this if you are SURE you are not sharing the publisher and"
" artifact stores with other NFDs"
)
print("There is no undo. Type the publisher name to confirm.")
if not input_ack(self.config.publisher_name.lower(), "Confirm delete:"):
print("Not proceeding with delete")
return
else:
print(
"Are you sure you want to delete the NFD Version"
f" {self.config.version} and associated manifests from group"
f" {self.config.nfdg_name} and publisher {self.config.publisher_name}?"
)
print("There is no undo. Type 'delete' to confirm")
if not input_ack("delete", "Confirm delete:"):
print("Not proceeding with delete")
return

self.delete_nfdv()

Expand All @@ -86,24 +87,25 @@ def delete_nfd(self, clean: bool = False):
self.delete_artifact_store("sa")
self.delete_publisher()

def delete_nsd(self):
def delete_nsd(self, force: bool = False) -> None:
"""
Delete the NSDV and manifests.
If they don't exist it still reports them as deleted.
"""
assert isinstance(self.config, NSConfiguration)

print(
"Are you sure you want to delete the NSD Version"
f" {self.config.nsd_version}, the associated manifest"
f" {self.config.acr_manifest_name} and configuration group schema"
f" {self.config.cg_schema_name}?"
)
print("There is no undo. Type 'delete' to confirm")
if not input_ack("delete", "Confirm delete:"):
print("Not proceeding with delete")
return
if not force:
print(
"Are you sure you want to delete the NSD Version"
f" {self.config.nsd_version}, the associated manifest"
f" {self.config.acr_manifest_name} and configuration group schema"
f" {self.config.cg_schema_name}?"
)
print("There is no undo. Type 'delete' to confirm")
if not input_ack("delete", "Confirm delete:"):
print("Not proceeding with delete")
return

self.delete_nsdv()
self.delete_artifact_manifest("acr")
Expand Down
2 changes: 1 addition & 1 deletion src/aosm/azext_aosm/deploy/deploy_with_arm.py
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ def validate_and_deploy_arm_template(
current_time = str(time.time()).split(".", maxsplit=1)[0]

# Add a timestamp to the deployment name to ensure it is unique
deployment_name = f"AOSM_CLI_deployment_into_{resource_group}_{current_time}"
deployment_name = f"AOSM_CLI_deployment_{current_time}"

validation = self.api_clients.resource_client.deployments.begin_validate(
resource_group_name=resource_group,
Expand Down
114 changes: 67 additions & 47 deletions src/aosm/azext_aosm/tests/latest/test_cnf_publish_and_delete.py
Original file line number Diff line number Diff line change
@@ -1,66 +1,86 @@
from azure.cli.testsdk import ScenarioTest, ResourceGroupPreparer
from knack.log import get_logger
import os
from jinja2 import Template
from typing import Dict
# Currently commented out because of the timeout bug in the testing framework.

# from azure.cli.testsdk import ScenarioTest, ResourceGroupPreparer
# from knack.log import get_logger
# import os
# from jinja2 import Template
# from typing import Dict

logger = get_logger(__name__)

NFD_INPUT_TEMPLATE_NAME = "cnf_input_template.json"
NFD_INPUT_FILE_NAME = "cnf_input.json"
NSD_INPUT_TEMPLATE_NAME = "nsd_cnf_input_template.json"
NSD_INPUT_FILE_NAME = "input_nsd_cnf.json"
CHART_NAME = "nginxdemo-0.1.0.tgz"
# logger = get_logger(__name__)

def get_path_to_chart():
code_dir = os.path.dirname(__file__)
templates_dir = os.path.join(code_dir, "scenario_test_mocks", "cnf_mocks")
chart_path = os.path.join(templates_dir, CHART_NAME)
return chart_path
# NFD_INPUT_TEMPLATE_NAME = "cnf_input_template.json"
# NFD_INPUT_FILE_NAME = "cnf_input.json"
# NSD_INPUT_TEMPLATE_NAME = "nsd_cnf_input_template.json"
# NSD_INPUT_FILE_NAME = "input_nsd_cnf.json"
# CHART_NAME = "nginxdemo-0.1.0.tgz"

def update_input_file(input_template_name, output_file_name, params: Dict[str, str]):
code_dir = os.path.dirname(__file__)
templates_dir = os.path.join(code_dir, "scenario_test_mocks", "mock_input_templates")
input_template_path = os.path.join(templates_dir, input_template_name)

with open(input_template_path, "r", encoding="utf-8") as file:
contents = file.read()

jinja_template = Template(contents)
# def get_path_to_chart():
# code_dir = os.path.dirname(__file__)
# templates_dir = os.path.join(code_dir, "scenario_test_mocks", "cnf_mocks")
# chart_path = os.path.join(templates_dir, CHART_NAME)
# return chart_path

rendered_template = jinja_template.render(**params)

output_path = os.path.join(templates_dir, output_file_name)
# def update_input_file(input_template_name, output_file_name, params: Dict[str, str]):
# code_dir = os.path.dirname(__file__)
# templates_dir = os.path.join(
# code_dir, "scenario_test_mocks", "mock_input_templates"
# )
# input_template_path = os.path.join(templates_dir, input_template_name)

with open(output_path, "w", encoding="utf-8") as file:
file.write(rendered_template)
# with open(input_template_path, "r", encoding="utf-8") as file:
# contents = file.read()

return output_path
# jinja_template = Template(contents)

# rendered_template = jinja_template.render(**params)

class CnfNsdTest(ScenarioTest):
@ResourceGroupPreparer()
def test_cnf_nsd_publish_and_delete(self, resource_group):
# TODO: should be using a temporary resource group
resource_group = "patrykkulik-test"
# output_path = os.path.join(templates_dir, output_file_name)

chart_path = get_path_to_chart()
# with open(output_path, "w", encoding="utf-8") as file:
# file.write(rendered_template)

nfd_input_file_path = update_input_file(NFD_INPUT_TEMPLATE_NAME, NFD_INPUT_FILE_NAME, params={"publisher_resource_group_name": resource_group, "path_to_chart": chart_path})
# return output_path

self.cmd(f'az aosm nfd build -f "{nfd_input_file_path}" --definition-type cnf --force')

self.cmd(f'az aosm nfd publish -f "{nfd_input_file_path}" --definition-type cnf --debug')

# TODO: should I run gets on things to make sure they exist?

nsd_input_file_path = update_input_file(NSD_INPUT_TEMPLATE_NAME, NSD_INPUT_FILE_NAME, params={"publisher_resource_group_name": resource_group})
# class CnfNsdTest(ScenarioTest):
# @ResourceGroupPreparer()
# def test_cnf_nsd_publish_and_delete(self, resource_group):
# # We are overriding a resource group name here because we need to have some
# # resources predeployed in order to get around the timeout bug in the testing framework.
# resource_group = "patrykkulik-test"

self.cmd(f'az aosm nsd build -f "{nsd_input_file_path}" --debug --force')
self.cmd(f'az aosm nsd publish -f "{nsd_input_file_path}" --debug')

# chart_path = get_path_to_chart()

self.cmd(f'az aosm nfd delete --definition-type cnf -f "{nfd_input_file_path}" --debug --force')
self.cmd(f'az aosm nsd delete -f "{nsd_input_file_path}" --debug --force')
# nfd_input_file_path = update_input_file(
# NFD_INPUT_TEMPLATE_NAME,
# NFD_INPUT_FILE_NAME,
# params={
# "publisher_resource_group_name": resource_group,
# "path_to_chart": chart_path,
# },
# )

# self.cmd(
# f'az aosm nfd build -f "{nfd_input_file_path}" --definition-type cnf --force'
# )

# self.cmd(
# f'az aosm nfd publish -f "{nfd_input_file_path}" --definition-type cnf --debug'
# )

# nsd_input_file_path = update_input_file(
# NSD_INPUT_TEMPLATE_NAME,
# NSD_INPUT_FILE_NAME,
# params={"publisher_resource_group_name": resource_group},
# )

# self.cmd(f'az aosm nsd build -f "{nsd_input_file_path}" --debug --force')
# self.cmd(f'az aosm nsd publish -f "{nsd_input_file_path}" --debug')

# self.cmd(
# f'az aosm nfd delete --definition-type cnf -f "{nfd_input_file_path}" --debug --force'
# )
# self.cmd(f'az aosm nsd delete -f "{nsd_input_file_path}" --debug --force')
Loading

0 comments on commit cd8599d

Please sign in to comment.