Skip to content

Commit

Permalink
Adds resource support for charmhub deployments
Browse files Browse the repository at this point in the history
  • Loading branch information
tlm committed Aug 3, 2021
1 parent ef05ad8 commit 646749e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
9 changes: 7 additions & 2 deletions juju/client/overrides.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ class ResourcesFacade(Type):
"""

@ReturnMapping(_client.AddPendingResourcesResult)
async def AddPendingResources(self, application_tag="", charm_url="", resources=None):
async def AddPendingResources(self,
application_tag="",
charm_url="",
charm_origin=None,
resources=None):
"""Fix the calling signature of AddPendingResources.
The ResourcesFacade doesn't conform to the standard facade pattern in
Expand All @@ -81,11 +85,12 @@ async def AddPendingResources(self, application_tag="", charm_url="", resources=
_params = dict()
msg = dict(type='Resources',
request='AddPendingResources',
version=1,
version=2,
params=_params)
_params['tag'] = application_tag
_params['url'] = charm_url
_params['resources'] = resources
_params['charm-origin'] = charm_origin
reply = await self.rpc(msg)
return reply

Expand Down
48 changes: 44 additions & 4 deletions juju/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1595,7 +1595,12 @@ async def deploy(
# XXX: we're dropping local resources here, but we don't
# actually support them yet anyway
if not res.is_local:
await self._add_charm(identifier, res.origin)
charm_origin_res = add_charm_res = await self._add_charm(identifier, res.origin)

if Schema.CHARM_HUB.matches(url.schema):
resources = await self._add_charmhub_resources(res.app_name,
identifier,
charm_origin_res.charm_origin)

# TODO (stickupkid): Handle charmhub charms, for now we'll only
# handle charmstore charms.
Expand Down Expand Up @@ -1632,6 +1637,7 @@ async def deploy(
num_units=num_units,
placement=parse_placement(to),
devices=devices,
charm_origin=charm_origin_res.charm_origin,
)

async def _add_charm(self, charm_url, origin):
Expand Down Expand Up @@ -1661,8 +1667,8 @@ async def _resolve_charm(self, url, origin):
'charm-origin': {
'source': source,
'architecture': origin.architecture,
'track': origin.track,
'risk': origin.risk,
#'track': origin.track,
#'risk': origin.risk,
}
}])
if len(resp.results) != 1:
Expand All @@ -1684,6 +1690,39 @@ async def _resolve_architecture(self, url):

return DEFAULT_ARCHITECTURE

async def _add_charmhub_resources(self, application,
entity_url,
origin,
overrides=None):
charm_facade = client.CharmsFacade.from_connection(
self.connection())
res = await charm_facade.CharmInfo(entity_url)

resources = []
for resource in res.meta.resources.values():
resources.append({
'description': resource.description,
'name': resource.name,
'path': resource.path,
'type_': resource.type_,
'origin': 'store',
'revision': -1,
})

resources_facade = client.ResourcesFacade.from_connection(
self.connection())
response = await resources_facade.AddPendingResources(
application_tag=tag.application(application),
charm_url=entity_url,
charm_origin=origin,
resources=[client.CharmResource(**resource) for resource in resources],
)

resource_map = {resource['name']: pid
for resource, pid
in zip(resources, response.pending_ids)}
return resource_map

async def _add_store_resources(self, application, entity_url,
overrides=None):
entity = await self.charmstore.entity(entity_url,
Expand Down Expand Up @@ -1729,7 +1768,7 @@ async def _add_store_resources(self, application, entity_url,
async def _deploy(self, charm_url, application, series, config,
constraints, endpoint_bindings, resources, storage,
channel=None, num_units=None, placement=None,
devices=None):
devices=None, charm_origin=None):
"""Logic shared between `Model.deploy` and `BundleHandler.deploy`.
"""
log.info('Deploying %s', charm_url)
Expand All @@ -1747,6 +1786,7 @@ async def _deploy(self, charm_url, application, series, config,
application=application,
series=series,
channel=channel,
charm_origin=charm_origin,
config_yaml=config,
constraints=parse_constraints(constraints),
endpoint_bindings=endpoint_bindings,
Expand Down

0 comments on commit 646749e

Please sign in to comment.