yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/azure/classic_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 azure
    16  
    17  import (
    18  	"strings"
    19  
    20  	"yunion.io/x/jsonutils"
    21  	"yunion.io/x/pkg/util/netutils"
    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  	"yunion.io/x/onecloud/pkg/util/rbacutils"
    27  )
    28  
    29  type SClassicNetwork struct {
    30  	multicloud.SResourceBase
    31  	AzureTags
    32  	wire *SClassicWire
    33  
    34  	id            string
    35  	Name          string
    36  	AddressPrefix string `json:"addressPrefix,omitempty"`
    37  }
    38  
    39  func (self *SClassicNetwork) GetId() string {
    40  	return strings.ToLower(self.id)
    41  }
    42  
    43  func (self *SClassicNetwork) GetName() string {
    44  	return self.Name
    45  }
    46  
    47  func (self *SClassicNetwork) GetGlobalId() string {
    48  	return self.GetId()
    49  }
    50  
    51  func (self *SClassicNetwork) IsEmulated() bool {
    52  	return false
    53  }
    54  
    55  func (self *SClassicNetwork) GetStatus() string {
    56  	return api.NETWORK_STATUS_AVAILABLE
    57  }
    58  
    59  func (self *SClassicNetwork) Delete() error {
    60  	vpc := self.wire.vpc
    61  	subnets := []SClassicNetwork{}
    62  	for i := 0; i < len(vpc.Properties.Subnets); i++ {
    63  		network := vpc.Properties.Subnets[i]
    64  		if network.Name == self.Name && self.AddressPrefix == network.AddressPrefix {
    65  			continue
    66  		}
    67  		subnets = append(subnets, network)
    68  	}
    69  	return self.wire.vpc.region.update(jsonutils.Marshal(vpc), self.wire.vpc)
    70  }
    71  
    72  func (self *SClassicNetwork) GetGateway() string {
    73  	pref, _ := netutils.NewIPV4Prefix(self.AddressPrefix)
    74  	endIp := pref.Address.BroadcastAddr(pref.MaskLen) // 255
    75  	endIp = endIp.StepDown()                          // 254
    76  	return endIp.String()
    77  }
    78  
    79  func (self *SClassicNetwork) GetIWire() cloudprovider.ICloudWire {
    80  	return self.wire
    81  }
    82  
    83  func (self *SClassicNetwork) GetIpEnd() string {
    84  	pref, _ := netutils.NewIPV4Prefix(self.AddressPrefix)
    85  	endIp := pref.Address.BroadcastAddr(pref.MaskLen) // 255
    86  	endIp = endIp.StepDown()                          // 254
    87  	return endIp.String()
    88  }
    89  
    90  func (self *SClassicNetwork) GetIpMask() int8 {
    91  	pref, _ := netutils.NewIPV4Prefix(self.AddressPrefix)
    92  	return pref.MaskLen
    93  }
    94  
    95  // https://docs.microsoft.com/en-us/azure/virtual-network/virtual-networks-faq
    96  func (self *SClassicNetwork) GetIpStart() string {
    97  	pref, _ := netutils.NewIPV4Prefix(self.AddressPrefix)
    98  	startIp := pref.Address.NetAddr(pref.MaskLen) // 0
    99  	startIp = startIp.StepUp()                    // 1
   100  	startIp = startIp.StepUp()                    // 2
   101  	startIp = startIp.StepUp()                    // 3
   102  	startIp = startIp.StepUp()                    // 4
   103  	return startIp.String()
   104  }
   105  
   106  func (self *SClassicNetwork) GetIsPublic() bool {
   107  	return true
   108  }
   109  
   110  func (self *SClassicNetwork) GetPublicScope() rbacutils.TRbacScope {
   111  	return rbacutils.ScopeDomain
   112  }
   113  
   114  func (self *SClassicNetwork) GetServerType() string {
   115  	return api.NETWORK_TYPE_GUEST
   116  }
   117  
   118  func (self *SClassicNetwork) Refresh() error {
   119  	err := self.wire.vpc.Refresh()
   120  	if err != nil {
   121  		return err
   122  	}
   123  	for _, network := range self.wire.vpc.Properties.Subnets {
   124  		if network.Name == self.Name {
   125  			self.AddressPrefix = network.AddressPrefix
   126  			return nil
   127  		}
   128  	}
   129  	return cloudprovider.ErrNotFound
   130  }
   131  
   132  func (self *SClassicNetwork) GetAllocTimeoutSeconds() int {
   133  	return 120 // 2 minutes
   134  }
   135  
   136  func (self *SClassicNetwork) GetProjectId() string {
   137  	return ""
   138  }