yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/azure/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  	"net/url"
    19  	"strings"
    20  
    21  	"yunion.io/x/jsonutils"
    22  	"yunion.io/x/pkg/errors"
    23  
    24  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    25  	"yunion.io/x/cloudmux/pkg/multicloud"
    26  )
    27  
    28  type AddressSpace struct {
    29  	AddressPrefixes []string `json:"addressPrefixes,omitempty"`
    30  }
    31  
    32  type SubnetPropertiesFormat struct {
    33  	AddressPrefix string `json:"addressPrefix,omitempty"`
    34  	//ProvisioningState string
    35  }
    36  
    37  type VirtualNetworkPropertiesFormat struct {
    38  	ProvisioningState      string
    39  	Status                 string
    40  	VirtualNetworkSiteName string
    41  	AddressSpace           AddressSpace `json:"addressSpace,omitempty"`
    42  	Subnets                []SNetwork   `json:"subnets,omitempty"`
    43  }
    44  
    45  type SVpc struct {
    46  	multicloud.SVpc
    47  	AzureTags
    48  	region *SRegion
    49  
    50  	ID         string
    51  	Name       string
    52  	Etag       string
    53  	Type       string
    54  	Location   string
    55  	Properties VirtualNetworkPropertiesFormat `json:"properties,omitempty"`
    56  }
    57  
    58  func (self *SVpc) GetTags() (map[string]string, error) {
    59  	return self.Tags, nil
    60  }
    61  
    62  func (self *SVpc) GetId() string {
    63  	return self.ID
    64  }
    65  
    66  func (self *SVpc) GetName() string {
    67  	return self.Name
    68  }
    69  
    70  func (self *SVpc) GetGlobalId() string {
    71  	return strings.ToLower(self.ID)
    72  }
    73  
    74  func (self *SVpc) IsEmulated() bool {
    75  	return false
    76  }
    77  
    78  func (self *SVpc) GetIsDefault() bool {
    79  	return true
    80  }
    81  
    82  func (self *SVpc) GetCidrBlock() string {
    83  	if len(self.Properties.AddressSpace.AddressPrefixes) > 0 {
    84  		return self.Properties.AddressSpace.AddressPrefixes[0]
    85  	}
    86  	return ""
    87  }
    88  
    89  func (self *SVpc) Delete() error {
    90  	return self.region.DeleteVpc(self.ID)
    91  }
    92  
    93  func (self *SRegion) DeleteVpc(vpcId string) error {
    94  	return self.del(vpcId)
    95  }
    96  
    97  func (self *SVpc) GetISecurityGroups() ([]cloudprovider.ICloudSecurityGroup, error) {
    98  	secgroups, err := self.region.ListSecgroups()
    99  	if err != nil {
   100  		return nil, errors.Wrapf(err, "ListSecgroups")
   101  	}
   102  	ret := []cloudprovider.ICloudSecurityGroup{}
   103  	for i := range secgroups {
   104  		secgroups[i].region = self.region
   105  		ret = append(ret, &secgroups[i])
   106  	}
   107  	return ret, nil
   108  }
   109  
   110  func (self *SVpc) GetIRouteTables() ([]cloudprovider.ICloudRouteTable, error) {
   111  	rts := []cloudprovider.ICloudRouteTable{}
   112  	return rts, nil
   113  }
   114  
   115  func (self *SVpc) GetIRouteTableById(routeTableId string) (cloudprovider.ICloudRouteTable, error) {
   116  	return nil, cloudprovider.ErrNotSupported
   117  }
   118  
   119  func (self *SVpc) GetIWireById(wireId string) (cloudprovider.ICloudWire, error) {
   120  	wire := self.getWire()
   121  	if wire.GetGlobalId() != wireId {
   122  		return nil, errors.Wrapf(cloudprovider.ErrNotFound, wireId)
   123  	}
   124  	return wire, nil
   125  }
   126  
   127  func (self *SVpc) getWire() *SWire {
   128  	zone := self.region.getZone()
   129  	return &SWire{zone: zone, vpc: self}
   130  }
   131  
   132  func (self *SVpc) GetIWires() ([]cloudprovider.ICloudWire, error) {
   133  	return []cloudprovider.ICloudWire{self.getWire()}, nil
   134  }
   135  
   136  func (self *SVpc) GetRegion() cloudprovider.ICloudRegion {
   137  	return self.region
   138  }
   139  
   140  func (self *SVpc) GetStatus() string {
   141  	if strings.ToLower(self.Properties.ProvisioningState) == "succeeded" {
   142  		return "available"
   143  	}
   144  	return "disabled"
   145  }
   146  
   147  func (region *SRegion) GetVpc(vpcId string) (*SVpc, error) {
   148  	vpc := SVpc{region: region}
   149  	return &vpc, region.get(vpcId, url.Values{}, &vpc)
   150  }
   151  
   152  func (self *SVpc) Refresh() error {
   153  	vpc, err := self.region.GetVpc(self.ID)
   154  	if err != nil {
   155  		return err
   156  	}
   157  	return jsonutils.Update(self, vpc)
   158  }
   159  
   160  func (self *SVpc) GetNetworks() []SNetwork {
   161  	return self.Properties.Subnets
   162  }
   163  
   164  func (self *SRegion) GetNetwork(networkId string) (*SNetwork, error) {
   165  	network := SNetwork{}
   166  	return &network, self.get(networkId, url.Values{}, &network)
   167  }