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 )
0 commit comments