yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/azure/classic_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 azure
    16  
    17  import (
    18  	"net/url"
    19  	"strings"
    20  	"time"
    21  
    22  	"yunion.io/x/jsonutils"
    23  	"yunion.io/x/log"
    24  
    25  	billing_api "yunion.io/x/cloudmux/pkg/apis/billing"
    26  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    27  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    28  	"yunion.io/x/cloudmux/pkg/multicloud"
    29  )
    30  
    31  type ClassicEipProperties struct {
    32  	IpAddress         string       `json:"ipAddress,omitempty"`
    33  	Status            string       `json:"status,omitempty"`
    34  	ProvisioningState string       `json:"provisioningState,omitempty"`
    35  	InUse             bool         `json:"inUse,omitempty"`
    36  	AttachedTo        *SubResource `joson:"attachedTo,omitempty"`
    37  }
    38  
    39  type SClassicEipAddress struct {
    40  	region *SRegion
    41  	multicloud.SEipBase
    42  	AzureTags
    43  
    44  	ID         string
    45  	instanceId string
    46  	Name       string
    47  	Location   string
    48  	Properties ClassicEipProperties `json:"properties,omitempty"`
    49  	Type       string
    50  }
    51  
    52  func (self *SClassicEipAddress) Associate(conf *cloudprovider.AssociateConfig) error {
    53  	return cloudprovider.ErrNotImplemented
    54  }
    55  
    56  func (self *SClassicEipAddress) ChangeBandwidth(bw int) error {
    57  	return cloudprovider.ErrNotSupported
    58  }
    59  
    60  func (self *SClassicEipAddress) Delete() error {
    61  	if !self.IsEmulated() {
    62  		return self.region.DeallocateEIP(self.ID)
    63  	}
    64  	return nil
    65  }
    66  
    67  func (self *SClassicEipAddress) Dissociate() error {
    68  	return cloudprovider.ErrNotImplemented
    69  }
    70  
    71  func (self *SClassicEipAddress) GetAssociationExternalId() string {
    72  	// TODO
    73  	return self.instanceId
    74  }
    75  
    76  func (self *SClassicEipAddress) GetAssociationType() string {
    77  	if len(self.instanceId) > 0 {
    78  		return api.EIP_ASSOCIATE_TYPE_SERVER
    79  	}
    80  	return ""
    81  }
    82  
    83  func (self *SClassicEipAddress) GetBandwidth() int {
    84  	return 0
    85  }
    86  
    87  func (self *SClassicEipAddress) GetINetworkId() string {
    88  	return ""
    89  }
    90  
    91  func (self *SClassicEipAddress) GetGlobalId() string {
    92  	return strings.ToLower(self.ID)
    93  }
    94  
    95  func (self *SClassicEipAddress) GetId() string {
    96  	return strings.ToLower(self.ID)
    97  }
    98  
    99  func (self *SClassicEipAddress) GetInternetChargeType() string {
   100  	return api.EIP_CHARGE_TYPE_BY_TRAFFIC
   101  }
   102  
   103  func (self *SClassicEipAddress) GetIpAddr() string {
   104  	return self.Properties.IpAddress
   105  }
   106  
   107  func (self *SClassicEipAddress) GetMode() string {
   108  	// TODO
   109  	if self.instanceId == self.ID {
   110  		return api.EIP_MODE_INSTANCE_PUBLICIP
   111  	}
   112  	return api.EIP_MODE_STANDALONE_EIP
   113  }
   114  
   115  func (self *SClassicEipAddress) GetName() string {
   116  	return self.Name
   117  }
   118  
   119  func (self *SClassicEipAddress) GetStatus() string {
   120  	switch self.Properties.Status {
   121  	case "Created", "":
   122  		return api.EIP_STATUS_READY
   123  	default:
   124  		log.Errorf("Unknown eip status: %s", self.Properties.ProvisioningState)
   125  		return api.EIP_STATUS_UNKNOWN
   126  	}
   127  }
   128  
   129  func (self *SClassicEipAddress) IsEmulated() bool {
   130  	if self.ID == self.instanceId {
   131  		return true
   132  	}
   133  	return false
   134  }
   135  
   136  func (region *SRegion) GetClassicEip(eipId string) (*SClassicEipAddress, error) {
   137  	eip := SClassicEipAddress{region: region}
   138  	return &eip, region.get(eipId, url.Values{}, &eip)
   139  }
   140  
   141  func (self *SClassicEipAddress) Refresh() error {
   142  	eip, err := self.region.GetClassicEip(self.ID)
   143  	if err != nil {
   144  		return err
   145  	}
   146  	return jsonutils.Update(self, eip)
   147  }
   148  
   149  func (region *SRegion) GetClassicEips() ([]SClassicEipAddress, error) {
   150  	eips := []SClassicEipAddress{}
   151  	err := region.list("Microsoft.ClassicNetwork/reservedIps", url.Values{}, &eips)
   152  	if err != nil {
   153  		return nil, err
   154  	}
   155  	result := []SClassicEipAddress{}
   156  	for i := 0; i < len(eips); i++ {
   157  		if eips[i].Location == region.Name {
   158  			eips[i].region = region
   159  			result = append(result, eips[i])
   160  		}
   161  	}
   162  	return result, nil
   163  }
   164  
   165  func (self *SClassicEipAddress) GetBillingType() string {
   166  	return billing_api.BILLING_TYPE_POSTPAID
   167  }
   168  
   169  func (self *SClassicEipAddress) GetCreatedAt() time.Time {
   170  	return time.Time{}
   171  }
   172  
   173  func (self *SClassicEipAddress) GetExpiredAt() time.Time {
   174  	return time.Time{}
   175  }
   176  
   177  func (self *SClassicEipAddress) GetProjectId() string {
   178  	return getResourceGroup(self.ID)
   179  }