Skip to content

Commit 2b70c4a

Browse files
committed
Rework Usage Documentation
1 parent 120b01c commit 2b70c4a

3 files changed

Lines changed: 173 additions & 26 deletions

File tree

docs/usage.rst

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,24 @@ To use Hetzner Cloud Python in a project(Procedural style)::
77
from hcloud import HcloudClient
88

99
# Create a client
10+
from hcloud.images.domain import Image
1011
from hcloud.servers.domain import Server
12+
from hcloud.server_types.domain import ServerType
1113
from hcloud.volumes.domain import Volume
1214

1315
client = HcloudClient(token="project-token")
1416

1517
# Create 2 servers
1618
response1 = client.servers.create(
17-
"Server1",
18-
"cx11",
19-
4711
19+
name="Server1",
20+
server_type=ServerType(name="cx11"),
21+
image=Image(id=4711)
2022
)
2123

2224
response2 = client.servers.create(
2325
"Server2",
24-
"cx11",
25-
4711
26+
server_type=ServerType(name="cx11"),
27+
image=Image(id=4711)
2628
)
2729

2830
server1 = response1.server
@@ -38,12 +40,14 @@ To use Hetzner Cloud Python in a project(Procedural style)::
3840
# Create 2 volumes
3941

4042
response1 = client.volumes.create(
41-
30,
42-
"Volume1"
43+
size=15,
44+
name="Volume1",
45+
location=server1.location
4346
)
4447
response2 = client.volumes.create(
45-
10,
46-
"Volume2"
48+
size=10,
49+
name="Volume2",
50+
location=server2.location
4751
)
4852

4953
volume1 = response1.volume
@@ -65,8 +69,8 @@ To use Hetzner Cloud Python in a project(Procedural style)::
6569

6670
server33 = Server(id=33)
6771
response = client.volumes.create(
68-
33,
69-
"Volume33",
72+
size=33,
73+
name="Volume33",
7074
server=server33
7175
)
7276

@@ -75,17 +79,18 @@ To use Hetzner Cloud Python in a project(Procedural style)::
7579

7680
# Create one more server and attach 2 volumes to it
7781
client.servers.create(
78-
"Server2",
79-
"cx11",
80-
4711,
82+
"Server3",
83+
server_type=ServerType(name="cx11"),
84+
image=Image(id=4711),
8185
volumes=[Volume(id=221), Volume(id=222)]
8286
)
8387

8488

8589
To use Hetzner Cloud Python in a project(OO style)::
8690

8791
from hcloud import HcloudClient
88-
92+
from hcloud.images.domain import Image
93+
from hcloud.server_types.domain import ServerType
8994

9095
# Create a client
9196
client = HcloudClient(token="project-token")
@@ -94,31 +99,34 @@ To use Hetzner Cloud Python in a project(OO style)::
9499
# Create 2 servers
95100
response1 = client.servers.create(
96101
"Server1",
97-
"cx11",
98-
4711
102+
server_type=ServerType(name="cx11"),
103+
image=Image(id=4711)
99104
)
100105

101106
response2 = client.servers.create(
102107
"Server2",
103-
"cx11",
104-
4711
108+
server_type=ServerType(name="cx11"),
109+
image=Image(id=4711)
105110
)
106111
# Get all servers
112+
server1 = response1.server
113+
server2 = response2.server
107114

108115
servers = client.servers.get_all()
109116

110-
assert servers[0].id == response1.server.id
111-
assert servers[1].id == response2.server.id
112-
117+
assert servers[0].id == server1.id
118+
assert servers[1].id == server2.id
113119
# Create 2 volumes
114120

115121
response1 = client.volumes.create(
116-
30,
117-
"Volume1"
122+
size=15,
123+
name="Volume1",
124+
location=server1.location
118125
)
119126
response2 = client.volumes.create(
120-
10,
121-
"Volume2"
127+
size=10,
128+
name="Volume2",
129+
location=server2.location
122130
)
123131

