-
Notifications
You must be signed in to change notification settings - Fork 5
/
example_test.go
175 lines (148 loc) · 4.98 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
// Copyright 2022-2023 The sacloud/iaas-api-go Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package iaas_test
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"github.com/sacloud/iaas-api-go"
"github.com/sacloud/iaas-api-go/helper/power"
"github.com/sacloud/iaas-api-go/naked"
"github.com/sacloud/iaas-api-go/types"
"github.com/sacloud/packages-go/size"
)
func Example_basic() {
// APIクライアントの基本的な使い方の例
// APIキー
token := os.Getenv("SAKURACLOUD_ACCESS_TOKEN")
secret := os.Getenv("SAKURACLOUD_ACCESS_TOKEN_SECRET")
// クライアントの作成
client := iaas.NewClient(token, secret)
// スイッチの作成
swOp := iaas.NewSwitchOp(client)
sw, err := swOp.Create(context.Background(), "is1a", &iaas.SwitchCreateRequest{
Name: "libsacloud-example",
Description: "description",
Tags: types.Tags{"tag1", "tag2"},
})
if err != nil {
log.Fatal(err)
}
fmt.Printf("Name: %s", sw.Name)
}
func Example_serverCRUD() {
// ServerのCRUDを行う例
// Note: サーバの作成を行いたい場合、通常はgithub.com/libsacloud/v2/utils/serverパッケージを利用してください。
// この例はServer APIを直接利用したい場合向けです。
// APIキー
token := os.Getenv("SAKURACLOUD_ACCESS_TOKEN")
secret := os.Getenv("SAKURACLOUD_ACCESS_TOKEN_SECRET")
// クライアントの作成
client := iaas.NewClient(token, secret)
// サーバの作成(ディスクレス)
ctx := context.Background()
serverOp := iaas.NewServerOp(client)
server, err := serverOp.Create(ctx, "is1a", &iaas.ServerCreateRequest{
CPU: 1,
MemoryMB: 1 * size.GiB,
ServerPlanCommitment: types.Commitments.Standard,
ServerPlanGeneration: types.PlanGenerations.Default,
ConnectedSwitches: []*iaas.ConnectedSwitch{{Scope: types.Scopes.Shared}},
InterfaceDriver: types.InterfaceDrivers.VirtIO,
Name: "libsacloud-example",
Description: "description",
Tags: types.Tags{"tag1", "tag2"},
//IconID: 0,
WaitDiskMigration: false,
})
if err != nil {
log.Fatal(err)
}
// 更新
server, err = serverOp.Update(ctx, "is1a", server.ID, &iaas.ServerUpdateRequest{
Name: "libsacloud-example-updated",
Description: "description-updated",
Tags: types.Tags{"tag1-updated", "tag2-updated"},
// IconID: 0,
})
if err != nil {
log.Fatal(err)
}
// 起動
if err := power.BootServer(ctx, serverOp, "is1a", server.ID); err != nil {
log.Fatal(err)
}
// シャットダウン(force)
if err := power.ShutdownServer(ctx, serverOp, "is1a", server.ID, true); err != nil {
log.Fatal(err)
}
// 削除
if err := serverOp.Delete(ctx, "is1a", server.ID); err != nil {
log.Fatal(err)
}
}
func ExampleClient_Do_direct() {
// iaas.Clientを直接利用する例
// Note: 通常はiaas.xxxOpを通じて操作してください。
// クライアントの作成
if os.Getenv("SAKURACLOUD_ACCESS_TOKEN") == "" ||
os.Getenv("SAKURACLOUD_ACCESS_TOKEN_SECRET") == "" {
log.Fatal("required: SAKURACLOUD_ACCESS_TOKEN and SAKURACLOUD_ACCESS_TOKEN_SECRET")
}
client := iaas.NewClientFromEnv()
// ゾーン一覧を取得する例
url := "https://secure.sakura.ad.jp/cloud/zone/is1a/api/cloud/1.1/zone"
data, err := client.Do(context.Background(), http.MethodGet, url, nil)
if err != nil {
log.Fatal(err)
}
var zones map[string]interface{}
err = json.Unmarshal(data, &zones)
if err != nil {
log.Fatal(err)
}
fmt.Print(zones)
}
func ExampleClient_Do_withNaked() {
// iaas.Clientを直接利用する例
// レスポンスとしてnakedパッケージを利用する
// Note: 通常はiaas.xxxOpを通じて操作してください。
// クライアントの作成
if os.Getenv("SAKURACLOUD_ACCESS_TOKEN") == "" ||
os.Getenv("SAKURACLOUD_ACCESS_TOKEN_SECRET") == "" {
log.Fatal("required: SAKURACLOUD_ACCESS_TOKEN and SAKURACLOUD_ACCESS_TOKEN_SECRET")
}
client := iaas.NewClientFromEnv()
// ゾーン一覧を取得する例
url := "https://secure.sakura.ad.jp/cloud/zone/is1a/api/cloud/1.1/zone"
data, err := client.Do(context.Background(), http.MethodGet, url, nil)
if err != nil {
log.Fatal(err)
}
// レスポンスを受けるためのstruct
type searchResult struct {
Zones []*naked.Zone
}
result := &searchResult{}
err = json.Unmarshal(data, &result)
if err != nil {
log.Fatal(err)
}
for _, zone := range result.Zones {
fmt.Printf("ID: %v Name: %v\n", zone.ID, zone.Name)
}
}