yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/hcs/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 hcs
    16  
    17  import (
    18  	"fmt"
    19  	"net/url"
    20  
    21  	"yunion.io/x/jsonutils"
    22  	"yunion.io/x/pkg/util/netutils"
    23  
    24  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    25  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    26  	"yunion.io/x/cloudmux/pkg/multicloud"
    27  	"yunion.io/x/onecloud/pkg/util/rbacutils"
    28  )
    29  
    30  /*
    31  Subnets
    32  */
    33  
    34  // https://support.huaweicloud.com/api-vpc/zh-cn_topic_0020090590.html
    35  type SNetwork struct {
    36  	multicloud.SResourceBase
    37  	HcsTags
    38  	wire *SWire
    39  
    40  	AvailabilityZone string   `json:"availability_zone"`
    41  	CIDR             string   `json:"cidr"`
    42  	DHCPEnable       bool     `json:"dhcp_enable"`
    43  	DNSList          []string `json:"dnsList"`
    44  	GatewayIP        string   `json:"gateway_ip"`
    45  	Id               string   `json:"id"`
    46  	Ipv6Enable       bool     `json:"ipv6_enable"`
    47  	Name             string   `json:"name"`
    48  	NeutronNetworkId string   `json:"neutron_network_id"`
    49  	NeutronSubnetId  string   `json:"neutron_subnet_id"`
    50  	PrimaryDNS       string   `json:"primary_dns"`
    51  	SecondaryDNS     string   `json:"secondary_dns"`
    52  	Status           string   `json:"status"`
    53  	VpcId            string   `json:"vpc_id"`
    54  }
    55  
    56  func (self *SNetwork) GetId() string {
    57  	return self.Id
    58  }
    59  
    60  func (self *SNetwork) GetName() string {
    61  	if len(self.Name) == 0 {
    62  		return self.Id
    63  	}
    64  	return self.Name
    65  }
    66  
    67  func (self *SNetwork) GetGlobalId() string {
    68  	return self.Id
    69  }
    70  
    71  // https://support.huaweicloud.com/api-vpc/zh-cn_topic_0020090591.html
    72  func (self *SNetwork) GetStatus() string {
    73  	switch self.Status {
    74  	case "ACTIVE", "UNKNOWN":
    75  		return api.NETWORK_STATUS_AVAILABLE // ? todo: // UNKNOWN
    76  	case "ERROR":
    77  		return api.NETWORK_STATUS_UNKNOWN
    78  	default:
    79  		return api.NETWORK_STATUS_UNKNOWN
    80  	}
    81  }
    82  
    83  func (self *SNetwork) Refresh() error {
    84  	ret, err := self.wire.region.GetNetwork(self.Id)
    85  	if err != nil {
    86  		return err
    87  	}
    88  	return jsonutils.Update(self, ret)
    89  }
    90  
    91  func (self *SNetwork) GetIWire() cloudprovider.ICloudWire {
    92  	return self.wire
    93  }
    94  
    95  func (self *SNetwork) GetIpStart() string {
    96  	pref, _ := netutils.NewIPV4Prefix(self.CIDR)
    97  	startIp := pref.Address.NetAddr(pref.MaskLen) // 0
    98  	startIp = startIp.StepUp()                    // 1
    99  	startIp = startIp.StepUp()                    // 2
   100  	return startIp.String()
   101  }
   102  
   103  func (self *SNetwork) GetIpEnd() string {
   104  	pref, _ := netutils.NewIPV4Prefix(self.CIDR)
   105  	endIp := pref.Address.BroadcastAddr(pref.MaskLen) // 255
   106  	endIp = endIp.StepDown()                          // 254
   107  	endIp = endIp.StepDown()                          // 253
   108  	endIp = endIp.StepDown()                          // 252
   109  	return endIp.String()
   110  }
   111  
   112  func (self *SNetwork) GetIpMask() int8 {
   113  	pref, _ := netutils.NewIPV4Prefix(self.CIDR)
   114  	return pref.MaskLen
   115  }
   116  
   117  func (self *SNetwork) GetGateway() string {
   118  	pref, _ := netutils.NewIPV4Prefix(self.CIDR)
   119  	startIp := pref.Address.NetAddr(pref.MaskLen) // 0
   120  	startIp = startIp.StepUp()                    // 1
   121  	return startIp.String()
   122  }
   123  
   124  func (self *SNetwork) GetServerType() string {
   125  	return api.NETWORK_TYPE_GUEST
   126  }
   127  
   128  func (self *SNetwork) GetIsPublic() bool {
   129  	return true
   130  }
   131  
   132  func (self *SNetwork) GetPublicScope() rbacutils.TRbacScope {
   133  	return rbacutils.ScopeDomain
   134  }
   135  
   136  func (self *SNetwork) Delete() error {
   137  	return self.wire.region.DeleteNetwork(self.Id)
   138  }
   139  
   140  func (self *SNetwork) GetAllocTimeoutSeconds() int {
   141  	return 120 // 2 minutes
   142  }
   143  
   144  func (self *SRegion) GetNetwork(id string) (*SNetwork, error) {
   145  	ret := &SNetwork{}
   146  	res := fmt.Sprintf("subnets/%s", id)
   147  	return ret, self.vpcGet(res, ret)
   148  }
   149  
   150  // https://support.huaweicloud.com/api-vpc/zh-cn_topic_0020090592.html
   151  func (self *SRegion) GetNetwroks(vpcId string) ([]SNetwork, error) {
   152  	params := url.Values{}
   153  	if len(vpcId) > 0 {
   154  		params.Set("vpc_id", vpcId)
   155  	}
   156  	ret := []SNetwork{}
   157  	return ret, self.vpcList("subnets", params, &ret)
   158  }
   159  
   160  func (self *SRegion) DeleteNetwork(id string) error {
   161  	res := fmt.Sprintf("subnets/%s", id)
   162  	return self.vpcDelete(res)
   163  }
   164  
   165  func (self *SNetwork) GetProjectId() string {
   166  	return self.wire.vpc.EnterpriseProjectId
   167  }