Skip to content

Commit cc2f8d5

Browse files
committed
Azure: check for SAS in all targets
This commit refactors the `AzureService.publish` to look up for the existing SAS uri in all targets (`preview`, `live`, `draft`) before assuming it's not present and making changes. It keeps the behavior of `overwrite` which always adds the new SAS URI to the plan, regardless of finding it or not. Refers to SPSTRAT-595 Signed-off-by: Jonathan Gangi <[email protected]>
1 parent 2b2b61b commit cc2f8d5

File tree

3 files changed

+180
-79
lines changed

3 files changed

+180
-79
lines changed

cloudpub/ms_azure/service.py

Lines changed: 69 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
create_disk_version_from_scratch,
4242
is_azure_job_not_complete,
4343
is_sas_present,
44+
list_all_targets,
4445
logdiff,
4546
seek_disk_version,
4647
set_new_sas_disk_version,
@@ -258,10 +259,7 @@ def get_product(self, product_id: str, first_target: str = "preview") -> Product
258259
Returns:
259260
Product: the requested product
260261
"""
261-
targets = [first_target]
262-
for tgt in ["preview", "draft", "live"]:
263-
if tgt not in targets:
264-
targets.append(tgt)
262+
targets = list_all_targets(first_target)
265263

266264
for t in targets:
267265
log.info("Requesting the product ID \"%s\" with state \"%s\".", product_id, t)
@@ -377,20 +375,24 @@ def get_plan_by_name(self, product: Product, plan_name: str) -> PlanSummary:
377375
self._raise_error(NotFoundError, f"No such plan with name \"{plan_name}\"")
378376

379377
def get_product_plan_by_name(
380-
self, product_name: str, plan_name: str
378+
self,
379+
product_name: str,
380+
plan_name: str,
381+
first_target: str = "preview",
381382
) -> Tuple[Product, PlanSummary, str]:
382383
"""Return a tuple with the desired Product and Plan after iterating over all targets.
383384
384385
Args:
385386
product_name (str): The name of the product to search for
386387
plan_name (str): The name of the plan to search for
387-
388+
first_target (str, optional)
389+
The first target to lookup into. Defaults to ``preview``.
388390
Returns:
389391
Tuple[Product, PlanSummary, str]: The Product, PlanSummary and target when fonud
390392
Raises:
391393
NotFoundError whenever all targets are exhausted and no information was found
392394
"""
393-
targets = ["preview", "draft", "live"]
395+
targets = list_all_targets(first_target)
394396

395397
for tgt in targets:
396398
try:
@@ -605,71 +607,93 @@ def publish(self, metadata: AzurePublishingMetadata) -> None:
605607
# "product-name/plan-name"
606608
product_name = metadata.destination.split("/")[0]
607609
plan_name = metadata.destination.split("/")[-1]
608-
product, plan, tgt = self.get_product_plan_by_name(product_name, plan_name)
610+
disk_version = None
611+
sas_found = False
609612
log.info(
610-
"Preparing to associate the image \"%s\" with the plan \"%s\" from product \"%s\" on \"%s\"", # noqa: E501
613+
"Preparing to associate the image \"%s\" with the plan \"%s\" from product \"%s\"",
611614
metadata.image_path,
612615
plan_name,
613616
product_name,
614-
tgt,
615617
)
616618

617-
# 2. Retrieve the VM Technical configuration for the given plan
618-
log.info("Retrieving the technical config for \"%s\" on \"%s\".", metadata.destination, tgt)
619-
tech_config = self.get_plan_tech_config(product, plan)
620-
621-
# 3. Prepare the Disk Version
619+
# 2. Prepare the Disk Version
622620
log.info("Creating the VMImageResource with SAS for image: \"%s\"", metadata.image_path)
623621
sas = OSDiskURI(uri=metadata.image_path)
624622
source = VMImageSource(source_type="sasUri", os_disk=sas.to_json(), data_disks=[])
625623

624+
# 3. Set the new Disk Version into the product/plan if required
625+
#
626626
# Note: If `overwrite` is True it means we can set this VM image as the only one in the
627627
# plan's technical config and discard all other VM images which may've been present.
628-
disk_version = None # just to make mypy happy
629628
if metadata.overwrite is True:
629+
product, plan, tgt = self.get_product_plan_by_name(product_name, plan_name)
630630
log.warning(
631631
"Overwriting the plan \"%s\" on \"%s\" with the given image: \"%s\".",
632632
plan_name,
633633
tgt,
634634
metadata.image_path,
635635
)
636+
tech_config = self.get_plan_tech_config(product, plan)
636637
disk_version = create_disk_version_from_scratch(metadata, source)
637638
tech_config.disk_versions = [disk_version]
639+
else:
640+
# Otherwise we need to check whether SAS isn't already present
641+
# 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"):
638643

639-
# We just want to append a new image if the SAS is not already present.
640-
elif not is_sas_present(tech_config, metadata.image_path, metadata.check_base_sas_only):
641-
# Here we can have the metadata.disk_version set or empty.
642-
# When set we want to get the existing disk_version which matches its value.
643-
log.info(
644-
"Scanning the disk versions from \"%s\" on \"%s\" for the image \"%s\"",
645-
metadata.destination,
646-
tgt,
647-
metadata.image_path,
648-
)
649-
disk_version = seek_disk_version(tech_config, metadata.disk_version)
650-
651-
# Check the images of the selected DiskVersion if it exists
652-
if disk_version:
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
647+
)
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
664+
break
665+
else:
666+
# Seek SAS URI until it reaches the last target
667+
continue
668+
669+
if not sas_found:
670+
# At this point there's no SAS URI in any target so we can safely add it
671+
672+
# Here we can have the metadata.disk_version set or empty.
673+
# When set we want to get the existing disk_version which matches its value.
653674
log.info(
654-
"DiskVersion \"%s\" exists in \"%s\" on \"%s\" for the image \"%s\".",
655-
disk_version.version_number,
675+
"Scanning the disk versions from \"%s\" on \"%s\" for the image \"%s\"",
656676
metadata.destination,
657677
tgt,
658678
metadata.image_path,
659679
)
660-
disk_version = set_new_sas_disk_version(disk_version, metadata, source)
661-
662-
else: # The disk version doesn't exist, we need to create one from scratch
663-
log.info("The DiskVersion doesn't exist, creating one from scratch.")
664-
disk_version = create_disk_version_from_scratch(metadata, source)
665-
tech_config.disk_versions.append(disk_version)
666-
else:
667-
log.info(
668-
"The destination \"%s\" on \"%s\" already contains the SAS URI: \"%s\".",
669-
metadata.destination,
670-
tgt,
671-
metadata.image_path,
672-
)
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)
673697

674698
# 4. With the updated disk_version we should adjust the SKUs and submit the changes
675699
if disk_version:

cloudpub/ms_azure/utils.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,3 +596,18 @@ def logdiff(diff: DeepDiff) -> None:
596596
"""Log the offer diff if it exists."""
597597
if diff:
598598
log.warning("Found the following offer diff before publishing:\n%s", diff.pretty())
599+
600+
601+
def list_all_targets(start_with: str = "preview") -> List[str]:
602+
"""List all the possible publishing targets order to seek data from Azure.
603+
604+
Args:
605+
start_with (str): The first target to lookup into.
606+
Returns:
607+
List[Str]: The ordered list with targets to lookup.
608+
"""
609+
targets = [start_with]
610+
for tgt in ["preview", "live", "draft"]:
611+
if tgt not in targets:
612+
targets.append(tgt)
613+
return targets

0 commit comments

Comments
 (0)