Skip to content

Commit 02ee3ca

Browse files
committed
Fix application.refresh to point to the correct charm origin
1 parent 0d1e0e7 commit 02ee3ca

1 file changed

Lines changed: 44 additions & 17 deletions

File tree

juju/application.py

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from .errors import JujuError, JujuApplicationConfigError
2626
from .bundle import get_charm_series
2727
from .placement import parse as parse_placement
28+
from .origin import Channel
2829

2930
log = logging.getLogger(__name__)
3031

@@ -621,6 +622,7 @@ async def refresh(
621622
raise ValueError("switch and revision are mutually exclusive")
622623

623624
resources_facade = client.ResourcesFacade.from_connection(self.connection)
625+
624626
app_facade = self._facade()
625627

626628
charmstore = self.model.charmstore
@@ -661,6 +663,30 @@ async def refresh(
661663
# Charmhub charms
662664
charmhub = self.model.charmhub
663665
charm_resources = await charmhub.list_resources(charm_name)
666+
667+
res = await app_facade.GetCharmURLOrigin(application=charm_name)
668+
charm_url = res.url
669+
origin = res.charm_origin
670+
671+
if channel:
672+
ch = Channel.parse(channel).normalize()
673+
origin.risk = ch.risk
674+
origin.track = ch.track
675+
676+
charms_facade = client.CharmsFacade.from_connection(self.connection)
677+
resolved_charm = await charms_facade.ResolveCharms(resolve=[client.ResolveCharmWithChannel(
678+
charm_origin=origin,
679+
switch_charm=False,
680+
reference=charm_url,
681+
)])
682+
# TODO: error check here
683+
dest_origin = resolved_charm.results[0].charm_origin
684+
charm_url = resolved_charm.results[0].url
685+
686+
await charms_facade.AddCharm(url=charm_url,
687+
force=force,
688+
charm_origin=dest_origin)
689+
664690
else:
665691
charms_facade = client.CharmsFacade.from_connection(self.connection)
666692
charmstore = self.model.charmstore
@@ -691,7 +717,6 @@ async def refresh(
691717
charm_resources = charmstore_entity['Meta']['resources']
692718

693719
# Update resources
694-
695720
request_data = [client.Entity(self.tag)]
696721
response = await resources_facade.ListResources(entities=request_data)
697722
existing_resources = {
@@ -701,31 +726,33 @@ async def refresh(
701726

702727
resources_to_update = [
703728
resource for resource in charm_resources
704-
if resource['Name'] not in existing_resources or
705-
existing_resources[resource['Name']].origin != 'upload'
729+
if resource.get('Name', resource.get('name')) not in existing_resources or
730+
existing_resources[resource.get('Name', resource.get('name'))].origin != 'upload'
706731
]
707732

708733
if resources_to_update:
709-
request_data = [
710-
client.CharmResource(
711-
description=resource.get('Description'),
712-
fingerprint=resource['Fingerprint'],
713-
name=resource['Name'],
714-
path=resource['Path'],
715-
revision=resource['Revision'],
716-
size=resource['Size'],
717-
type_=resource['Type'],
734+
request_data = []
735+
for resource in resources_to_update:
736+
request_data.append(client.CharmResource(
737+
description=resource.get('Description', resource.get('description')),
738+
fingerprint=resource.get('Fingerprint', resource.get('fingerprint')),
739+
name=resource.get('Name', resource.get('name')),
740+
path=resource.get('Path', resource.get('filename')),
741+
# revision=-1,
742+
revision=resource.get('Revision', resource.get('revision', -1)),
743+
size=resource.get('Size', resource.get('size')),
744+
type_=resource.get('Type', resource.get('type')),
718745
origin='store',
719-
) for resource in resources_to_update
720-
]
746+
))
721747
response = await resources_facade.AddPendingResources(
722748
application_tag=self.tag,
723749
charm_url=charm_url,
724-
resources=request_data
750+
resources=request_data,
751+
charm_origin=dest_origin,
725752
)
726753
pending_ids = response.pending_ids
727754
resource_ids = {
728-
resource['Name']: id
755+
resource.get('Name', resource.get('name')): id
729756
for resource, id in zip(resources_to_update, pending_ids)
730757
}
731758
else:
@@ -734,8 +761,8 @@ async def refresh(
734761
# Update application
735762
await app_facade.SetCharm(
736763
application=self.entity_id,
737-
channel=channel,
738764
charm_url=charm_url,
765+
charm_origin=dest_origin,
739766
config_settings=None,
740767
config_settings_yaml=None,
741768
force=force,

0 commit comments

Comments
 (0)