yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/nutanix/vpc.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 nutanix
    16  
    17  import (
    18  	"fmt"
    19  	"net/url"
    20  	"sort"
    21  	"strconv"
    22  	"strings"
    23  
    24  	"yunion.io/x/jsonutils"
    25  	"yunion.io/x/pkg/errors"
    26  
    27  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    28  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    29  	"yunion.io/x/cloudmux/pkg/multicloud"
    30  )
    31  
    32  type DhcpOptions struct {
    33  }
    34  
    35  type SPool struct {
    36  	Range string `json:"range"`
    37  }
    38  
    39  type IPConfig struct {
    40  	NetworkAddress    string      `json:"network_address"`
    41  	PrefixLength      int         `json:"prefix_length"`
    42  	DefaultGateway    string      `json:"default_gateway"`
    43  	DhcpOptions       DhcpOptions `json:"dhcp_options"`
    44  	Pool              []SPool     `json:"pool"`
    45  	DhcpServerAddress string      `json:"dhcp_server_address"`
    46  }
    47  
    48  type SVpc struct {
    49  	multicloud.SVpc
    50  	multicloud.STagBase
    51  
    52  	region *SRegion
    53  
    54  	LogicalTimestamp int      `json:"logical_timestamp"`
    55  	VlanID           int      `json:"vlan_id"`
    56  	UUID             string   `json:"uuid"`
    57  	Name             string   `json:"name"`
    58  	IPConfig         IPConfig `json:"ip_config,omitempty"`
    59  }
    60  
    61  func (self *SVpc) GetName() string {
    62  	return self.Name
    63  }
    64  
    65  func (self *SVpc) GetId() string {
    66  	return self.UUID
    67  }
    68  
    69  func (self *SVpc) GetGlobalId() string {
    70  	return self.UUID
    71  }
    72  
    73  func (self *SVpc) Delete() error {
    74  	return self.region.DeleteVpc(self.UUID)
    75  }
    76  
    77  func (self *SVpc) GetCidrBlock() string {
    78  	if len(self.IPConfig.NetworkAddress) > 0 {
    79  		return fmt.Sprintf("%s/%d", self.IPConfig.NetworkAddress, self.IPConfig.PrefixLength)
    80  	}
    81  	return ""
    82  }
    83  
    84  func (self *SVpc) GetIRouteTables() ([]cloudprovider.ICloudRouteTable, error) {
    85  	return []cloudprovider.ICloudRouteTable{}, nil
    86  }
    87  
    88  func (self *SVpc) GetIRouteTableById(routeTableId string) (cloudprovider.ICloudRouteTable, error) {
    89  	return nil, cloudprovider.ErrNotFound
    90  }
    91  
    92  func (self *SVpc) GetISecurityGroups() ([]cloudprovider.ICloudSecurityGroup, error) {
    93  	return []cloudprovider.ICloudSecurityGroup{}, nil
    94  }
    95  
    96  func (self *SRegion) GetVpcs() ([]SVpc, error) {
    97  	vpcs := []SVpc{}
    98  	_, err := self.list("networks", url.Values{}, &vpcs)
    99  	return vpcs, err
   100  }
   101  
   102  func (self *SVpc) getWire() *SWire {
   103  	return &SWire{vpc: self}
   104  }
   105  
   106  func (self *SVpc) GetIWires() ([]cloudprovider.ICloudWire, error) {
   107  	wire := self.getWire()
   108  	return []cloudprovider.ICloudWire{wire}, nil
   109  }
   110  
   111  func (self *SVpc) GetIWireById(wireId string) (cloudprovider.ICloudWire, error) {
   112  	wires, err := self.GetIWires()
   113  	if err != nil {
   114  		return nil, err
   115  	}
   116  	for i := range wires {
   117  		if wires[i].GetGlobalId() == wireId {
   118  			return wires[i], nil
   119  		}
   120  	}
   121  	return nil, cloudprovider.ErrNotFound
   122  }
   123  
   124  func (self *SVpc) GetIsDefault() bool {
   125  	return len(self.GetCidrBlock()) > 0
   126  }
   127  
   128  func (self *SVpc) GetRegion() cloudprovider.ICloudRegion {
   129  	return self.region
   130  }
   131  
   132  func (self *SVpc) GetStatus() string {
   133  	return api.VPC_STATUS_AVAILABLE
   134  }
   135  
   136  func (self *SRegion) GetVpc(id string) (*SVpc, error) {
   137  	vpc := &SVpc{region: self}
   138  	return vpc, self.get("networks", id, url.Values{}, vpc)
   139  }
   140  
   141  func (self *SRegion) CreateVpc(opts *cloudprovider.VpcCreateOptions) (*SVpc, error) {
   142  	ipConfig := map[string]interface{}{}
   143  	if len(opts.CIDR) > 0 {
   144  		if addrs := strings.Split(opts.CIDR, "/"); len(addrs) == 2 {
   145  			ipConfig["network_address"] = addrs[0]
   146  			ipConfig["prefix_length"], _ = strconv.Atoi(addrs[1])
   147  		}
   148  
   149  	}
   150  	vpcs, err := self.GetVpcs()
   151  	if err != nil {
   152  		return nil, errors.Wrapf(err, "GetVpcs")
   153  	}
   154  	vlanId, vlanIds := -1, []int{}
   155  	for i := range vpcs {
   156  		vlanIds = append(vlanIds, vpcs[i].VlanID)
   157  	}
   158  	sort.Ints(vlanIds)
   159  	for _, vlan := range vlanIds {
   160  		if vlan == vlanId+1 {
   161  			vlanId = vlan
   162  		}
   163  	}
   164  	params := map[string]interface{}{
   165  		"name":       opts.NAME,
   166  		"annotation": opts.Desc,
   167  		"ip_config":  ipConfig,
   168  		"vlan_id":    vlanId + 1,
   169  	}
   170  	ret := struct {
   171  		NetworkUUID string
   172  	}{}
   173  	err = self.post("networks", jsonutils.Marshal(params), &ret)
   174  	if err != nil {
   175  		return nil, err
   176  	}
   177  	return self.GetVpc(ret.NetworkUUID)
   178  }
   179  
   180  func (self *SRegion) DeleteVpc(id string) error {
   181  	return self.delete("networks", id)
   182  }