# Copyright 2023 Canonical Ltd.
# Licensed under the Apache V2, see LICENCE file for details.
from __future__ import annotations
# TODO: Tags should be a proper class, so that we can distinguish whether
# something is already a tag or not. For example, 'user-foo' is a valid
# username, but is ambiguous with the already-tagged username 'foo'.
def _prefix(prefix: str, s: str) -> str:
if s and not s.startswith(prefix):
return f"{prefix}{s}"
return s
[docs]def untag(prefix: str, s: str) -> str:
if s and s.startswith(prefix):
return s[len(prefix) :]
return s
[docs]def cloud(cloud_name: str) -> str:
return _prefix("cloud-", cloud_name)
[docs]def controller(controller_uuid: str) -> str:
return _prefix("controller-", controller_uuid)
[docs]def credential(cloud: str, user: str, credential_name: str) -> str:
credential_string = f"{cloud}_{user}_{credential_name}"
return _prefix("cloudcred-", credential_string)
[docs]def model(model_uuid: str) -> str:
return _prefix("model-", model_uuid)
[docs]def machine(machine_id: str) -> str:
return _prefix("machine-", machine_id)
[docs]def user(username: str) -> str:
return _prefix("user-", username)
[docs]def application(app_name: str) -> str:
return _prefix("application-", app_name)
[docs]def storage(app_name: str) -> str:
return _prefix("storage-", app_name)
[docs]def unit(unit_name: str) -> str:
return _prefix("unit-", unit_name.replace("/", "-"))
[docs]def action(action_uuid: str) -> str:
return _prefix("action-", action_uuid)
[docs]def space(space_name: str) -> str:
return _prefix("space-", space_name)