From 17e590e023c818280eea9cbe28bdf026c4e643ed Mon Sep 17 00:00:00 2001 From: Zalan Blenessy Date: Tue, 6 Aug 2024 11:44:51 +0200 Subject: [PATCH] API versioning: /v1 Closes #5 --- README.md | 6 +++--- example/main.tf | 6 +++--- src/__main__.py | 16 +++++++++++----- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 5e7a95b..b9c3ff6 100644 --- a/README.md +++ b/README.md @@ -35,9 +35,9 @@ hosted at `https://example.com`: ```hcl terraform { backend "http" { - address = "https://example.com/state/" - lock_address = "https://example.com/lock/" - unlock_address = "https://example.com/lock/" + address = "https://example.com/v1/state/" + lock_address = "https://example.com/v1/lock/" + unlock_address = "https://example.com/v1/lock/" lock_method = "POST" unlock_method = "DELETE" } diff --git a/example/main.tf b/example/main.tf index 8837370..bf3463e 100644 --- a/example/main.tf +++ b/example/main.tf @@ -7,9 +7,9 @@ terraform { } backend "http" { - address = "http://127.0.0.1:8300/state/terraform" - lock_address = "http://127.0.0.1:8300/lock/terraform" - unlock_address = "http://127.0.0.1:8300/lock/terraform" + address = "http://127.0.0.1:8300/v1/state/terraform" + lock_address = "http://127.0.0.1:8300/v1/lock/terraform" + unlock_address = "http://127.0.0.1:8300/v1/lock/terraform" lock_method = "POST" unlock_method = "DELETE" } diff --git a/src/__main__.py b/src/__main__.py index ea95ffb..5553808 100644 --- a/src/__main__.py +++ b/src/__main__.py @@ -531,14 +531,20 @@ async def read_root_head() -> Response: """Support HTTP GET and HEAD for /.""" return Response() +@app.head("/v1") +@app.get("/v1") +async def read_v1_head() -> Response: + """Support HTTP GET and HEAD for /.""" + return Response() + -@app.get("/state/{secrets_path:path}") +@app.get("/v1/state/{secrets_path:path}") async def get_state(vault: VaultDep, token: TokenDep) -> StateData: """Get the Vault state.""" return vault.get_state(token=token) -@app.post("/state/{secrets_path:path}") +@app.post("/v1/state/{secrets_path:path}") async def update_state(request: Request, vault: VaultDep, token: TokenDep) -> None: """Update the Vault state.""" try: @@ -550,20 +556,20 @@ async def update_state(request: Request, vault: VaultDep, token: TokenDep) -> No vault.set_state(token=token, value=data) -@app.get("/lock/{secrets_path:path}") +@app.get("/v1/lock/{secrets_path:path}") async def get_lock_info(vault: VaultDep, token: TokenDep) -> LockData: """Get lock info.""" return vault.get_lock_data(token=token) -@app.post("/lock/{secrets_path:path}") +@app.post("/v1/lock/{secrets_path:path}") async def acquire_lock(request: Request, vault: VaultDep, token: TokenDep) -> None: """Acquire the lock for Terraform state.""" data = await request.json() return vault.acquire_lock(token=token, lock_data=data) -@app.delete("/lock/{secrets_path:path}") +@app.delete("/v1/lock/{secrets_path:path}") async def release_lock(vault: VaultDep, token: TokenDep) -> None: """Acquire the lock for Terraform state.""" return vault.release_lock(token=token)