from juju import jasyncio
from juju.model import Model
from pathlib import Path


async def main():
    model = Model()
    print('Connecting to model')
    # connect to current model with current user, per Juju CLI
    await model.connect()

    try:
        print('Deploying local-charm')
        base_dir = Path(__file__).absolute().parent.parent
        charm_path = '{}/tests/integration/file-resource-charm'.format(base_dir)
        resources = {"file-res": "test.file"}
        application = await model.deploy(
            charm_path,
            resources=resources,
        )

        print('Waiting for active')
        await model.wait_for_idle()

        print('Removing Charm')
        await application.remove()
    finally:
        print('Disconnecting from model')
        await model.disconnect()


if __name__ == '__main__':
    jasyncio.run(main())

from juju import jasyncio
from juju.model import Model
from pathlib import Path


async def main():
    model = Model()
    print('Connecting to model')
    # connect to current model with current user, per Juju CLI
    await model.connect()

    application = None
    try:
        print('Deploying local-charm')
        base_dir = Path(__file__).absolute().parent.parent
        charm_path = '{}/tests/integration/file-resource-charm'.format(base_dir)
        resources = {"file-res": "test.file"}
        application = await model.deploy(
            charm_path,
            resources=resources,
        )

        print('Waiting for active')
        await model.wait_for_idle()

        print('Removing Charm')
        await application.remove()
    except Exception as e:
        print(e)
        if application:
            await application.remove()
        await model.disconnect()
    finally:
        print('Disconnecting from model')
        await model.disconnect()


if __name__ == '__main__':
    jasyncio.run(main())

Make sure we cleanup even if fails in example
Make sure we cleanup even if fails in example

  from juju import jasyncio
  from juju.model import Model
  from pathlib import Path
  
  
  async def main():
      model = Model()
      print('Connecting to model')
      # connect to current model with current user, per Juju CLI
      await model.connect()
  
+     application = None
      try:
          print('Deploying local-charm')
          base_dir = Path(__file__).absolute().parent.parent
          charm_path = '{}/tests/integration/file-resource-charm'.format(base_dir)
          resources = {"file-res": "test.file"}
          application = await model.deploy(
              charm_path,
              resources=resources,
          )
  
          print('Waiting for active')
          await model.wait_for_idle()
  
          print('Removing Charm')
          await application.remove()
+     except Exception as e:
+         print(e)
+         if application:
+             await application.remove()
+         await model.disconnect()
      finally:
          print('Disconnecting from model')
          await model.disconnect()
  
  
  if __name__ == '__main__':
      jasyncio.run(main())
  
Make sure we cleanup even if fails in example
## Code Before:
from juju import jasyncio
from juju.model import Model
from pathlib import Path


async def main():
    model = Model()
    print('Connecting to model')
    # connect to current model with current user, per Juju CLI
    await model.connect()

    try:
        print('Deploying local-charm')
        base_dir = Path(__file__).absolute().parent.parent
        charm_path = '{}/tests/integration/file-resource-charm'.format(base_dir)
        resources = {"file-res": "test.file"}
        application = await model.deploy(
            charm_path,
            resources=resources,
        )

        print('Waiting for active')
        await model.wait_for_idle()

        print('Removing Charm')
        await application.remove()
    finally:
        print('Disconnecting from model')
        await model.disconnect()


if __name__ == '__main__':
    jasyncio.run(main())

## Instruction:
Make sure we cleanup even if fails in example
## Code After:
from juju import jasyncio
from juju.model import Model
from pathlib import Path


async def main():
    model = Model()
    print('Connecting to model')
    # connect to current model with current user, per Juju CLI
    await model.connect()

    application = None
    try:
        print('Deploying local-charm')
        base_dir = Path(__file__).absolute().parent.parent
        charm_path = '{}/tests/integration/file-resource-charm'.format(base_dir)
        resources = {"file-res": "test.file"}
        application = await model.deploy(
            charm_path,
            resources=resources,
        )

        print('Waiting for active')
        await model.wait_for_idle()

        print('Removing Charm')
        await application.remove()
    except Exception as e:
        print(e)
        if application:
            await application.remove()
        await model.disconnect()
    finally:
        print('Disconnecting from model')
        await model.disconnect()


if __name__ == '__main__':
    jasyncio.run(main())
