yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/jdcloud/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 jdcloud
    16  
    17  import (
    18  	"fmt"
    19  	"time"
    20  
    21  	"github.com/jdcloud-api/jdcloud-sdk-go/services/vpc/apis"
    22  	"github.com/jdcloud-api/jdcloud-sdk-go/services/vpc/client"
    23  	"github.com/jdcloud-api/jdcloud-sdk-go/services/vpc/models"
    24  
    25  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    26  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    27  	"yunion.io/x/cloudmux/pkg/multicloud"
    28  )
    29  
    30  type SEip struct {
    31  	region *SRegion
    32  	multicloud.SEipBase
    33  	JdcloudTags
    34  
    35  	models.ElasticIp
    36  }
    37  
    38  func (e *SEip) GetId() string {
    39  	return e.ElasticIpId
    40  }
    41  
    42  func (e *SEip) GetName() string {
    43  	return e.ElasticIpAddress
    44  }
    45  
    46  func (e *SEip) GetGlobalId() string {
    47  	return e.GetId()
    48  }
    49  
    50  func (e *SEip) GetStatus() string {
    51  	return api.EIP_STATUS_READY
    52  }
    53  
    54  func (e *SEip) Refresh() error {
    55  	return nil
    56  }
    57  
    58  func (e *SEip) IsEmulated() bool {
    59  	return false
    60  }
    61  
    62  func (e *SEip) GetIpAddr() string {
    63  	return e.ElasticIpAddress
    64  }
    65  
    66  func (e *SEip) GetMode() string {
    67  	return api.EIP_MODE_STANDALONE_EIP
    68  }
    69  
    70  func (e *SEip) GetAssociationType() string {
    71  	switch e.InstanceType {
    72  	case "compute":
    73  		return api.EIP_ASSOCIATE_TYPE_SERVER
    74  	case "lb":
    75  		return api.EIP_ASSOCIATE_TYPE_LOADBALANCER
    76  	default:
    77  		return e.InstanceType
    78  	}
    79  }
    80  
    81  func (e *SEip) GetAssociationExternalId() string {
    82  	return e.InstanceId
    83  }
    84  
    85  func (e *SEip) GetBillingType() string {
    86  	return billingType(&e.Charge)
    87  }
    88  
    89  func (e *SEip) GetCreatedAt() time.Time {
    90  	return parseTime(e.CreatedTime)
    91  }
    92  
    93  func (e *SEip) GetExpiredAt() time.Time {
    94  	return expireAt(&e.Charge)
    95  }
    96  
    97  func (e *SEip) Delete() error {
    98  	return nil
    99  }
   100  
   101  func (e *SEip) GetBandwidth() int {
   102  	return e.BandwidthMbps
   103  }
   104  
   105  func (e *SEip) GetINetworkId() string {
   106  	return ""
   107  }
   108  
   109  func (e *SEip) GetInternetChargeType() string {
   110  	switch e.Charge.ChargeMode {
   111  	case "postpaid_by_usage":
   112  		return api.EIP_CHARGE_TYPE_BY_TRAFFIC
   113  	case "postpaid_by_duration":
   114  		return api.EIP_CHARGE_TYPE_BY_BANDWIDTH
   115  	default:
   116  		return api.EIP_CHARGE_TYPE_BY_TRAFFIC
   117  	}
   118  }
   119  
   120  func (e *SEip) Associate(conf *cloudprovider.AssociateConfig) error {
   121  	return nil
   122  }
   123  
   124  func (e *SEip) Dissociate() error {
   125  	return nil
   126  }
   127  
   128  func (e *SEip) ChangeBandwidth(bw int) error {
   129  	return nil
   130  }
   131  
   132  func (e *SEip) GetProjectId() string {
   133  	return ""
   134  }
   135  
   136  func (r *SRegion) GetEIPById(id string) (*SEip, error) {
   137  	req := apis.NewDescribeElasticIpRequest(r.ID, id)
   138  	client := client.NewVpcClient(r.getCredential())
   139  	client.Logger = Logger{debug: r.client.debug}
   140  	resp, err := client.DescribeElasticIp(req)
   141  	if err != nil {
   142  		return nil, err
   143  	}
   144  	if resp.Error.Code >= 400 {
   145  		return nil, fmt.Errorf(resp.Error.Message)
   146  	}
   147  	return &SEip{
   148  		region:    r,
   149  		ElasticIp: resp.Result.ElasticIp,
   150  	}, nil
   151  }