-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfloating_ips.py
More file actions
86 lines (65 loc) · 3.19 KB
/
floating_ips.py
File metadata and controls
86 lines (65 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from __future__ import annotations
import os
from gcore import Gcore
def main() -> None:
# TODO set API key before running
# api_key = os.environ["GCORE_API_KEY"]
# TODO set cloud project ID before running
# cloud_project_id = os.environ["GCORE_CLOUD_PROJECT_ID"]
# TODO set cloud region ID before running
# cloud_region_id = os.environ["GCORE_CLOUD_REGION_ID"]
# TODO set cloud port ID before running
cloud_port_id = os.environ["GCORE_CLOUD_PORT_ID"]
gcore = Gcore(
# No need to explicitly pass to Gcore constructor if using environment variables
# api_key=api_key,
# cloud_project_id=cloud_project_id,
# cloud_region_id=cloud_region_id,
)
floating_ip_id = create_floating_ip(client=gcore)
list_floating_ips(client=gcore)
get_floating_ip(client=gcore, floating_ip_id=floating_ip_id)
assign_floating_ip(client=gcore, floating_ip_id=floating_ip_id, port_id=cloud_port_id)
unassign_floating_ip(client=gcore, floating_ip_id=floating_ip_id)
delete_floating_ip(client=gcore, floating_ip_id=floating_ip_id)
def create_floating_ip(*, client: Gcore) -> str:
print("\n=== CREATE FLOATING IP ===")
floating_ip = client.cloud.floating_ips.create_and_poll(tags={"name": "gcore-go-example"})
print(f"Created Floating IP: ID={floating_ip.id}")
print("========================")
return floating_ip.id
def list_floating_ips(*, client: Gcore) -> None:
print("\n=== LIST FLOATING IPS ===")
floating_ips = client.cloud.floating_ips.list()
for count, ip in enumerate(floating_ips, 1):
print(f"{count}. Floating IP: ID={ip.id}, status={ip.status}, floating IP address={ip.floating_ip_address}")
if not floating_ips:
print("No floating IPs found.")
print("========================")
def get_floating_ip(*, client: Gcore, floating_ip_id: str) -> None:
print("\n=== GET FLOATING IP ===")
floating_ip = client.cloud.floating_ips.get(floating_ip_id=floating_ip_id)
print(
f"Floating IP: ID={floating_ip.id}, status={floating_ip.status}, floating IP address={floating_ip.floating_ip_address}"
)
print("========================")
def assign_floating_ip(*, client: Gcore, floating_ip_id: str, port_id: str) -> None:
print("\n=== ASSIGN FLOATING IP ===")
floating_ip = client.cloud.floating_ips.assign( # pyright: ignore[reportDeprecated]
floating_ip_id=floating_ip_id,
port_id=port_id,
)
print(f"Assigned floating IP: ID={floating_ip.id}, port ID={floating_ip.port_id}")
print("========================")
def unassign_floating_ip(*, client: Gcore, floating_ip_id: str) -> None:
print("\n=== UNASSIGN FLOATING IP ===")
floating_ip = client.cloud.floating_ips.unassign(floating_ip_id=floating_ip_id) # pyright: ignore[reportDeprecated]
print(f"Unassigned floating IP: ID={floating_ip.id}")
print("========================")
def delete_floating_ip(*, client: Gcore, floating_ip_id: str) -> None:
print("\n=== DELETE FLOATING IP ===")
client.cloud.floating_ips.delete_and_poll(floating_ip_id=floating_ip_id)
print(f"Deleted floating IP: ID={floating_ip_id}")
print("========================")
if __name__ == "__main__":
main()