yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/apsara/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 apsara
    16  
    17  import (
    18  	"strings"
    19  	"time"
    20  
    21  	"yunion.io/x/jsonutils"
    22  	"yunion.io/x/log"
    23  
    24  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    25  	"yunion.io/x/cloudmux/pkg/multicloud"
    26  )
    27  
    28  const (
    29  	VpcAvailable = "Available"
    30  	VpcPending   = "Pending"
    31  )
    32  
    33  // "CidrBlock":"172.31.0.0/16","CreationTime":"2017-03-19T13:37:40Z","Description":"System created default VPC.","IsDefault":true,"RegionId":"cn-hongkong","Status":"Available","UserCidrs":{"UserCidr":[]},"VRouterId":"vrt-j6c00qrol733dg36iq4qj","VSwitchIds":{"VSwitchId":["vsw-j6c3gig5ub4fmi2veyrus"]},"VpcId":"vpc-j6c86z3sh8ufhgsxwme0q","VpcName":""
    34  
    35  type SUserCIDRs struct {
    36  	UserCidr []string
    37  }
    38  
    39  type SVSwitchIds struct {
    40  	VSwitchId []string
    41  }
    42  
    43  type SVpc struct {
    44  	multicloud.SVpc
    45  	ApsaraTags
    46  
    47  	region *SRegion
    48  
    49  	iwires []cloudprovider.ICloudWire
    50  
    51  	secgroups   []cloudprovider.ICloudSecurityGroup
    52  	routeTables []cloudprovider.ICloudRouteTable
    53  
    54  	CidrBlock    string
    55  	CreationTime time.Time
    56  	Description  string
    57  	IsDefault    bool
    58  	RegionId     string
    59  	Status       string
    60  	UserCidrs    SUserCIDRs
    61  	VRouterId    string
    62  	VSwitchIds   SVSwitchIds
    63  	VpcId        string
    64  	VpcName      string
    65  
    66  	DepartmentInfo
    67  }
    68  
    69  func (self *SVpc) GetId() string {
    70  	return self.VpcId
    71  }
    72  
    73  func (self *SVpc) GetName() string {
    74  	if len(self.VpcName) > 0 {
    75  		return self.VpcName
    76  	}
    77  	return self.VpcId
    78  }
    79  
    80  func (self *SVpc) GetGlobalId() string {
    81  	return self.VpcId
    82  }
    83  
    84  func (self *SVpc) IsEmulated() bool {
    85  	return false
    86  }
    87  
    88  func (self *SVpc) GetIsDefault() bool {
    89  	return self.IsDefault
    90  }
    91  
    92  func (self *SVpc) GetCidrBlock() string {
    93  	return self.CidrBlock
    94  }
    95  
    96  func (self *SVpc) GetStatus() string {
    97  	return strings.ToLower(self.Status)
    98  }
    99  
   100  func (self *SVpc) GetCreatedAt() time.Time {
   101  	return self.CreationTime
   102  }
   103  
   104  func (self *SVpc) Refresh() error {
   105  	new, err := self.region.getVpc(self.VpcId)
   106  	if err != nil {
   107  		return err
   108  	}
   109  	return jsonutils.Update(self, new)
   110  }
   111  
   112  func (self *SVpc) GetRegion() cloudprovider.ICloudRegion {
   113  	return self.region
   114  }
   115  
   116  func (self *SVpc) GetSysTags() map[string]string {
   117  	tags := self.ApsaraTags.GetSysTags()
   118  	if len(self.ResourceGroup) > 0 {
   119  		tags["ResourceGroup"] = self.ResourceGroup
   120  	}
   121  	if len(self.ResourceGroupId) > 0 {
   122  		tags["ResourceGroupId"] = self.ResourceGroupId
   123  	}
   124  	if len(self.Department) > 0 {
   125  		tags["Department"] = self.Department
   126  	}
   127  	if len(self.DepartmentName) > 0 {
   128  		tags["DepartmentName"] = self.DepartmentName
   129  	}
   130  	if len(self.ResourceGroupName) > 0 {
   131  		groupName := strings.TrimPrefix(self.ResourceGroupName, "ResourceSet(")
   132  		groupName = strings.TrimSuffix(groupName, ")")
   133  		tags["ResourceGroupName"] = groupName
   134  	}
   135  	return tags
   136  }
   137  
   138  func (self *SVpc) addWire(wire *SWire) {
   139  	if self.iwires == nil {
   140  		self.iwires = make([]cloudprovider.ICloudWire, 0)
   141  	}
   142  	self.iwires = append(self.iwires, wire)
   143  }
   144  
   145  func (self *SVpc) getWireByZoneId(zoneId string) *SWire {
   146  	for i := 0; i <= len(self.iwires); i += 1 {
   147  		wire := self.iwires[i].(*SWire)
   148  		if wire.zone.ZoneId == zoneId {
   149  			return wire
   150  		}
   151  	}
   152  	return nil
   153  }
   154  
   155  func (self *SVpc) fetchVSwitches() error {
   156  	switches, total, err := self.region.GetVSwitches(nil, self.VpcId, 0, 50)
   157  	if err != nil {
   158  		return err
   159  	}
   160  	if total > len(switches) {
   161  		switches, _, err = self.region.GetVSwitches(nil, self.VpcId, 0, total)
   162  		if err != nil {
   163  			return err
   164  		}
   165  	}
   166  	for i := 0; i < len(switches); i += 1 {
   167  		wire := self.getWireByZoneId(switches[i].ZoneId)
   168  		switches[i].wire = wire
   169  		wire.addNetwork(&switches[i])
   170  	}
   171  	return nil
   172  }
   173  
   174  func (self *SVpc) GetIWires() ([]cloudprovider.ICloudWire, error) {
   175  	if self.iwires == nil {
   176  		err := self.fetchVSwitches()
   177  		if err != nil {
   178  			return nil, err
   179  		}
   180  	}
   181  	return self.iwires, nil
   182  }
   183  
   184  func (self *SVpc) GetIWireById(wireId string) (cloudprovider.ICloudWire, error) {
   185  	if self.iwires == nil {
   186  		err := self.fetchVSwitches()
   187  		if err != nil {
   188  			return nil, err
   189  		}
   190  	}
   191  	for i := 0; i < len(self.iwires); i += 1 {
   192  		if self.iwires[i].GetGlobalId() == wireId {
   193  			return self.iwires[i], nil
   194  		}
   195  	}
   196  	return nil, cloudprovider.ErrNotFound
   197  }
   198  
   199  func (self *SVpc) fetchSecurityGroups() error {
   200  	secgroups := make([]SSecurityGroup, 0)
   201  	for {
   202  		parts, total, err := self.region.GetSecurityGroups(self.VpcId, "", []string{}, len(secgroups), 50)
   203  		if err != nil {
   204  			return err
   205  		}
   206  		secgroups = append(secgroups, parts...)
   207  		if len(secgroups) >= total {
   208  			break
   209  		}
   210  	}
   211  	self.secgroups = make([]cloudprovider.ICloudSecurityGroup, len(secgroups))
   212  	for i := 0; i < len(secgroups); i++ {
   213  		secgroups[i].vpc = self
   214  		self.secgroups[i] = &secgroups[i]
   215  	}
   216  	return nil
   217  }
   218  
   219  func (self *SVpc) GetISecurityGroups() ([]cloudprovider.ICloudSecurityGroup, error) {
   220  	if self.secgroups == nil {
   221  		err := self.fetchSecurityGroups()
   222  		if err != nil {
   223  			return nil, err
   224  		}
   225  	}
   226  	return self.secgroups, nil
   227  }
   228  
   229  func (self *SVpc) fetchRouteTables() error {
   230  	routeTables := make([]*SRouteTable, 0)
   231  	for {
   232  		parts, total, err := self.RemoteGetRouteTableList(len(routeTables), 50)
   233  		if err != nil {
   234  			return err
   235  		}
   236  		routeTables = append(routeTables, parts...)
   237  		if len(routeTables) >= total {
   238  			break
   239  		}
   240  	}
   241  	self.routeTables = make([]cloudprovider.ICloudRouteTable, len(routeTables))
   242  	for i := 0; i < len(routeTables); i++ {
   243  		routeTables[i].vpc = self
   244  		self.routeTables[i] = routeTables[i]
   245  	}
   246  	return nil
   247  }
   248  
   249  func (self *SVpc) GetIRouteTables() ([]cloudprovider.ICloudRouteTable, error) {
   250  	if self.routeTables == nil {
   251  		err := self.fetchRouteTables()
   252  		if err != nil {
   253  			return nil, err
   254  		}
   255  	}
   256  	return self.routeTables, nil
   257  }
   258  
   259  func (self *SVpc) GetIRouteTableById(routeTableId string) (cloudprovider.ICloudRouteTable, error) {
   260  	return nil, cloudprovider.ErrNotSupported
   261  }
   262  
   263  func (self *SVpc) Delete() error {
   264  	err := self.fetchSecurityGroups()
   265  	if err != nil {
   266  		log.Errorf("fetchSecurityGroup for VPC delete fail %s", err)
   267  		return err
   268  	}
   269  	for i := 0; i < len(self.secgroups); i += 1 {
   270  		secgroup := self.secgroups[i].(*SSecurityGroup)
   271  		err := self.region.DeleteSecurityGroup(secgroup.SecurityGroupId)
   272  		if err != nil {
   273  			log.Errorf("deleteSecurityGroup for VPC delete fail %s", err)
   274  			return err
   275  		}
   276  	}
   277  	return self.region.DeleteVpc(self.VpcId)
   278  }
   279  
   280  func (self *SVpc) getNatGateways() ([]SNatGetway, error) {
   281  	natgatways := make([]SNatGetway, 0)
   282  	gwTotal := -1
   283  	for gwTotal < 0 || len(natgatways) < gwTotal {
   284  		parts, total, err := self.region.GetNatGateways(self.VpcId, "", len(natgatways), 50)
   285  		if err != nil {
   286  			return nil, err
   287  		}
   288  		if len(parts) > 0 {
   289  			natgatways = append(natgatways, parts...)
   290  		}
   291  		gwTotal = total
   292  	}
   293  	for i := 0; i < len(natgatways); i += 1 {
   294  		natgatways[i].vpc = self
   295  	}
   296  	return natgatways, nil
   297  }
   298  
   299  func (self *SVpc) GetINatGateways() ([]cloudprovider.ICloudNatGateway, error) {
   300  	nats := []SNatGetway{}
   301  	for {
   302  		parts, total, err := self.region.GetNatGateways(self.VpcId, "", len(nats), 50)
   303  		if err != nil {
   304  			return nil, err
   305  		}
   306  		nats = append(nats, parts...)
   307  		if len(nats) >= total {
   308  			break
   309  		}
   310  	}
   311  	inats := []cloudprovider.ICloudNatGateway{}
   312  	for i := 0; i < len(nats); i++ {
   313  		nats[i].vpc = self
   314  		inats = append(inats, &nats[i])
   315  	}
   316  	return inats, nil
   317  }