yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/aws/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 aws
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"yunion.io/x/log"
    21  	"yunion.io/x/pkg/errors"
    22  
    23  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    24  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    25  	"yunion.io/x/cloudmux/pkg/multicloud"
    26  )
    27  
    28  type SWire struct {
    29  	multicloud.SResourceBase
    30  	AwsTags
    31  	zone *SZone
    32  	vpc  *SVpc
    33  
    34  	inetworks []cloudprovider.ICloudNetwork
    35  }
    36  
    37  func (self *SWire) GetId() string {
    38  	return fmt.Sprintf("%s-%s", self.vpc.GetId(), self.zone.GetId())
    39  }
    40  
    41  func (self *SWire) GetName() string {
    42  	return self.GetId()
    43  }
    44  
    45  func (self *SWire) GetGlobalId() string {
    46  	return fmt.Sprintf("%s-%s", self.vpc.GetGlobalId(), self.zone.GetGlobalId())
    47  }
    48  
    49  func (self *SWire) GetStatus() string {
    50  	return api.WIRE_STATUS_AVAILABLE
    51  }
    52  
    53  func (self *SWire) Refresh() error {
    54  	return nil
    55  }
    56  
    57  func (self *SWire) IsEmulated() bool {
    58  	return true
    59  }
    60  
    61  func (self *SWire) GetIVpc() cloudprovider.ICloudVpc {
    62  	return self.vpc
    63  }
    64  
    65  func (self *SWire) GetIZone() cloudprovider.ICloudZone {
    66  	return self.zone
    67  }
    68  
    69  func (self *SWire) GetINetworks() ([]cloudprovider.ICloudNetwork, error) {
    70  	if self.inetworks == nil {
    71  		err := self.vpc.fetchNetworks()
    72  		if err != nil {
    73  			return nil, errors.Wrap(err, "fetchNetworks")
    74  		}
    75  	}
    76  	return self.inetworks, nil
    77  }
    78  
    79  func (self *SWire) GetBandwidth() int {
    80  	return 10000
    81  }
    82  
    83  func (self *SWire) GetINetworkById(netid string) (cloudprovider.ICloudNetwork, error) {
    84  	networks, err := self.GetINetworks()
    85  	if err != nil {
    86  		return nil, errors.Wrap(err, "GetINetworks")
    87  	}
    88  	for i := 0; i < len(networks); i += 1 {
    89  		if networks[i].GetGlobalId() == netid {
    90  			return networks[i], nil
    91  		}
    92  	}
    93  	return nil, errors.Wrap(cloudprovider.ErrNotFound, "GetINetworkById")
    94  }
    95  
    96  func (self *SWire) CreateINetwork(opts *cloudprovider.SNetworkCreateOptions) (cloudprovider.ICloudNetwork, error) {
    97  	networkId, err := self.zone.region.createNetwork(self.zone.ZoneId, self.vpc.VpcId, opts.Name, opts.Cidr, opts.Desc)
    98  	if err != nil {
    99  		log.Errorf("createNetwork error %s", err)
   100  		return nil, errors.Wrap(err, "createNetwork")
   101  	}
   102  	self.inetworks = nil
   103  	network := self.getNetworkById(networkId)
   104  	if network == nil {
   105  		log.Errorf("cannot find network after create????")
   106  		return nil, errors.Wrap(cloudprovider.ErrNotFound, "getNetworkById")
   107  	}
   108  	return network, nil
   109  }
   110  
   111  func (self *SWire) getNetworkById(networkId string) *SNetwork {
   112  	networks, err := self.GetINetworks()
   113  	if err != nil {
   114  		return nil
   115  	}
   116  	log.Debugf("search for networks %d", len(networks))
   117  	for i := 0; i < len(networks); i += 1 {
   118  		log.Debugf("search %s", networks[i].GetName())
   119  		network := networks[i]
   120  		if network.GetId() == networkId {
   121  			return network.(*SNetwork)
   122  		}
   123  	}
   124  	return nil
   125  }
   126  
   127  func (self *SWire) addNetwork(network *SNetwork) {
   128  	if self.inetworks == nil {
   129  		self.inetworks = make([]cloudprovider.ICloudNetwork, 0)
   130  	}
   131  	find := false
   132  	for i := 0; i < len(self.inetworks); i += 1 {
   133  		if self.inetworks[i].GetId() == network.NetworkId {
   134  			find = true
   135  			break
   136  		}
   137  	}
   138  	if !find {
   139  		self.inetworks = append(self.inetworks, network)
   140  	}
   141  }