github.com/sacloud/libsacloud/v2@v2.32.3/helper/builder/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  	"context"
    19  	"fmt"
    20  	"time"
    21  
    22  	"github.com/sacloud/libsacloud/v2/sacloud"
    23  	"github.com/sacloud/libsacloud/v2/sacloud/types"
    24  )
    25  
    26  // EditRequest 汎用ディスクの修正リクエストパラメータ DiskDirectorが利用する
    27  type EditRequest UnixEditRequest
    28  
    29  // ToUnixDiskEditRequest Unix系パラメータへの変換
    30  func (d *EditRequest) ToUnixDiskEditRequest() *UnixEditRequest {
    31  	if d == nil {
    32  		return nil
    33  	}
    34  	req := UnixEditRequest(*d)
    35  	return &req
    36  }
    37  
    38  // ToWindowsDiskEditRequest Windows系パラメータへの変換
    39  func (d *EditRequest) ToWindowsDiskEditRequest() *WindowsEditRequest {
    40  	if d == nil {
    41  		return nil
    42  	}
    43  	return &WindowsEditRequest{
    44  		IPAddress:      d.IPAddress,
    45  		NetworkMaskLen: d.NetworkMaskLen,
    46  		DefaultRoute:   d.DefaultRoute,
    47  	}
    48  }
    49  
    50  // UnixEditRequest Unix系の場合のディスクの修正リクエスト
    51  type UnixEditRequest struct {
    52  	HostName string
    53  	Password string
    54  
    55  	DisablePWAuth       bool
    56  	EnableDHCP          bool
    57  	ChangePartitionUUID bool
    58  
    59  	IPAddress      string
    60  	NetworkMaskLen int
    61  	DefaultRoute   string
    62  
    63  	SSHKeys   []string
    64  	SSHKeyIDs []types.ID
    65  
    66  	// IsSSHKeysEphemeral trueの場合、SSHキーを生成する場合に生成したSSHキーリソースをサーバ作成後に削除する
    67  	IsSSHKeysEphemeral bool
    68  	// GenerateSSHKeyName 設定されていた場合、クラウドAPIを用いてキーペアを生成する。
    69  	GenerateSSHKeyName        string
    70  	GenerateSSHKeyPassPhrase  string
    71  	GenerateSSHKeyDescription string
    72  
    73  	IsNotesEphemeral bool
    74  	NoteContents     []string
    75  	Notes            []*sacloud.DiskEditNote
    76  }
    77  
    78  // Validate 設定値の検証
    79  func (u *UnixEditRequest) Validate(ctx context.Context, client *APIClient) error {
    80  	for _, id := range u.SSHKeyIDs {
    81  		if _, err := client.SSHKey.Read(ctx, id); err != nil {
    82  			return err
    83  		}
    84  	}
    85  	for _, note := range u.Notes {
    86  		if _, err := client.Note.Read(ctx, note.ID); err != nil {
    87  			return err
    88  		}
    89  	}
    90  	return nil
    91  }
    92  
    93  func (u *UnixEditRequest) prepareDiskEditParameter(ctx context.Context, client *APIClient) (*sacloud.DiskEditRequest, *sacloud.SSHKeyGenerated, []*sacloud.Note, error) {
    94  	editReq := &sacloud.DiskEditRequest{
    95  		Background:          true,
    96  		Password:            u.Password,
    97  		DisablePWAuth:       u.DisablePWAuth,
    98  		EnableDHCP:          u.EnableDHCP,
    99  		ChangePartitionUUID: u.ChangePartitionUUID,
   100  		HostName:            u.HostName,
   101  	}
   102  
   103  	if u.IPAddress != "" {
   104  		editReq.UserIPAddress = u.IPAddress
   105  	}
   106  	if u.NetworkMaskLen > 0 || u.DefaultRoute != "" {
   107  		editReq.UserSubnet = &sacloud.DiskEditUserSubnet{
   108  			NetworkMaskLen: u.NetworkMaskLen,
   109  			DefaultRoute:   u.DefaultRoute,
   110  		}
   111  	}
   112  
   113  	// ssh key
   114  	var sshKeys []*sacloud.DiskEditSSHKey
   115  	for _, key := range u.SSHKeys {
   116  		sshKeys = append(sshKeys, &sacloud.DiskEditSSHKey{
   117  			PublicKey: key,
   118  		})
   119  	}
   120  	for _, id := range u.SSHKeyIDs {
   121  		sshKeys = append(sshKeys, &sacloud.DiskEditSSHKey{
   122  			ID: id,
   123  		})
   124  	}
   125  
   126  	var generatedSSHKey *sacloud.SSHKeyGenerated
   127  	if u.GenerateSSHKeyName != "" {
   128  		generated, err := client.SSHKey.Generate(ctx, &sacloud.SSHKeyGenerateRequest{
   129  			Name:        u.GenerateSSHKeyName,
   130  			PassPhrase:  u.GenerateSSHKeyPassPhrase,
   131  			Description: u.GenerateSSHKeyDescription,
   132  		})
   133  		if err != nil {
   134  			return nil, nil, nil, err
   135  		}
   136  		generatedSSHKey = generated
   137  		sshKeys = append(sshKeys, &sacloud.DiskEditSSHKey{
   138  			ID: generated.ID,
   139  		})
   140  	}
   141  	editReq.SSHKeys = sshKeys
   142  
   143  	// startup script
   144  	var notes []*sacloud.DiskEditNote
   145  	var generatedNotes []*sacloud.Note
   146  
   147  	for _, note := range u.NoteContents {
   148  		created, err := client.Note.Create(ctx, &sacloud.NoteCreateRequest{
   149  			Name:    fmt.Sprintf("note-%s", time.Now().Format(time.RFC3339)),
   150  			Class:   "shell",
   151  			Content: note,
   152  		})
   153  		if err != nil {
   154  			return nil, nil, nil, err
   155  		}
   156  		notes = append(notes, &sacloud.DiskEditNote{
   157  			ID: created.ID,
   158  		})
   159  		generatedNotes = append(generatedNotes, created)
   160  	}
   161  	for _, note := range u.Notes {
   162  		notes = append(notes, &sacloud.DiskEditNote{
   163  			ID:        note.ID,
   164  			APIKeyID:  note.APIKeyID,
   165  			Variables: note.Variables,
   166  		})
   167  	}
   168  	editReq.Notes = notes
   169  
   170  	return editReq, generatedSSHKey, generatedNotes, nil
   171  }
   172  
   173  // WindowsEditRequest Windows系の場合のディスクの修正リクエスト
   174  type WindowsEditRequest struct {
   175  	IPAddress      string
   176  	NetworkMaskLen int
   177  	DefaultRoute   string
   178  }
   179  
   180  func (w *WindowsEditRequest) prepareDiskEditParameter() *sacloud.DiskEditRequest {
   181  	editReq := &sacloud.DiskEditRequest{Background: true}
   182  
   183  	if w.IPAddress != "" {
   184  		editReq.UserIPAddress = w.IPAddress
   185  	}
   186  	if w.NetworkMaskLen > 0 || w.DefaultRoute != "" {
   187  		editReq.UserSubnet = &sacloud.DiskEditUserSubnet{
   188  			NetworkMaskLen: w.NetworkMaskLen,
   189  			DefaultRoute:   w.DefaultRoute,
   190  		}
   191  	}
   192  	return editReq
   193  }