Skip to content

Commit 9abe4b6

Browse files
Clean up facade error on bundle download
The following removes an indention when checking for a facade. This makes it a lot easier to read. Additionally cleaned up the deploy_bundle example.
1 parent 0e6444e commit 9abe4b6

2 files changed

Lines changed: 64 additions & 54 deletions

File tree

examples/deploy_bundle.py

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,43 @@ async def main():
1515
# connect to current controller with current user, per Juju CLI
1616
await controller.connect()
1717

18-
bundles = [('cs:~juju-qa/bundle/basic-0', 'beta'), ('juju-qa-bundle-test', None)]
19-
for i in range(len(bundles)):
20-
deployment = bundles[i]
21-
model = await controller.add_model('model{}'.format(i))
22-
23-
try:
24-
print('Deploying bundle')
25-
applications = await model.deploy(
26-
deployment[0],
27-
channel=deployment[1],
28-
)
29-
30-
print('Waiting for active')
31-
await model.block_until(
32-
lambda: all(unit.workload_status == 'active'
33-
for application in applications for unit in application.units))
34-
print("Successfully deployed!")
35-
print('Removing bundle')
36-
for application in applications:
37-
await application.remove()
38-
finally:
39-
print('Disconnecting from model')
40-
await model.disconnect()
41-
print("Success")
18+
# Deploy charmhub bundle
19+
await deploy_bundle(controller, 'juju-qa-bundle-test')
20+
21+
# Deploy legacy bundle
22+
await deploy_bundle(controller, 'cs:~juju-qa/bundle/basic-0', 'beta')
4223

4324
await controller.disconnect()
4425

4526

27+
async def deploy_bundle(controller, url, channel=None):
28+
models = await controller.list_models()
29+
model = await controller.add_model('model{}'.format(len(models) + 1))
30+
31+
try:
32+
print('Deploying bundle')
33+
34+
applications = await deploy_and_wait_for_bundle(model, url, channel)
35+
36+
print("Successfully deployed!")
37+
print('Removing bundle')
38+
for application in applications:
39+
await application.remove()
40+
finally:
41+
print('Disconnecting from model')
42+
await model.disconnect()
43+
print("Success")
44+
45+
46+
async def deploy_and_wait_for_bundle(model, url, channel=None):
47+
applications = await model.deploy(url, channel=channel)
48+
49+
print('Waiting for active')
50+
await model.block_until(
51+
lambda: all(unit.workload_status == 'active'
52+
for application in applications for unit in application.units))
53+
54+
return applications
55+
4656
if __name__ == '__main__':
4757
loop.run(main())

juju/bundle.py

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -178,36 +178,36 @@ async def fetch_plan(self, charm_url, origin):
178178
raise JujuError(self.plan.errors)
179179

180180
async def _download_bundle(self, charm_url, origin):
181-
if self.charms_facade is not None:
182-
resp = await self.charms_facade.GetDownloadInfos(entities=[{
183-
'charm-url': str(charm_url),
184-
'charm-origin': {
185-
'source': origin.source,
186-
'type': origin.type_,
187-
'id': origin.id_,
188-
'hash': origin.hash_,
189-
'revision': origin.revision,
190-
'risk': origin.risk,
191-
'track': origin.track,
192-
'architecture': origin.architecture,
193-
'os': origin.os,
194-
'series': origin.series,
195-
}
196-
}])
197-
if len(resp.results) != 1:
198-
raise JujuError("expected one result, received {}".format(resp.results))
199-
200-
result = resp.results[0]
201-
if not result.url:
202-
raise JujuError("no url found for bundle {}".format(charm_url.name))
203-
204-
bundle_resp = requests.get(result.url)
205-
bundle_resp.raise_for_status()
206-
207-
with closing(bundle_resp), zipfile.ZipFile(io.BytesIO(bundle_resp.content)) as archive:
208-
return self._get_bundle_yaml(archive)
209-
210-
raise JujuError('charm facade not supported')
181+
if self.charms_facade is None:
182+
raise JujuError('unable to download bundle for {} using the new charms facade. Upgrade controller to proceed.'.format(charm_url))
183+
184+
resp = await self.charms_facade.GetDownloadInfos(entities=[{
185+
'charm-url': str(charm_url),
186+
'charm-origin': {
187+
'source': origin.source,
188+
'type': origin.type_,
189+
'id': origin.id_,
190+
'hash': origin.hash_,
191+
'revision': origin.revision,
192+
'risk': origin.risk,
193+
'track': origin.track,
194+
'architecture': origin.architecture,
195+
'os': origin.os,
196+
'series': origin.series,
197+
}
198+
}])
199+
if len(resp.results) != 1:
200+
raise JujuError("expected one result, received {}".format(resp.results))
201+
202+
result = resp.results[0]
203+
if not result.url:
204+
raise JujuError("no url found for bundle {}".format(charm_url.name))
205+
206+
bundle_resp = requests.get(result.url)
207+
bundle_resp.raise_for_status()
208+
209+
with closing(bundle_resp), zipfile.ZipFile(io.BytesIO(bundle_resp.content)) as archive:
210+
return self._get_bundle_yaml(archive)
211211

212212
def _get_bundle_yaml(self, archive):
213213
for member in archive.infolist():

0 commit comments

Comments
 (0)