github.com/sacloud/libsacloud/v2@v2.32.3/helper/service/proxylb/update_service.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 proxylb
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  
    21  	"github.com/sacloud/libsacloud/v2/helper/plans"
    22  	"github.com/sacloud/libsacloud/v2/pkg/util"
    23  	"github.com/sacloud/libsacloud/v2/sacloud"
    24  )
    25  
    26  func (s *Service) Update(req *UpdateRequest) (*sacloud.ProxyLB, error) {
    27  	return s.UpdateWithContext(context.Background(), req)
    28  }
    29  
    30  func (s *Service) UpdateWithContext(ctx context.Context, req *UpdateRequest) (*sacloud.ProxyLB, error) {
    31  	if err := req.Validate(); err != nil {
    32  		return nil, err
    33  	}
    34  
    35  	client := sacloud.NewProxyLBOp(s.caller)
    36  	current, err := client.Read(ctx, req.ID)
    37  	if err != nil {
    38  		return nil, fmt.Errorf("reading ProxyLB[%s] failed: %s", req.ID, err)
    39  	}
    40  
    41  	params, err := req.ToRequestParameter(current)
    42  	if err != nil {
    43  		return nil, fmt.Errorf("processing request parameter failed: %s", err)
    44  	}
    45  
    46  	updated, err := client.Update(ctx, req.ID, params)
    47  	if err != nil {
    48  		return nil, err
    49  	}
    50  
    51  	if !util.IsEmpty(req.Plan) && updated.Plan != *req.Plan {
    52  		return plans.ChangeProxyLBPlan(ctx, s.caller, updated.ID, req.Plan.Int())
    53  	}
    54  
    55  	return updated, err
    56  }