yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/ecloud/eip.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 ecloud
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"time"
    21  
    22  	billing_api "yunion.io/x/cloudmux/pkg/apis/billing"
    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 SEip struct {
    29  	region *SRegion
    30  	multicloud.SEipBase
    31  	EcloudTags
    32  
    33  	BandWidthMbSize int
    34  	BandWidthType   string
    35  	BindType        string
    36  	// 使用,未使用
    37  	Bound bool
    38  	// bandwidthCharge, trafficCharge
    39  	ChargeModeEnum string
    40  	CreateTime     time.Time
    41  	Frozen         bool
    42  	// 备案状态
    43  	IcpStatus     string
    44  	Id            string
    45  	IpType        string
    46  	Ipv6          string
    47  	Name          string //公网IPv4地址
    48  	NicName       string
    49  	PortNetworkId string
    50  	Region        string
    51  	ResourceId    string
    52  	RouterId      string
    53  	// BINDING, UNBOUND, FROZEN
    54  	Status string
    55  }
    56  
    57  func (e *SEip) GetId() string {
    58  	return e.Id
    59  }
    60  
    61  func (e *SEip) GetName() string {
    62  	return e.Name
    63  }
    64  
    65  func (e *SEip) GetGlobalId() string {
    66  	return e.Id
    67  }
    68  
    69  func (e *SEip) GetStatus() string {
    70  	switch e.Status {
    71  	case "BINDING", "UNBOUND":
    72  		return api.EIP_STATUS_READY
    73  	default:
    74  		return api.EIP_STATUS_UNKNOWN
    75  	}
    76  }
    77  
    78  func (e *SEip) Refresh() error {
    79  	return cloudprovider.ErrNotImplemented
    80  }
    81  
    82  func (e *SEip) IsEmulated() bool {
    83  	return false
    84  }
    85  
    86  func (e *SEip) GetIpAddr() string {
    87  	return e.Name
    88  }
    89  
    90  func (e *SEip) GetMode() string {
    91  	return api.EIP_MODE_STANDALONE_EIP
    92  }
    93  
    94  func (e *SEip) GetAssociationType() string {
    95  	switch e.BindType {
    96  	case "vm":
    97  		return api.EIP_ASSOCIATE_TYPE_SERVER
    98  	case "snat", "dnat":
    99  		return api.EIP_ASSOCIATE_TYPE_NAT_GATEWAY
   100  	case "elb":
   101  		return api.EIP_ASSOCIATE_TYPE_LOADBALANCER
   102  	default:
   103  		return e.BindType
   104  	}
   105  }
   106  
   107  func (e *SEip) GetAssociationExternalId() string {
   108  	return e.ResourceId
   109  }
   110  
   111  func (e *SEip) GetBillingType() string {
   112  	return billing_api.BILLING_TYPE_POSTPAID
   113  }
   114  
   115  func (e *SEip) GetCreatedAt() time.Time {
   116  	return e.CreateTime
   117  }
   118  
   119  func (e *SEip) GetExpiredAt() time.Time {
   120  	return time.Time{}
   121  }
   122  
   123  func (e *SEip) Delete() error {
   124  	return cloudprovider.ErrNotImplemented
   125  }
   126  
   127  func (e *SEip) GetBandwidth() int {
   128  	return e.BandWidthMbSize
   129  }
   130  
   131  func (e *SEip) GetINetworkId() string {
   132  	return e.PortNetworkId
   133  }
   134  
   135  func (e *SEip) GetInternetChargeType() string {
   136  	switch e.ChargeModeEnum {
   137  	// bandwidthCharge, trafficCharge
   138  	case "trafficCharge":
   139  		return api.EIP_CHARGE_TYPE_BY_TRAFFIC
   140  	case "bandwidthCharge":
   141  		return api.EIP_CHARGE_TYPE_BY_BANDWIDTH
   142  	default:
   143  		return "unknown"
   144  	}
   145  }
   146  
   147  func (e *SEip) Associate(conf *cloudprovider.AssociateConfig) error {
   148  	return cloudprovider.ErrNotImplemented
   149  }
   150  
   151  func (e *SEip) Dissociate() error {
   152  	return cloudprovider.ErrNotImplemented
   153  }
   154  
   155  func (e *SEip) ChangeBandwidth(bw int) error {
   156  	return cloudprovider.ErrNotImplemented
   157  }
   158  
   159  func (e *SEip) GetProjectId() string {
   160  	return ""
   161  }
   162  
   163  func (r *SRegion) GetEipById(id string) (*SEip, error) {
   164  	var eip SEip
   165  	request := NewConsoleRequest(r.ID, fmt.Sprintf("/api/v2/floatingIp/getRespWithBw/%s", id), nil, nil)
   166  	err := r.client.doGet(context.Background(), request, &eip)
   167  	if err != nil {
   168  		return nil, err
   169  	}
   170  	return &eip, nil
   171  }