yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/qcloud/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 qcloud
    16  
    17  import (
    18  	"time"
    19  
    20  	"github.com/pkg/errors"
    21  
    22  	"yunion.io/x/jsonutils"
    23  	"yunion.io/x/log"
    24  	"yunion.io/x/pkg/util/netutils"
    25  
    26  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    27  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    28  	"yunion.io/x/cloudmux/pkg/multicloud"
    29  	"yunion.io/x/onecloud/pkg/util/rbacutils"
    30  )
    31  
    32  type SNetwork struct {
    33  	multicloud.SResourceBase
    34  	QcloudTags
    35  	wire *SWire
    36  
    37  	CidrBlock               string
    38  	Zone                    string
    39  	SubnetId                string
    40  	VpcId                   string
    41  	SubnetName              string
    42  	AvailableIpAddressCount int
    43  	CreatedTime             time.Time
    44  	EnableBroadcast         bool
    45  	IsDefault               bool
    46  	RouteTableId            string
    47  }
    48  
    49  func (self *SNetwork) GetId() string {
    50  	return self.SubnetId
    51  }
    52  
    53  func (self *SNetwork) GetName() string {
    54  	if len(self.SubnetName) > 0 {
    55  		return self.SubnetName
    56  	}
    57  	return self.SubnetId
    58  }
    59  
    60  func (self *SNetwork) GetGlobalId() string {
    61  	return self.SubnetId
    62  }
    63  
    64  func (self *SNetwork) IsEmulated() bool {
    65  	return false
    66  }
    67  
    68  func (self *SNetwork) GetStatus() string {
    69  	return api.NETWORK_STATUS_AVAILABLE
    70  }
    71  
    72  func (self *SNetwork) Delete() error {
    73  	return self.wire.zone.region.DeleteNetwork(self.SubnetId)
    74  }
    75  
    76  func (self *SRegion) DeleteNetwork(networkId string) error {
    77  	params := make(map[string]string)
    78  	params["SubnetId"] = networkId
    79  
    80  	interfaces := []SNetworkInterface{}
    81  	for {
    82  		_interfaces, total, err := self.GetNetworkInterfaces([]string{}, "", networkId, len(interfaces), 50)
    83  		if err != nil {
    84  			return errors.Wrapf(err, "DeleteNetwork.GetNetworkInterfaces")
    85  		}
    86  		interfaces = append(interfaces, _interfaces...)
    87  		if len(interfaces) >= total {
    88  			break
    89  		}
    90  	}
    91  
    92  	for _, nic := range interfaces {
    93  		err := self.DeleteNetworkInterface(nic.NetworkInterfaceId)
    94  		if err != nil {
    95  			return errors.Wrapf(err, "DeleteNetwork.DeleteNetworkInterface")
    96  		}
    97  	}
    98  
    99  	_, err := self.vpcRequest("DeleteSubnet", params)
   100  	if err != nil {
   101  		return errors.Wrapf(err, "vpcRequest.DeleteSubnet")
   102  	}
   103  	return nil
   104  }
   105  
   106  func (self *SNetwork) GetIWire() cloudprovider.ICloudWire {
   107  	return self.wire
   108  }
   109  
   110  func (self *SNetwork) GetAllocTimeoutSeconds() int {
   111  	return 120 // 2 minutes
   112  }
   113  
   114  func (self *SNetwork) SetTags(tags map[string]string, replace bool) error {
   115  	return self.wire.vpc.region.SetResourceTags("vpc", "subnet", []string{self.SubnetId}, tags, replace)
   116  }
   117  
   118  func (self *SNetwork) GetGateway() string {
   119  	pref, _ := netutils.NewIPV4Prefix(self.CidrBlock)
   120  	endIp := pref.Address.BroadcastAddr(pref.MaskLen) // 255
   121  	endIp = endIp.StepDown()                          // 254
   122  	return endIp.String()
   123  }
   124  
   125  //https://cloud.tencent.com/document/product/215/20046
   126  func (self *SNetwork) GetIpStart() string {
   127  	pref, _ := netutils.NewIPV4Prefix(self.CidrBlock)
   128  	startIp := pref.Address.NetAddr(pref.MaskLen) // 0
   129  	startIp = startIp.StepUp()                    // 1
   130  	startIp = startIp.StepUp()                    // 2
   131  	return startIp.String()
   132  }
   133  
   134  func (self *SNetwork) GetIpEnd() string {
   135  	pref, _ := netutils.NewIPV4Prefix(self.CidrBlock)
   136  	endIp := pref.Address.BroadcastAddr(pref.MaskLen) // 255
   137  	endIp = endIp.StepDown()                          // 254
   138  	return endIp.String()
   139  }
   140  
   141  func (self *SNetwork) GetIpMask() int8 {
   142  	pref, _ := netutils.NewIPV4Prefix(self.CidrBlock)
   143  	return pref.MaskLen
   144  }
   145  
   146  func (self *SNetwork) GetIsPublic() bool {
   147  	// return self.IsDefault
   148  	return true
   149  }
   150  
   151  func (self *SNetwork) GetPublicScope() rbacutils.TRbacScope {
   152  	return rbacutils.ScopeDomain
   153  }
   154  
   155  func (self *SNetwork) GetServerType() string {
   156  	return api.NETWORK_TYPE_GUEST
   157  }
   158  
   159  func (self *SNetwork) Refresh() error {
   160  	log.Debugf("network refresh %s", self.SubnetId)
   161  	new, err := self.wire.zone.region.GetNetwork(self.SubnetId)
   162  	if err != nil {
   163  		return err
   164  	}
   165  	return jsonutils.Update(self, new)
   166  }
   167  
   168  func (self *SRegion) CreateNetwork(zoneId string, vpcId string, name string, cidr string, desc string) (string, error) {
   169  	params := make(map[string]string)
   170  	params["Zone"] = zoneId
   171  	params["VpcId"] = vpcId
   172  	params["CidrBlock"] = cidr
   173  	params["SubnetName"] = name
   174  	body, err := self.vpcRequest("CreateSubnet", params)
   175  	if err != nil {
   176  		return "", err
   177  	}
   178  	return body.GetString("Subnet", "SubnetId")
   179  }
   180  
   181  func (self *SNetwork) GetProjectId() string {
   182  	return ""
   183  }