yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/azure/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 SNetwork struct {
    30  	multicloud.SResourceBase
    31  	AzureTags
    32  	wire *SWire
    33  
    34  	//AvailableIpAddressCount int `json:"availableIpAddressCount,omitempty"`
    35  	ID            string
    36  	Name          string
    37  	Properties    SubnetPropertiesFormat
    38  	AddressPrefix string `json:"addressPrefix,omitempty"`
    39  }
    40  
    41  func (self *SNetwork) GetTags() (map[string]string, error) {
    42  	return self.Tags, nil
    43  }
    44  
    45  func (self *SNetwork) GetId() string {
    46  	return self.ID
    47  }
    48  
    49  func (self *SNetwork) GetName() string {
    50  	return self.Name
    51  }
    52  
    53  func (self *SNetwork) GetGlobalId() string {
    54  	return strings.ToLower(self.ID)
    55  }
    56  
    57  func (self *SNetwork) GetStatus() string {
    58  	return api.NETWORK_STATUS_AVAILABLE
    59  }
    60  
    61  func (self *SNetwork) Delete() error {
    62  	return self.wire.vpc.region.del(self.ID)
    63  }
    64  
    65  func (self *SNetwork) GetGateway() string {
    66  	pref, _ := netutils.NewIPV4Prefix(self.Properties.AddressPrefix)
    67  	endIp := pref.Address.BroadcastAddr(pref.MaskLen) // 255
    68  	endIp = endIp.StepDown()                          // 254
    69  	return endIp.String()
    70  }
    71  
    72  func (self *SNetwork) GetIWire() cloudprovider.ICloudWire {
    73  	return self.wire
    74  }
    75  
    76  func (self *SNetwork) GetIpEnd() string {
    77  	pref, _ := netutils.NewIPV4Prefix(self.Properties.AddressPrefix)
    78  	endIp := pref.Address.BroadcastAddr(pref.MaskLen) // 255
    79  	endIp = endIp.StepDown()                          // 254
    80  	return endIp.String()
    81  }
    82  
    83  func (self *SNetwork) GetIpMask() int8 {
    84  	pref, _ := netutils.NewIPV4Prefix(self.Properties.AddressPrefix)
    85  	return pref.MaskLen
    86  }
    87  
    88  // https://docs.microsoft.com/en-us/azure/virtual-network/virtual-networks-faq
    89  func (self *SNetwork) GetIpStart() string {
    90  	pref, _ := netutils.NewIPV4Prefix(self.Properties.AddressPrefix)
    91  	startIp := pref.Address.NetAddr(pref.MaskLen) // 0
    92  	startIp = startIp.StepUp()                    // 1
    93  	startIp = startIp.StepUp()                    // 2
    94  	startIp = startIp.StepUp()                    // 3
    95  	startIp = startIp.StepUp()                    // 4
    96  	return startIp.String()
    97  }
    98  
    99  func (self *SNetwork) GetIsPublic() bool {
   100  	return true
   101  }
   102  
   103  func (self *SNetwork) GetPublicScope() rbacutils.TRbacScope {
   104  	return rbacutils.ScopeDomain
   105  }
   106  
   107  func (self *SNetwork) GetServerType() string {
   108  	return api.NETWORK_TYPE_GUEST
   109  }
   110  
   111  func (self *SNetwork) Refresh() error {
   112  	network, err := self.wire.zone.region.GetNetwork(self.ID)
   113  	if err != nil {
   114  		return err
   115  	}
   116  	return jsonutils.Update(self, network)
   117  }
   118  
   119  func (self *SNetwork) GetAllocTimeoutSeconds() int {
   120  	return 120 // 2 minutes
   121  }
   122  
   123  func (self *SNetwork) GetProjectId() string {
   124  	return getResourceGroup(self.ID)
   125  }