Skip to content

Commit 6af3253

Browse files
committed
Refactor AzureService.publish
Refactor the code from `AzureService.publish` by decoupling some of its internals into privdate methods. The goal here is to clean up the code and make it more legible. Refers to SPSTRAT-595 Signed-off-by: Jonathan Gangi <[email protected]>
1 parent cc2f8d5 commit 6af3253

File tree

2 files changed

+115
-55
lines changed

2 files changed

+115
-55
lines changed

cloudpub/ms_azure/service.py

Lines changed: 101 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
AzureResource,
1919
ConfigureStatus,
2020
CustomerLeads,
21+
DiskVersion,
2122
Listing,
2223
ListingAsset,
2324
ListingTrailer,
@@ -38,6 +39,7 @@
3839
from cloudpub.ms_azure.session import PartnerPortalSession
3940
from cloudpub.ms_azure.utils import (
4041
AzurePublishingMetadata,
42+
TechnicalConfigLookUpData,
4143
create_disk_version_from_scratch,
4244
is_azure_job_not_complete,
4345
is_sas_present,
@@ -594,6 +596,90 @@ def _publish_live(self, product: Product, product_name: str) -> None:
594596
)
595597
raise RuntimeError(failure_msg)
596598

599+
def _overwrite_disk_version(
600+
self,
601+
metadata: AzurePublishingMetadata,
602+
product_name: str,
603+
plan_name: str,
604+
source: VMImageSource,
605+
) -> TechnicalConfigLookUpData:
606+
product, plan, tgt = self.get_product_plan_by_name(product_name, plan_name)
607+
log.warning(
608+
"Overwriting the plan \"%s\" on \"%s\" with the given image: \"%s\".",
609+
plan_name,
610+
tgt,
611+
metadata.image_path,
612+
)
613+
tech_config = self.get_plan_tech_config(product, plan)
614+
disk_version = create_disk_version_from_scratch(metadata, source)
615+
tech_config.disk_versions = [disk_version]
616+
return {
617+
"metadata": metadata,
618+
"tech_config": tech_config,
619+
"sas_found": False,
620+
"product": product,
621+
"plan": plan,
622+
"target": tgt,
623+
}
624+
625+
def _look_up_sas_on_technical_config(
626+
self, metadata, product_name, plan_name, target
627+
) -> TechnicalConfigLookUpData:
628+
product, plan, tgt = self.get_product_plan_by_name(
629+
product_name, plan_name, first_target=target
630+
)
631+
log.info(
632+
"Retrieving the technical config for \"%s\" on \"%s\".",
633+
metadata.destination,
634+
tgt,
635+
)
636+
tech_config = self.get_plan_tech_config(product, plan)
637+
sas_found = False
638+
639+
if is_sas_present(tech_config, metadata.image_path, metadata.check_base_sas_only):
640+
log.info(
641+
"The destination \"%s\" on \"%s\" already contains the SAS URI: \"%s\".",
642+
metadata.destination,
643+
tgt,
644+
metadata.image_path,
645+
)
646+
sas_found = True
647+
return {
648+
"metadata": metadata,
649+
"tech_config": tech_config,
650+
"sas_found": sas_found,
651+
"product": product,
652+
"plan": plan,
653+
"target": tgt,
654+
}
655+
656+
def _produce_disk_version_if_required(
657+
self,
658+
tech_config_lookup: TechnicalConfigLookUpData,
659+
source: VMImageSource,
660+
disk_version: Optional[DiskVersion],
661+
) -> DiskVersion:
662+
metadata = tech_config_lookup["metadata"]
663+
target = tech_config_lookup["target"]
664+
tech_config = tech_config_lookup["tech_config"]
665+
666+
# Check the images of the selected DiskVersion if it exists
667+
if disk_version:
668+
log.info(
669+
"DiskVersion \"%s\" exists in \"%s\" on \"%s\" for the image \"%s\".",
670+
disk_version.version_number,
671+
metadata.destination,
672+
target,
673+
metadata.image_path,
674+
)
675+
disk_version = set_new_sas_disk_version(disk_version, metadata, source)
676+
return disk_version
677+
# The disk version doesn't exist, we need to create one from scratch
678+
log.info("The DiskVersion doesn't exist, creating one from scratch.")
679+
disk_version = create_disk_version_from_scratch(metadata, source)
680+
tech_config.disk_versions.append(disk_version)
681+
return disk_version
682+
597683
def publish(self, metadata: AzurePublishingMetadata) -> None:
598684
"""
599685
Associate a VM image with a given product listing (destination) and publish it if required.
@@ -608,7 +694,6 @@ def publish(self, metadata: AzurePublishingMetadata) -> None:
608694
product_name = metadata.destination.split("/")[0]
609695
plan_name = metadata.destination.split("/")[-1]
610696
disk_version = None
611-
sas_found = False
612697
log.info(
613698
"Preparing to associate the image \"%s\" with the plan \"%s\" from product \"%s\"",
614699
metadata.image_path,
@@ -626,47 +711,23 @@ def publish(self, metadata: AzurePublishingMetadata) -> None:
626711
# Note: If `overwrite` is True it means we can set this VM image as the only one in the
627712
# plan's technical config and discard all other VM images which may've been present.
628713
if metadata.overwrite is True:
629-
product, plan, tgt = self.get_product_plan_by_name(product_name, plan_name)
630-
log.warning(
631-
"Overwriting the plan \"%s\" on \"%s\" with the given image: \"%s\".",
632-
plan_name,
633-
tgt,
634-
metadata.image_path,
635-
)
636-
tech_config = self.get_plan_tech_config(product, plan)
637-
disk_version = create_disk_version_from_scratch(metadata, source)
638-
tech_config.disk_versions = [disk_version]
714+
res = self._overwrite_disk_version(metadata, product_name, plan_name, source)
715+
tgt = res["target"]
716+
tech_config = res["tech_config"]
717+
disk_version = tech_config.disk_versions[0] # only 1 as it was overwritten
639718
else:
640719
# Otherwise we need to check whether SAS isn't already present
641720
# in any of the targets "preview", "live" or "draft" and if not attach and publish it.
642-
for initial_target in list_all_targets(start_with="preview"):
643-
644-
# 3.1 Retrieve the VM Technical configuration for the given plan
645-
product, plan, tgt = self.get_product_plan_by_name(
646-
product_name, plan_name, first_target=initial_target
721+
for target in list_all_targets(start_with="preview"):
722+
res = self._look_up_sas_on_technical_config(
723+
metadata, product_name, plan_name, target
647724
)
648-
log.info(
649-
"Retrieving the technical config for \"%s\" on \"%s\".",
650-
metadata.destination,
651-
tgt,
652-
)
653-
tech_config = self.get_plan_tech_config(product, plan)
654-
655-
if is_sas_present(tech_config, metadata.image_path, metadata.check_base_sas_only):
656-
log.info(
657-
"The destination \"%s\" on \"%s\" already contains the SAS URI: \"%s\".",
658-
metadata.destination,
659-
tgt,
660-
metadata.image_path,
661-
)
662-
# We don't want to seek for SAS anymore as it was already found
663-
sas_found = True
725+
tech_config = res["tech_config"]
726+
tgt = res["target"]
727+
# We don't want to seek for SAS anymore as it was already found
728+
if res["sas_found"]:
664729
break
665-
else:
666-
# Seek SAS URI until it reaches the last target
667-
continue
668-
669-
if not sas_found:
730+
else:
670731
# At this point there's no SAS URI in any target so we can safely add it
671732

672733
# Here we can have the metadata.disk_version set or empty.
@@ -677,23 +738,8 @@ def publish(self, metadata: AzurePublishingMetadata) -> None:
677738
tgt,
678739
metadata.image_path,
679740
)
680-
disk_version = seek_disk_version(tech_config, metadata.disk_version)
681-
682-
# Check the images of the selected DiskVersion if it exists
683-
if disk_version:
684-
log.info(
685-
"DiskVersion \"%s\" exists in \"%s\" on \"%s\" for the image \"%s\".",
686-
disk_version.version_number,
687-
metadata.destination,
688-
tgt,
689-
metadata.image_path,
690-
)
691-
disk_version = set_new_sas_disk_version(disk_version, metadata, source)
692-
693-
else: # The disk version doesn't exist, we need to create one from scratch
694-
log.info("The DiskVersion doesn't exist, creating one from scratch.")
695-
disk_version = create_disk_version_from_scratch(metadata, source)
696-
tech_config.disk_versions.append(disk_version)
741+
dv = seek_disk_version(tech_config, metadata.disk_version)
742+
disk_version = self._produce_disk_version_if_required(res, source, dv)
697743

698744
# 4. With the updated disk_version we should adjust the SKUs and submit the changes
699745
if disk_version:
@@ -714,6 +760,7 @@ def publish(self, metadata: AzurePublishingMetadata) -> None:
714760
# 5. Proceed to publishing if it was requested.
715761
# Note: The publishing will only occur if it made changes in disk_version.
716762
if not metadata.keepdraft:
763+
product = res["product"]
717764
# Get the submission state
718765
submission: ProductSubmission = cast(
719766
List[ProductSubmission],

cloudpub/ms_azure/utils.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
# SPDX-License-Identifier: GPL-3.0-or-later
22
import logging
33
from operator import attrgetter
4-
from typing import Any, Dict, List, Optional, Tuple
4+
from typing import Any, Dict, List, Optional, Tuple, TypedDict
55

66
from deepdiff import DeepDiff
77

88
from cloudpub.common import PublishingMetadata # Cannot circular import AzurePublishingMetadata
99
from cloudpub.models.ms_azure import (
1010
ConfigureStatus,
1111
DiskVersion,
12+
PlanSummary,
13+
Product,
1214
VMImageDefinition,
1315
VMImageSource,
1416
VMIPlanTechConfig,
@@ -107,6 +109,17 @@ def __validate(self):
107109
raise ValueError(f"Invalid SAS URI \"{self.image_path}\". Expected: http/https URL.")
108110

109111

112+
class TechnicalConfigLookUpData(TypedDict):
113+
"""A typed dict to be used for private methods data exchange."""
114+
115+
metadata: AzurePublishingMetadata
116+
tech_config: VMIPlanTechConfig
117+
sas_found: bool
118+
product: Product
119+
plan: PlanSummary
120+
target: str
121+
122+
110123
def get_image_type_mapping(architecture: str, generation: str) -> str:
111124
"""Return the image type required by VMImageDefinition."""
112125
gen_map = {

0 commit comments

Comments
 (0)