- 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>
120 lines
3.0 KiB
Python
120 lines
3.0 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Vultr ACL (IP 접근 제어) 관리 예제
|
|
|
|
IP를 등록해야 API에 접근할 수 있을 때 사용하는 예제입니다.
|
|
"""
|
|
|
|
import os
|
|
import sys
|
|
import argparse
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
from vultr_api import VultrClient
|
|
from vultr_api.client import VultrAPIError
|
|
|
|
|
|
def get_client():
|
|
api_key = os.environ.get("VULTR_API_KEY")
|
|
if not api_key:
|
|
print("VULTR_API_KEY 환경 변수를 설정해주세요")
|
|
sys.exit(1)
|
|
return VultrClient(api_key=api_key)
|
|
|
|
|
|
def list_acl(client):
|
|
"""현재 ACL 목록 조회"""
|
|
acl = client.account.get_acl()
|
|
print(f"ACL 활성화: {acl.get('acl_enabled', False)}")
|
|
acls = acl.get("acls", [])
|
|
if acls:
|
|
print("허용된 IP/CIDR:")
|
|
for ip in acls:
|
|
print(f" - {ip}")
|
|
else:
|
|
print("허용된 IP 없음 (모든 IP 허용)")
|
|
|
|
|
|
def add_ip(client, ip):
|
|
"""IP 추가"""
|
|
print(f"IP 추가 중: {ip}")
|
|
client.account.add_acl(ip)
|
|
print("완료!")
|
|
list_acl(client)
|
|
|
|
|
|
def remove_ip(client, ip):
|
|
"""IP 제거"""
|
|
print(f"IP 제거 중: {ip}")
|
|
client.account.remove_acl(ip)
|
|
print("완료!")
|
|
list_acl(client)
|
|
|
|
|
|
def set_ips(client, ips):
|
|
"""IP 목록 설정"""
|
|
print(f"IP 목록 설정 중: {ips}")
|
|
client.account.set_acl(ips)
|
|
print("완료!")
|
|
list_acl(client)
|
|
|
|
|
|
def clear_acl(client):
|
|
"""ACL 초기화"""
|
|
print("ACL 초기화 중 (모든 IP 허용)...")
|
|
client.account.clear_acl()
|
|
print("완료!")
|
|
list_acl(client)
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description="Vultr ACL 관리")
|
|
subparsers = parser.add_subparsers(dest="command", help="명령")
|
|
|
|
# list 명령
|
|
subparsers.add_parser("list", help="ACL 목록 조회")
|
|
|
|
# add 명령
|
|
add_parser = subparsers.add_parser("add", help="IP 추가")
|
|
add_parser.add_argument("ip", help="추가할 IP/CIDR (예: 192.168.1.1/32)")
|
|
|
|
# remove 명령
|
|
remove_parser = subparsers.add_parser("remove", help="IP 제거")
|
|
remove_parser.add_argument("ip", help="제거할 IP/CIDR")
|
|
|
|
# set 명령
|
|
set_parser = subparsers.add_parser("set", help="IP 목록 설정")
|
|
set_parser.add_argument("ips", nargs="+", help="설정할 IP/CIDR 목록")
|
|
|
|
# clear 명령
|
|
subparsers.add_parser("clear", help="ACL 초기화 (모든 IP 허용)")
|
|
|
|
args = parser.parse_args()
|
|
|
|
if not args.command:
|
|
parser.print_help()
|
|
sys.exit(1)
|
|
|
|
try:
|
|
client = get_client()
|
|
|
|
if args.command == "list":
|
|
list_acl(client)
|
|
elif args.command == "add":
|
|
add_ip(client, args.ip)
|
|
elif args.command == "remove":
|
|
remove_ip(client, args.ip)
|
|
elif args.command == "set":
|
|
set_ips(client, args.ips)
|
|
elif args.command == "clear":
|
|
clear_acl(client)
|
|
|
|
except VultrAPIError as e:
|
|
print(f"API 오류: {e.message}")
|
|
sys.exit(1)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|