github.com/sacloud/libsacloud/v2@v2.32.3/helper/service/disk/edit_request.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 disk
    16  
    17  import (
    18  	"github.com/sacloud/libsacloud/v2/helper/validate"
    19  	"github.com/sacloud/libsacloud/v2/sacloud"
    20  	"github.com/sacloud/libsacloud/v2/sacloud/types"
    21  )
    22  
    23  type EditRequest struct {
    24  	Zone string   `request:"-" validate:"required"`
    25  	ID   types.ID `request:"-"`
    26  
    27  	NoWait bool `request:"-"` // trueの場合ディスクの修正完了まで待たずに即時復帰する
    28  
    29  	HostName string
    30  	Password string
    31  
    32  	DisablePWAuth       bool
    33  	EnableDHCP          bool
    34  	ChangePartitionUUID bool
    35  
    36  	IPAddress      string
    37  	NetworkMaskLen int
    38  	DefaultRoute   string
    39  
    40  	SSHKeys   []string
    41  	SSHKeyIDs []types.ID
    42  
    43  	Notes []*sacloud.DiskEditNote // スタートアップスクリプトをIDで指定(変数や埋め込むAPIキーを指定可能)
    44  }
    45  
    46  func (req *EditRequest) Validate() error {
    47  	return validate.Struct(req)
    48  }
    49  
    50  func (req *EditRequest) ToRequestParameter() (*sacloud.DiskEditRequest, error) {
    51  	// TODO builderからコピーしたもの。builderとの統合後に整理する
    52  	editReq := &sacloud.DiskEditRequest{
    53  		Background:          true,
    54  		Password:            req.Password,
    55  		DisablePWAuth:       req.DisablePWAuth,
    56  		EnableDHCP:          req.EnableDHCP,
    57  		ChangePartitionUUID: req.ChangePartitionUUID,
    58  		HostName:            req.HostName,
    59  	}
    60  
    61  	if req.IPAddress != "" {
    62  		editReq.UserIPAddress = req.IPAddress
    63  	}
    64  	if req.NetworkMaskLen > 0 || req.DefaultRoute != "" {
    65  		editReq.UserSubnet = &sacloud.DiskEditUserSubnet{
    66  			NetworkMaskLen: req.NetworkMaskLen,
    67  			DefaultRoute:   req.DefaultRoute,
    68  		}
    69  	}
    70  
    71  	// ssh key
    72  	var sshKeys []*sacloud.DiskEditSSHKey
    73  	for _, key := range req.SSHKeys {
    74  		sshKeys = append(sshKeys, &sacloud.DiskEditSSHKey{
    75  			PublicKey: key,
    76  		})
    77  	}
    78  	for _, id := range req.SSHKeyIDs {
    79  		sshKeys = append(sshKeys, &sacloud.DiskEditSSHKey{
    80  			ID: id,
    81  		})
    82  	}
    83  	editReq.SSHKeys = sshKeys
    84  
    85  	// startup script
    86  	var notes []*sacloud.DiskEditNote
    87  	for _, note := range req.Notes {
    88  		notes = append(notes, &sacloud.DiskEditNote{
    89  			ID:        note.ID,
    90  			APIKeyID:  note.APIKeyID,
    91  			Variables: note.Variables,
    92  		})
    93  	}
    94  	editReq.Notes = notes
    95  
    96  	return editReq, nil
    97  }