yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/proxmox/network.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 proxmox
    16  
    17  import (
    18  	"fmt"
    19  	"net/url"
    20  	"strings"
    21  
    22  	"yunion.io/x/jsonutils"
    23  	"yunion.io/x/pkg/errors"
    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  	"yunion.io/x/onecloud/pkg/util/rbacutils"
    30  )
    31  
    32  type SNetwork struct {
    33  	multicloud.SResourceBase
    34  	ProxmoxTags
    35  
    36  	wire *SWire
    37  
    38  	Id string
    39  
    40  	Priority        int      `json:"priority"`
    41  	Families        []string `json:"families"`
    42  	Method          string   `json:"method"`
    43  	Autostart       int      `json:"autostart,omitempty"`
    44  	Iface           string   `json:"iface"`
    45  	BridgeFd        string   `json:"bridge_fd,omitempty"`
    46  	Method6         string   `json:"method6"`
    47  	BridgePorts     string   `json:"bridge_ports,omitempty"`
    48  	Type            string   `json:"type"`
    49  	Active          int      `json:"active"`
    50  	BridgeStp       string   `json:"bridge_stp,omitempty"`
    51  	BridgeVids      string   `json:"bridge_vids,omitempty"`
    52  	Netmask         string   `json:"netmask,omitempty"`
    53  	Address         string   `json:"address,omitempty"`
    54  	BridgeVlanAware int      `json:"bridge_vlan_aware,omitempty"`
    55  	Gateway         string   `json:"gateway,omitempty"`
    56  	Cidr            string   `json:"cidr,omitempty"`
    57  	Exists          int      `json:"exists,omitempty"`
    58  	Options         []string `json:"options,omitempty"`
    59  }
    60  
    61  func (self *SNetwork) GetName() string {
    62  	return self.Iface
    63  }
    64  
    65  func (self *SNetwork) GetId() string {
    66  	return self.Id
    67  }
    68  
    69  func (self *SNetwork) GetGlobalId() string {
    70  	return self.GetId()
    71  }
    72  
    73  func (self *SNetwork) Refresh() error {
    74  	ret, err := self.wire.region.GetNetwork(self.Id)
    75  	if err != nil {
    76  		return err
    77  	}
    78  	return jsonutils.Update(self, ret)
    79  }
    80  
    81  func (self *SNetwork) Delete() error {
    82  	return cloudprovider.ErrNotFound
    83  }
    84  
    85  func (self *SNetwork) GetAllocTimeoutSeconds() int {
    86  	return 120 // 2 minutes
    87  }
    88  
    89  func (self *SNetwork) GetGateway() string {
    90  	return self.Gateway
    91  }
    92  
    93  func (self *SNetwork) GetIWire() cloudprovider.ICloudWire {
    94  	return self.wire
    95  }
    96  
    97  func (self *SNetwork) GetIpStart() string {
    98  	pref, _ := netutils.NewIPV4Prefix(self.Cidr)
    99  	startIp := pref.Address.NetAddr(pref.MaskLen) // 0
   100  	startIp = startIp.StepUp()                    // 1
   101  	startIp = startIp.StepUp()                    // 2
   102  	return startIp.String()
   103  }
   104  
   105  func (self *SNetwork) GetIpEnd() string {
   106  	pref, _ := netutils.NewIPV4Prefix(self.Cidr)
   107  	endIp := pref.Address.BroadcastAddr(pref.MaskLen) // 255
   108  	endIp = endIp.StepDown()                          // 254
   109  	endIp = endIp.StepDown()                          // 253
   110  	endIp = endIp.StepDown()                          // 252
   111  	return endIp.String()
   112  }
   113  
   114  func (self *SNetwork) Contains(_ip string) bool {
   115  	start, _ := netutils.NewIPV4Addr(self.GetIpStart())
   116  	end, _ := netutils.NewIPV4Addr(self.GetIpEnd())
   117  	ip, _ := netutils.NewIPV4Addr(_ip)
   118  	return netutils.NewIPV4AddrRange(start, end).Contains(ip)
   119  }
   120  
   121  func (self *SNetwork) GetIpMask() int8 {
   122  	pref, _ := netutils.NewIPV4Prefix(self.Cidr)
   123  	return pref.MaskLen
   124  }
   125  
   126  func (self *SNetwork) GetProjectId() string {
   127  	return ""
   128  }
   129  
   130  func (self *SNetwork) GetPublicScope() rbacutils.TRbacScope {
   131  	return rbacutils.ScopeDomain
   132  }
   133  
   134  func (self *SNetwork) GetServerType() string {
   135  	return api.NETWORK_TYPE_GUEST
   136  }
   137  
   138  func (self *SNetwork) GetStatus() string {
   139  	return api.NETWORK_STATUS_AVAILABLE
   140  }
   141  
   142  func (self *SRegion) GetNetworks() ([]SNetwork, error) {
   143  	ret := []SNetwork{}
   144  
   145  	resources, err := self.GetClusterNodeResources()
   146  	if err != nil {
   147  		return nil, err
   148  	}
   149  
   150  	for _, res := range resources {
   151  		networks := []SNetwork{}
   152  		urlNetwork := fmt.Sprintf("/nodes/%s/network", res.Node)
   153  		self.get(urlNetwork, url.Values{}, &networks)
   154  
   155  		for _, iface := range networks {
   156  			id := fmt.Sprintf("network/%s/%s", res.Node, iface.Iface)
   157  			iface.Id = id
   158  
   159  			ret = append(ret, iface)
   160  		}
   161  
   162  	}
   163  
   164  	return ret, nil
   165  }
   166  
   167  func (self *SRegion) GetNetwork(id string) (*SNetwork, error) {
   168  	ret := &SNetwork{}
   169  
   170  	//"id": "network/nodeNAME/iface",
   171  	splited := strings.Split(id, "/")
   172  	nodeName := ""
   173  	networkName := ""
   174  
   175  	if len(splited) == 3 {
   176  		nodeName = splited[1]
   177  		networkName = splited[2]
   178  	} else {
   179  		return nil, errors.Errorf("failed to get network by %s", id)
   180  	}
   181  
   182  	res := fmt.Sprintf("/nodes/%s/network/%s", nodeName, networkName)
   183  	err := self.get(res, url.Values{}, ret)
   184  	ret.Id = id
   185  
   186  	return ret, err
   187  }