yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/apsara/ipv6_gateway.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 apsara
    16  
    17  import (
    18  	"fmt"
    19  	"time"
    20  
    21  	"yunion.io/x/pkg/errors"
    22  
    23  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    24  	"yunion.io/x/cloudmux/pkg/multicloud"
    25  )
    26  
    27  type SIPv6Gateway struct {
    28  	multicloud.SResourceBase
    29  	ApsaraTags
    30  	region *SRegion
    31  
    32  	Status             string    `json:"Status"`
    33  	Description        string    `json:"Description"`
    34  	InstanceChargeType string    `json:"InstanceChargeType"`
    35  	Ipv6GatewayId      string    `json:"Ipv6GatewayId"`
    36  	BusinessStatus     string    `json:"BusinessStatus"`
    37  	Name               string    `json:"Name"`
    38  	VpcID              string    `json:"VpcId"`
    39  	ExpiredTime        string    `json:"ExpiredTime"`
    40  	CreationTime       time.Time `json:"CreationTime"`
    41  	RegionId           string    `json:"RegionId"`
    42  	Spec               string    `json:"Spec"`
    43  
    44  	DepartmentInfo
    45  }
    46  
    47  func (self *SIPv6Gateway) GetGlobalId() string {
    48  	return self.Ipv6GatewayId
    49  }
    50  
    51  func (self *SIPv6Gateway) GetId() string {
    52  	return self.Ipv6GatewayId
    53  }
    54  
    55  func (self *SIPv6Gateway) GetName() string {
    56  	if len(self.Name) > 0 {
    57  		return self.Name
    58  	}
    59  	return self.Ipv6GatewayId
    60  }
    61  
    62  func (self *SIPv6Gateway) GetStatus() string {
    63  	return self.Status
    64  }
    65  
    66  func (self *SIPv6Gateway) GetInstanceType() string {
    67  	return self.Spec
    68  }
    69  
    70  func (self *SIPv6Gateway) GetCreatedAt() time.Time {
    71  	return self.CreationTime
    72  }
    73  
    74  func (self *SRegion) GetIPv6Gateways(vpcId string, pageNumber, pageSize int) ([]SIPv6Gateway, int, error) {
    75  	if pageSize < 1 {
    76  		pageSize = 50
    77  	}
    78  	if pageNumber < 1 {
    79  		pageNumber = 1
    80  	}
    81  	params := map[string]string{
    82  		"PageSize":   fmt.Sprintf("%d", pageSize),
    83  		"PageNumber": fmt.Sprintf("%d", 1),
    84  	}
    85  	if len(vpcId) > 0 {
    86  		params["VpcId"] = vpcId
    87  	}
    88  	resp, err := self.vpcRequest("DescribeIpv6Gateways", params)
    89  	if err != nil {
    90  		return nil, 0, errors.Wrapf(err, "DescribeIpv6Gateways")
    91  	}
    92  	ret := []SIPv6Gateway{}
    93  	err = resp.Unmarshal(&ret, "Ipv6Gateways", "Ipv6Gateway")
    94  	if err != nil {
    95  		return nil, 0, errors.Wrapf(err, "resp.Unmarshal")
    96  	}
    97  	totalCount, _ := resp.Int("TotalCount")
    98  	return ret, int(totalCount), nil
    99  }
   100  
   101  func (self *SRegion) GetIPv6Gateway(id string) (*SIPv6Gateway, error) {
   102  	params := map[string]string{
   103  		"Ipv6GatewayId": id,
   104  	}
   105  	resp, err := self.vpcRequest("DescribeIpv6GatewayAttribute", params)
   106  	if err != nil {
   107  		return nil, err
   108  	}
   109  	ret := &SIPv6Gateway{region: self}
   110  	return ret, resp.Unmarshal(ret)
   111  }
   112  
   113  func (self *SVpc) GetICloudIPv6Gateways() ([]cloudprovider.ICloudIPv6Gateway, error) {
   114  	res, pageNumber := []SIPv6Gateway{}, 1
   115  	for {
   116  		part, total, err := self.region.GetIPv6Gateways(self.VpcId, pageNumber, 50)
   117  		if err != nil {
   118  			return nil, err
   119  		}
   120  		res = append(res, part...)
   121  		if len(res) >= total {
   122  			break
   123  		}
   124  		pageNumber++
   125  	}
   126  	ret := []cloudprovider.ICloudIPv6Gateway{}
   127  	for i := range res {
   128  		res[i].region = self.region
   129  		ret = append(ret, &res[i])
   130  	}
   131  	return ret, nil
   132  }