yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/zstack/vip.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 zstack
    16  
    17  import (
    18  	"fmt"
    19  	"net/url"
    20  
    21  	"github.com/pkg/errors"
    22  
    23  	"yunion.io/x/jsonutils"
    24  	"yunion.io/x/pkg/util/netutils"
    25  
    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 SVirtualIP struct {
    32  	multicloud.SNetworkInterfaceBase
    33  	ZStackTags
    34  	region *SRegion
    35  	ZStackBasic
    36  	IPRangeUUID        string   `json:"ipRangeUuid"`
    37  	L3NetworkUUID      string   `json:"l3NetworkUuid"`
    38  	IP                 string   `json:"ip"`
    39  	State              string   `json:"state"`
    40  	Gateway            string   `json:"gateway"`
    41  	Netmask            string   `json:"netmask"`
    42  	PrefixLen          int      `json:"prefixLen"`
    43  	ServiceProvider    string   `json:"serviceProvider"`
    44  	PeerL3NetworkUuids []string `json:"peerL3NetworkUuids"`
    45  	UseFor             string   `json:"useFor"`
    46  	UsedIPUUID         string   `json:"usedIpUuid"`
    47  	ZStackTime
    48  }
    49  
    50  type SInterfaceIP struct {
    51  	IP            string
    52  	L3NetworkUUID string
    53  	IPRangeUUID   string
    54  }
    55  
    56  func (ip *SInterfaceIP) GetIP() string {
    57  	return ip.IP
    58  }
    59  
    60  func (ip *SInterfaceIP) GetINetworkId() string {
    61  	return fmt.Sprintf("%s/%s", ip.L3NetworkUUID, ip.IPRangeUUID)
    62  }
    63  
    64  func (ip *SInterfaceIP) IsPrimary() bool {
    65  	return true
    66  }
    67  
    68  func (ip *SInterfaceIP) GetGlobalId() string {
    69  	return ip.IP
    70  }
    71  
    72  func (vip *SVirtualIP) GetName() string {
    73  	if len(vip.Name) > 0 {
    74  		return vip.Name
    75  	}
    76  	return vip.UUID
    77  }
    78  
    79  func (vip *SVirtualIP) GetId() string {
    80  	return vip.UUID
    81  }
    82  
    83  func (vip *SVirtualIP) GetGlobalId() string {
    84  	return vip.UUID
    85  }
    86  
    87  func (vip *SVirtualIP) GetMacAddress() string {
    88  	ip, _ := netutils.NewIPV4Addr(vip.IP)
    89  	return ip.ToMac("00:16:")
    90  }
    91  
    92  func (vip *SVirtualIP) GetAssociateType() string {
    93  	switch vip.UseFor {
    94  	case "LoadBalancer":
    95  		return api.NETWORK_INTERFACE_ASSOCIATE_TYPE_LOADBALANCER
    96  	case "Eip":
    97  		return api.NETWORK_INTERFACE_ASSOCIATE_TYPE_RESERVED
    98  	}
    99  	return vip.UseFor
   100  }
   101  
   102  func (vip *SVirtualIP) GetAssociateId() string {
   103  	return vip.UsedIPUUID
   104  }
   105  
   106  func (vip *SVirtualIP) GetStatus() string {
   107  	if vip.State == "Enabled" {
   108  		return api.NETWORK_INTERFACE_STATUS_AVAILABLE
   109  	}
   110  	return api.NETWORK_INTERFACE_STATUS_UNKNOWN
   111  }
   112  
   113  func (vip *SVirtualIP) GetICloudInterfaceAddresses() ([]cloudprovider.ICloudInterfaceAddress, error) {
   114  	ip := &SInterfaceIP{IP: vip.IP, IPRangeUUID: vip.IPRangeUUID, L3NetworkUUID: vip.L3NetworkUUID}
   115  	return []cloudprovider.ICloudInterfaceAddress{ip}, nil
   116  }
   117  
   118  func (region *SRegion) GetVirtualIP(vipId string) (*SVirtualIP, error) {
   119  	vip := &SVirtualIP{}
   120  	return vip, region.client.getResource("vips", vipId, vip)
   121  }
   122  
   123  func (region *SRegion) GetNetworkId(vip *SVirtualIP) string {
   124  	networks, err := region.GetNetworks("", "", vip.L3NetworkUUID, "")
   125  	if err == nil {
   126  		for _, network := range networks {
   127  			if network.Contains(vip.IP) {
   128  				return fmt.Sprintf("%s/%s", vip.L3NetworkUUID, network.UUID)
   129  			}
   130  		}
   131  	}
   132  	return ""
   133  }
   134  
   135  func (region *SRegion) GetVirtualIPs(vipId string) ([]SVirtualIP, error) {
   136  	vips := []SVirtualIP{}
   137  	params := url.Values{}
   138  	if len(vipId) > 0 {
   139  		params.Add("q", "uuid="+vipId)
   140  	}
   141  	return vips, region.client.listAll("vips", params, &vips)
   142  }
   143  
   144  func (region *SRegion) CreateVirtualIP(name, desc, ip string, l3Id string) (*SVirtualIP, error) {
   145  	vip := SVirtualIP{}
   146  	params := map[string]map[string]string{
   147  		"params": {
   148  			"name":          name,
   149  			"description":   desc,
   150  			"l3NetworkUuid": l3Id,
   151  		},
   152  	}
   153  	if len(ip) > 0 {
   154  		params["params"]["requiredIp"] = ip
   155  	}
   156  	resp, err := region.client.post("vips", jsonutils.Marshal(params))
   157  	if err != nil {
   158  		return nil, err
   159  	}
   160  	return &vip, resp.Unmarshal(&vip, "inventory")
   161  }
   162  
   163  func (region *SRegion) DeleteVirtualIP(vipId string) error {
   164  	return region.client.delete("vips", vipId, "")
   165  }
   166  
   167  func (region *SRegion) GetINetworkInterfaces() ([]cloudprovider.ICloudNetworkInterface, error) {
   168  	vips, err := region.GetVirtualIPs("")
   169  	if err != nil {
   170  		return nil, errors.Wrap(err, "region.GetVirtualIPs")
   171  	}
   172  	ret := []cloudprovider.ICloudNetworkInterface{}
   173  	for i := 0; i < len(vips); i++ {
   174  		if vips[i].UseFor != "Eip" {
   175  			vips[i].region = region
   176  			ret = append(ret, &vips[i])
   177  		}
   178  	}
   179  	return ret, nil
   180  }