github.com/sacloud/libsacloud/v2@v2.32.3/helper/builder/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  	"github.com/sacloud/libsacloud/v2/sacloud"
    21  	"github.com/sacloud/libsacloud/v2/sacloud/types"
    22  )
    23  
    24  // Builder ローカルルータの構築を行う
    25  type Builder struct {
    26  	Name        string
    27  	Description string
    28  	Tags        types.Tags
    29  	IconID      types.ID
    30  
    31  	Switch       *sacloud.LocalRouterSwitch
    32  	Interface    *sacloud.LocalRouterInterface
    33  	Peers        []*sacloud.LocalRouterPeer
    34  	StaticRoutes []*sacloud.LocalRouterStaticRoute
    35  
    36  	SettingsHash string
    37  
    38  	Client *APIClient
    39  }
    40  
    41  // Validate 設定値の検証
    42  func (b *Builder) Validate(_ context.Context) error {
    43  	return nil
    44  }
    45  
    46  // Build ローカルルータの作成や設定をまとめて行う
    47  func (b *Builder) Build(ctx context.Context) (*sacloud.LocalRouter, error) {
    48  	if err := b.Validate(ctx); err != nil {
    49  		return nil, err
    50  	}
    51  
    52  	localRouter, err := b.Client.LocalRouter.Create(ctx, &sacloud.LocalRouterCreateRequest{
    53  		Name:        b.Name,
    54  		Description: b.Description,
    55  		Tags:        b.Tags,
    56  		IconID:      b.IconID,
    57  	})
    58  	if err != nil {
    59  		return nil, err
    60  	}
    61  
    62  	if b.hasNetworkSettings() {
    63  		lr, err := b.Client.LocalRouter.UpdateSettings(ctx, localRouter.ID, &sacloud.LocalRouterUpdateSettingsRequest{
    64  			Switch:       b.Switch,
    65  			Interface:    b.Interface,
    66  			StaticRoutes: b.StaticRoutes,
    67  			SettingsHash: b.SettingsHash,
    68  		})
    69  		if err != nil {
    70  			return localRouter, err
    71  		}
    72  		localRouter = lr
    73  
    74  		if len(b.Peers) > 0 {
    75  			lr, err := b.Client.LocalRouter.UpdateSettings(ctx, localRouter.ID, &sacloud.LocalRouterUpdateSettingsRequest{
    76  				Switch:       localRouter.Switch,
    77  				Interface:    localRouter.Interface,
    78  				StaticRoutes: localRouter.StaticRoutes,
    79  				Peers:        b.Peers,
    80  				SettingsHash: localRouter.SettingsHash,
    81  			})
    82  			if err != nil {
    83  				return localRouter, err
    84  			}
    85  			localRouter = lr
    86  		}
    87  	}
    88  
    89  	return localRouter, nil
    90  }
    91  
    92  func (b *Builder) hasNetworkSettings() bool {
    93  	return b.Interface != nil && b.Switch != nil &&
    94  		b.Interface.NetworkMaskLen > 0 &&
    95  		b.Interface.VirtualIPAddress != "" &&
    96  		len(b.Interface.IPAddress) > 0 &&
    97  		b.Switch.Code != ""
    98  }
    99  
   100  // Update ローカルルータの更新
   101  func (b *Builder) Update(ctx context.Context, id types.ID) (*sacloud.LocalRouter, error) {
   102  	if err := b.Validate(ctx); err != nil {
   103  		return nil, err
   104  	}
   105  
   106  	// check Internet is exists
   107  	_, err := b.Client.LocalRouter.Read(ctx, id)
   108  	if err != nil {
   109  		return nil, err
   110  	}
   111  
   112  	localRouter, err := b.Client.LocalRouter.Update(ctx, id, &sacloud.LocalRouterUpdateRequest{
   113  		Switch:       b.Switch,
   114  		Interface:    b.Interface,
   115  		Peers:        b.Peers,
   116  		StaticRoutes: b.StaticRoutes,
   117  		SettingsHash: b.SettingsHash,
   118  		Name:         b.Name,
   119  		Description:  b.Description,
   120  		Tags:         b.Tags,
   121  		IconID:       b.IconID,
   122  	})
   123  	if err != nil {
   124  		return nil, err
   125  	}
   126  
   127  	return localRouter, nil
   128  }