yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/ecloud/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 ecloud
    16  
    17  import (
    18  	"context"
    19  
    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/onecloud/pkg/util/rbacutils"
    26  )
    27  
    28  type SNetwork struct {
    29  	multicloud.SResourceBase
    30  	EcloudTags
    31  	SZoneRegionBase
    32  
    33  	wire *SWire
    34  
    35  	Id       string
    36  	Name     string
    37  	Shared   bool
    38  	Enabled  bool
    39  	EcStatus string
    40  	Subnets  []SSubnet
    41  }
    42  
    43  type SSubnet struct {
    44  	Id         string
    45  	Name       string
    46  	NetworkId  string
    47  	Region     string
    48  	GatewayIp  string
    49  	EnableDHCP bool
    50  	Cidr       string
    51  	IpVersion  string
    52  }
    53  
    54  func (n *SNetwork) GetId() string {
    55  	return n.Id
    56  }
    57  
    58  func (n *SNetwork) GetName() string {
    59  	return n.Name
    60  }
    61  
    62  func (n *SNetwork) GetGlobalId() string {
    63  	return n.Id
    64  }
    65  
    66  func (n *SNetwork) GetStatus() string {
    67  	switch n.EcStatus {
    68  	case "ACTIVE":
    69  		return api.NETWORK_STATUS_AVAILABLE
    70  	case "DOWN", "BUILD", "ERROR":
    71  		return api.NETWORK_STATUS_UNAVAILABLE
    72  	case "PENDING_DELETE":
    73  		return api.NETWORK_STATUS_DELETING
    74  	case "PENDING_CREATE", "PENDING_UPDATE":
    75  		return api.NETWORK_STATUS_PENDING
    76  	default:
    77  		return api.NETWORK_STATUS_UNKNOWN
    78  	}
    79  }
    80  
    81  func (n *SNetwork) Refresh() error {
    82  	return nil
    83  	// nn, err := n.wire.vpc.region.GetNetworkById(n.wire.vpc.RouterId, n.wire.zone.Region, n.GetId())
    84  	// if err != nil {
    85  	// 	return err
    86  	// }
    87  	// return jsonutils.Update(n, nn)
    88  }
    89  
    90  func (n *SNetwork) IsEmulated() bool {
    91  	return false
    92  }
    93  
    94  func (n *SNetwork) GetProjectId() string {
    95  	return ""
    96  }
    97  
    98  func (n *SNetwork) GetIWire() cloudprovider.ICloudWire {
    99  	return n.wire
   100  }
   101  
   102  func (n *SNetwork) GetIpStart() string {
   103  	pref, _ := netutils.NewIPV4Prefix(n.Cidr())
   104  	startIp := pref.Address.NetAddr(pref.MaskLen) // 0
   105  	startIp = startIp.StepUp()                    // 1
   106  	startIp = startIp.StepUp()                    // 2
   107  	return startIp.String()
   108  }
   109  
   110  func (n *SNetwork) GetIpEnd() string {
   111  	pref, _ := netutils.NewIPV4Prefix(n.Cidr())
   112  	endIp := pref.Address.BroadcastAddr(pref.MaskLen) // 255
   113  	endIp = endIp.StepDown()                          // 254
   114  	return endIp.String()
   115  }
   116  
   117  func (n *SNetwork) Cidr() string {
   118  	return n.Subnets[0].Cidr
   119  }
   120  
   121  func (n *SNetwork) GetIpMask() int8 {
   122  	pref, _ := netutils.NewIPV4Prefix(n.Cidr())
   123  	return pref.MaskLen
   124  }
   125  
   126  func (n *SNetwork) GetGateway() string {
   127  	return n.Subnets[0].GatewayIp
   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 cloudprovider.ErrNotImplemented
   144  }
   145  
   146  func (self *SNetwork) GetAllocTimeoutSeconds() int {
   147  	return 120
   148  }
   149  
   150  func (r *SRegion) GetNetworks(routerId, zoneRegion string) ([]SNetwork, error) {
   151  	queryParams := map[string]string{
   152  		"networkTypeEnum": "VM",
   153  	}
   154  	if len(routerId) > 0 {
   155  		queryParams["routerId"] = routerId
   156  	}
   157  	if len(zoneRegion) > 0 {
   158  		queryParams["region"] = zoneRegion
   159  	}
   160  	request := NewConsoleRequest(r.ID, "/api/v2/netcenter/network/NetworkResps", queryParams, nil)
   161  	networks := make([]SNetwork, 0)
   162  	err := r.client.doList(context.Background(), request, &networks)
   163  	if err != nil {
   164  		return nil, err
   165  	}
   166  	return networks, nil
   167  }
   168  
   169  func (r *SRegion) GetNetworkById(routerId, zoneRegion, netId string) (*SNetwork, error) {
   170  	queryParams := map[string]string{
   171  		"rangeInNetworkIds": netId,
   172  		"networkTypeEnum":   "VM",
   173  	}
   174  	if len(routerId) > 0 {
   175  		queryParams["routerId"] = routerId
   176  	}
   177  	if len(zoneRegion) > 0 {
   178  		queryParams["region"] = zoneRegion
   179  	}
   180  	request := NewConsoleRequest(r.ID, "/api/v2/netcenter/network/NetworkResps", queryParams, nil)
   181  	networks := make([]SNetwork, 0, 1)
   182  	err := r.client.doList(context.Background(), request, &networks)
   183  	if err != nil {
   184  		return nil, err
   185  	}
   186  	if len(networks) == 0 {
   187  		return nil, cloudprovider.ErrNotFound
   188  	}
   189  	return &networks[0], nil
   190  }