X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=modules%2Flibjuju%2Fjuju%2Fapplication.py;fp=modules%2Flibjuju%2Fjuju%2Fapplication.py;h=620e9c998aff2f4c174a41a6fd29df2f4dd6d053;hb=1a15d1c84fc826fa7996c1c9d221a324edd33432;hp=8719a629d2153b63374a3959b70f29c89d825793;hpb=fd577a36aaf408c845f3bb8b3f28ecbbf3332107;p=osm%2FN2VC.git diff --git a/modules/libjuju/juju/application.py b/modules/libjuju/juju/application.py index 8719a62..620e9c9 100644 --- a/modules/libjuju/juju/application.py +++ b/modules/libjuju/juju/application.py @@ -1,3 +1,17 @@ +# Copyright 2016 Canonical Ltd. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + import asyncio import logging @@ -342,8 +356,13 @@ class Application(model.ModelEntity): raise ValueError("switch and revision are mutually exclusive") client_facade = client.ClientFacade.from_connection(self.connection) + resources_facade = client.ResourcesFacade.from_connection( + self.connection) app_facade = client.ApplicationFacade.from_connection(self.connection) + charmstore = self.model.charmstore + charmstore_entity = None + if switch is not None: charm_url = switch if not charm_url.startswith('cs:'): @@ -354,18 +373,65 @@ class Application(model.ModelEntity): if revision is not None: charm_url = "%s-%d" % (charm_url, revision) else: - charmstore = self.model.charmstore - entity = await charmstore.entity(charm_url, channel=channel) - charm_url = entity['Id'] + charmstore_entity = await charmstore.entity(charm_url, + channel=channel) + charm_url = charmstore_entity['Id'] if charm_url == self.data['charm-url']: raise JujuError('already running charm "%s"' % charm_url) + # Update charm await client_facade.AddCharm( url=charm_url, channel=channel ) + # Update resources + if not charmstore_entity: + charmstore_entity = await charmstore.entity(charm_url, + channel=channel) + store_resources = charmstore_entity['Meta']['resources'] + + request_data = [client.Entity(self.tag)] + response = await resources_facade.ListResources(request_data) + existing_resources = { + resource.name: resource + for resource in response.results[0].resources + } + + resources_to_update = [ + resource for resource in store_resources + if resource['Name'] not in existing_resources or + existing_resources[resource['Name']].origin != 'upload' + ] + + if resources_to_update: + request_data = [ + client.CharmResource( + description=resource.get('Description'), + fingerprint=resource['Fingerprint'], + name=resource['Name'], + path=resource['Path'], + revision=resource['Revision'], + size=resource['Size'], + type_=resource['Type'], + origin='store', + ) for resource in resources_to_update + ] + response = await resources_facade.AddPendingResources( + self.tag, + charm_url, + request_data + ) + pending_ids = response.pending_ids + resource_ids = { + resource['Name']: id + for resource, id in zip(resources_to_update, pending_ids) + } + else: + resource_ids = None + + # Update application await app_facade.SetCharm( application=self.entity_id, channel=channel, @@ -374,7 +440,7 @@ class Application(model.ModelEntity): config_settings_yaml=None, force_series=force_series, force_units=force_units, - resource_ids=None, + resource_ids=resource_ids, storage_constraints=None )