yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/azure/classic_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 azure
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  
    21  	"yunion.io/x/log"
    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 SClassicWire struct {
    29  	multicloud.SResourceBase
    30  	AzureTags
    31  
    32  	zone      *SZone
    33  	vpc       *SClassicVpc
    34  	inetworks []cloudprovider.ICloudNetwork
    35  }
    36  
    37  func (self *SClassicWire) GetId() string {
    38  	return fmt.Sprintf("%s/%s/%s-classic", self.zone.region.GetGlobalId(), self.zone.region.client.subscriptionId, self.vpc.GetName())
    39  }
    40  
    41  func (self *SClassicWire) GetGlobalId() string {
    42  	return strings.ToLower(self.GetId())
    43  }
    44  
    45  func (self *SClassicWire) GetName() string {
    46  	return fmt.Sprintf("%s-%s-classic", self.zone.region.client.cpcfg.Name, self.vpc.GetName())
    47  }
    48  
    49  func (self *SClassicWire) IsEmulated() bool {
    50  	return true
    51  }
    52  
    53  func (self *SClassicWire) GetStatus() string {
    54  	return api.WIRE_STATUS_AVAILABLE
    55  }
    56  
    57  func (self *SClassicWire) CreateINetwork(opts *cloudprovider.SNetworkCreateOptions) (cloudprovider.ICloudNetwork, error) {
    58  	return nil, cloudprovider.ErrNotImplemented
    59  }
    60  
    61  func (self *SClassicWire) GetBandwidth() int {
    62  	return 10000
    63  }
    64  
    65  func (self *SClassicWire) GetINetworkById(netid string) (cloudprovider.ICloudNetwork, error) {
    66  	networks, err := self.GetINetworks()
    67  	if err != nil {
    68  		return nil, err
    69  	}
    70  	for i := 0; i < len(networks); i += 1 {
    71  		if networks[i].GetGlobalId() == strings.ToLower(netid) {
    72  			return networks[i], nil
    73  		}
    74  	}
    75  	return nil, cloudprovider.ErrNotFound
    76  }
    77  
    78  func (self *SClassicWire) GetINetworks() ([]cloudprovider.ICloudNetwork, error) {
    79  	networks := self.vpc.GetNetworks()
    80  	ret := []cloudprovider.ICloudNetwork{}
    81  	for i := range networks {
    82  		networks[i].wire = self
    83  		ret = append(ret, &networks[i])
    84  	}
    85  	return ret, nil
    86  }
    87  
    88  func (self *SClassicWire) GetIVpc() cloudprovider.ICloudVpc {
    89  	return self.vpc
    90  }
    91  
    92  func (self *SClassicWire) GetIZone() cloudprovider.ICloudZone {
    93  	return self.zone
    94  }
    95  
    96  func (self *SClassicWire) getNetworkById(networkId string) *SClassicNetwork {
    97  	networks, err := self.GetINetworks()
    98  	if err != nil {
    99  		log.Errorf("getNetworkById error: %v", err)
   100  		return nil
   101  	}
   102  	log.Debugf("search for networks %d networkId: %s", len(networks), networkId)
   103  	for i := 0; i < len(networks); i++ {
   104  		network := networks[i].(*SClassicNetwork)
   105  		if network.GetId() == strings.ToLower(networkId) {
   106  			return network
   107  		}
   108  	}
   109  	return nil
   110  }