Make API endpoints compatible with Vultr API v2 format
- Change auth from X-API-Key header to Authorization: Bearer format - Add /v2 prefix to all endpoints to match Vultr API URL structure - Fix router paths (dns, firewall) to avoid duplicate path segments - Split VPC 2.0 into separate router at /v2/vpc2 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
"""VPC router"""
|
||||
"""VPC router (VPC 1.0)"""
|
||||
from fastapi import APIRouter, Depends, Query
|
||||
from typing import Optional, List
|
||||
from typing import Optional
|
||||
from pydantic import BaseModel
|
||||
|
||||
from vultr_api import VultrClient
|
||||
@@ -20,18 +20,6 @@ class UpdateVPCRequest(BaseModel):
|
||||
description: str
|
||||
|
||||
|
||||
class CreateVPC2Request(BaseModel):
|
||||
region: str
|
||||
description: Optional[str] = None
|
||||
ip_block: Optional[str] = None
|
||||
prefix_length: Optional[int] = None
|
||||
|
||||
|
||||
class AttachVPC2Request(BaseModel):
|
||||
nodes: List[dict] # [{"id": "instance_id", "ip_address": "10.0.0.1"}, ...]
|
||||
|
||||
|
||||
# VPC 1.0
|
||||
@router.get("")
|
||||
async def list_vpcs(
|
||||
per_page: int = Query(25, le=500),
|
||||
@@ -70,67 +58,3 @@ async def update_vpc(vpc_id: str, req: UpdateVPCRequest, client: VultrClient = D
|
||||
async def delete_vpc(vpc_id: str, client: VultrClient = Depends(get_client)):
|
||||
"""Delete a VPC"""
|
||||
return client.vpc.delete(vpc_id)
|
||||
|
||||
|
||||
# VPC 2.0
|
||||
@router.get("/v2/list")
|
||||
async def list_vpc2s(
|
||||
per_page: int = Query(25, le=500),
|
||||
cursor: Optional[str] = None,
|
||||
client: VultrClient = Depends(get_client)
|
||||
):
|
||||
"""List all VPC 2.0 networks"""
|
||||
return client.vpc.list_vpc2(per_page=per_page, cursor=cursor)
|
||||
|
||||
|
||||
@router.get("/v2/all")
|
||||
async def list_all_vpc2s(client: VultrClient = Depends(get_client)):
|
||||
"""List all VPC 2.0 networks (auto-paginated)"""
|
||||
return {"vpcs": client.vpc.list_all_vpc2()}
|
||||
|
||||
|
||||
@router.get("/v2/{vpc_id}")
|
||||
async def get_vpc2(vpc_id: str, client: VultrClient = Depends(get_client)):
|
||||
"""Get VPC 2.0 details"""
|
||||
return client.vpc.get_vpc2(vpc_id)
|
||||
|
||||
|
||||
@router.post("/v2")
|
||||
async def create_vpc2(req: CreateVPC2Request, client: VultrClient = Depends(get_client)):
|
||||
"""Create a VPC 2.0 network"""
|
||||
return client.vpc.create_vpc2(**req.model_dump(exclude_none=True))
|
||||
|
||||
|
||||
@router.patch("/v2/{vpc_id}")
|
||||
async def update_vpc2(vpc_id: str, description: str, client: VultrClient = Depends(get_client)):
|
||||
"""Update a VPC 2.0 network"""
|
||||
return client.vpc.update_vpc2(vpc_id, description=description)
|
||||
|
||||
|
||||
@router.delete("/v2/{vpc_id}")
|
||||
async def delete_vpc2(vpc_id: str, client: VultrClient = Depends(get_client)):
|
||||
"""Delete a VPC 2.0 network"""
|
||||
return client.vpc.delete_vpc2(vpc_id)
|
||||
|
||||
|
||||
@router.get("/v2/{vpc_id}/nodes")
|
||||
async def list_vpc2_nodes(
|
||||
vpc_id: str,
|
||||
per_page: int = Query(25, le=500),
|
||||
cursor: Optional[str] = None,
|
||||
client: VultrClient = Depends(get_client)
|
||||
):
|
||||
"""List nodes attached to a VPC 2.0"""
|
||||
return client.vpc.list_vpc2_nodes(vpc_id, per_page=per_page, cursor=cursor)
|
||||
|
||||
|
||||
@router.post("/v2/{vpc_id}/nodes/attach")
|
||||
async def attach_vpc2_nodes(vpc_id: str, req: AttachVPC2Request, client: VultrClient = Depends(get_client)):
|
||||
"""Attach nodes to a VPC 2.0"""
|
||||
return client.vpc.attach_vpc2_nodes(vpc_id, nodes=req.nodes)
|
||||
|
||||
|
||||
@router.post("/v2/{vpc_id}/nodes/detach")
|
||||
async def detach_vpc2_nodes(vpc_id: str, req: AttachVPC2Request, client: VultrClient = Depends(get_client)):
|
||||
"""Detach nodes from a VPC 2.0"""
|
||||
return client.vpc.detach_vpc2_nodes(vpc_id, nodes=req.nodes)
|
||||
|
||||
Reference in New Issue
Block a user