yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/ecloud/wire.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  	"fmt"
    19  
    20  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    21  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    22  	"yunion.io/x/cloudmux/pkg/multicloud"
    23  )
    24  
    25  type SWire struct {
    26  	multicloud.SResourceBase
    27  	EcloudTags
    28  	vpc       *SVpc
    29  	zone      *SZone
    30  	inetworks []cloudprovider.ICloudNetwork
    31  }
    32  
    33  func (w *SWire) GetId() string {
    34  	return fmt.Sprintf("%s-%s", w.vpc.GetId(), w.vpc.region.GetId())
    35  }
    36  
    37  func (w *SWire) GetName() string {
    38  	return w.GetId()
    39  }
    40  
    41  func (w *SWire) GetGlobalId() string {
    42  	return fmt.Sprintf("%s-%s", w.vpc.GetGlobalId(), w.vpc.region.GetGlobalId())
    43  }
    44  
    45  func (w *SWire) GetStatus() string {
    46  	return api.WIRE_STATUS_AVAILABLE
    47  }
    48  
    49  func (w *SWire) Refresh() error {
    50  	return nil
    51  	// TODO? w.fetchNetworks()
    52  }
    53  
    54  func (w *SWire) IsEmulated() bool {
    55  	return true
    56  }
    57  
    58  func (w *SWire) GetIVpc() cloudprovider.ICloudVpc {
    59  	return w.vpc
    60  }
    61  
    62  func (w *SWire) GetIZone() cloudprovider.ICloudZone {
    63  	return w.zone
    64  }
    65  
    66  func (w *SWire) GetINetworks() ([]cloudprovider.ICloudNetwork, error) {
    67  	if w.inetworks == nil {
    68  		err := w.fetchNetworks()
    69  		if err != nil {
    70  			return nil, err
    71  		}
    72  	}
    73  	return w.inetworks, nil
    74  }
    75  
    76  func (w *SWire) GetBandwidth() int {
    77  	return 10000
    78  }
    79  
    80  func (w *SWire) GetINetworkById(netid string) (cloudprovider.ICloudNetwork, error) {
    81  	n, err := w.vpc.region.GetNetworkById(w.vpc.RouterId, w.zone.Region, netid)
    82  	if err != nil {
    83  		return nil, err
    84  	}
    85  	n.wire = w
    86  	return n, nil
    87  }
    88  
    89  func (w *SWire) CreateINetwork(opts *cloudprovider.SNetworkCreateOptions) (cloudprovider.ICloudNetwork, error) {
    90  	return nil, nil
    91  }
    92  
    93  func (w *SWire) CreateNetworks() (*SNetwork, error) {
    94  	// data := jsonutils.NewDict()
    95  	// req := jsonutils.NewDict()
    96  	// req.Set("availabilityZoneHints", jsonutils.NewString("RegionOne"))
    97  	// req.Set("networkName", jsonutils.NewString("zyone"))
    98  	// req.Set("networkTypeEnum", jsonutils.NewString("VM"))
    99  	// req.Set("region", jsonutils.NewString("RegionOne"))
   100  	// req.Set("routerId", jsonutils.NewString(w.vpc.RouterId))
   101  	// subnet := jsonutils.NewDict()
   102  	// subnet.Set("cidr", jsonutils.NewString("192.168.46.0/24"))
   103  	// subnet.Set("ipVersion", jsonutils.NewString("4"))
   104  	// subnet.Set("subnetName", jsonutils.NewString("zyone"))
   105  	// req.Set("subnets", jsonutils.NewArray(subnet))
   106  	// data.Set("networkCreateReq", req)
   107  	// fmt.Printf("data:\n%s", data.PrettyString())
   108  	// request := NewConsoleRequest(w.vpc.region.ID, "/api/v2/netcenter/network", nil, req)
   109  	// request.SetMethod("POST")
   110  	// resp, err := w.vpc.region.client.request(context.Background(), request)
   111  	// if err != nil {
   112  	// 	return nil, err
   113  	// }
   114  	// fmt.Printf("resp:%s", resp)
   115  	// return nil, nil
   116  	return nil, cloudprovider.ErrNotImplemented
   117  }
   118  
   119  func (w *SWire) GetNetworkById(netId string) (*SNetwork, error) {
   120  	n, err := w.vpc.region.GetNetworkById(w.vpc.RouterId, w.zone.Region, netId)
   121  	if err != nil {
   122  		return nil, err
   123  	}
   124  	n.wire = w
   125  	return n, nil
   126  }
   127  
   128  func (w *SWire) fetchNetworks() error {
   129  	networks, err := w.vpc.region.GetNetworks(w.vpc.RouterId, w.zone.Region)
   130  	if err != nil {
   131  		return err
   132  	}
   133  	inetworks := make([]cloudprovider.ICloudNetwork, len(networks))
   134  	for i := range networks {
   135  		networks[i].wire = w
   136  		inetworks[i] = &networks[i]
   137  	}
   138  	w.inetworks = inetworks
   139  	return nil
   140  }