-
Notifications
You must be signed in to change notification settings - Fork 44
/
example_test.go
218 lines (187 loc) · 5.45 KB
/
example_test.go
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package scalewaysdkgo
import (
"fmt"
"log"
"time"
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
"github.com/scaleway/scaleway-sdk-go/api/lb/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
)
func Example_apiClient() {
// Create a Scaleway client
client, err := scw.NewClient(
scw.WithAuth("ACCESS_KEY", "SECRET_KEY"), // Get your credentials at https://console.scaleway.com/project/credentials
)
if err != nil {
// handle error
log.Fatal(err)
}
// Create SDK objects for specific Scaleway Products
instanceAPI := instance.NewAPI(client)
lbAPI := lb.NewZonedAPI(client)
// Start using the SDKs
_, _ = instanceAPI, lbAPI
}
func Example_apiClientWithConfig() {
// Get Scaleway Config
config, err := scw.LoadConfig()
if err != nil {
// handle error
log.Fatal(err)
}
// Use active profile
profile, err := config.GetActiveProfile()
if err != nil {
// handle error
log.Fatal(err)
}
// Create a Scaleway client
client, err := scw.NewClient(
scw.WithProfile(profile),
scw.WithEnv(), // env variable may overwrite profile values
)
if err != nil {
// handle error
log.Fatal(err)
}
// Create SDK objects for specific Scaleway Products
instanceAPI := instance.NewAPI(client)
lbAPI := lb.NewZonedAPI(client)
// Start using the SDKs
_, _ = instanceAPI, lbAPI
}
func Example_listServers() {
// Create a Scaleway client
client, err := scw.NewClient(
scw.WithAuth("ACCESS_KEY", "SECRET_KEY"), // Get your credentials at https://console.scaleway.com/project/credentials
)
if err != nil {
// handle error
log.Fatal(err)
}
// Create SDK objects for Scaleway Instance product
instanceAPI := instance.NewAPI(client)
// Call the ListServers method on the Instance SDK
response, err := instanceAPI.ListServers(&instance.ListServersRequest{
Zone: scw.ZoneFrPar1,
})
if err != nil {
// handle error
log.Fatal(err)
}
// Do something with the response...
fmt.Println(response)
}
func Example_listServersWithZones() {
// Create a Scaleway client
client, err := scw.NewClient(
scw.WithAuth("ACCESS_KEY", "SECRET_KEY"), // Get your credentials at https://console.scaleway.com/project/credentials
)
if err != nil {
// handle error
log.Fatal(err)
}
// Create SDK objects for Scaleway Instance product
instanceAPI := instance.NewAPI(client)
// Call the ListServers method on the Instance SDK
response, err := instanceAPI.ListServers(&instance.ListServersRequest{},
// Add WithZones option to list servers from multiple zones
scw.WithZones(scw.ZoneFrPar1, scw.ZoneNlAms1, scw.ZonePlWaw1))
if err != nil {
// handle error
log.Fatal(err)
}
// Do something with the response...
fmt.Println(response)
}
func Example_createServer() {
// Create a Scaleway client
client, err := scw.NewClient(
scw.WithAuth("ACCESS_KEY", "SECRET_KEY"), // Get your credentials at https://console.scaleway.com/project/credentials
scw.WithDefaultOrganizationID("ORGANIZATION_ID"),
scw.WithDefaultZone(scw.ZoneFrPar1),
)
if err != nil {
panic(err)
}
// Create SDK objects for Scaleway Instance and marketplace
instanceAPI := instance.NewAPI(client)
serverType := "DEV1-S"
image := "ubuntu_focal"
// Create a new DEV1-S server
createRes, err := instanceAPI.CreateServer(&instance.CreateServerRequest{
Name: "my-server-01",
CommercialType: serverType,
Image: scw.StringPtr(image),
DynamicIPRequired: scw.BoolPtr(true),
})
if err != nil {
panic(err)
}
// Start the server and wait until it's ready.
timeout := 5 * time.Minute
err = instanceAPI.ServerActionAndWait(&instance.ServerActionAndWaitRequest{
ServerID: createRes.Server.ID,
Action: instance.ServerActionPoweron,
Timeout: &timeout,
})
if err != nil {
panic(err)
}
}
func Example_rebootAllServers() {
// Create a Scaleway client
client, err := scw.NewClient(
scw.WithAuth("ACCESS_KEY", "SECRET_KEY"), // Get your credentials at https://console.scaleway.com/project/credentials
scw.WithDefaultZone(scw.ZoneFrPar1),
)
if err != nil {
panic(err)
}
// Create SDK objects for Scaleway Instance product
instanceAPI := instance.NewAPI(client)
// Call the ListServers method of the Instance SDK
response, err := instanceAPI.ListServers(&instance.ListServersRequest{})
if err != nil {
panic(err)
}
// For each server if they are running we reboot them using ServerActionAndWait
timeout := 5 * time.Minute
for _, server := range response.Servers {
if server.State == instance.ServerStateRunning {
fmt.Println("Rebooting server with ID", server.ID)
err = instanceAPI.ServerActionAndWait(&instance.ServerActionAndWaitRequest{
ServerID: server.ID,
Action: instance.ServerActionReboot,
Timeout: &timeout,
})
if err != nil {
panic(err)
}
}
}
fmt.Println("All servers were successfully rebooted")
}
func Example_createLoadBalancer() {
// Create a Scaleway client
client, err := scw.NewClient(
scw.WithAuth("ACCESS_KEY", "SECRET_KEY"), // Get your credentials at https://console.scaleway.com/project/credentials
)
if err != nil {
// handle error
log.Fatal(err)
}
// Create SDK objects for Scaleway LoadConfig Balancer product
lbAPI := lb.NewZonedAPI(client)
// Call the CreateLb method on the LB SDK to create a new load balancer.
newLB, err := lbAPI.CreateLB(&lb.ZonedAPICreateLBRequest{
Name: "My new load balancer",
Description: "This is a example of a load balancer",
})
if err != nil {
// handle error
log.Fatal(err)
}
// Do something with the newly created LB...
fmt.Println(newLB)
}