Skip to content

Commit 0fa3a5c

Browse files
committed
azure: properly compute targets
This commit creates a new `AzureService` method named `compute_targets` which will only return the existing targets from a given product based on its product ID. In order to find the product ID it also creates a new method named `get_productid` which will return the expected value for the given product. Finally, it updates the `publish` method to use the `compute_targets` method to generate the list of all existing submission targets to lookup for the SAS URI. Refers to SPSTRAT-595 Signed-off-by: Jonathan Gangi <[email protected]>
1 parent 6d9278a commit 0fa3a5c

File tree

4 files changed

+191
-24
lines changed

4 files changed

+191
-24
lines changed

cloudpub/ms_azure/service.py

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
create_disk_version_from_scratch,
4444
is_azure_job_not_complete,
4545
is_sas_present,
46-
list_all_targets,
4746
logdiff,
4847
seek_disk_version,
4948
set_new_sas_disk_version,
@@ -244,6 +243,20 @@ def list_products(self) -> List[ProductSummary]:
244243
self._products = [p for p in self.products]
245244
return self._products
246245

246+
def get_productid(self, product_name: str) -> str:
247+
"""Retrieve the desired product ID for the requested product name.
248+
249+
Args:
250+
product_name (str): the product's name to retrieve its product ID.
251+
Returns:
252+
The requested product ID when found.
253+
Raises NotFoundError when the product was not found.
254+
"""
255+
for product in self.list_products():
256+
if product.identity.name == product_name:
257+
return product.id
258+
raise NotFoundError(f"No such product with name {product_name}")
259+
247260
def get_product(self, product_id: str, target: str) -> Product:
248261
"""
249262
Return the requested Product by its ID.
@@ -553,6 +566,31 @@ def get_modular_resources_to_publish(
553566
)[0]
554567
return [prod_res, plan_res, tech_config]
555568

569+
def compute_targets(self, product_id: str) -> List[str]:
570+
"""List all the possible publishing targets order to seek data from Azure.
571+
572+
It also returns the ordered list of targets with the following precedence:
573+
``preview`` -> ``live`` -> ``draft``
574+
575+
Args:
576+
product_id (str)
577+
The product_id to retrieve all existing submission targets.
578+
579+
Returns:
580+
List[Str]: The ordered list with targets to lookup.
581+
"""
582+
all_targets = ["preview", "live", "draft"]
583+
computed_targets = []
584+
585+
# We cannot simply return all targets above because the existing product might
586+
# lack one of them. So now we need to filter out unexisting targets.
587+
product_submissions = self.get_submissions(product_id)
588+
product_targets = [s.target.targetType for s in product_submissions]
589+
for t in all_targets:
590+
if t in product_targets:
591+
computed_targets.append(t)
592+
return computed_targets
593+
556594
def _is_submission_in_preview(self, current: ProductSubmission) -> bool:
557595
"""Return True if the latest submission state is "preview", False otherwise.
558596
@@ -778,6 +816,7 @@ def publish(self, metadata: AzurePublishingMetadata) -> None:
778816
# "product-name/plan-name"
779817
product_name = metadata.destination.split("/")[0]
780818
plan_name = metadata.destination.split("/")[-1]
819+
product_id = self.get_productid(product_name)
781820
disk_version = None
782821
log.info(
783822
"Preparing to associate the image \"%s\" with the plan \"%s\" from product \"%s\"",
@@ -796,14 +835,14 @@ def publish(self, metadata: AzurePublishingMetadata) -> None:
796835
# Note: If `overwrite` is True it means we can set this VM image as the only one in the
797836
# plan's technical config and discard all other VM images which may've been present.
798837
if metadata.overwrite is True:
799-
target = "draft"
838+
target = "draft" # It's expected to exist for whenever product.
800839
res = self._overwrite_disk_version(metadata, product_name, plan_name, source, target)
801840
tech_config = res["tech_config"]
802841
disk_version = tech_config.disk_versions[0] # only 1 as it was overwritten
803842
else:
804843
# Otherwise we need to check whether SAS isn't already present
805844
# in any of the targets "preview", "live" or "draft" and if not attach and publish it.
806-
for target in list_all_targets():
845+
for target in self.compute_targets(product_id):
807846
res = self._look_up_sas_on_technical_config(
808847
metadata, product_name, plan_name, target
809848
)

cloudpub/ms_azure/utils.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -615,18 +615,3 @@ def logdiff(diff: DeepDiff) -> None:
615615
"""Log the offer diff if it exists."""
616616
if diff:
617617
log.warning("Found the following offer diff before publishing:\n%s", diff.pretty())
618-
619-
620-
def list_all_targets(start_with: str = "preview") -> List[str]:
621-
"""List all the possible publishing targets order to seek data from Azure.
622-
623-
Args:
624-
start_with (str): The first target to lookup into.
625-
Returns:
626-
List[Str]: The ordered list with targets to lookup.
627-
"""
628-
targets = [start_with]
629-
for tgt in ["preview", "live", "draft"]:
630-
if tgt not in targets:
631-
targets.append(tgt)
632-
return targets

tests/ms_azure/conftest.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from copy import deepcopy
12
from typing import Any, Dict, List
23
from unittest import mock
34

@@ -173,6 +174,17 @@ def products_list(product_summary) -> Dict[str, Any]:
173174
return {"value": [product_summary]}
174175

175176

177+
@pytest.fixture
178+
def more_products_list(product_summary) -> Dict[str, Any]:
179+
values = []
180+
for i in range(5):
181+
copy_dict = deepcopy(product_summary)
182+
copy_dict['id'] = f"product/{i + 1}"
183+
copy_dict['identity'] = {"externalId": f"product-{i + 1}"}
184+
values.append(copy_dict)
185+
return {"value": values}
186+
187+
176188
@pytest.fixture
177189
def customer_leads() -> Dict[str, str]:
178190
return {

0 commit comments

Comments
 (0)