File: usage_oop.py

package info (click to toggle)
hcloud-python 2.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,396 kB
  • sloc: python: 15,311; makefile: 43; javascript: 3
file content (52 lines) | stat: -rw-r--r-- 1,203 bytes parent folder | download
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
from __future__ import annotations

from os import environ

from hcloud import Client
from hcloud.images import Image
from hcloud.server_types import ServerType

assert (
    "HCLOUD_TOKEN" in environ
), "Please export your API token in the HCLOUD_TOKEN environment variable"
token = environ["HCLOUD_TOKEN"]

# Create a client
client = Client(token=token)

# Create 2 servers
# Create 2 servers
response1 = client.servers.create(
    "Server1", server_type=ServerType(name="cx22"), image=Image(id=4711)
)

response2 = client.servers.create(
    "Server2", server_type=ServerType(name="cx22"), image=Image(id=4711)
)
# Get all servers
server1 = response1.server
server2 = response2.server

servers = client.servers.get_all()

assert servers[0].id == server1.id
assert servers[1].id == server2.id
# Create 2 volumes

response1 = client.volumes.create(size=15, name="Volume1", location=server1.location)
response2 = client.volumes.create(size=10, name="Volume2", location=server2.location)

volume1 = response1.volume
volume2 = response2.volume

# Attach volume to server

volume1.attach(server1)
volume2.attach(server2)

# Detach second volume

volume2.detach()

# Poweroff 2nd server
server2.power_off()