github.com/sacloud/libsacloud/v2@v2.32.3/helper/builder/server/nic.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 server
    16  
    17  import (
    18  	"context"
    19  	"errors"
    20  	"fmt"
    21  
    22  	"github.com/sacloud/libsacloud/v2/sacloud"
    23  	"github.com/sacloud/libsacloud/v2/sacloud/types"
    24  )
    25  
    26  type nicState struct {
    27  	upstreamType   types.EUpstreamNetworkType
    28  	switchID       types.ID
    29  	packetFilterID types.ID
    30  	displayIP      string
    31  }
    32  
    33  // NICSettingHolder NIC設定を保持するためのインターフェース
    34  type NICSettingHolder interface {
    35  	GetConnectedSwitchParam() *sacloud.ConnectedSwitch
    36  	GetDisplayIPAddress() string
    37  
    38  	GetPacketFilterID() types.ID
    39  	Validate(ctx context.Context, client *APIClient, zone string) error
    40  	state() *nicState
    41  }
    42  
    43  // AdditionalNICSettingHolder 追加NIC設定を保持するためのインターフェース
    44  type AdditionalNICSettingHolder interface {
    45  	GetSwitchID() types.ID
    46  
    47  	GetDisplayIPAddress() string
    48  	GetPacketFilterID() types.ID
    49  	Validate(ctx context.Context, client *APIClient, zone string) error
    50  	state() *nicState
    51  }
    52  
    53  // SharedNICSetting サーバ作成時に共有セグメントに接続するためのパラメータ
    54  //
    55  // NICSettingHolderを実装し、Builder.NICに利用できる。
    56  type SharedNICSetting struct {
    57  	PacketFilterID types.ID
    58  }
    59  
    60  // GetConnectedSwitchParam サーバ作成時の接続先指定パラメータを作成して返す
    61  func (c *SharedNICSetting) GetConnectedSwitchParam() *sacloud.ConnectedSwitch {
    62  	return &sacloud.ConnectedSwitch{Scope: types.Scopes.Shared}
    63  }
    64  
    65  // GetPacketFilterID このNICに接続するパケットフィルタのIDを返す
    66  func (c *SharedNICSetting) GetPacketFilterID() types.ID {
    67  	return c.PacketFilterID
    68  }
    69  
    70  // Validate 設定値の検証
    71  func (c *SharedNICSetting) Validate(ctx context.Context, client *APIClient, zone string) error {
    72  	if !c.PacketFilterID.IsEmpty() {
    73  		if _, err := client.PacketFilter.Read(ctx, zone, c.PacketFilterID); err != nil {
    74  			return fmt.Errorf("reading packet filter info(id:%d) is failed: %s", c.PacketFilterID, err)
    75  		}
    76  	}
    77  	return nil
    78  }
    79  
    80  // GetDisplayIPAddress 表示用IPアドレスを返す
    81  func (c *SharedNICSetting) GetDisplayIPAddress() string {
    82  	return ""
    83  }
    84  
    85  func (c *SharedNICSetting) state() *nicState {
    86  	return &nicState{
    87  		upstreamType:   types.UpstreamNetworkTypes.Shared,
    88  		switchID:       types.ID(0),
    89  		packetFilterID: c.PacketFilterID,
    90  		displayIP:      "",
    91  	}
    92  }
    93  
    94  // ConnectedNICSetting サーバ作成時にスイッチに接続するためのパラメータ
    95  //
    96  // NICSettingHolderとAdditionalNICSettingHolderを実装し、Builder.NIC/Builder.AdditionalNICsに利用できる。
    97  type ConnectedNICSetting struct {
    98  	SwitchID         types.ID
    99  	DisplayIPAddress string
   100  	PacketFilterID   types.ID
   101  }
   102  
   103  // GetConnectedSwitchParam サーバ作成時の接続先指定パラメータを作成して返す
   104  func (c *ConnectedNICSetting) GetConnectedSwitchParam() *sacloud.ConnectedSwitch {
   105  	return &sacloud.ConnectedSwitch{ID: c.SwitchID}
   106  }
   107  
   108  // GetSwitchID このNICが接続するスイッチのIDを返す
   109  func (c *ConnectedNICSetting) GetSwitchID() types.ID {
   110  	return c.SwitchID
   111  }
   112  
   113  // GetDisplayIPAddress 表示用IPアドレスを返す
   114  func (c *ConnectedNICSetting) GetDisplayIPAddress() string {
   115  	return c.DisplayIPAddress
   116  }
   117  
   118  // GetPacketFilterID このNICに接続するパケットフィルタのIDを返す
   119  func (c *ConnectedNICSetting) GetPacketFilterID() types.ID {
   120  	return c.PacketFilterID
   121  }
   122  
   123  // Validate 設定値の検証
   124  func (c *ConnectedNICSetting) Validate(ctx context.Context, client *APIClient, zone string) error {
   125  	if c.SwitchID.IsEmpty() {
   126  		return errors.New("ConnectedNICSetting: SwitchID is required")
   127  	}
   128  
   129  	if _, err := client.Switch.Read(ctx, zone, c.SwitchID); err != nil {
   130  		return fmt.Errorf("reading switch info(id:%d) is failed: %s", c.SwitchID, err)
   131  	}
   132  
   133  	if !c.PacketFilterID.IsEmpty() {
   134  		if _, err := client.PacketFilter.Read(ctx, zone, c.PacketFilterID); err != nil {
   135  			return fmt.Errorf("reading packet filter info(id:%d) is failed: %s", c.PacketFilterID, err)
   136  		}
   137  	}
   138  
   139  	return nil
   140  }
   141  
   142  func (c *ConnectedNICSetting) state() *nicState {
   143  	return &nicState{
   144  		upstreamType:   types.UpstreamNetworkTypes.Switch,
   145  		switchID:       c.SwitchID,
   146  		packetFilterID: c.PacketFilterID,
   147  		displayIP:      c.DisplayIPAddress,
   148  	}
   149  }
   150  
   151  // DisconnectedNICSetting 切断状態のNICを作成するためのパラメータ
   152  //
   153  // NICSettingHolderとAdditionalNICSettingHolderを実装し、Builder.NIC/Builder.AdditionalNICsに利用できる。
   154  type DisconnectedNICSetting struct{}
   155  
   156  // GetConnectedSwitchParam サーバ作成時の接続先指定パラメータを作成して返す
   157  func (d *DisconnectedNICSetting) GetConnectedSwitchParam() *sacloud.ConnectedSwitch {
   158  	return nil
   159  }
   160  
   161  // GetSwitchID このNICが接続するスイッチのIDを返す
   162  func (d *DisconnectedNICSetting) GetSwitchID() types.ID {
   163  	return types.ID(0)
   164  }
   165  
   166  // GetDisplayIPAddress 表示用IPアドレスを返す
   167  func (d *DisconnectedNICSetting) GetDisplayIPAddress() string {
   168  	return ""
   169  }
   170  
   171  // GetPacketFilterID このNICに接続するパケットフィルタのIDを返す
   172  func (d *DisconnectedNICSetting) GetPacketFilterID() types.ID {
   173  	return types.ID(0)
   174  }
   175  
   176  // Validate 設定値の検証
   177  func (d *DisconnectedNICSetting) Validate(ctx context.Context, client *APIClient, zone string) error {
   178  	return nil
   179  }
   180  
   181  func (d *DisconnectedNICSetting) state() *nicState {
   182  	return &nicState{
   183  		upstreamType:   types.UpstreamNetworkTypes.None,
   184  		switchID:       types.ID(0),
   185  		packetFilterID: types.ID(0),
   186  		displayIP:      "",
   187  	}
   188  }