Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add optional secureJsonData to grafana datasources #297

Merged
merged 4 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion lib/charms/grafana_k8s/v0/grafana_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
`refresh_event`: A `PebbleReady` event from `charm`, used to refresh
the IP address sent to Grafana on a charm lifecycle event or
pod restart
`extra_fields`: None
PietroPasotti marked this conversation as resolved.
Show resolved Hide resolved
`secure_extra_fields`: None

The value of `source_url` should be a fully-resolvable URL for a valid Grafana
source, e.g., `http://example.com/api` or similar.
Expand Down Expand Up @@ -160,7 +162,7 @@ def __init__(self, *args):

# Increment this PATCH version before using `charmcraft publish-lib` or reset
# to 0 if you are raising the major API version
LIBPATCH = 20
LIBPATCH = 21

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -324,6 +326,7 @@ def __init__(
refresh_event: Optional[Union[BoundEvent, List[BoundEvent]]] = None,
relation_name: str = DEFAULT_RELATION_NAME,
extra_fields: Optional[dict] = None,
secure_extra_fields: Optional[dict] = None,
) -> None:
"""Construct a Grafana charm client.

Expand Down Expand Up @@ -364,6 +367,8 @@ def __init__(
machine/VM restart.
extra_fields: a :dict: which is used for additional information required
for some datasources in the `jsonData` field
secure_extra_fields: a :dict: which is used for additional information required
for some datasources in the `secureJsonData`
"""
_validate_relation_by_interface_and_direction(
charm, relation_name, RELATION_INTERFACE_NAME, RelationRole.provides
Expand All @@ -382,6 +387,7 @@ def __init__(
extra_fields["implementation"] = "prometheus"

self._extra_fields = extra_fields
self._secure_extra_fields = secure_extra_fields

if not refresh_event:
if len(self._charm.meta.containers) == 1:
Expand Down Expand Up @@ -450,6 +456,7 @@ def _scrape_data(self) -> Dict:
"application": str(self._charm.model.app.name),
"type": self._source_type,
"extra_fields": self._extra_fields,
"secure_extra_fields": self._secure_extra_fields,
}
return data

Expand Down Expand Up @@ -574,6 +581,9 @@ def _get_source_config(self, rel: Relation):
if source_data.get("extra_fields", None):
host_data["extra_fields"] = source_data.get("extra_fields")

if source_data.get("secure_extra_fields", None):
host_data["secure_extra_fields"] = source_data.get("secure_extra_fields")

if host_data["source_name"] in sources_to_delete:
sources_to_delete.remove(host_data["source_name"])

Expand Down
2 changes: 2 additions & 0 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -1131,6 +1131,8 @@ def _generate_datasource_config(self) -> str:
}
if source_info.get("extra_fields", None):
source["jsonData"] = source_info.get("extra_fields")
if source_info.get("secure_extra_fields", None):
source["secureJsonData"] = source_info.get("secure_extra_fields")
lucabello marked this conversation as resolved.
Show resolved Hide resolved

# set timeout for querying this data source
timeout = int(source.get("jsonData", {}).get("timeout", 0))
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ deps =
aiohttp
asyncstdlib
# Libjuju needs to track the juju version
juju ~= 3.3.0
juju<=3.3.0,>=3.0
pytest
pytest-operator
pytest-playwright
Expand Down
Loading