Skip to content

Commit 5293f00

Browse files
committed
Merge the refresh logic for CharmHub and CharmStore
1 parent 2a97c0e commit 5293f00

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
@@ -643,79 +643,72 @@ async def refresh(
643643
if switch is not None and revision is not None:
644644
raise ValueError("switch and revision are mutually exclusive")
645645

646+
app_facade = self._facade()
646647
resources_facade = client.ResourcesFacade.from_connection(self.connection)
648+
charms_facade = client.CharmsFacade.from_connection(self.connection)
647649

648-
app_facade = self._facade()
650+
# Get the charm URL and charm origin of the given application is running at present.
651+
charm_url_origin_result = await app_facade.GetCharmURLOrigin(application=self.name)
652+
if charm_url_origin_result.error is not None:
653+
err = charm_url_origin_result.error
654+
raise JujuError(f'{err.code} : {err.message}')
655+
charm_url = switch or charm_url_origin_result.url
656+
origin = charm_url_origin_result.charm_origin
649657

650-
url = switch or self.data['charm-url']
651-
parsed_url = URL.parse(url)
658+
parsed_url = URL.parse(charm_url)
652659
charm_name = parsed_url.name
653660

654-
# First we need to make sure we have the resources for the charm that's
655-
# coming
656-
657661
if parsed_url.schema is None:
658662
raise JujuError(f'A ch: or cs: schema is required for application refresh, given : {str(parsed_url)}')
659663

660-
# Get the list of resources needed to deploy this charm
661-
if Schema.CHARM_HUB.matches(parsed_url.schema):
662-
# Charmhub charms
663-
charmhub = self.model.charmhub
664-
charm_resources = await charmhub.list_resources(charm_name)
665-
666-
charm_url_origin_result = await app_facade.GetCharmURLOrigin(application=self.name)
664+
if revision is not None:
665+
origin.revision = revision
667666

668-
if charm_url_origin_result.error is not None:
669-
err = charm_url_origin_result.error
670-
raise JujuError(f'{err.code} : {err.message}')
671-
charm_url = switch or charm_url_origin_result.url
672-
origin = charm_url_origin_result.charm_origin
667+
if Schema.CHARM_HUB.matches(parsed_url.schema):
673668
origin.source = 'charm-hub'
674-
675669
if channel:
676670
ch = Channel.parse(channel).normalize()
677671
origin.risk = ch.risk
678672
origin.track = ch.track
679673

680-
charms_facade = client.CharmsFacade.from_connection(self.connection)
681-
resolved_charm_with_channel_results = await charms_facade.ResolveCharms(resolve=[client.ResolveCharmWithChannel(
682-
charm_origin=origin,
683-
switch_charm=True if switch else False, # rpc expects boolean type
684-
reference=charm_url,
685-
)])
686-
resolved_charm = resolved_charm_with_channel_results.results[0]
687-
688-
if resolved_charm.error is not None:
689-
err = resolved_charm.error
690-
raise JujuError(f'{err.code} : {err.message}')
691-
dest_origin = resolved_charm.charm_origin
692-
charm_url = resolved_charm.url
693-
674+
charmhub = self.model.charmhub
675+
charm_resources = await charmhub.list_resources(charm_name)
694676
else:
695-
charms_facade = client.CharmsFacade.from_connection(self.connection)
696677
charmstore = self.model.charmstore
697-
charmstore_entity = None
698-
if switch is not None:
699-
charm_url = switch
700-
if not charm_url.startswith('cs:'):
701-
charm_url = 'cs:' + charm_url
702-
else:
703-
charm_url = self.data['charm-url']
678+
charmstore_entity = await charmstore.entity(charm_url, channel=channel)
679+
680+
if switch is None:
704681
charm_url = charm_url.rpartition('-')[0]
705682
if revision is not None:
706683
charm_url = "%s-%d" % (charm_url, revision)
707684
else:
708-
charmstore_entity = await charmstore.entity(charm_url, channel=channel)
709685
charm_url = charmstore_entity['Id']
686+
origin.source = 'charm-store'
687+
if channel:
688+
origin.risk = channel
710689

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

714-
dest_origin = client.CharmOrigin(source="charm-store", risk=channel)
692+
# resolve the given charm URLs with an optionally specified preferred channel.
693+
# Channel provided via CharmOrigin.
694+
resolved_charm_with_channel_results = await charms_facade.ResolveCharms(
695+
resolve=[client.ResolveCharmWithChannel(
696+
charm_origin=origin,
697+
switch_charm=True if switch else False, # rpc expects boolean type
698+
reference=charm_url,
699+
)])
700+
resolved_charm = resolved_charm_with_channel_results.results[0]
715701

716-
if not charmstore_entity:
717-
charmstore_entity = await charmstore.entity(charm_url, channel=channel)
718-
charm_resources = charmstore_entity['Meta']['resources']
702+
# Get the destination origin and destination charm_url
703+
# from the resolved charm
704+
if resolved_charm.error is not None:
705+
err = resolved_charm.error
706+
raise JujuError(f'{err.code} : {err.message}')
707+
dest_origin = resolved_charm.charm_origin
708+
charm_url = resolved_charm.url
709+
710+
# Then we need to take care of the resources:
711+
# Get the list of resources needed to deploy this charm
719712

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

0 commit comments

Comments
 (0)