Skip to content

Commit

Permalink
API versioning: /v1
Browse files Browse the repository at this point in the history
Closes #5
  • Loading branch information
zbleness committed Aug 6, 2024
1 parent fc0bb1d commit 17e590e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ hosted at `https://example.com`:
```hcl
terraform {
backend "http" {
address = "https://example.com/state/<path>"
lock_address = "https://example.com/lock/<path>"
unlock_address = "https://example.com/lock/<path>"
address = "https://example.com/v1/state/<path>"
lock_address = "https://example.com/v1/lock/<path>"
unlock_address = "https://example.com/v1/lock/<path>"
lock_method = "POST"
unlock_method = "DELETE"
}
Expand Down
6 changes: 3 additions & 3 deletions example/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
16 changes: 11 additions & 5 deletions src/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
Expand Down

0 comments on commit 17e590e

Please sign in to comment.