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
@@ -620,8 +620,7 @@ async def refresh(
620620 if switch is not None and revision is not None :
621621 raise ValueError ("switch and revision are mutually exclusive" )
622622
623- resources_facade = client .ResourcesFacade .from_connection (
624- self .connection )
623+ resources_facade = client .ResourcesFacade .from_connection (self .connection )
625624 app_facade = self ._facade ()
626625
627626 charmstore = self .model .charmstore
@@ -651,11 +650,47 @@ async def refresh(
651650 channel = channel
652651 )
653652 charm_url = self .data ['charm-url' ]
654- charm_name = URL .parse (charm_url ).name
653+ parsed_url = URL .parse (charm_url )
654+ charm_name = parsed_url .name
655+
656+ # First we need to make sure we have the resources for the charm that's
657+ # coming
658+
659+ # Get the listof resources needed to deploy this charm
660+ if Schema .CHARM_HUB .matches (parsed_url .schema ):
661+ # Charmhub charms
662+ charmhub = self .model .charmhub
663+ charm_resources = await charmhub .list_resources (charm_name )
664+ else :
665+ charms_facade = client .CharmsFacade .from_connection (self .connection )
666+ charmstore = self .model .charmstore
667+ charmstore_entity = None
668+ if switch is not None :
669+ charm_url = switch
670+ if not charm_url .startswith ('cs:' ):
671+ charm_url = 'cs:' + charm_url
672+ else :
673+ charm_url = self .data ['charm-url' ]
674+ charm_url = charm_url .rpartition ('-' )[0 ]
675+ if revision is not None :
676+ charm_url = "%s-%d" % (charm_url , revision )
677+ else :
678+ charmstore_entity = await charmstore .entity (charm_url , channel = channel )
679+ charm_url = charmstore_entity ['Id' ]
680+
681+ if charm_url == self .data ['charm-url' ]:
682+ raise JujuError ('already running charm "%s"' % charm_url )
683+
684+ origin = client .CharmOrigin (source = "charm-store" , risk = channel )
685+ # Update charm
686+ await charms_facade .AddCharm (url = charm_url ,
687+ force = force ,
688+ charm_origin = origin )
689+ if not charmstore_entity :
690+ charmstore_entity = await charmstore .entity (charm_url , channel = channel )
691+ charm_resources = charmstore_entity ['Meta' ]['resources' ]
655692
656693 # Update resources
657- charmhub = self .model .charmhub
658- charmhub_resources = await charmhub .list_resources (charm_name )
659694
660695 request_data = [client .Entity (self .tag )]
661696 response = await resources_facade .ListResources (entities = request_data )
@@ -665,7 +700,7 @@ async def refresh(
665700 }
666701
667702 resources_to_update = [
668- resource for resource in charmhub_resources
703+ resource for resource in charm_resources
669704 if resource ['Name' ] not in existing_resources or
670705 existing_resources [resource ['Name' ]].origin != 'upload'
671706 ]
0 commit comments