Skip to content

Commit 51804c4

Browse files
authored
Merge pull request #453 from SimonRichardson/cross-controller-model-example
#453 The following adds an example of consuming a model from another controller that we also know about. The helps with consuming CMR and the flakey nature of pylibjuju not finding a model from the current model.
2 parents a7e902d + d3b8d24 commit 51804c4

1 file changed

Lines changed: 85 additions & 0 deletions

File tree

examples/crossmodel_controller.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
"""
2+
This example:
3+
4+
1. Connects to test and test2 controllers
5+
2. Creates models on each controllers
6+
3. Deploys a charm and waits until it reports itself active
7+
4. Creates an offer
8+
5. Lists the offer
9+
6. Consumes the offer
10+
7. Exports the bundle
11+
8. Removes the SAAS
12+
9. Removes the offer
13+
10. Destroys models and disconnects
14+
"""
15+
import tempfile
16+
from logging import getLogger
17+
18+
from juju import loop
19+
from juju.controller import Controller
20+
21+
log = getLogger(__name__)
22+
23+
24+
async def main():
25+
controller1 = Controller()
26+
print("Connecting to controller")
27+
await controller1.connect("test")
28+
29+
controller2 = Controller()
30+
print("Connecting to controller")
31+
await controller2.connect("test2")
32+
33+
try:
34+
print('Creating models')
35+
offering_model = await controller1.add_model('test-cmr-1')
36+
consuming_model = await controller2.add_model('test-cmr-2')
37+
38+
print('Deploying mysql')
39+
application = await offering_model.deploy(
40+
'mysql',
41+
application_name='mysql',
42+
series='trusty',
43+
channel='stable',
44+
)
45+
46+
print('Waiting for active')
47+
await offering_model.block_until(
48+
lambda: all(unit.workload_status == 'active'
49+
for unit in application.units))
50+
51+
print('Adding offer')
52+
await offering_model.create_offer("mysql:db")
53+
54+
offers = await offering_model.list_offers()
55+
print('Show offers', ', '.join("%s: %s" % item for offer in offers.results for item in vars(offer).items()))
56+
57+
print('Consuming offer')
58+
await consuming_model.consume("admin/test-cmr-1.mysql", controller_name="test")
59+
60+
print('Exporting bundle')
61+
with tempfile.TemporaryDirectory() as dirpath:
62+
await offering_model.export_bundle("{}/bundle.yaml".format(dirpath))
63+
64+
print("Remove SAAS")
65+
await consuming_model.remove_saas("mysql")
66+
67+
print('Removing offer')
68+
await offering_model.remove_offer("admin/test-cmr-1.mysql", force=True)
69+
70+
print('Destroying models')
71+
await controller1.destroy_model(offering_model.info.uuid)
72+
await controller2.destroy_model(consuming_model.info.uuid)
73+
74+
except Exception:
75+
log.exception("Example failed!")
76+
raise
77+
78+
finally:
79+
print('Disconnecting from controller')
80+
await controller1.disconnect()
81+
await controller2.disconnect()
82+
83+
84+
if __name__ == '__main__':
85+
loop.run(main())

0 commit comments

Comments
 (0)