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 7, 2024
1 parent fc0bb1d commit 5075ca1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 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
21 changes: 14 additions & 7 deletions src/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,17 +528,24 @@ def get_vault_token(credentials: SecurityDep) -> str:
@app.head("/")
@app.get("/")
async def read_root_head() -> Response:
"""Support HTTP GET and HEAD for /."""
"""General health check API."""
return Response()


@app.get("/state/{secrets_path:path}")
@app.head("/v1")
@app.get("/v1")
async def read_v1_head() -> Response:
"""V1 health check API."""
return Response()


@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,22 +557,22 @@ 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."""
"""Release the lock for Terraform state."""
return vault.release_lock(token=token)


Expand Down

0 comments on commit 5075ca1

Please sign in to comment.