yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/hcso/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 hcso
    16  
    17  import (
    18  	"yunion.io/x/jsonutils"
    19  	"yunion.io/x/log"
    20  	"yunion.io/x/pkg/util/netutils"
    21  
    22  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    23  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    24  	"yunion.io/x/cloudmux/pkg/multicloud"
    25  	"yunion.io/x/cloudmux/pkg/multicloud/hcso/client/modules"
    26  	"yunion.io/x/cloudmux/pkg/multicloud/huawei"
    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  	huawei.HuaweiTags
    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  
    65  	return self.Name
    66  }
    67  
    68  func (self *SNetwork) GetGlobalId() string {
    69  	return self.ID
    70  }
    71  
    72  // https://support.huaweicloud.com/api-vpc/zh-cn_topic_0020090591.html
    73  func (self *SNetwork) GetStatus() string {
    74  	switch self.Status {
    75  	case "ACTIVE", "UNKNOWN":
    76  		return api.NETWORK_STATUS_AVAILABLE // ? todo: // UNKNOWN
    77  	case "ERROR":
    78  		return api.NETWORK_STATUS_UNKNOWN
    79  	default:
    80  		return api.NETWORK_STATUS_UNKNOWN
    81  	}
    82  }
    83  
    84  func (self *SNetwork) Refresh() error {
    85  	log.Debugf("network refresh %s", self.GetId())
    86  	new, err := self.wire.region.getNetwork(self.GetId())
    87  	if err != nil {
    88  		return err
    89  	}
    90  	return jsonutils.Update(self, new)
    91  }
    92  
    93  func (self *SNetwork) IsEmulated() bool {
    94  	return false
    95  }
    96  
    97  func (self *SNetwork) GetIWire() cloudprovider.ICloudWire {
    98  	return self.wire
    99  }
   100  
   101  func (self *SNetwork) GetIpStart() string {
   102  	pref, _ := netutils.NewIPV4Prefix(self.CIDR)
   103  	startIp := pref.Address.NetAddr(pref.MaskLen) // 0
   104  	startIp = startIp.StepUp()                    // 1
   105  	startIp = startIp.StepUp()                    // 2
   106  	return startIp.String()
   107  }
   108  
   109  func (self *SNetwork) GetIpEnd() string {
   110  	pref, _ := netutils.NewIPV4Prefix(self.CIDR)
   111  	endIp := pref.Address.BroadcastAddr(pref.MaskLen) // 255
   112  	endIp = endIp.StepDown()                          // 254
   113  	endIp = endIp.StepDown()                          // 253
   114  	endIp = endIp.StepDown()                          // 252
   115  	return endIp.String()
   116  }
   117  
   118  func (self *SNetwork) GetIpMask() int8 {
   119  	pref, _ := netutils.NewIPV4Prefix(self.CIDR)
   120  	return pref.MaskLen
   121  }
   122  
   123  func (self *SNetwork) GetGateway() string {
   124  	pref, _ := netutils.NewIPV4Prefix(self.CIDR)
   125  	startIp := pref.Address.NetAddr(pref.MaskLen) // 0
   126  	startIp = startIp.StepUp()                    // 1
   127  	return startIp.String()
   128  }
   129  
   130  func (self *SNetwork) GetServerType() string {
   131  	return api.NETWORK_TYPE_GUEST
   132  }
   133  
   134  func (self *SNetwork) GetIsPublic() bool {
   135  	return true
   136  }
   137  
   138  func (self *SNetwork) GetPublicScope() rbacutils.TRbacScope {
   139  	return rbacutils.ScopeDomain
   140  }
   141  
   142  func (self *SNetwork) Delete() error {
   143  	return self.wire.region.deleteNetwork(self.VpcID, self.GetId())
   144  }
   145  
   146  func (self *SNetwork) GetAllocTimeoutSeconds() int {
   147  	return 120 // 2 minutes
   148  }
   149  
   150  func (self *SRegion) getNetwork(networkId string) (*SNetwork, error) {
   151  	network := SNetwork{}
   152  	err := DoGet(self.ecsClient.Subnets.Get, networkId, nil, &network)
   153  	return &network, err
   154  }
   155  
   156  // https://support.huaweicloud.com/api-vpc/zh-cn_topic_0020090592.html
   157  func (self *SRegion) GetNetwroks(vpcId string) ([]SNetwork, error) {
   158  	querys := map[string]string{}
   159  	if len(vpcId) > 0 {
   160  		querys["vpc_id"] = vpcId
   161  	}
   162  
   163  	networks := make([]SNetwork, 0)
   164  	err := doListAllWithMarker(self.ecsClient.Subnets.List, querys, &networks)
   165  	return networks, err
   166  }
   167  
   168  func (self *SRegion) deleteNetwork(vpcId string, networkId string) error {
   169  	ctx := &modules.SManagerContext{InstanceId: vpcId, InstanceManager: self.ecsClient.Vpcs}
   170  	return DoDeleteWithSpec(self.ecsClient.Subnets.DeleteInContextWithSpec, ctx, networkId, "", nil, nil)
   171  }
   172  
   173  func (self *SNetwork) GetProjectId() string {
   174  	return self.wire.vpc.EnterpriseProjectID
   175  }