yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/qcloud/natgateway.go (about)

     1  // Copyright 2019 Yunion
     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 qcloud
    16  
    17  import (
    18  	"fmt"
    19  	"time"
    20  
    21  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    22  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    23  	"yunion.io/x/cloudmux/pkg/multicloud"
    24  )
    25  
    26  type SNatGateway struct {
    27  	multicloud.SNatGatewayBase
    28  	QcloudTags
    29  	vpc *SVpc
    30  
    31  	NatId            string  `json:"natId"`
    32  	NatName          string  `json:"natName"`
    33  	ProductionStatus float32 `json:"productionStatus"`
    34  	State            float32 `json:"state"`
    35  	UnVpcId          string  `json:"unVpcId"`
    36  	VpcId            float32 `json:"vpcId"`
    37  	VpcName          string  `json:"vpcName"`
    38  	Zone             string  `json:"zone"`
    39  
    40  	Bandwidth     float32   `json:"bandwidth"`
    41  	CreateTime    time.Time `json:"createTime"`
    42  	EipCount      float32   `json:"eipCount"`
    43  	MaxConcurrent float32   `json:"maxConcurrent"`
    44  }
    45  
    46  func (nat *SNatGateway) GetName() string {
    47  	if len(nat.NatName) > 0 {
    48  		return nat.NatName
    49  	}
    50  	return nat.NatId
    51  }
    52  
    53  func (nat *SNatGateway) GetId() string {
    54  	return nat.NatId
    55  }
    56  
    57  func (nat *SNatGateway) GetGlobalId() string {
    58  	return nat.NatId
    59  }
    60  
    61  func (self *SNatGateway) GetINetworkId() string {
    62  	return ""
    63  }
    64  
    65  func (nat *SNatGateway) GetStatus() string {
    66  	// NAT网关状态,0:运行中, 1:不可用, 2:欠费停服
    67  	switch int(nat.State) {
    68  	case 0:
    69  		return api.NAT_STAUTS_AVAILABLE
    70  	case 1, 2:
    71  		return api.NAT_STATUS_UNKNOWN
    72  	default:
    73  		return api.NAT_STATUS_UNKNOWN
    74  	}
    75  }
    76  
    77  func (nat *SNatGateway) GetNatSpec() string {
    78  	switch int(nat.MaxConcurrent) {
    79  	case 100 * 10000:
    80  		return api.QCLOUD_NAT_SPEC_SMALL
    81  	case 300 * 10000:
    82  		return api.QCLOUD_NAT_SPEC_MIDDLE
    83  	case 1000 * 10000:
    84  		return api.QCLOUD_NAT_SPEC_LARGE
    85  	}
    86  	return ""
    87  }
    88  
    89  func (nat *SNatGateway) GetIEips() ([]cloudprovider.ICloudEIP, error) {
    90  	eips := []SEipAddress{}
    91  	for {
    92  		part, total, err := nat.vpc.region.GetEips("", nat.NatId, len(eips), 50)
    93  		if err != nil {
    94  			return nil, err
    95  		}
    96  		eips = append(eips, part...)
    97  		if len(eips) >= total {
    98  			break
    99  		}
   100  	}
   101  	ieips := []cloudprovider.ICloudEIP{}
   102  	for i := 0; i < len(eips); i++ {
   103  		eips[i].region = nat.vpc.region
   104  		ieips = append(ieips, &eips[i])
   105  	}
   106  	return ieips, nil
   107  }
   108  
   109  func (nat *SNatGateway) GetINatSTable() ([]cloudprovider.ICloudNatSEntry, error) {
   110  	return []cloudprovider.ICloudNatSEntry{}, nil
   111  }
   112  
   113  func (nat *SNatGateway) GetINatDTable() ([]cloudprovider.ICloudNatDEntry, error) {
   114  	tables, err := nat.vpc.region.GetDTables(nat.NatId, nat.vpc.VpcId)
   115  	if err != nil {
   116  		return nil, err
   117  	}
   118  	itables := []cloudprovider.ICloudNatDEntry{}
   119  	for i := 0; i < len(tables); i++ {
   120  		tables[i].nat = nat
   121  		itables = append(itables, &tables[i])
   122  	}
   123  	return itables, nil
   124  }
   125  
   126  func (nat *SNatGateway) GetINatDEntryByID(id string) (cloudprovider.ICloudNatDEntry, error) {
   127  	return nil, cloudprovider.ErrNotImplemented
   128  }
   129  
   130  func (nat *SNatGateway) GetINatSEntryByID(id string) (cloudprovider.ICloudNatSEntry, error) {
   131  	return nil, cloudprovider.ErrNotImplemented
   132  }
   133  
   134  func (nat *SNatGateway) CreateINatDEntry(rule cloudprovider.SNatDRule) (cloudprovider.ICloudNatDEntry, error) {
   135  	return nil, cloudprovider.ErrNotImplemented
   136  }
   137  
   138  func (nat *SNatGateway) CreateINatSEntry(rule cloudprovider.SNatSRule) (cloudprovider.ICloudNatSEntry, error) {
   139  	return nil, cloudprovider.ErrNotImplemented
   140  }
   141  
   142  func (region *SRegion) GetNatGateways(vpcId string, offset int, limit int) ([]SNatGateway, int, error) {
   143  	if limit > 50 || limit <= 0 {
   144  		limit = 50
   145  	}
   146  
   147  	params := make(map[string]string)
   148  	params["Limit"] = fmt.Sprintf("%d", limit)
   149  	params["Offset"] = fmt.Sprintf("%d", offset)
   150  	if len(vpcId) > 0 {
   151  		params["vpcId"] = vpcId
   152  	}
   153  	body, err := region.vpc2017Request("DescribeNatGateway", params)
   154  	if err != nil {
   155  		return nil, 0, err
   156  	}
   157  	nats := []SNatGateway{}
   158  	err = body.Unmarshal(&nats, "data")
   159  	if err != nil {
   160  		return nil, 0, err
   161  	}
   162  	total, _ := body.Float("totalCount")
   163  	return nats, int(total), nil
   164  }