Skip to content

Commit 08eb0b7

Browse files
authored
doc: add reboot all server example (#168)
1 parent ede2cb7 commit 08eb0b7

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

example_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package scalewaysdkgo
22

33
import (
44
"fmt"
5+
"time"
56

67
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
78
"github.com/scaleway/scaleway-sdk-go/api/lb/v1"
@@ -84,6 +85,43 @@ func Example_listServers() {
8485
fmt.Println(response)
8586
}
8687

88+
func Example_rebootAllServers() {
89+
90+
// Create a Scaleway client
91+
client, err := scw.NewClient(
92+
scw.WithAuth("ACCESS_KEY", "SECRET_KEY"), // Get your credentials at https://console.scaleway.com/account/credentials
93+
scw.WithDefaultZone(scw.ZoneFrPar1),
94+
)
95+
if err != nil {
96+
panic(err)
97+
}
98+
99+
// Create SDK objects for Scaleway Instance product
100+
instanceAPI := instance.NewAPI(client)
101+
102+
// Call the ListServers method of the Instance SDK
103+
response, err := instanceAPI.ListServers(&instance.ListServersRequest{})
104+
if err != nil {
105+
panic(err)
106+
}
107+
108+
// For each server if they are running we reboot them using ServerActionAndWait
109+
for _, server := range response.Servers {
110+
if server.State == instance.ServerStateRunning {
111+
fmt.Println("Rebooting server with ID", server.ID)
112+
err = instanceAPI.ServerActionAndWait(&instance.ServerActionAndWaitRequest{
113+
ServerID: server.ID,
114+
Action: instance.ServerActionReboot,
115+
Timeout: 5 * time.Minute,
116+
})
117+
if err != nil {
118+
panic(err)
119+
}
120+
}
121+
}
122+
fmt.Println("All servers were successfully rebooted")
123+
}
124+
87125
func Example_createLoadBalancer() {
88126

89127
// Create a Scaleway client

0 commit comments

Comments
 (0)