Skip to content

Commit 5caf36f

Browse files
committed
Merge the refresh logic for CharmHub and CharmStore
1 parent d04ed93 commit 5caf36f

1 file changed

Lines changed: 41 additions & 48 deletions

File tree

juju/application.py

Lines changed: 41 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -621,79 +621,72 @@ async def refresh(
621621
if switch is not None and revision is not None:
622622
raise ValueError("switch and revision are mutually exclusive")
623623

624+
app_facade = self._facade()
624625
resources_facade = client.ResourcesFacade.from_connection(self.connection)
626+
charms_facade = client.CharmsFacade.from_connection(self.connection)
625627

626-
app_facade = self._facade()
628+
# Get the charm URL and charm origin of the given application is running at present.
629+
charm_url_origin_result = await app_facade.GetCharmURLOrigin(application=self.name)
630+
if charm_url_origin_result.error is not None:
631+
err = charm_url_origin_result.error
632+
raise JujuError(f'{err.code} : {err.message}')
633+
charm_url = switch or charm_url_origin_result.url
634+
origin = charm_url_origin_result.charm_origin
627635

628-
url = switch or self.data['charm-url']
629-
parsed_url = URL.parse(url)
636+
parsed_url = URL.parse(charm_url)
630637
charm_name = parsed_url.name
631638

632-
# First we need to make sure we have the resources for the charm that's
633-
# coming
634-
635639
if parsed_url.schema is None:
636640
raise JujuError(f'A ch: or cs: schema is required for application refresh, given : {str(parsed_url)}')
637641

638-
# Get the list of resources needed to deploy this charm
639-
if Schema.CHARM_HUB.matches(parsed_url.schema):
640-
# Charmhub charms
641-
charmhub = self.model.charmhub
642-
charm_resources = await charmhub.list_resources(charm_name)
643-
644-
charm_url_origin_result = await app_facade.GetCharmURLOrigin(application=self.name)
642+
if revision is not None:
643+
origin.revision = revision
645644

646-
if charm_url_origin_result.error is not None:
647-
err = charm_url_origin_result.error
648-
raise JujuError(f'{err.code} : {err.message}')
649-
charm_url = switch or charm_url_origin_result.url
650-
origin = charm_url_origin_result.charm_origin
645+
if Schema.CHARM_HUB.matches(parsed_url.schema):
651646
origin.source = 'charm-hub'
652-
653647
if channel:
654648
ch = Channel.parse(channel).normalize()
655649
origin.risk = ch.risk
656650
origin.track = ch.track
657651

658-
charms_facade = client.CharmsFacade.from_connection(self.connection)
659-
resolved_charm_with_channel_results = await charms_facade.ResolveCharms(resolve=[client.ResolveCharmWithChannel(
660-
charm_origin=origin,
661-
switch_charm=True if switch else False, # rpc expects boolean type
662-
reference=charm_url,
663-
)])
664-
resolved_charm = resolved_charm_with_channel_results.results[0]
665-
666-
if resolved_charm.error is not None:
667-
err = resolved_charm.error
668-
raise JujuError(f'{err.code} : {err.message}')
669-
dest_origin = resolved_charm.charm_origin
670-
charm_url = resolved_charm.url
671-
652+
charmhub = self.model.charmhub
653+
charm_resources = await charmhub.list_resources(charm_name)
672654
else:
673-
charms_facade = client.CharmsFacade.from_connection(self.connection)
674655
charmstore = self.model.charmstore
675-
charmstore_entity = None
676-
if switch is not None:
677-
charm_url = switch
678-
if not charm_url.startswith('cs:'):
679-
charm_url = 'cs:' + charm_url
680-
else:
681-
charm_url = self.data['charm-url']
656+
charmstore_entity = await charmstore.entity(charm_url, channel=channel)
657+
658+
if switch is None:
682659
charm_url = charm_url.rpartition('-')[0]
683660
if revision is not None:
684661
charm_url = "%s-%d" % (charm_url, revision)
685662
else:
686-
charmstore_entity = await charmstore.entity(charm_url, channel=channel)
687663
charm_url = charmstore_entity['Id']
664+
origin.source = 'charm-store'
665+
if channel:
666+
origin.risk = channel
688667

689-
if charm_url == self.data['charm-url']:
690-
raise JujuError('already running charm "%s"' % charm_url)
668+
charm_resources = charmstore_entity['Meta']['resources']
691669

692-
dest_origin = client.CharmOrigin(source="charm-store", risk=channel)
670+
# resolve the given charm URLs with an optionally specified preferred channel.
671+
# Channel provided via CharmOrigin.
672+
resolved_charm_with_channel_results = await charms_facade.ResolveCharms(
673+
resolve=[client.ResolveCharmWithChannel(
674+
charm_origin=origin,
675+
switch_charm=True if switch else False, # rpc expects boolean type
676+
reference=charm_url,
677+
)])
678+
resolved_charm = resolved_charm_with_channel_results.results[0]
693679

694-
if not charmstore_entity:
695-
charmstore_entity = await charmstore.entity(charm_url, channel=channel)
696-
charm_resources = charmstore_entity['Meta']['resources']
680+
# Get the destination origin and destination charm_url
681+
# from the resolved charm
682+
if resolved_charm.error is not None:
683+
err = resolved_charm.error
684+
raise JujuError(f'{err.code} : {err.message}')
685+
dest_origin = resolved_charm.charm_origin
686+
charm_url = resolved_charm.url
687+
688+
# Then we need to take care of the resources:
689+
# Get the list of resources needed to deploy this charm
697690

698691
# Add the charm with the new origin
699692
charm_origin_result = await charms_facade.AddCharm(url=charm_url,

0 commit comments

Comments
 (0)