1// Copyright 2016-2020 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 15package sacloud 16 17import ( 18 "fmt" 19 "net" 20) 21 22// Switch スイッチ 23type Switch struct { 24 *Resource // ID 25 propName // 名称 26 propDescription // 説明 27 propServiceClass // サービスクラス 28 propIcon // アイコン 29 propTags // タグ 30 propCreatedAt // 作成日時 31 propZone // ゾーン 32 33 ServerCount int `json:",omitempty"` // 接続サーバー数 34 ApplianceCount int `json:",omitempty"` // 接続アプライアンス数 35 Scope EScope `json:",omitempty"` // スコープ 36 Subnet *Subnet `json:",omitempty"` // サブネット 37 UserSubnet *Subnet `json:",omitempty"` // ユーザー定義サブネット 38 Subnets []SwitchSubnet `json:",omitempty"` // サブネット 39 IPv6Nets []IPv6Net `json:",omitempty"` // IPv6サブネットリスト 40 Internet *Internet `json:",omitempty"` // ルーター 41 42 Bridge *struct { // 接続先ブリッジ(Info.Switches配下のIDデータ型HACK) 43 *Bridge // ブリッジ 44 Info *struct { 45 Switches []*Switch 46 } 47 } `json:",omitempty"` 48 49 //HybridConnection //REMARK: !!ハイブリッド接続 not support!! 50} 51 52// SwitchSubnet スイッチサブネット 53type SwitchSubnet struct { 54 *Subnet 55 IPAddresses struct { // IPアドレス範囲 56 Min string `json:",omitempty"` // IPアドレス開始 57 Max string `json:",omitempty"` // IPアドレス終了 58 } 59} 60 61// GetDefaultIPAddressesForVPCRouter VPCルーター接続用にサブネットからIPアドレスを3つ取得 62func (s *Switch) GetDefaultIPAddressesForVPCRouter() (string, string, string, error) { 63 64 if s.Subnets == nil || len(s.Subnets) < 1 { 65 return "", "", "", fmt.Errorf("switch[%d].Subnets is nil", s.ID) 66 } 67 68 baseAddress := net.ParseIP(s.Subnets[0].IPAddresses.Min).To4() 69 address1 := net.IPv4(baseAddress[0], baseAddress[1], baseAddress[2], baseAddress[3]+1) 70 address2 := net.IPv4(baseAddress[0], baseAddress[1], baseAddress[2], baseAddress[3]+2) 71 72 return baseAddress.String(), address1.String(), address2.String(), nil 73} 74 75// GetIPAddressList IPアドレス範囲内の全てのIPアドレスを取得 76func (s *Switch) GetIPAddressList() ([]string, error) { 77 if s.Subnets == nil || len(s.Subnets) < 1 { 78 return nil, fmt.Errorf("switch[%d].Subnets is nil", s.ID) 79 } 80 81 //さくらのクラウドの仕様上/24までしか割り当てできないためこのロジックでOK 82 baseIP := net.ParseIP(s.Subnets[0].IPAddresses.Min).To4() 83 min := baseIP[3] 84 max := net.ParseIP(s.Subnets[0].IPAddresses.Max).To4()[3] 85 86 var i byte 87 ret := []string{} 88 for (min + i) <= max { //境界含む 89 ip := net.IPv4(baseIP[0], baseIP[1], baseIP[2], baseIP[3]+i) 90 ret = append(ret, ip.String()) 91 i++ 92 } 93 94 return ret, nil 95} 96