yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/aliyun/natdtable.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 aliyun
    16  
    17  import (
    18  	"fmt"
    19  	"strconv"
    20  
    21  	"yunion.io/x/jsonutils"
    22  
    23  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    24  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    25  	"yunion.io/x/cloudmux/pkg/multicloud"
    26  )
    27  
    28  type SForwardTableEntry struct {
    29  	multicloud.SResourceBase
    30  	AliyunTags
    31  	nat *SNatGateway
    32  
    33  	ForwardEntryId   string
    34  	ForwardEntryName string
    35  	IpProtocol       string
    36  	Status           string
    37  	ExternalIp       string
    38  	ForwardTableId   string
    39  	ExternalPort     string
    40  	InternalPort     string
    41  	InternalIp       string
    42  }
    43  
    44  func (dtable *SForwardTableEntry) GetName() string {
    45  	if len(dtable.ForwardEntryName) > 0 {
    46  		return dtable.ForwardEntryName
    47  	}
    48  	return dtable.ForwardEntryId
    49  }
    50  
    51  func (dtable *SForwardTableEntry) GetId() string {
    52  	return dtable.ForwardEntryId
    53  }
    54  
    55  func (dtable *SForwardTableEntry) GetGlobalId() string {
    56  	return dtable.ForwardEntryId
    57  }
    58  
    59  func (dtable *SForwardTableEntry) GetStatus() string {
    60  	switch dtable.Status {
    61  	case "Available":
    62  		return api.NAT_STAUTS_AVAILABLE
    63  	default:
    64  		return api.NAT_STATUS_UNKNOWN
    65  	}
    66  }
    67  
    68  func (dtable *SForwardTableEntry) GetIpProtocol() string {
    69  	return dtable.IpProtocol
    70  }
    71  
    72  func (dtable *SForwardTableEntry) GetExternalIp() string {
    73  	return dtable.ExternalIp
    74  }
    75  
    76  func (dtable *SForwardTableEntry) GetExternalPort() int {
    77  	port, _ := strconv.Atoi(dtable.ExternalPort)
    78  	return port
    79  }
    80  
    81  func (dtable *SForwardTableEntry) GetInternalIp() string {
    82  	return dtable.InternalIp
    83  }
    84  
    85  func (dtable *SForwardTableEntry) GetInternalPort() int {
    86  	port, _ := strconv.Atoi(dtable.InternalPort)
    87  	return port
    88  }
    89  
    90  func (region *SRegion) GetAllDTables(tableId string) ([]SForwardTableEntry, error) {
    91  	dtables := []SForwardTableEntry{}
    92  	for {
    93  		part, total, err := region.GetForwardTableEntries(tableId, len(dtables), 50)
    94  		if err != nil {
    95  			return nil, err
    96  		}
    97  		dtables = append(dtables, part...)
    98  		if len(dtables) >= total {
    99  			break
   100  		}
   101  	}
   102  	return dtables, nil
   103  }
   104  
   105  func (dtable *SForwardTableEntry) Delete() error {
   106  	return dtable.nat.vpc.region.DeleteForwardTableEntry(dtable.ForwardTableId, dtable.GetId())
   107  }
   108  
   109  func (region *SRegion) GetForwardTableEntries(tableId string, offset int, limit int) ([]SForwardTableEntry, int, error) {
   110  	if limit > 50 || limit <= 0 {
   111  		limit = 50
   112  	}
   113  	params := make(map[string]string)
   114  	params["RegionId"] = region.RegionId
   115  	params["PageSize"] = fmt.Sprintf("%d", limit)
   116  	params["PageNumber"] = fmt.Sprintf("%d", (offset/limit)+1)
   117  	params["ForwardTableId"] = tableId
   118  
   119  	body, err := region.vpcRequest("DescribeForwardTableEntries", params)
   120  	if err != nil {
   121  		return nil, 0, err
   122  	}
   123  
   124  	dtables := []SForwardTableEntry{}
   125  	err = body.Unmarshal(&dtables, "ForwardTableEntries", "ForwardTableEntry")
   126  	if err != nil {
   127  		return nil, 0, err
   128  	}
   129  	total, _ := body.Int("TotalCount")
   130  	return dtables, int(total), nil
   131  }
   132  
   133  func (region *SRegion) GetForwardTableEntry(tableID, forwardEntryID string) (SForwardTableEntry, error) {
   134  	params := make(map[string]string)
   135  	params["RegionId"] = region.RegionId
   136  	params["ForwardTableId"] = tableID
   137  	params["ForwardEntryId"] = forwardEntryID
   138  	body, err := region.vpcRequest("DescribeForwardTableEntries", params)
   139  	if err != nil {
   140  		return SForwardTableEntry{}, err
   141  	}
   142  
   143  	dtables := []SForwardTableEntry{}
   144  	err = body.Unmarshal(&dtables, "ForwardTableEntries", "ForwardTableEntry")
   145  	if err != nil {
   146  		return SForwardTableEntry{}, err
   147  	}
   148  	if len(dtables) == 0 {
   149  		return SForwardTableEntry{}, cloudprovider.ErrNotFound
   150  	}
   151  	return dtables[0], nil
   152  }
   153  
   154  func (region *SRegion) DeleteForwardTableEntry(tableId string, entryId string) error {
   155  	params := make(map[string]string)
   156  	params["RegionId"] = region.RegionId
   157  	params["ForwardTableId"] = tableId
   158  	params["ForwardEntryId"] = entryId
   159  	_, err := region.vpcRequest("DeleteForwardEntry", params)
   160  	return err
   161  }
   162  
   163  func (region *SRegion) CreateForwardTableEntry(rule cloudprovider.SNatDRule, tableID string) (string, error) {
   164  	params := make(map[string]string)
   165  	params["RegionId"] = region.RegionId
   166  	params["ForwardTableId"] = tableID
   167  	params["ExternalIp"] = rule.ExternalIP
   168  	params["ExternalPort"] = strconv.Itoa(rule.ExternalPort)
   169  	params["InternalIp"] = rule.InternalIP
   170  	params["InternalPort"] = strconv.Itoa(rule.InternalPort)
   171  	params["IpProtocol"] = rule.Protocol
   172  	body, err := region.vpcRequest("CreateForwardEntry", params)
   173  	if err != nil {
   174  		return "", err
   175  	}
   176  
   177  	entryID, _ := body.GetString("ForwardEntryId")
   178  	return entryID, nil
   179  }
   180  
   181  func (dtable *SForwardTableEntry) Refresh() error {
   182  	new, err := dtable.nat.vpc.region.GetForwardTableEntry(dtable.ForwardTableId, dtable.ForwardEntryId)
   183  	if err != nil {
   184  		return err
   185  	}
   186  	return jsonutils.Update(dtable, new)
   187  }