mobile_gateway.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  	"strings"
  20  	"time"
  21  
  22  	"github.com/sacloud/iaas-api-go/types"
  23  )
  24  
  25  // MobileGateway モバイルゲートウェイ
  26  type MobileGateway struct {
  27  	ID           types.ID                `json:",omitempty" yaml:"id,omitempty" structs:",omitempty"`
  28  	Class        string                  `json:",omitempty" yaml:"class,omitempty" structs:",omitempty"`
  29  	Name         string                  `json:",omitempty" yaml:"name,omitempty" structs:",omitempty"`
  30  	Tags         types.Tags              `yaml:"tags"`
  31  	Description  string                  `yaml:"description"`
  32  	Plan         *AppliancePlan          `json:",omitempty" yaml:"plan,omitempty" structs:",omitempty"`
  33  	Settings     *MobileGatewaySettings  `json:",omitempty" yaml:"settings,omitempty" structs:",omitempty"`
  34  	SettingsHash string                  `json:",omitempty" yaml:"settings_hash,omitempty" structs:",omitempty"`
  35  	Remark       *ApplianceRemark        `json:",omitempty" yaml:"remark,omitempty" structs:",omitempty"`
  36  	Availability types.EAvailability     `json:",omitempty" yaml:"availability,omitempty" structs:",omitempty"`
  37  	Instance     *Instance               `json:",omitempty" yaml:"instance,omitempty" structs:",omitempty"`
  38  	ServiceClass string                  `json:",omitempty" yaml:"service_class,omitempty" structs:",omitempty"`
  39  	CreatedAt    *time.Time              `json:",omitempty" yaml:"created_at,omitempty" structs:",omitempty"`
  40  	Icon         *Icon                   `json:",omitempty" yaml:"icon,omitempty" structs:",omitempty"`
  41  	Switch       *Switch                 `json:",omitempty" yaml:"switch,omitempty" structs:",omitempty"`
  42  	Interfaces   MobileGatewayInterfaces `json:",omitempty" yaml:"interfaces,omitempty" structs:",omitempty"`
  43  }
  44  
  45  // MobileGatewaySettingsUpdate モバイルゲートウェイ
  46  type MobileGatewaySettingsUpdate struct {
  47  	Settings     *MobileGatewaySettings `json:",omitempty" yaml:"settings,omitempty" structs:",omitempty"`
  48  	SettingsHash string                 `json:",omitempty" yaml:"settings_hash,omitempty" structs:",omitempty"`
  49  }
  50  
  51  // MobileGatewayInterfaces 要素がnullにことがある場合に対応するためのtype
  52  //
  53  // 例: モバイルゲートウェイ 作成時、eth0/eth1の2要素が返ってくるがeth1の分はnullとなっている。
  54  type MobileGatewayInterfaces []*Interface
  55  
  56  // UnmarshalJSON 配列中にnullが返ってくる(VPCルータなど)への対応
  57  func (i *MobileGatewayInterfaces) UnmarshalJSON(b []byte) error {
  58  	type alias MobileGatewayInterfaces
  59  	var a alias
  60  	if err := json.Unmarshal(b, &a); err != nil {
  61  		return err
  62  	}
  63  
  64  	var dest []*Interface
  65  	for i, v := range a {
  66  		if v != nil {
  67  			if v.Index == 0 {
  68  				v.Index = i
  69  			}
  70  			dest = append(dest, v)
  71  		}
  72  	}
  73  
  74  	*i = MobileGatewayInterfaces(dest)
  75  	return nil
  76  }
  77  
  78  // MobileGatewaySettings モバイルゲートウェイ セッティング
  79  type MobileGatewaySettings struct {
  80  	MobileGateway *MobileGatewaySetting `json:",omitempty" yaml:"mobile_gateway,omitempty" structs:",omitempty"`
  81  }
  82  
  83  // MobileGatewaySetting モバイルゲートウェイ セッティング
  84  type MobileGatewaySetting struct {
  85  	Interfaces               MobileGatewayInterfacesSettings        `json:",omitempty" yaml:"interfaces,omitempty" structs:",omitempty"`
  86  	InternetConnection       *MobileGatewayInternetConnection       `json:",omitempty" yaml:"internet_connection,omitempty" structs:",omitempty"`
  87  	StaticRoutes             []*MobileGatewayStaticRoute            `json:",omitempty" yaml:"static_routes,omitempty" structs:",omitempty"`
  88  	InterDeviceCommunication *MobileGatewayInterDeviceCommunication `json:",omitempty" yaml:"inter_device_communication,omitempty" structs:",omitempty"`
  89  }
  90  
  91  // MobileGatewayInterDeviceCommunication デバイス間通信
  92  type MobileGatewayInterDeviceCommunication struct {
  93  	Enabled types.StringFlag `yaml:"enabled"`
  94  }
  95  
  96  // MobileGatewayInternetConnection インターネット接続
  97  type MobileGatewayInternetConnection struct {
  98  	Enabled types.StringFlag `yaml:"enabled"`
  99  }
 100  
 101  // MobileGatewayStaticRoute スタティックルート
 102  type MobileGatewayStaticRoute struct {
 103  	Prefix  string `json:",omitempty" yaml:",omitempty" structs:",omitempty"`
 104  	NextHop string `json:",omitempty" yaml:",omitempty" structs:",omitempty"`
 105  }
 106  
 107  // MobileGatewayResolver DNS登録用パラメータ
 108  type MobileGatewayResolver struct {
 109  	SimGroup *MobileGatewaySIMGroup `json:"sim_group,omitempty" yaml:"sim_group,omitempty" structs:",omitempty"`
 110  }
 111  
 112  // MobileGatewaySIMGroup DNS登録用SIMグループ値
 113  type MobileGatewaySIMGroup struct {
 114  	DNS1 string `json:"dns_1,omitempty" yaml:"dns_1,omitempty" structs:",omitempty"`
 115  	DNS2 string `json:"dns_2,omitempty" yaml:"dns_2,omitempty" structs:",omitempty"`
 116  }
 117  
 118  // UnmarshalJSON JSONアンマーシャル(配列、オブジェクトが混在するためここで対応)
 119  func (m *MobileGatewaySIMGroup) UnmarshalJSON(data []byte) error {
 120  	targetData := strings.ReplaceAll(strings.ReplaceAll(string(data), " ", ""), "\n", "")
 121  	if targetData == `[]` {
 122  		return nil
 123  	}
 124  
 125  	type alias MobileGatewaySIMGroup
 126  	tmp := alias{}
 127  	if err := json.Unmarshal(data, &tmp); err != nil {
 128  		return err
 129  	}
 130  	*m = MobileGatewaySIMGroup(tmp)
 131  	return nil
 132  }
 133  
 134  // MobileGatewaySIMRoute SIMルート
 135  type MobileGatewaySIMRoute struct {
 136  	ICCID      string `json:"iccid,omitempty" yaml:"iccid,omitempty" structs:",omitempty"`
 137  	Prefix     string `json:"prefix,omitempty" yaml:"prefix,omitempty" structs:",omitempty"`
 138  	ResourceID string `json:"resource_id,omitempty" yaml:"resource_id,omitempty" structs:",omitempty"`
 139  }
 140  
 141  // MobileGatewaySIMRoutes SIMルート一覧
 142  type MobileGatewaySIMRoutes struct {
 143  	SIMRoutes []*MobileGatewaySIMRoute `json:"sim_routes" yaml:"sim_routes,omitempty" structs:",omitempty"`
 144  }
 145  
 146  // TrafficStatus トラフィックコントロール 当月通信量
 147  type TrafficStatus struct {
 148  	UplinkBytes    types.StringNumber `json:"uplink_bytes,omitempty" yaml:"uplink_bytes,omitempty" structs:",omitempty"`
 149  	DownlinkBytes  types.StringNumber `json:"downlink_bytes,omitempty" yaml:"downlink_bytes,omitempty" structs:",omitempty"`
 150  	TrafficShaping bool               `json:"traffic_shaping" yaml:"traffic_shaping"` // 帯域制限
 151  }
 152  
 153  // UnmarshalJSON JSONアンマーシャル(uint64文字列対応)
 154  func (s *TrafficStatus) UnmarshalJSON(data []byte) error {
 155  	type alias TrafficStatus
 156  	tmp := alias{}
 157  	if err := json.Unmarshal(data, &tmp); err != nil {
 158  		return err
 159  	}
 160  	*s = TrafficStatus(tmp)
 161  	return nil
 162  }
 163  
 164  // TrafficMonitoringConfig トラフィックコントロール 設定
 165  type TrafficMonitoringConfig struct {
 166  	TrafficQuotaInMB     int                          `json:"traffic_quota_in_mb" yaml:"traffic_quota_in_mb"`
 167  	BandWidthLimitInKbps int                          `json:"bandwidth_limit_in_kbps" yaml:"bandwidth_limit_in_kbps"`
 168  	EMailConfig          TrafficMonitoringNotifyEmail `json:"email_config" yaml:"email_config"`
 169  	SlackConfig          TrafficMonitoringNotifySlack `json:"slack_config" yaml:"slack_config"`
 170  	AutoTrafficShaping   bool                         `json:"auto_traffic_shaping" yaml:"auto_traffic_shaping"`
 171  }
 172  
 173  // TrafficMonitoringNotifyEmail トラフィックコントロール通知設定
 174  type TrafficMonitoringNotifyEmail struct {
 175  	Enabled bool `json:"enabled" yaml:"enabled"` // 有効/無効
 176  }
 177  
 178  // TrafficMonitoringNotifySlack トラフィックコントロール通知設定
 179  type TrafficMonitoringNotifySlack struct {
 180  	Enabled             bool   `json:"enabled" yaml:"enabled"`                         // 有効/無効
 181  	IncomingWebhooksURL string `json:"slack_url,omitempty" yaml:"slack_url,omitempty"` // Slack通知の場合のWebhook URL
 182  }
 183  
 184  // MobileGatewayInterface インターフェース
 185  type MobileGatewayInterface struct {
 186  	IPAddress      []string `json:",omitempty" yaml:",omitempty" structs:",omitempty"`
 187  	NetworkMaskLen int      `json:",omitempty" yaml:",omitempty" structs:",omitempty"`
 188  	// Index 仮想フィールド、VPCルータなどでInterfaces(実体は[]*Interface)を扱う場合にUnmarshalJSONの中で設定される
 189  	//
 190  	// Findした際のAPIからの応答にも同名のフィールドが含まれるが無関係。
 191  	Index int
 192  }
 193  
 194  // MobileGatewayInterfacesSettings Interface配列
 195  //
 196  // 配列中にnullが返ってくる(VPCルータなど)への対応のためのtype
 197  type MobileGatewayInterfacesSettings []*MobileGatewayInterface
 198  
 199  // UnmarshalJSON 配列中にnullが返ってくる(VPCルータなど)への対応
 200  func (i *MobileGatewayInterfacesSettings) UnmarshalJSON(b []byte) error {
 201  	type alias MobileGatewayInterfacesSettings
 202  	var a alias
 203  	if err := json.Unmarshal(b, &a); err != nil {
 204  		return err
 205  	}
 206  
 207  	var dest []*MobileGatewayInterface
 208  	for i, v := range a {
 209  		if v != nil {
 210  			if v.Index == 0 {
 211  				v.Index = i
 212  			}
 213  			dest = append(dest, v)
 214  		}
 215  	}
 216  
 217  	*i = MobileGatewayInterfacesSettings(dest)
 218  	return nil
 219  }
 220  
 221  // MarshalJSON 配列中にnullが入る場合(VPCルータなど)への対応
 222  func (i MobileGatewayInterfacesSettings) MarshalJSON() ([]byte, error) {
 223  	max := 0
 224  	for _, iface := range i {
 225  		if max < iface.Index {
 226  			max = iface.Index
 227  		}
 228  	}
 229  
 230  	var dest = make([]*MobileGatewayInterface, max+1)
 231  	for _, iface := range i {
 232  		dest[iface.Index] = iface
 233  	}
 234  
 235  	return json.Marshal(dest)
 236  }
 237  
 238  // MarshalJSON JSON
 239  func (i *MobileGatewayInterface) MarshalJSON() ([]byte, error) {
 240  	type alias struct {
 241  		IPAddress      []string `json:",omitempty" yaml:",omitempty" structs:",omitempty"`
 242  		NetworkMaskLen int      `json:",omitempty" yaml:",omitempty" structs:",omitempty"`
 243  	}
 244  
 245  	tmp := alias{
 246  		IPAddress:      i.IPAddress,
 247  		NetworkMaskLen: i.NetworkMaskLen,
 248  	}
 249  	return json.Marshal(tmp)
 250  }
 251