yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/openstack/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 openstack
    16  
    17  import (
    18  	"yunion.io/x/pkg/errors"
    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  	OpenStackTags
    28  
    29  	vpc *SVpc
    30  }
    31  
    32  func (wire *SWire) GetId() string {
    33  	return wire.vpc.GetId()
    34  }
    35  
    36  func (wire *SWire) GetName() string {
    37  	return wire.GetId()
    38  }
    39  
    40  func (wire *SWire) IsEmulated() bool {
    41  	return true
    42  }
    43  
    44  func (wire *SWire) GetStatus() string {
    45  	return api.WIRE_STATUS_AVAILABLE
    46  }
    47  
    48  func (wire *SWire) Refresh() error {
    49  	return nil
    50  }
    51  
    52  func (wire *SWire) GetGlobalId() string {
    53  	return wire.vpc.GetGlobalId()
    54  }
    55  
    56  func (wire *SWire) GetIVpc() cloudprovider.ICloudVpc {
    57  	return wire.vpc
    58  }
    59  
    60  func (wire *SWire) GetIZone() cloudprovider.ICloudZone {
    61  	return nil
    62  }
    63  
    64  func (wire *SWire) GetBandwidth() int {
    65  	return 10000
    66  }
    67  
    68  func (wire *SWire) CreateINetwork(opts *cloudprovider.SNetworkCreateOptions) (cloudprovider.ICloudNetwork, error) {
    69  	network, err := wire.vpc.region.CreateNetwork(wire.vpc.Id, opts.ProjectId, opts.Name, opts.Cidr, opts.Desc)
    70  	if err != nil {
    71  		return nil, errors.Wrap(err, "CreateNetwork")
    72  	}
    73  	network.wire = wire
    74  	return network, nil
    75  }
    76  
    77  func (wire *SWire) GetINetworkById(netid string) (cloudprovider.ICloudNetwork, error) {
    78  	networks, err := wire.GetINetworks()
    79  	if err != nil {
    80  		return nil, err
    81  	}
    82  	for i := 0; i < len(networks); i++ {
    83  		if networks[i].GetGlobalId() == netid {
    84  			return networks[i], nil
    85  		}
    86  	}
    87  	return nil, errors.Wrapf(cloudprovider.ErrNotFound, netid)
    88  }
    89  
    90  func (wire *SWire) GetINetworks() ([]cloudprovider.ICloudNetwork, error) {
    91  	_networks, err := wire.vpc.region.GetNetworks(wire.vpc.Id)
    92  	if err != nil {
    93  		return nil, errors.Wrapf(err, "GetNetworks(%s)", wire.vpc.Id)
    94  	}
    95  	networks := []SNetwork{}
    96  	for i := range _networks {
    97  		pools := _networks[i].AllocationPools
    98  		for j := range pools {
    99  			if !pools[j].IsValid() {
   100  				continue
   101  			}
   102  			_networks[i].AllocationPools = []AllocationPool{pools[j]}
   103  			networks = append(networks, _networks[i])
   104  		}
   105  	}
   106  	inetworks := []cloudprovider.ICloudNetwork{}
   107  	for i := range networks {
   108  		networks[i].wire = wire
   109  		inetworks = append(inetworks, &networks[i])
   110  	}
   111  	return inetworks, nil
   112  }