github.com/sacloud/libsacloud/v2@v2.32.3/helper/service/localrouter/builder.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 localrouter
    16  
    17  import (
    18  	"context"
    19  
    20  	localrouterBuilder "github.com/sacloud/libsacloud/v2/helper/builder/localrouter"
    21  	"github.com/sacloud/libsacloud/v2/sacloud"
    22  	"github.com/sacloud/libsacloud/v2/sacloud/types"
    23  )
    24  
    25  // Builder ローカルルータの構築を行う
    26  type Builder struct {
    27  	ID          types.ID
    28  	Name        string
    29  	Description string
    30  	Tags        types.Tags
    31  	IconID      types.ID
    32  
    33  	Switch       *sacloud.LocalRouterSwitch
    34  	Interface    *sacloud.LocalRouterInterface
    35  	Peers        []*sacloud.LocalRouterPeer
    36  	StaticRoutes []*sacloud.LocalRouterStaticRoute
    37  
    38  	SettingsHash string
    39  
    40  	Caller sacloud.APICaller
    41  }
    42  
    43  func BuilderFromResource(ctx context.Context, caller sacloud.APICaller, id types.ID) (*Builder, error) {
    44  	client := sacloud.NewLocalRouterOp(caller)
    45  	current, err := client.Read(ctx, id)
    46  	if err != nil {
    47  		return nil, err
    48  	}
    49  
    50  	return &Builder{
    51  		Name:         current.Name,
    52  		Description:  current.Description,
    53  		Tags:         current.Tags,
    54  		IconID:       current.IconID,
    55  		Switch:       current.Switch,
    56  		Interface:    current.Interface,
    57  		Peers:        current.Peers,
    58  		StaticRoutes: current.StaticRoutes,
    59  		Caller:       caller,
    60  	}, nil
    61  }
    62  
    63  func (b *Builder) Build(ctx context.Context) (*sacloud.LocalRouter, error) {
    64  	builder := &localrouterBuilder.Builder{
    65  		Name:         b.Name,
    66  		Description:  b.Description,
    67  		Tags:         b.Tags,
    68  		IconID:       b.IconID,
    69  		Switch:       b.Switch,
    70  		Interface:    b.Interface,
    71  		Peers:        b.Peers,
    72  		StaticRoutes: b.StaticRoutes,
    73  		SettingsHash: b.SettingsHash,
    74  		Client:       localrouterBuilder.NewAPIClient(b.Caller),
    75  	}
    76  
    77  	if b.ID.IsEmpty() {
    78  		return builder.Build(ctx)
    79  	}
    80  	return builder.Update(ctx, b.ID)
    81  }