Initial commit: Vultr API v2 Python wrapper with FastAPI server
- vultr_api/: Python library wrapping Vultr API v2 - 17 resource modules (instances, dns, firewall, vpc, etc.) - Pagination support, error handling - server/: FastAPI REST server - All API endpoints exposed via HTTP - X-API-Key header authentication - Swagger docs at /docs - Podman quadlet config for systemd deployment Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
115
vultr_api/resources/account.py
Normal file
115
vultr_api/resources/account.py
Normal file
@@ -0,0 +1,115 @@
|
||||
"""
|
||||
Account Resource
|
||||
|
||||
Includes account info and ACL (Access Control List) management
|
||||
"""
|
||||
|
||||
from typing import Dict, List, Optional
|
||||
from .base import BaseResource
|
||||
|
||||
|
||||
class AccountResource(BaseResource):
|
||||
"""
|
||||
Account and ACL management
|
||||
|
||||
Usage:
|
||||
# Get account info
|
||||
account = client.account.get()
|
||||
|
||||
# Get bandwidth
|
||||
bandwidth = client.account.get_bandwidth()
|
||||
|
||||
# ACL (IP access control)
|
||||
acl = client.account.get_acl()
|
||||
client.account.set_acl(acls=["192.168.1.1/32", "10.0.0.0/8"])
|
||||
"""
|
||||
|
||||
def get(self) -> Dict:
|
||||
"""
|
||||
Get account information
|
||||
|
||||
Returns:
|
||||
Account info including name, email, balance, etc.
|
||||
"""
|
||||
response = self.client.get("account")
|
||||
return response.get("account", {})
|
||||
|
||||
def get_bandwidth(self) -> Dict:
|
||||
"""
|
||||
Get account bandwidth usage
|
||||
|
||||
Returns:
|
||||
Bandwidth usage info
|
||||
"""
|
||||
return self.client.get("account/bandwidth")
|
||||
|
||||
# ACL (Access Control List) methods
|
||||
def get_acl(self) -> Dict:
|
||||
"""
|
||||
Get API access control list
|
||||
|
||||
Returns:
|
||||
ACL info including enabled status and list of allowed IPs/CIDRs
|
||||
"""
|
||||
return self.client.get("account/acl")
|
||||
|
||||
def set_acl(self, acls: List[str]) -> Dict:
|
||||
"""
|
||||
Update API access control list
|
||||
|
||||
Args:
|
||||
acls: List of IP addresses or CIDR ranges to allow.
|
||||
Pass empty list to disable ACL.
|
||||
|
||||
Returns:
|
||||
Updated ACL info
|
||||
|
||||
Example:
|
||||
# Allow specific IPs
|
||||
client.account.set_acl(["192.168.1.100/32", "10.0.0.0/8"])
|
||||
|
||||
# Disable ACL (allow all)
|
||||
client.account.set_acl([])
|
||||
"""
|
||||
return self.client.post("account/acl", {"acls": acls})
|
||||
|
||||
def add_acl(self, ip: str) -> Dict:
|
||||
"""
|
||||
Add an IP/CIDR to the ACL
|
||||
|
||||
Args:
|
||||
ip: IP address or CIDR to add (e.g., "192.168.1.1/32")
|
||||
|
||||
Returns:
|
||||
Updated ACL info
|
||||
"""
|
||||
current = self.get_acl()
|
||||
acls = current.get("acls", [])
|
||||
if ip not in acls:
|
||||
acls.append(ip)
|
||||
return self.set_acl(acls)
|
||||
|
||||
def remove_acl(self, ip: str) -> Dict:
|
||||
"""
|
||||
Remove an IP/CIDR from the ACL
|
||||
|
||||
Args:
|
||||
ip: IP address or CIDR to remove
|
||||
|
||||
Returns:
|
||||
Updated ACL info
|
||||
"""
|
||||
current = self.get_acl()
|
||||
acls = current.get("acls", [])
|
||||
if ip in acls:
|
||||
acls.remove(ip)
|
||||
return self.set_acl(acls)
|
||||
|
||||
def clear_acl(self) -> Dict:
|
||||
"""
|
||||
Clear all ACL entries (disable ACL)
|
||||
|
||||
Returns:
|
||||
Updated ACL info
|
||||
"""
|
||||
return self.set_acl([])
|
||||
Reference in New Issue
Block a user