github.com/sacloud/libsacloud/v2@v2.32.3/helper/service/nfs/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 nfs
    16  
    17  import (
    18  	"context"
    19  
    20  	"github.com/sacloud/libsacloud/v2/helper/query"
    21  
    22  	"github.com/sacloud/libsacloud/v2/helper/service"
    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=1,max=512"`
    34  	Tags        *types.Tags `request:",omitempty"`
    35  	IconID      *types.ID   `request:",omitempty"`
    36  }
    37  
    38  func (req *UpdateRequest) Validate() error {
    39  	return validate.Struct(req)
    40  }
    41  
    42  func (req *UpdateRequest) ApplyRequest(ctx context.Context, caller sacloud.APICaller) (*ApplyRequest, error) {
    43  	client := sacloud.NewNFSOp(caller)
    44  	current, err := client.Read(ctx, req.Zone, req.ID)
    45  	if err != nil {
    46  		return nil, err
    47  	}
    48  
    49  	// find plan
    50  	plan, err := query.GetNFSPlanInfo(ctx, sacloud.NewNoteOp(caller), current.PlanID)
    51  	if err != nil {
    52  		return nil, err
    53  	}
    54  
    55  	applyRequest := &ApplyRequest{
    56  		ID:             current.ID,
    57  		Zone:           req.Zone,
    58  		Name:           current.Name,
    59  		Description:    current.Description,
    60  		Tags:           current.Tags,
    61  		IconID:         current.IconID,
    62  		SwitchID:       current.SwitchID,
    63  		Plan:           plan.DiskPlanID,
    64  		Size:           plan.Size,
    65  		IPAddresses:    current.IPAddresses,
    66  		NetworkMaskLen: current.NetworkMaskLen,
    67  		DefaultRoute:   current.DefaultRoute,
    68  	}
    69  	if err := service.RequestConvertTo(req, applyRequest); err != nil {
    70  		return nil, err
    71  	}
    72  	return applyRequest, nil
    73  }