yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/jdcloud/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 jdcloud
    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  	JdcloudTags
    28  
    29  	vpc       *SVpc
    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  }
    52  
    53  func (w *SWire) IsEmulated() bool {
    54  	return true
    55  }
    56  
    57  func (w *SWire) GetIVpc() cloudprovider.ICloudVpc {
    58  	return w.vpc
    59  }
    60  
    61  func (w *SWire) GetIZone() cloudprovider.ICloudZone {
    62  	return nil
    63  }
    64  
    65  func (w *SWire) GetINetworks() ([]cloudprovider.ICloudNetwork, error) {
    66  	if w.inetworks == nil {
    67  		nets, err := w.networks()
    68  		if err != nil {
    69  			return nil, err
    70  		}
    71  		w.inetworks = make([]cloudprovider.ICloudNetwork, 0, len(nets))
    72  		for i := range nets {
    73  			nets[i].wire = w
    74  			w.inetworks = append(w.inetworks, &nets[i])
    75  		}
    76  	}
    77  	return w.inetworks, nil
    78  }
    79  
    80  func (w *SWire) networks() ([]SNetwork, error) {
    81  	nets := make([]SNetwork, 0)
    82  	n := 1
    83  	for {
    84  		parts, total, err := w.vpc.region.GetNetworks(w.vpc.VpcId, n, 100)
    85  		if err != nil {
    86  			return nil, err
    87  		}
    88  		nets = append(nets, parts...)
    89  		if len(nets) >= total {
    90  			break
    91  		}
    92  		n++
    93  	}
    94  	return nets, nil
    95  }
    96  
    97  func (w *SWire) GetBandwidth() int {
    98  	return 10000
    99  }
   100  
   101  func (w *SWire) GetINetworkById(netid string) (cloudprovider.ICloudNetwork, error) {
   102  	networks, err := w.GetINetworks()
   103  	if err != nil {
   104  		return nil, err
   105  	}
   106  	for i := 0; i < len(networks); i += 1 {
   107  		if networks[i].GetGlobalId() == netid {
   108  			return networks[i], nil
   109  		}
   110  	}
   111  	return nil, cloudprovider.ErrNotFound
   112  }
   113  
   114  func (w *SWire) CreateINetwork(opts *cloudprovider.SNetworkCreateOptions) (cloudprovider.ICloudNetwork, error) {
   115  	return nil, cloudprovider.ErrNotImplemented
   116  }
   117  
   118  func (w *SWire) CreateNetworks() (*SNetwork, error) {
   119  	return nil, cloudprovider.ErrNotImplemented
   120  }