Skip to content

Commit cc779d6

Browse files
committed
Add comments for the application.refresh logic
1 parent 5293f00 commit cc779d6

1 file changed

Lines changed: 20 additions & 12 deletions

File tree

juju/application.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,10 @@ async def refresh(
647647
resources_facade = client.ResourcesFacade.from_connection(self.connection)
648648
charms_facade = client.CharmsFacade.from_connection(self.connection)
649649

650+
# 1 - Figure out the destination origin and destination charm_url
651+
# 2 - Then take care of the resources
652+
# 3 - Finally execute the upgrade
653+
650654
# Get the charm URL and charm origin of the given application is running at present.
651655
charm_url_origin_result = await app_facade.GetCharmURLOrigin(application=self.name)
652656
if charm_url_origin_result.error is not None:
@@ -664,6 +668,8 @@ async def refresh(
664668
if revision is not None:
665669
origin.revision = revision
666670

671+
# Make the source-specific changes to the origin/channel/url
672+
# (and also get the resources necessary to deploy the (destination) charm -- for later)
667673
if Schema.CHARM_HUB.matches(parsed_url.schema):
668674
origin.source = 'charm-hub'
669675
if channel:
@@ -675,21 +681,22 @@ async def refresh(
675681
charm_resources = await charmhub.list_resources(charm_name)
676682
else:
677683
charmstore = self.model.charmstore
678-
charmstore_entity = await charmstore.entity(charm_url, channel=channel)
679-
684+
charmstore_entity = None
680685
if switch is None:
681686
charm_url = charm_url.rpartition('-')[0]
682687
if revision is not None:
683688
charm_url = "%s-%d" % (charm_url, revision)
684689
else:
690+
charmstore_entity = await charmstore.entity(charm_url, channel=channel)
685691
charm_url = charmstore_entity['Id']
686692
origin.source = 'charm-store'
687693
if channel:
688694
origin.risk = channel
689-
695+
if charmstore_entity is None:
696+
charmstore_entity = await charmstore.entity(charm_url, channel=channel)
690697
charm_resources = charmstore_entity['Meta']['resources']
691698

692-
# resolve the given charm URLs with an optionally specified preferred channel.
699+
# Resolve the given charm URLs with an optionally specified preferred channel.
693700
# Channel provided via CharmOrigin.
694701
resolved_charm_with_channel_results = await charms_facade.ResolveCharms(
695702
resolve=[client.ResolveCharmWithChannel(
@@ -699,39 +706,40 @@ async def refresh(
699706
)])
700707
resolved_charm = resolved_charm_with_channel_results.results[0]
701708

702-
# Get the destination origin and destination charm_url
703-
# from the resolved charm
709+
# Get the destination origin and destination charm_url from the resolved charm
704710
if resolved_charm.error is not None:
705711
err = resolved_charm.error
706712
raise JujuError(f'{err.code} : {err.message}')
707713
dest_origin = resolved_charm.charm_origin
708714
charm_url = resolved_charm.url
709715

710-
# Then we need to take care of the resources:
711-
# Get the list of resources needed to deploy this charm
712-
713-
# Add the charm with the new origin
716+
# Add the charm with the destination url and origin
714717
charm_origin_result = await charms_facade.AddCharm(url=charm_url,
715718
force=force,
716719
charm_origin=dest_origin)
717720
if charm_origin_result.error is not None:
718721
err = charm_origin_result.error
719722
raise JujuError(f'{err.code} : {err.message}')
720723

721-
# Update resources
724+
# Now take care of the resources:
725+
726+
# Already prepped the charm_resources
727+
# Now get the existing resources from the ResourcesFacade
722728
request_data = [client.Entity(self.tag)]
723729
response = await resources_facade.ListResources(entities=request_data)
724730
existing_resources = {
725731
resource.name: resource
726732
for resource in response.results[0].resources
727733
}
728734

735+
# Compute the difference btw resources needed and the existing resources
729736
resources_to_update = [
730737
resource for resource in charm_resources
731738
if resource.get('Name', resource.get('name')) not in existing_resources or
732739
existing_resources[resource.get('Name', resource.get('name'))].origin != 'upload'
733740
]
734741

742+
# Update the resources
735743
if resources_to_update:
736744
request_data = []
737745
for resource in resources_to_update:
@@ -759,7 +767,7 @@ async def refresh(
759767
else:
760768
resource_ids = None
761769

762-
# Update application
770+
# Update the application
763771
await app_facade.SetCharm(
764772
application=self.entity_id,
765773
charm_url=charm_url,

0 commit comments

Comments
 (0)