1818import pathlib
1919
2020from . import model , tag , utils , jasyncio
21- from .url import URL
21+ from .url import URL , Schema
2222from .status import derive_status
2323from .annotationhelper import _get_annotations , _set_annotations
2424from .client import client
@@ -642,16 +642,51 @@ async def refresh(
642642 if switch is not None and revision is not None :
643643 raise ValueError ("switch and revision are mutually exclusive" )
644644
645- resources_facade = client .ResourcesFacade .from_connection (
646- self .connection )
645+ resources_facade = client .ResourcesFacade .from_connection (self .connection )
647646 app_facade = self ._facade ()
648647
649648 charm_url = self .data ['charm-url' ]
650- charm_name = URL .parse (charm_url ).name
649+ parsed_url = URL .parse (charm_url )
650+ charm_name = parsed_url .name
651+
652+ # First we need to make sure we have the resources for the charm that's
653+ # coming
654+
655+ # Get the listof resources needed to deploy this charm
656+ if Schema .CHARM_HUB .matches (parsed_url .schema ):
657+ # Charmhub charms
658+ charmhub = self .model .charmhub
659+ charm_resources = await charmhub .list_resources (charm_name )
660+ else :
661+ charms_facade = client .CharmsFacade .from_connection (self .connection )
662+ charmstore = self .model .charmstore
663+ charmstore_entity = None
664+ if switch is not None :
665+ charm_url = switch
666+ if not charm_url .startswith ('cs:' ):
667+ charm_url = 'cs:' + charm_url
668+ else :
669+ charm_url = self .data ['charm-url' ]
670+ charm_url = charm_url .rpartition ('-' )[0 ]
671+ if revision is not None :
672+ charm_url = "%s-%d" % (charm_url , revision )
673+ else :
674+ charmstore_entity = await charmstore .entity (charm_url , channel = channel )
675+ charm_url = charmstore_entity ['Id' ]
676+
677+ if charm_url == self .data ['charm-url' ]:
678+ raise JujuError ('already running charm "%s"' % charm_url )
679+
680+ origin = client .CharmOrigin (source = "charm-store" , risk = channel )
681+ # Update charm
682+ await charms_facade .AddCharm (url = charm_url ,
683+ force = force ,
684+ charm_origin = origin )
685+ if not charmstore_entity :
686+ charmstore_entity = await charmstore .entity (charm_url , channel = channel )
687+ charm_resources = charmstore_entity ['Meta' ]['resources' ]
651688
652689 # Update resources
653- charmhub = self .model .charmhub
654- charmhub_resources = await charmhub .list_resources (charm_name )
655690
656691 request_data = [client .Entity (self .tag )]
657692 response = await resources_facade .ListResources (entities = request_data )
@@ -661,7 +696,7 @@ async def refresh(
661696 }
662697
663698 resources_to_update = [
664- resource for resource in charmhub_resources
699+ resource for resource in charm_resources
665700 if resource ['Name' ] not in existing_resources or
666701 existing_resources [resource ['Name' ]].origin != 'upload'
667702 ]
0 commit comments