github.com/sacloud/iaas-api-go@v1.12.0/example_test.go (about) 1 // Copyright 2022-2023 The sacloud/iaas-api-go Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package iaas_test 16 17 import ( 18 "context" 19 "encoding/json" 20 "fmt" 21 "log" 22 "net/http" 23 "os" 24 25 "github.com/sacloud/iaas-api-go" 26 "github.com/sacloud/iaas-api-go/helper/power" 27 "github.com/sacloud/iaas-api-go/naked" 28 "github.com/sacloud/iaas-api-go/types" 29 "github.com/sacloud/packages-go/size" 30 ) 31 32 func Example_basic() { 33 // APIクライアントの基本的な使い方の例 34 35 // APIキー 36 token := os.Getenv("SAKURACLOUD_ACCESS_TOKEN") 37 secret := os.Getenv("SAKURACLOUD_ACCESS_TOKEN_SECRET") 38 39 // クライアントの作成 40 client := iaas.NewClient(token, secret) 41 42 // スイッチの作成 43 swOp := iaas.NewSwitchOp(client) 44 sw, err := swOp.Create(context.Background(), "is1a", &iaas.SwitchCreateRequest{ 45 Name: "libsacloud-example", 46 Description: "description", 47 Tags: types.Tags{"tag1", "tag2"}, 48 }) 49 if err != nil { 50 log.Fatal(err) 51 } 52 53 fmt.Printf("Name: %s", sw.Name) 54 } 55 56 func Example_serverCRUD() { 57 // ServerのCRUDを行う例 58 59 // Note: サーバの作成を行いたい場合、通常はgithub.com/libsacloud/v2/utils/serverパッケージを利用してください。 60 // この例はServer APIを直接利用したい場合向けです。 61 62 // APIキー 63 token := os.Getenv("SAKURACLOUD_ACCESS_TOKEN") 64 secret := os.Getenv("SAKURACLOUD_ACCESS_TOKEN_SECRET") 65 66 // クライアントの作成 67 client := iaas.NewClient(token, secret) 68 69 // サーバの作成(ディスクレス) 70 ctx := context.Background() 71 serverOp := iaas.NewServerOp(client) 72 server, err := serverOp.Create(ctx, "is1a", &iaas.ServerCreateRequest{ 73 CPU: 1, 74 MemoryMB: 1 * size.GiB, 75 ServerPlanCommitment: types.Commitments.Standard, 76 ServerPlanGeneration: types.PlanGenerations.Default, 77 ConnectedSwitches: []*iaas.ConnectedSwitch{{Scope: types.Scopes.Shared}}, 78 InterfaceDriver: types.InterfaceDrivers.VirtIO, 79 Name: "libsacloud-example", 80 Description: "description", 81 Tags: types.Tags{"tag1", "tag2"}, 82 //IconID: 0, 83 WaitDiskMigration: false, 84 }) 85 if err != nil { 86 log.Fatal(err) 87 } 88 89 // 更新 90 server, err = serverOp.Update(ctx, "is1a", server.ID, &iaas.ServerUpdateRequest{ 91 Name: "libsacloud-example-updated", 92 Description: "description-updated", 93 Tags: types.Tags{"tag1-updated", "tag2-updated"}, 94 // IconID: 0, 95 }) 96 if err != nil { 97 log.Fatal(err) 98 } 99 100 // 起動 101 if err := power.BootServer(ctx, serverOp, "is1a", server.ID); err != nil { 102 log.Fatal(err) 103 } 104 105 // シャットダウン(force) 106 if err := power.ShutdownServer(ctx, serverOp, "is1a", server.ID, true); err != nil { 107 log.Fatal(err) 108 } 109 110 // 削除 111 if err := serverOp.Delete(ctx, "is1a", server.ID); err != nil { 112 log.Fatal(err) 113 } 114 } 115 116 func ExampleClient_Do_direct() { 117 // iaas.Clientを直接利用する例 118 // Note: 通常はiaas.xxxOpを通じて操作してください。 119 120 // クライアントの作成 121 if os.Getenv("SAKURACLOUD_ACCESS_TOKEN") == "" || 122 os.Getenv("SAKURACLOUD_ACCESS_TOKEN_SECRET") == "" { 123 log.Fatal("required: SAKURACLOUD_ACCESS_TOKEN and SAKURACLOUD_ACCESS_TOKEN_SECRET") 124 } 125 client := iaas.NewClientFromEnv() 126 127 // ゾーン一覧を取得する例 128 url := "https://secure.sakura.ad.jp/cloud/zone/is1a/api/cloud/1.1/zone" 129 data, err := client.Do(context.Background(), http.MethodGet, url, nil) 130 if err != nil { 131 log.Fatal(err) 132 } 133 134 var zones map[string]interface{} 135 err = json.Unmarshal(data, &zones) 136 if err != nil { 137 log.Fatal(err) 138 } 139 140 fmt.Print(zones) 141 } 142 143 func ExampleClient_Do_withNaked() { 144 // iaas.Clientを直接利用する例 145 // レスポンスとしてnakedパッケージを利用する 146 // Note: 通常はiaas.xxxOpを通じて操作してください。 147 148 // クライアントの作成 149 if os.Getenv("SAKURACLOUD_ACCESS_TOKEN") == "" || 150 os.Getenv("SAKURACLOUD_ACCESS_TOKEN_SECRET") == "" { 151 log.Fatal("required: SAKURACLOUD_ACCESS_TOKEN and SAKURACLOUD_ACCESS_TOKEN_SECRET") 152 } 153 client := iaas.NewClientFromEnv() 154 155 // ゾーン一覧を取得する例 156 url := "https://secure.sakura.ad.jp/cloud/zone/is1a/api/cloud/1.1/zone" 157 data, err := client.Do(context.Background(), http.MethodGet, url, nil) 158 if err != nil { 159 log.Fatal(err) 160 } 161 162 // レスポンスを受けるためのstruct 163 type searchResult struct { 164 Zones []*naked.Zone 165 } 166 result := &searchResult{} 167 err = json.Unmarshal(data, &result) 168 if err != nil { 169 log.Fatal(err) 170 } 171 172 for _, zone := range result.Zones { 173 fmt.Printf("ID: %v Name: %v\n", zone.ID, zone.Name) 174 } 175 }