diff --git a/nomad_vars.py b/nomad_vars.py index 431cde3..a3d1ffe 100755 --- a/nomad_vars.py +++ b/nomad_vars.py @@ -10,18 +10,16 @@ NOMAD_ADDR = getenv("NOMAD_ADDR", "http://127.0.0.1:4646") NOMAD_TOKEN = getenv("NOMAD_TOKEN") -def write_var(path: str, items: dict[str, str | float | int]) -> dict: +def nomad_req(method: str, path: str, json: dict|None = None) -> dict: headers = {} if NOMAD_TOKEN: headers["X-Nomad-Token"] = NOMAD_TOKEN - result = requests.post( - f"{NOMAD_ADDR}/v1/var/{path}", + result = requests.request( + method, + f"{NOMAD_ADDR}/v1/{path}", headers=headers, - json={ - "Path": path, - "Items": {k: str(v) for k, v in items.items()}, - }, + json=json, ) print(result.text) @@ -30,9 +28,24 @@ def write_var(path: str, items: dict[str, str | float | int]) -> dict: return result.json() + +def write_var(path: str, items: dict[str, str | float | int]) -> dict: + return nomad_req("GET", f"var/{path}", + json={ + "Path": path, + "Items": {k: str(v) for k, v in items.items()}, + }, + ) + + +def delete_var(path: str) -> dict: + return nomad_req("DELETE", f"var/{path}") + + def write_consul(): with open("./ansible_playbooks/vars/consul_values.yml") as f: vars = yaml.load(f, yaml.CLoader)["consul_values"] + prefix = "insecure/" key_values = defaultdict(list) for path, value in vars.items(): @@ -41,7 +54,7 @@ def write_consul(): for path, items in key_values.items(): print("path", path, "items", items) - response = write_var(path, dict(items)) + response = write_var(prefix + path, dict(items)) print(response) @@ -55,13 +68,18 @@ def write_vault(): response = write_var(prefix + path, items) print(response) + def write_nomad(): with open("./ansible_playbooks/vars/nomad_vars.yml") as f: vars = yaml.load(f, yaml.CLoader) for path, items in vars.items(): print("path", path, "items", items) - response = write_var(path, items) + response = None + if items == "DELETE": + response = delete_var(path) + else: + response = write_var(path, items) print(response)