Fix Bug 2204 - Grafana Charm is failing to be deployed in Charmed OSM Master
[osm/devops.git] / installers / charm / grafana / src / charm.py
index 778b5eb..caa0277 100755 (executable)
@@ -148,16 +148,22 @@ class GrafanaCharm(CharmedOsmBase):
         prometheus_user = self.prometheus_client.user
         prometheus_password = self.prometheus_client.password
         enable_basic_auth = all([prometheus_user, prometheus_password])
+        kwargs = {
+            "prometheus_host": self.prometheus_client.hostname,
+            "prometheus_port": self.prometheus_client.port,
+            "enable_basic_auth": enable_basic_auth,
+            "user": "",
+            "password": "",
+        }
+        if enable_basic_auth:
+            kwargs["user"] = f"basic_auth_user: {prometheus_user}"
+            kwargs[
+                "password"
+            ] = f"secure_json_data:\n      basicAuthPassword: {prometheus_password}"
         files_builder.add_file(
             "datasource_prometheus.yaml",
             Template(Path("templates/default_datasources.yaml").read_text()).substitute(
-                prometheus_host=self.prometheus_client.hostname,
-                prometheus_port=self.prometheus_client.port,
-                enable_basic_auth=enable_basic_auth,
-                user=f"user: {prometheus_user}" if enable_basic_auth else "",
-                password=f"password: {prometheus_password}"
-                if enable_basic_auth
-                else "",
+                **kwargs
             ),
         )
         return files_builder.build()
@@ -203,6 +209,8 @@ class GrafanaCharm(CharmedOsmBase):
             {
                 "admin-password": admin_initial_password,
                 "mysql-url": mysql_config.mysql_uri or self.mysql_client.get_uri(),
+                "prometheus-user": self.prometheus_client.user,
+                "prometheus-password": self.prometheus_client.password,
             },
         )
 
@@ -239,7 +247,6 @@ class GrafanaCharm(CharmedOsmBase):
             "/etc/grafana/provisioning/datasources/",
             self._build_datasources_files(),
         )
-
         container_builder.add_envs(
             {
                 "GF_SERVER_HTTP_PORT": config.port,
@@ -252,6 +259,8 @@ class GrafanaCharm(CharmedOsmBase):
             envs={
                 "GF_SECURITY_ADMIN_PASSWORD": "admin-password",
                 "GF_DATABASE_URL": "mysql-url",
+                "PROMETHEUS_USER": "prometheus-user",
+                "PROMETHEUS_PASSWORD": "prometheus-password",
             },
         )
         container = container_builder.build()