+
+ async def list_offers(self, model_name: str) -> QueryApplicationOffersResults:
+ """List models with certain names
+
+ :param: model_name: Model name
+
+ :return: Returns list of offers
+ """
+
+ controller = await self.get_controller()
+ try:
+ return await controller.list_offers(model_name)
+ finally:
+ await self.disconnect_controller(controller)
+
+ async def add_k8s(
+ self,
+ name: str,
+ rbac_id: str,
+ token: str,
+ client_cert_data: str,
+ configuration: Configuration,
+ storage_class: str,
+ credential_name: str = None,
+ ):
+ """
+ Add a Kubernetes cloud to the controller
+
+ Similar to the `juju add-k8s` command in the CLI
+
+ :param: name: Name for the K8s cloud
+ :param: configuration: Kubernetes configuration object
+ :param: storage_class: Storage Class to use in the cloud
+ :param: credential_name: Storage Class to use in the cloud
+ """
+
+ if not storage_class:
+ raise Exception("storage_class must be a non-empty string")
+ if not name:
+ raise Exception("name must be a non-empty string")
+ if not configuration:
+ raise Exception("configuration must be provided")
+
+ endpoint = configuration.host
+ credential = self.get_k8s_cloud_credential(
+ configuration,
+ client_cert_data,
+ token,
+ )
+ credential.attrs[RBAC_LABEL_KEY_NAME] = rbac_id
+ cloud = client.Cloud(
+ type_="kubernetes",
+ auth_types=[credential.auth_type],
+ endpoint=endpoint,
+ ca_certificates=[client_cert_data],
+ config={
+ "operator-storage": storage_class,
+ "workload-storage": storage_class,
+ },
+ )
+
+ return await self.add_cloud(
+ name, cloud, credential, credential_name=credential_name
+ )
+
+ def get_k8s_cloud_credential(
+ self,
+ configuration: Configuration,
+ client_cert_data: str,
+ token: str = None,
+ ) -> client.CloudCredential:
+ attrs = {}
+ # TODO: Test with AKS
+ key = None # open(configuration.key_file, "r").read()
+ username = configuration.username
+ password = configuration.password
+
+ if client_cert_data:
+ attrs["ClientCertificateData"] = client_cert_data
+ if key:
+ attrs["ClientKeyData"] = key
+ if token:
+ if username or password:
+ raise JujuInvalidK8sConfiguration("Cannot set both token and user/pass")
+ attrs["Token"] = token
+
+ auth_type = None
+ if key:
+ auth_type = "oauth2"
+ if client_cert_data:
+ auth_type = "oauth2withcert"
+ if not token:
+ raise JujuInvalidK8sConfiguration(
+ "missing token for auth type {}".format(auth_type)
+ )
+ elif username:
+ if not password:
+ self.log.debug(
+ "credential for user {} has empty password".format(username)
+ )
+ attrs["username"] = username
+ attrs["password"] = password
+ if client_cert_data:
+ auth_type = "userpasswithcert"
+ else:
+ auth_type = "userpass"
+ elif client_cert_data and token:
+ auth_type = "certificate"
+ else:
+ raise JujuInvalidK8sConfiguration("authentication method not supported")
+ return client.CloudCredential(auth_type=auth_type, attrs=attrs)
+
+ async def add_cloud(
+ self,
+ name: str,
+ cloud: Cloud,
+ credential: CloudCredential = None,
+ credential_name: str = None,
+ ) -> Cloud:
+ """
+ Add cloud to the controller
+
+ :param: name: Name of the cloud to be added
+ :param: cloud: Cloud object
+ :param: credential: CloudCredentials object for the cloud
+ :param: credential_name: Credential name.
+ If not defined, cloud of the name will be used.
+ """
+ controller = await self.get_controller()
+ try:
+ _ = await controller.add_cloud(name, cloud)
+ if credential:
+ await controller.add_credential(
+ credential_name or name, credential=credential, cloud=name
+ )
+ # Need to return the object returned by the controller.add_cloud() function
+ # I'm returning the original value now until this bug is fixed:
+ # https://github.com/juju/python-libjuju/issues/443
+ return cloud
+ finally:
+ await self.disconnect_controller(controller)
+
+ async def remove_cloud(self, name: str):
+ """
+ Remove cloud
+
+ :param: name: Name of the cloud to be removed
+ """
+ controller = await self.get_controller()
+ try:
+ await controller.remove_cloud(name)
+ except juju.errors.JujuError as e:
+ if len(e.errors) == 1 and f'cloud "{name}" not found' == e.errors[0]:
+ self.log.warning(f"Cloud {name} not found, so it could not be deleted.")
+ else:
+ raise e
+ finally:
+ await self.disconnect_controller(controller)
+
+ @retry(attempts=20, delay=5, fallback=JujuLeaderUnitNotFound())
+ async def _get_leader_unit(self, application: Application) -> Unit:
+ unit = None
+ for u in application.units:
+ if await u.is_leader_from_status():
+ unit = u
+ break
+ if not unit:
+ raise Exception()
+ return unit
+
+ async def get_cloud_credentials(self, cloud: Cloud) -> typing.List:
+ """
+ Get cloud credentials
+
+ :param: cloud: Cloud object. The returned credentials will be from this cloud.
+
+ :return: List of credentials object associated to the specified cloud
+
+ """
+ controller = await self.get_controller()
+ try:
+ facade = client.CloudFacade.from_connection(controller.connection())
+ cloud_cred_tag = tag.credential(
+ cloud.name, self.vca_connection.data.user, cloud.credential_name
+ )
+ params = [client.Entity(cloud_cred_tag)]
+ return (await facade.Credential(params)).results
+ finally:
+ await self.disconnect_controller(controller)
+
+ async def check_application_exists(self, model_name, application_name) -> bool:
+ """Check application exists
+
+ :param: model_name: Model Name
+ :param: application_name: Application Name
+
+ :return: Boolean
+ """
+
+ model = None
+ controller = await self.get_controller()
+ try:
+ model = await self.get_model(controller, model_name)
+ self.log.debug(
+ "Checking if application {} exists in model {}".format(
+ application_name, model_name
+ )
+ )
+ return self._get_application(model, application_name) is not None
+ finally:
+ if model:
+ await self.disconnect_model(model)
+ await self.disconnect_controller(controller)