local_router.go raw

   1  // Copyright 2022-2025 The sacloud/iaas-api-go 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 naked
  16  
  17  import (
  18  	"encoding/json"
  19  	"fmt"
  20  	"time"
  21  
  22  	"github.com/sacloud/iaas-api-go/types"
  23  )
  24  
  25  // LocalRouter ローカルルータ
  26  type LocalRouter struct {
  27  	ID           types.ID             `json:",omitempty" yaml:"id,omitempty" structs:",omitempty"`
  28  	Name         string               `json:",omitempty" yaml:"name,omitempty" structs:",omitempty"`
  29  	Description  string               `yaml:"description"`
  30  	Tags         types.Tags           `yaml:"tags"`
  31  	Icon         *Icon                `json:",omitempty" yaml:"icon,omitempty" structs:",omitempty"`
  32  	CreatedAt    *time.Time           `json:",omitempty" yaml:"created_at,omitempty" structs:",omitempty"`
  33  	ModifiedAt   *time.Time           `json:",omitempty" yaml:"modified_at,omitempty" structs:",omitempty"`
  34  	Availability types.EAvailability  `json:",omitempty" yaml:"availability,omitempty" structs:",omitempty"`
  35  	Provider     *Provider            `json:",omitempty" yaml:"provider,omitempty" structs:",omitempty"`
  36  	Settings     *LocalRouterSettings `json:",omitempty" yaml:"settings,omitempty" structs:",omitempty"`
  37  	SettingsHash string               `json:",omitempty" yaml:"settings_hash,omitempty" structs:",omitempty"`
  38  	Status       *LocalRouterStatus   `json:",omitempty" yaml:"status" structs:",omitempty"`
  39  	ServiceClass string               `json:",omitempty" yaml:"service_class,omitempty" structs:",omitempty"`
  40  }
  41  
  42  // LocalRouterSettings セッティング
  43  type LocalRouterSettings struct {
  44  	LocalRouter *LocalRouterSetting `json:",omitempty" yaml:"local_router,omitempty" structs:",omitempty"`
  45  }
  46  
  47  // LocalRouterSettingsUpdate ローカルルータ セッティング更新
  48  type LocalRouterSettingsUpdate struct {
  49  	Settings     *LocalRouterSettings `json:",omitempty" yaml:"settings,omitempty" structs:",omitempty"`
  50  	SettingsHash string               `json:",omitempty" yaml:"settings_hash,omitempty" structs:",omitempty"`
  51  }
  52  
  53  // LocalRouterSetting セッティング
  54  type LocalRouterSetting struct {
  55  	Switch       *LocalRouterSettingSwitch        `yaml:"switch"`
  56  	Interface    *LocalRouterSettingInterface     `yaml:"interface"`
  57  	Peers        []*LocalRouterSettingPeer        `yaml:"peers"`
  58  	StaticRoutes []*LocalRouterSettingStaticRoute `yaml:"static_routes"`
  59  }
  60  
  61  // LocalRouterSettingSwitch ローカルルータのスイッチ設定
  62  type LocalRouterSettingSwitch struct {
  63  	Code     string `json:",omitempty" yaml:",omitempty" structs:",omitempty"` // リソースIDなど
  64  	Category string `json:",omitempty" yaml:",omitempty" structs:",omitempty"` // cloud/vps/専用サーバなどを表す
  65  	ZoneID   string `json:",omitempty" yaml:",omitempty" structs:",omitempty"` // クラウドの場合is1aなど Note: VPSの場合は数値型となる
  66  }
  67  
  68  // UnmarshalJSON ZoneIDに数値/文字列が混在する問題への対応
  69  func (l *LocalRouterSettingSwitch) UnmarshalJSON(b []byte) error {
  70  	type alias struct {
  71  		Code     string      `json:",omitempty" yaml:",omitempty" structs:",omitempty"` // リソースIDなど
  72  		Category string      `json:",omitempty" yaml:",omitempty" structs:",omitempty"` // cloud/vps/専用サーバなどを表す
  73  		ZoneID   interface{} `json:",omitempty" yaml:",omitempty" structs:",omitempty"` // クラウドの場合is1aなど Note: VPSの場合は数値型となる
  74  	}
  75  
  76  	var a alias
  77  	if err := json.Unmarshal(b, &a); err != nil {
  78  		return err
  79  	}
  80  	zoneID := ""
  81  	switch v := a.ZoneID.(type) {
  82  	case string:
  83  		zoneID = v
  84  	case int, int8, int16, int32, int64:
  85  		zoneID = fmt.Sprintf("%d", v)
  86  	case float32, float64:
  87  		zoneID = fmt.Sprintf("%d", int(v.(float64)))
  88  	}
  89  
  90  	*l = LocalRouterSettingSwitch{
  91  		Code:     a.Code,
  92  		Category: a.Category,
  93  		ZoneID:   zoneID,
  94  	}
  95  	return nil
  96  }
  97  
  98  // LocalRouterSettingInterface インターフェース設定
  99  type LocalRouterSettingInterface struct {
 100  	VirtualIPAddress string   `json:",omitempty" yaml:",omitempty" structs:",omitempty"`
 101  	IPAddress        []string `json:",omitempty" yaml:",omitempty" structs:",omitempty"`
 102  	NetworkMaskLen   int      `json:",omitempty" yaml:",omitempty" structs:",omitempty"`
 103  	VRID             int      `json:",omitempty" yaml:",omitempty" structs:",omitempty"`
 104  }
 105  
 106  // LocalRouterSettingPeer ピア設定
 107  type LocalRouterSettingPeer struct {
 108  	ID          string `yaml:"id"` // 文字列でないとエラーになるためtypes.IDではなくstringとする
 109  	SecretKey   string `yaml:"secret_key"`
 110  	Enabled     bool   `yaml:"enabled"`
 111  	Description string `yaml:"description"`
 112  }
 113  
 114  // LocalRouterSettingStaticRoute スタティックルート
 115  type LocalRouterSettingStaticRoute struct {
 116  	Prefix  string `yaml:"prefix"`
 117  	NextHop string `yaml:"next_hop"`
 118  }
 119  
 120  // LocalRouterStatus ステータス
 121  type LocalRouterStatus struct {
 122  	SecretKeys []string `json:",omitempty" yaml:",omitempty" structs:",omitempty"`
 123  }
 124  
 125  // LocalRouterHealth ローカルルータのヘルスチェック結果
 126  type LocalRouterHealth struct {
 127  	Peers []*struct {
 128  		ID     types.ID
 129  		Status types.EServerInstanceStatus
 130  		Routes []string
 131  	} `yaml:"peers"`
 132  }
 133