124132
volume1 = response1.volume
@@ -135,3 +143,9 @@ To use Hetzner Cloud Python in a project(OO style)::
135143

136144
# Poweroff 2nd server
137145
server2.power_off()
146+
147+
# Poweroff 2nd server
148+
server2.power_off()
149+
150+
151+
More samples are in the repository: https://github.com/hetznercloud/hcloud-python/tree/master/examples.

examples/usage_oop.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
from hcloud import HcloudClient
2+
from hcloud.images.domain import Image
3+
from hcloud.server_types.domain import ServerType
4+
5+
# Create a client
6+
client = HcloudClient(token="project-token")
7+
8+
# Create 2 servers
9+
# Create 2 servers
10+
response1 = client.servers.create(
11+
"Server1",
12+
server_type=ServerType(name="cx11"),
13+
image=Image(id=4711)
14+
)
15+
16+
response2 = client.servers.create(
17+
"Server2",
18+
server_type=ServerType(name="cx11"),
19+
image=Image(id=4711)
20+
)
21+
# Get all servers
22+
server1 = response1.server
23+
server2 = response2.server
24+
25+
servers = client.servers.get_all()
26+
27+
assert servers[0].id == server1.id
28+
assert servers[1].id == server2.id
29+
# Create 2 volumes
30+
31+
response1 = client.volumes.create(
32+
size=15,
33+
name="Volume1",
34+
location=server1.location
35+
)
36+
response2 = client.volumes.create(
37+
size=10,
38+
name="Volume2",
39+
location=server2.location
40+
)
41+
42+
volume1 = response1.volume
43+
volume2 = response2.volume
44+
45+
# Attach volume to server
46+
47+
volume1.attach(server1)
48+
volume2.attach(server2)
49+
50+
# Detach second volume
51+
52+
volume2.detach()
53+
54+
# Poweroff 2nd server
55+
server2.power_off()

examples/usage_procedurale.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
from hcloud import HcloudClient
2+
3+
from hcloud.images.domain import Image
4+
from hcloud.servers.domain import Server
5+
from hcloud.server_types.domain import ServerType
6+
from hcloud.volumes.domain import Volume
7+
8+
client = HcloudClient(token="project-token")
9+
10+
# Create 2 servers
11+
response1 = client.servers.create(
12+
name="Server1",
13+
server_type=ServerType(name="cx11"),
14+
image=Image(id=4711)
15+
)
16+
17+
response2 = client.servers.create(
18+
"Server2",
19+
server_type=ServerType(name="cx11"),
20+
image=Image(id=4711)
21+
)
22+
23+
server1 = response1.server
24+
server2 = response2.server
25+
26+
# Get all servers
27+
28+
servers = client.servers.get_all()
29+
30+
assert servers[0].id == server1.id
31+
assert servers[1].id == server2.id
32+
33+
# Create 2 volumes
34+
35+
response1 = client.volumes.create(
36+
size=15,
37+
name="Volume1",
38+
location=server1.location
39+
)
40+
response2 = client.volumes.create(
41+
size=10,
42+
name="Volume2",
43+
location=server2.location
44+
)
45+
46+
volume1 = response1.volume
47+
volume2 = response2.volume
48+
49+
# Attach volume to server
50+
51+
client.volumes.attach(server1, volume1)
52+
client.volumes.attach(server2, volume2)
53+
54+
# Detach second volume
55+
56+
client.volumes.detach(volume2)
57+
58+
# Poweroff 2nd server
59+
client.servers.power_off(server2)
60+
61+
# Create one more volume and attach it to server with id=33
62+
63+
server33 = Server(id=33)
64+
response = client.volumes.create(
65+
size=33,
66+
name="Volume33",
67+
server=server33
68+
)
69+
70+
print(response.action.status)
71+
72+
# Create one more server and attach 2 volumes to it
73+
client.servers.create(
74+
"Server3",
75+
server_type=ServerType(name="cx11"),
76+
image=Image(id=4711),
77+
volumes = [Volume(id=221), Volume(id=222)]
78+
)

0 commit comments

Comments
 (0)