yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/zstack/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 zstack
    16  
    17  import (
    18  	"fmt"
    19  	"net/url"
    20  	"strings"
    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  )
    26  
    27  type SWire struct {
    28  	multicloud.SResourceBase
    29  	ZStackTags
    30  	vpc *SVpc
    31  
    32  	inetworks []cloudprovider.ICloudNetwork
    33  
    34  	ZStackBasic
    35  	Vlan              int    `json:"vlan"`
    36  	ZoneUUID          string `json:"zoneUuid"`
    37  	PhysicalInterface string `json:"physicalInterface"`
    38  	Type              string `json:"type"`
    39  	ZStackTime
    40  	AttachedClusterUUIDs []string `json:"attachedClusterUuids"`
    41  }
    42  
    43  func (region *SRegion) GetWire(wireId string) (*SWire, error) {
    44  	wire := &SWire{vpc: region.GetVpc()}
    45  	return wire, region.client.getResource("l2-networks", wireId, wire)
    46  }
    47  
    48  func (region *SRegion) GetWires(zoneId string, wireId string, clusterId string) ([]SWire, error) {
    49  	wires := []SWire{}
    50  	clusterIds, err := region.GetClusterIds()
    51  	if err != nil {
    52  		return nil, err
    53  	}
    54  	params := url.Values{}
    55  	params.Add("q", "attachedClusterUuids?="+strings.Join(clusterIds, ","))
    56  	if len(clusterId) > 0 {
    57  		params.Set("q", "attachedClusterUuids?="+clusterId)
    58  	}
    59  	if len(zoneId) > 0 {
    60  		params.Add("q", "zone.uuid="+zoneId)
    61  	}
    62  	if len(wireId) > 0 {
    63  		params.Add("q", "uuid="+wireId)
    64  	}
    65  	err = region.client.listAll("l2-networks", params, &wires)
    66  	if err != nil {
    67  		return nil, err
    68  	}
    69  	for i := 0; i < len(wires); i++ {
    70  		wires[i].vpc = region.GetVpc()
    71  	}
    72  	return wires, nil
    73  }
    74  
    75  func (wire *SWire) GetId() string {
    76  	return wire.UUID
    77  }
    78  
    79  func (wire *SWire) GetName() string {
    80  	return wire.Name
    81  }
    82  
    83  func (wire *SWire) IsEmulated() bool {
    84  	return false
    85  }
    86  
    87  func (wire *SWire) GetStatus() string {
    88  	return api.WIRE_STATUS_AVAILABLE
    89  }
    90  
    91  func (wire *SWire) Refresh() error {
    92  	return nil
    93  }
    94  
    95  func (wire *SWire) GetGlobalId() string {
    96  	return wire.UUID
    97  }
    98  
    99  func (wire *SWire) GetIVpc() cloudprovider.ICloudVpc {
   100  	return nil
   101  }
   102  
   103  func (wire *SWire) GetIZone() cloudprovider.ICloudZone {
   104  	zone, _ := wire.vpc.region.GetZone(wire.ZoneUUID)
   105  	return zone
   106  }
   107  
   108  func (wire *SWire) GetINetworks() ([]cloudprovider.ICloudNetwork, error) {
   109  	if wire.inetworks == nil || len(wire.inetworks) == 0 {
   110  		networks, err := wire.vpc.region.GetNetworks(wire.ZoneUUID, wire.UUID, "", "")
   111  		if err != nil {
   112  			return nil, err
   113  		}
   114  		wire.inetworks = []cloudprovider.ICloudNetwork{}
   115  		for i := 0; i < len(networks); i++ {
   116  			networks[i].wire = wire
   117  			wire.inetworks = append(wire.inetworks, &networks[i])
   118  		}
   119  	}
   120  	return wire.inetworks, nil
   121  }
   122  
   123  func (wire *SWire) GetBandwidth() int {
   124  	return 10000
   125  }
   126  
   127  func (wire *SWire) CreateINetwork(opts *cloudprovider.SNetworkCreateOptions) (cloudprovider.ICloudNetwork, error) {
   128  	network, err := wire.vpc.region.CreateNetwork(opts.Name, opts.Cidr, wire.UUID, opts.Desc)
   129  	if err != nil {
   130  		return nil, err
   131  	}
   132  	network.wire = wire
   133  	return network, nil
   134  }
   135  
   136  func (wire *SWire) GetINetworkById(netid string) (cloudprovider.ICloudNetwork, error) {
   137  	idInfo := strings.Split(netid, "/")
   138  	if len(idInfo) == 2 {
   139  		network, err := wire.vpc.region.GetNetwork(wire.ZoneUUID, wire.UUID, idInfo[0], idInfo[1])
   140  		if err != nil {
   141  			return nil, err
   142  		}
   143  		network.wire = wire
   144  		return network, nil
   145  	}
   146  	return nil, fmt.Errorf("invalid netid %s", netid)
   147  }