github.com/sacloud/libsacloud/v2@v2.32.3/helper/service/server/update_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 server
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  
    21  	"github.com/sacloud/libsacloud/v2/helper/service"
    22  	diskService "github.com/sacloud/libsacloud/v2/helper/service/disk"
    23  	"github.com/sacloud/libsacloud/v2/helper/validate"
    24  	"github.com/sacloud/libsacloud/v2/sacloud"
    25  	"github.com/sacloud/libsacloud/v2/sacloud/types"
    26  )
    27  
    28  type UpdateRequest struct {
    29  	Zone string   `validate:"required"`
    30  	ID   types.ID `validate:"required"`
    31  
    32  	Name            *string                 `request:",omitempty" validate:"omitempty,min=1"`
    33  	Description     *string                 `request:",omitempty" validate:"omitempty,min=0,max=512"`
    34  	Tags            *types.Tags             `request:",omitempty"`
    35  	IconID          *types.ID               `request:",omitempty"`
    36  	CPU             *int                    `request:",omitempty"`
    37  	MemoryGB        *int                    `request:",omitempty"`
    38  	GPU             *int                    `request:",omitempty"`
    39  	Commitment      *types.ECommitment      `request:",omitempty"`
    40  	Generation      *types.EPlanGeneration  `request:",omitempty"`
    41  	InterfaceDriver *types.EInterfaceDriver `request:",omitempty"`
    42  
    43  	CDROMID       *types.ID `request:",omitempty"`
    44  	PrivateHostID *types.ID `request:",omitempty"`
    45  
    46  	NetworkInterfaces *[]*NetworkInterface         `request:",omitempty"`
    47  	Disks             *[]*diskService.ApplyRequest `request:",omitempty"`
    48  	NoWait            bool
    49  	ForceShutdown     bool
    50  }
    51  
    52  func (req *UpdateRequest) Validate() error {
    53  	return validate.Struct(req)
    54  }
    55  
    56  func (req *UpdateRequest) ApplyRequest(ctx context.Context, caller sacloud.APICaller) (*ApplyRequest, error) {
    57  	applyRequest, err := req.applyRequestFromResource(ctx, caller)
    58  	if err != nil {
    59  		return nil, err
    60  	}
    61  	return applyRequest, nil
    62  }
    63  
    64  func (req *UpdateRequest) applyRequestFromResource(ctx context.Context, caller sacloud.APICaller) (*ApplyRequest, error) {
    65  	serverOp := sacloud.NewServerOp(caller)
    66  	current, err := serverOp.Read(ctx, req.Zone, req.ID)
    67  	if err != nil {
    68  		return nil, err
    69  	}
    70  	if current.Availability != types.Availabilities.Available {
    71  		return nil, fmt.Errorf("target has invalid Availability: Zone=%s ID=%s Availability=%v", req.Zone, req.ID.String(), current.Availability)
    72  	}
    73  
    74  	var nics []*NetworkInterface
    75  	for _, iface := range current.Interfaces {
    76  		var upstream string
    77  
    78  		switch {
    79  		case iface.SwitchID.IsEmpty():
    80  			upstream = "disconnected"
    81  		case iface.SwitchScope == types.Scopes.Shared:
    82  			upstream = "shared"
    83  		default:
    84  			upstream = iface.SwitchID.String()
    85  		}
    86  
    87  		nics = append(nics, &NetworkInterface{
    88  			Upstream:       upstream,
    89  			PacketFilterID: iface.PacketFilterID,
    90  			UserIPAddress:  iface.UserIPAddress,
    91  		})
    92  	}
    93  
    94  	diskOp := sacloud.NewDiskOp(caller)
    95  	var disks []*diskService.ApplyRequest
    96  	for _, d := range current.Disks {
    97  		disk, err := diskOp.Read(ctx, req.Zone, d.ID)
    98  		if err != nil {
    99  			return nil, err
   100  		}
   101  		disks = append(disks, &diskService.ApplyRequest{
   102  			Zone:        req.Zone,
   103  			ID:          disk.ID,
   104  			Name:        disk.Name,
   105  			Description: disk.Description,
   106  			Tags:        disk.Tags,
   107  			IconID:      disk.IconID,
   108  			DiskPlanID:  disk.DiskPlanID,
   109  			Connection:  disk.Connection,
   110  			ServerID:    current.ID,
   111  			SizeGB:      disk.GetSizeGB(),
   112  			NoWait:      req.NoWait,
   113  		})
   114  	}
   115  
   116  	applyRequest := &ApplyRequest{
   117  		Zone:              req.Zone,
   118  		ID:                req.ID,
   119  		Name:              current.Name,
   120  		Description:       current.Description,
   121  		Tags:              current.Tags,
   122  		IconID:            current.IconID,
   123  		CPU:               current.CPU,
   124  		MemoryGB:          current.GetMemoryGB(),
   125  		GPU:               current.GPU,
   126  		Commitment:        current.ServerPlanCommitment,
   127  		Generation:        current.ServerPlanGeneration,
   128  		InterfaceDriver:   current.InterfaceDriver,
   129  		CDROMID:           current.CDROMID,
   130  		PrivateHostID:     current.PrivateHostID,
   131  		NetworkInterfaces: nics,
   132  		Disks:             disks,
   133  	}
   134  
   135  	if err := service.RequestConvertTo(req, applyRequest); err != nil {
   136  		return nil, err
   137  	}
   138  	return applyRequest, nil
   139  }