github.com/sacloud/iaas-api-go@v1.12.0/types/vpc_firewall_port.go (about)

     1  // Copyright 2022-2023 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 types
    16  
    17  import (
    18  	"fmt"
    19  	"sort"
    20  	"strings"
    21  )
    22  
    23  // VPCFirewallPort パケットフィルターのルールでのポート型
    24  //
    25  // 1~65535 の整数、その範囲指定(ハイフン区切り)、または複数指定(コンマ区切り 6個まで)の形式の形式のみ受け付ける
    26  type VPCFirewallPort string
    27  
    28  // SetPort 単一のポート番号の設定
    29  func (p *VPCFirewallPort) SetPort(port int) {
    30  	*p = VPCFirewallPort(fmt.Sprintf("%d", port))
    31  }
    32  
    33  // SetPortRange ポート範囲指定
    34  func (p *VPCFirewallPort) SetPortRange(from, to int) {
    35  	*p = VPCFirewallPort(fmt.Sprintf("%d-%d", from, to))
    36  }
    37  
    38  // SetPortMultiple 複数のポートを指定(6個まで)
    39  func (p *VPCFirewallPort) SetPortMultiple(ports ...int) {
    40  	sort.Ints(ports)
    41  	strPort := make([]string, len(ports))
    42  	for i := range ports {
    43  		strPort[i] = fmt.Sprintf("%d", ports[i])
    44  	}
    45  
    46  	*p = VPCFirewallPort(strings.Join(strPort, ","))
    47  }
    48  
    49  // IsEmpty 値が指定されているか
    50  func (p *VPCFirewallPort) IsEmpty() bool {
    51  	return p != nil && p.String() != ""
    52  }
    53  
    54  // String 文字列表現
    55  func (p *VPCFirewallPort) String() string {
    56  	return string(*p)
    57  }
    58  
    59  // Equal 指定のパケットフィルタポートと同じ値を持つか
    60  func (p *VPCFirewallPort) Equal(p2 *PacketFilterPort) bool {
    61  	return p.String() == p2.String()
    62  }