Skip to content

Commit 332bac9

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 42d22db commit 332bac9

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(start_with=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(start_with=first_target)
394396

395397
for tgt in targets:
396398
try:
@@ -664,71 +666,93 @@ def publish(self, metadata: AzurePublishingMetadata) -> None:
664666
# "product-name/plan-name"
665667
product_name = metadata.destination.split("/")[0]
666668
plan_name = metadata.destination.split("/")[-1]
667-
product, plan, tgt = self.get_product_plan_by_name(product_name, plan_name)
669+
disk_version = None
670+
sas_found = False
668671
log.info(
669-
"Preparing to associate the image \"%s\" with the plan \"%s\" from product \"%s\" on \"%s\"", # noqa: E501
672+
"Preparing to associate the image \"%s\" with the plan \"%s\" from product \"%s\"",
670673
metadata.image_path,
671674
plan_name,
672675
product_name,
673-
tgt,
674676
)
675677

676-
# 2. Retrieve the VM Technical configuration for the given plan
677-
log.info("Retrieving the technical config for \"%s\" on \"%s\".", metadata.destination, tgt)
678-
tech_config = self.get_plan_tech_config(product, plan)
679-
680-
# 3. Prepare the Disk Version
678+
# 2. Prepare the Disk Version
681679
log.info("Creating the VMImageResource with SAS for image: \"%s\"", metadata.image_path)
682680
sas = OSDiskURI(uri=metadata.image_path)
683681
source = VMImageSource(source_type="sasUri", os_disk=sas.to_json(), data_disks=[])
684682

683+
# 3. Set the new Disk Version into the product/plan if required
684+
#
685685
# Note: If `overwrite` is True it means we can set this VM image as the only one in the
686686
# plan's technical config and discard all other VM images which may've been present.
687-
disk_version = None # just to make mypy happy
688687
if metadata.overwrite is True:
688+
product, plan, tgt = self.get_product_plan_by_name(product_name, plan_name)
689689
log.warning(
690690
"Overwriting the plan \"%s\" on \"%s\" with the given image: \"%s\".",
691691
plan_name,
692692
tgt,
693693
metadata.image_path,
694694
)
695+
tech_config = self.get_plan_tech_config(product, plan)
695696
disk_version = create_disk_version_from_scratch(metadata, source)
696697
tech_config.disk_versions = [disk_version]
698+
else:
699+
# Otherwise we need to check whether SAS isn't already present
700+
# in any of the targets "preview", "live" or "draft" and if not attach and publish it.
701+
for initial_target in list_all_targets(start_with="preview"):
697702

698-
# We just want to append a new image if the SAS is not already present.
699-
elif not is_sas_present(tech_config, metadata.image_path, metadata.check_base_sas_only):
700-
# Here we can have the metadata.disk_version set or empty.
701-
# When set we want to get the existing disk_version which matches its value.
702-
log.info(
703-
"Scanning the disk versions from \"%s\" on \"%s\" for the image \"%s\"",
704-
metadata.destination,
705-
tgt,
706-
metadata.image_path,
707-
)
708-
disk_version = seek_disk_version(tech_config, metadata.disk_version)
709-
710-
# Check the images of the selected DiskVersion if it exists
711-
if disk_version:
703+
# 3.1 Retrieve the VM Technical configuration for the given plan
704+
product, plan, tgt = self.get_product_plan_by_name(
705+
product_name, plan_name, first_target=initial_target
706+
)
707+
log.info(
708+
"Retrieving the technical config for \"%s\" on \"%s\".",
709+
metadata.destination,
710+
tgt,
711+
)
712+
tech_config = self.get_plan_tech_config(product, plan)
713+
714+
if is_sas_present(tech_config, metadata.image_path, metadata.check_base_sas_only):
715+
log.info(
716+
"The destination \"%s\" on \"%s\" already contains the SAS URI: \"%s\".",
717+
metadata.destination,
718+
tgt,
719+
metadata.image_path,
720+
)
721+
# We don't want to seek for SAS anymore as it was already found
722+
sas_found = True
723+
break
724+
else:
725+
# Seek SAS URI until it reaches the last target
726+
continue
727+
728+
if not sas_found:
729+
# At this point there's no SAS URI in any target so we can safely add it
730+
731+
# Here we can have the metadata.disk_version set or empty.
732+
# When set we want to get the existing disk_version which matches its value.
712733
log.info(
713-
"DiskVersion \"%s\" exists in \"%s\" on \"%s\" for the image \"%s\".",
714-
disk_version.version_number,
734+
"Scanning the disk versions from \"%s\" on \"%s\" for the image \"%s\"",
715735
metadata.destination,
716736
tgt,
717737
metadata.image_path,
718738
)
719-
disk_version = set_new_sas_disk_version(disk_version, metadata, source)
720-
721-
else: # The disk version doesn't exist, we need to create one from scratch
722-
log.info("The DiskVersion doesn't exist, creating one from scratch.")
723-
disk_version = create_disk_version_from_scratch(metadata, source)
724-
tech_config.disk_versions.append(disk_version)
725-
else:
726-
log.info(
727-
"The destination \"%s\" on \"%s\" already contains the SAS URI: \"%s\".",
728-
metadata.destination,
729-
tgt,
730-
metadata.image_path,
731-
)
739+
disk_version = seek_disk_version(tech_config, metadata.disk_version)
740+
741+
# Check the images of the selected DiskVersion if it exists
742+
if disk_version:
743+
log.info(
744+
"DiskVersion \"%s\" exists in \"%s\" on \"%s\" for the image \"%s\".",
745+
disk_version.version_number,
746+
metadata.destination,
747+
tgt,
748+
metadata.image_path,
749+
)
750+
disk_version = set_new_sas_disk_version(disk_version, metadata, source)
751+
752+
else: # The disk version doesn't exist, we need to create one from scratch
753+
log.info("The DiskVersion doesn't exist, creating one from scratch.")
754+
disk_version = create_disk_version_from_scratch(metadata, source)
755+
tech_config.disk_versions.append(disk_version)
732756

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

cloudpub/ms_azure/utils.py

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

0 commit comments

Comments
 (0)