yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/azure/classic_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 azure
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  
    21  	"yunion.io/x/pkg/errors"
    22  
    23  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    24  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    25  	"yunion.io/x/cloudmux/pkg/multicloud"
    26  )
    27  
    28  type ClassicAddressSpace struct {
    29  	AddressPrefixes []string
    30  }
    31  
    32  type ClassicSubnet struct {
    33  	Name          string
    34  	AddressPrefix string
    35  }
    36  
    37  type ClassicVpcProperties struct {
    38  	ProvisioningState string
    39  	Status            string
    40  	SiteId            string
    41  	InUse             bool
    42  	AddressSpace      ClassicAddressSpace
    43  	Subnets           []SClassicNetwork
    44  }
    45  
    46  type SClassicVpc struct {
    47  	multicloud.SVpc
    48  	AzureTags
    49  
    50  	region *SRegion
    51  
    52  	ID         string
    53  	Name       string
    54  	Type       string
    55  	Location   string
    56  	Properties ClassicVpcProperties
    57  }
    58  
    59  func (self *SClassicVpc) GetId() string {
    60  	return self.ID
    61  }
    62  
    63  func (self *SClassicVpc) GetName() string {
    64  	return self.Name
    65  }
    66  
    67  func (self *SClassicVpc) GetGlobalId() string {
    68  	return strings.ToLower(self.ID)
    69  }
    70  
    71  func (self *SClassicVpc) IsEmulated() bool {
    72  	return false
    73  }
    74  
    75  func (self *SClassicVpc) GetIsDefault() bool {
    76  	return false
    77  }
    78  
    79  func (self *SClassicVpc) GetCidrBlock() string {
    80  	if len(self.Properties.AddressSpace.AddressPrefixes) > 0 {
    81  		return self.Properties.AddressSpace.AddressPrefixes[0]
    82  	}
    83  	return ""
    84  }
    85  
    86  func (self *SClassicVpc) Delete() error {
    87  	return self.region.del(self.ID)
    88  }
    89  
    90  func (self *SClassicVpc) GetISecurityGroups() ([]cloudprovider.ICloudSecurityGroup, error) {
    91  	secgroups, err := self.region.ListSecgroups()
    92  	if err != nil {
    93  		return nil, errors.Wrapf(err, "ListSecgroups")
    94  	}
    95  	ret := []cloudprovider.ICloudSecurityGroup{}
    96  	for i := range secgroups {
    97  		secgroups[i].region = self.region
    98  		ret = append(ret, &secgroups[i])
    99  	}
   100  	return ret, nil
   101  }
   102  
   103  func (self *SClassicVpc) GetIRouteTables() ([]cloudprovider.ICloudRouteTable, error) {
   104  	rts := []cloudprovider.ICloudRouteTable{}
   105  	return rts, nil
   106  }
   107  
   108  func (self *SClassicVpc) GetIRouteTableById(routeTableId string) (cloudprovider.ICloudRouteTable, error) {
   109  	return nil, cloudprovider.ErrNotSupported
   110  }
   111  
   112  func (self *SClassicVpc) getWire() *SClassicWire {
   113  	return &SClassicWire{vpc: self, zone: self.region.getZone()}
   114  }
   115  
   116  func (self *SClassicVpc) GetIWireById(wireId string) (cloudprovider.ICloudWire, error) {
   117  	wire := self.getWire()
   118  	if wire.GetGlobalId() != wireId {
   119  		return nil, errors.Wrapf(cloudprovider.ErrNotFound, wireId)
   120  	}
   121  	return wire, nil
   122  }
   123  
   124  func (self *SClassicVpc) GetIWires() ([]cloudprovider.ICloudWire, error) {
   125  	return []cloudprovider.ICloudWire{self.getWire()}, nil
   126  }
   127  
   128  func (self *SClassicVpc) GetRegion() cloudprovider.ICloudRegion {
   129  	return self.region
   130  }
   131  
   132  func (self *SClassicVpc) GetStatus() string {
   133  	return api.VPC_STATUS_AVAILABLE
   134  }
   135  
   136  func (self *SClassicVpc) GetNetworks() []SClassicNetwork {
   137  	for i := 0; i < len(self.Properties.Subnets); i++ {
   138  		self.Properties.Subnets[i].id = fmt.Sprintf("%s/%s", self.ID, self.Properties.Subnets[i].Name)
   139  	}
   140  	return self.Properties.Subnets
   141  }