@@ -625,6 +625,10 @@ async def refresh(
625625 resources_facade = client .ResourcesFacade .from_connection (self .connection )
626626 charms_facade = client .CharmsFacade .from_connection (self .connection )
627627
628+ # 1 - Figure out the destination origin and destination charm_url
629+ # 2 - Then take care of the resources
630+ # 3 - Finally execute the upgrade
631+
628632 # Get the charm URL and charm origin of the given application is running at present.
629633 charm_url_origin_result = await app_facade .GetCharmURLOrigin (application = self .name )
630634 if charm_url_origin_result .error is not None :
@@ -642,6 +646,8 @@ async def refresh(
642646 if revision is not None :
643647 origin .revision = revision
644648
649+ # Make the source-specific changes to the origin/channel/url
650+ # (and also get the resources necessary to deploy the (destination) charm -- for later)
645651 if Schema .CHARM_HUB .matches (parsed_url .schema ):
646652 origin .source = 'charm-hub'
647653 if channel :
@@ -653,21 +659,22 @@ async def refresh(
653659 charm_resources = await charmhub .list_resources (charm_name )
654660 else :
655661 charmstore = self .model .charmstore
656- charmstore_entity = await charmstore .entity (charm_url , channel = channel )
657-
662+ charmstore_entity = None
658663 if switch is None :
659664 charm_url = charm_url .rpartition ('-' )[0 ]
660665 if revision is not None :
661666 charm_url = "%s-%d" % (charm_url , revision )
662667 else :
668+ charmstore_entity = await charmstore .entity (charm_url , channel = channel )
663669 charm_url = charmstore_entity ['Id' ]
664670 origin .source = 'charm-store'
665671 if channel :
666672 origin .risk = channel
667-
673+ if charmstore_entity is None :
674+ charmstore_entity = await charmstore .entity (charm_url , channel = channel )
668675 charm_resources = charmstore_entity ['Meta' ]['resources' ]
669676
670- # resolve the given charm URLs with an optionally specified preferred channel.
677+ # Resolve the given charm URLs with an optionally specified preferred channel.
671678 # Channel provided via CharmOrigin.
672679 resolved_charm_with_channel_results = await charms_facade .ResolveCharms (
673680 resolve = [client .ResolveCharmWithChannel (
@@ -677,39 +684,40 @@ async def refresh(
677684 )])
678685 resolved_charm = resolved_charm_with_channel_results .results [0 ]
679686
680- # Get the destination origin and destination charm_url
681- # from the resolved charm
687+ # Get the destination origin and destination charm_url from the resolved charm
682688 if resolved_charm .error is not None :
683689 err = resolved_charm .error
684690 raise JujuError (f'{ err .code } : { err .message } ' )
685691 dest_origin = resolved_charm .charm_origin
686692 charm_url = resolved_charm .url
687693
688- # Then we need to take care of the resources:
689- # Get the list of resources needed to deploy this charm
690-
691- # Add the charm with the new origin
694+ # Add the charm with the destination url and origin
692695 charm_origin_result = await charms_facade .AddCharm (url = charm_url ,
693696 force = force ,
694697 charm_origin = dest_origin )
695698 if charm_origin_result .error is not None :
696699 err = charm_origin_result .error
697700 raise JujuError (f'{ err .code } : { err .message } ' )
698701
699- # Update resources
702+ # Now take care of the resources:
703+
704+ # Already prepped the charm_resources
705+ # Now get the existing resources from the ResourcesFacade
700706 request_data = [client .Entity (self .tag )]
701707 response = await resources_facade .ListResources (entities = request_data )
702708 existing_resources = {
703709 resource .name : resource
704710 for resource in response .results [0 ].resources
705711 }
706712
713+ # Compute the difference btw resources needed and the existing resources
707714 resources_to_update = [
708715 resource for resource in charm_resources
709716 if resource .get ('Name' , resource .get ('name' )) not in existing_resources or
710717 existing_resources [resource .get ('Name' , resource .get ('name' ))].origin != 'upload'
711718 ]
712719
720+ # Update the resources
713721 if resources_to_update :
714722 request_data = []
715723 for resource in resources_to_update :
@@ -737,7 +745,7 @@ async def refresh(
737745 else :
738746 resource_ids = None
739747
740- # Update application
748+ # Update the application
741749 await app_facade .SetCharm (
742750 application = self .entity_id ,
743751 charm_url = charm_url ,
0 commit comments