github.com/sacloud/libsacloud/v2@v2.32.3/helper/builder/server/example_test.go (about)

     1  // Copyright 2016-2022 The Libsacloud 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 server_test
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"log"
    21  	"os"
    22  
    23  	"github.com/sacloud/libsacloud/v2/helper/builder/disk"
    24  
    25  	"github.com/sacloud/libsacloud/v2/helper/builder/server"
    26  	"github.com/sacloud/libsacloud/v2/sacloud"
    27  	"github.com/sacloud/libsacloud/v2/sacloud/ostype"
    28  	"github.com/sacloud/libsacloud/v2/sacloud/types"
    29  )
    30  
    31  func Example_builder() {
    32  	// APIキー
    33  	token := os.Getenv("SAKURACLOUD_ACCESS_TOKEN")
    34  	secret := os.Getenv("SAKURACLOUD_ACCESS_TOKEN_SECRET")
    35  
    36  	// クライアントの作成
    37  	client := sacloud.NewClient(token, secret)
    38  
    39  	// ビルダーの準備
    40  	builder := &server.Builder{
    41  		Client:   server.NewBuildersAPIClient(client),
    42  		Name:     "libsacloud-example",
    43  		CPU:      2,
    44  		MemoryGB: 4,
    45  		// Commitment:      types.Commitments.Standard,
    46  		// Generation:      types.PlanGenerations.Default,
    47  		// InterfaceDriver: types.InterfaceDrivers.VirtIO,
    48  		Description:     "description",
    49  		Tags:            types.Tags{"tag1", "tag2"},
    50  		BootAfterCreate: true,
    51  		NIC:             &server.SharedNICSetting{
    52  			// PacketFilterID: types.ID(123456789012),
    53  		},
    54  		//AdditionalNICs: []server.AdditionalNICSettingHolder{
    55  		//	&server.ConnectedNICSetting{
    56  		//		SwitchID:        types.ID(123456789012),
    57  		//		DisplayIPAddress: "192.168.0.1",
    58  		//		PacketFilterID:  types.ID(123456789012),
    59  		//	},
    60  		//},
    61  
    62  		// IconID:          types.ID(123456789012),
    63  		// CDROMID:         types.ID(123456789012),
    64  		// PrivateHostID:   types.ID(123456789012),
    65  
    66  		DiskBuilders: []disk.Builder{
    67  			&disk.FromUnixBuilder{
    68  				Client:     disk.NewBuildersAPIClient(client),
    69  				OSType:     ostype.CentOS,
    70  				Name:       "libsacloud-example",
    71  				SizeGB:     20,
    72  				PlanID:     types.DiskPlans.SSD,
    73  				Connection: types.DiskConnections.VirtIO,
    74  				//DistantFrom:     []types.ID{types.ID(123456789012)},
    75  				Description: "description",
    76  				Tags:        types.Tags{"tag1", "tag2"},
    77  				EditParameter: &disk.UnixEditRequest{
    78  					HostName:            "libsacloud-example", // ホスト名
    79  					Password:            "P@ssW0rd",           // パスワード
    80  					DisablePWAuth:       false,                // パスワード認証の無効化
    81  					ChangePartitionUUID: true,                 // UUIDの変更
    82  					EnableDHCP:          false,                // DHCPの有効化
    83  
    84  					//IPAddress:                 "192.168.11.11",                    // IPアドレス(スイッチ or スイッチ+ルータに接続する場合)
    85  					//NetworkMaskLen:            24,                                 // ネットワークマスク長(スイッチ or スイッチ+ルータに接続する場合)
    86  					//DefaultRoute:              "192.168.11.1",                     // デフォルトルート(スイッチ or スイッチ+ルータに接続する場合)
    87  					//SSHKeys:                   []string{sshKey1, sshKey2},         // 公開鍵(文字列で指定)
    88  					//SSHKeyIDs:                 []types.ID{types.ID(123456789012)}, // 公開鍵(IDで指定)
    89  					//IsSSHKeysEphemeral:        false,                              // 公開鍵をさくらのクラウド側で生成した場合にサーバ作成後に該当鍵の削除を行うか
    90  					//GenerateSSHKeyName:        "",                                 // 生成する公開鍵の名称
    91  					//GenerateSSHKeyPassPhrase:  "",                                 // 生成する公開鍵のパスフレーズ
    92  					//GenerateSSHKeyDescription: "",                                 // 生成する公開鍵の説明
    93  					//IsNotesEphemeral:          false,                              // Notesで指定したスタートアップスクリプトをサーバ作成後に削除するか
    94  					//NoteContents:              []string{note1, note2},             // スタートアップスクリプト(文字列で指定)
    95  					//Notes:                     []*sacloud.DiskEditNote{ID:types.ID(123456789012)}, // スタートアップスクリプト(ID+パラメータで指定)
    96  				},
    97  				// IconID:          0,
    98  			},
    99  		},
   100  	}
   101  
   102  	result, err := builder.Build(context.Background(), "is1a")
   103  	if err != nil {
   104  		log.Fatal(err)
   105  	}
   106  
   107  	fmt.Printf("ServerID: %s", result.ServerID)
   108  }