|
| 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