yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/huawei/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 huawei
    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/huawei/client/modules"
    26  	"yunion.io/x/onecloud/pkg/util/rbacutils"
    27  )
    28  
    29  /*
    30  Subnets
    31  */
    32  
    33  // https://support.huaweicloud.com/api-vpc/zh-cn_topic_0020090590.html
    34  type SNetwork struct {
    35  	multicloud.SResourceBase
    36  	HuaweiTags
    37  	wire *SWire
    38  
    39  	AvailabilityZone string   `json:"availability_zone"`
    40  	CIDR             string   `json:"cidr"`
    41  	DHCPEnable       bool     `json:"dhcp_enable"`
    42  	DNSList          []string `json:"dnsList"`
    43  	GatewayIP        string   `json:"gateway_ip"`
    44  	ID               string   `json:"id"`
    45  	Ipv6Enable       bool     `json:"ipv6_enable"`
    46  	Name             string   `json:"name"`
    47  	NeutronNetworkID string   `json:"neutron_network_id"`
    48  	NeutronSubnetID  string   `json:"neutron_subnet_id"`
    49  	PrimaryDNS       string   `json:"primary_dns"`
    50  	SecondaryDNS     string   `json:"secondary_dns"`
    51  	Status           string   `json:"status"`
    52  	VpcID            string   `json:"vpc_id"`
    53  }
    54  
    55  func (self *SNetwork) GetId() string {
    56  	return self.ID
    57  }
    58  
    59  func (self *SNetwork) GetName() string {
    60  	if len(self.Name) == 0 {
    61  		return self.ID
    62  	}
    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  	log.Debugf("network refresh %s", self.GetId())
    85  	new, err := self.wire.region.getNetwork(self.GetId())
    86  	if err != nil {
    87  		return err
    88  	}
    89  	return jsonutils.Update(self, new)
    90  }
    91  
    92  func (self *SNetwork) IsEmulated() bool {
    93  	return false
    94  }
    95  
    96  func (self *SNetwork) GetIWire() cloudprovider.ICloudWire {
    97  	return self.wire
    98  }
    99  
   100  func (self *SNetwork) GetIpStart() string {
   101  	pref, _ := netutils.NewIPV4Prefix(self.CIDR)
   102  	startIp := pref.Address.NetAddr(pref.MaskLen) // 0
   103  	startIp = startIp.StepUp()                    // 1
   104  	startIp = startIp.StepUp()                    // 2
   105  	return startIp.String()
   106  }
   107  
   108  func (self *SNetwork) GetIpEnd() string {
   109  	pref, _ := netutils.NewIPV4Prefix(self.CIDR)
   110  	endIp := pref.Address.BroadcastAddr(pref.MaskLen) // 255
   111  	endIp = endIp.StepDown()                          // 254
   112  	endIp = endIp.StepDown()                          // 253
   113  	endIp = endIp.StepDown()                          // 252
   114  	return endIp.String()
   115  }
   116  
   117  func (self *SNetwork) GetIpMask() int8 {
   118  	pref, _ := netutils.NewIPV4Prefix(self.CIDR)
   119  	return pref.MaskLen
   120  }
   121  
   122  func (self *SNetwork) GetGateway() string {
   123  	pref, _ := netutils.NewIPV4Prefix(self.CIDR)
   124  	startIp := pref.Address.NetAddr(pref.MaskLen) // 0
   125  	startIp = startIp.StepUp()                    // 1
   126  	return startIp.String()
   127  }
   128  
   129  func (self *SNetwork) GetServerType() string {
   130  	return api.NETWORK_TYPE_GUEST
   131  }
   132  
   133  func (self *SNetwork) GetIsPublic() bool {
   134  	return true
   135  }
   136  
   137  func (self *SNetwork) GetPublicScope() rbacutils.TRbacScope {
   138  	return rbacutils.ScopeDomain
   139  }
   140  
   141  func (self *SNetwork) Delete() error {
   142  	return self.wire.region.deleteNetwork(self.VpcID, self.GetId())
   143  }
   144  
   145  func (self *SNetwork) GetAllocTimeoutSeconds() int {
   146  	return 120 // 2 minutes
   147  }
   148  
   149  func (self *SRegion) getNetwork(networkId string) (*SNetwork, error) {
   150  	network := SNetwork{}
   151  	err := DoGet(self.ecsClient.Subnets.Get, networkId, nil, &network)
   152  	return &network, err
   153  }
   154  
   155  // https://support.huaweicloud.com/api-vpc/zh-cn_topic_0020090592.html
   156  func (self *SRegion) GetNetwroks(vpcId string) ([]SNetwork, error) {
   157  	querys := map[string]string{}
   158  	if len(vpcId) > 0 {
   159  		querys["vpc_id"] = vpcId
   160  	}
   161  
   162  	networks := make([]SNetwork, 0)
   163  	err := doListAllWithMarker(self.ecsClient.Subnets.List, querys, &networks)
   164  	return networks, err
   165  }
   166  
   167  func (self *SRegion) deleteNetwork(vpcId string, networkId string) error {
   168  	ctx := &modules.SManagerContext{InstanceId: vpcId, InstanceManager: self.ecsClient.Vpcs}
   169  	return DoDeleteWithSpec(self.ecsClient.Subnets.DeleteInContextWithSpec, ctx, networkId, "", nil, nil)
   170  }
   171  
   172  func (self *SNetwork) GetProjectId() string {
   173  	return self.wire.vpc.EnterpriseProjectID
   174  }