yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/azure/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/jsonutils" 22 "yunion.io/x/pkg/errors" 23 24 api "yunion.io/x/cloudmux/pkg/apis/compute" 25 "yunion.io/x/cloudmux/pkg/cloudprovider" 26 "yunion.io/x/cloudmux/pkg/multicloud" 27 ) 28 29 type SWire struct { 30 multicloud.SResourceBase 31 AzureTags 32 33 zone *SZone 34 vpc *SVpc 35 inetworks []cloudprovider.ICloudNetwork 36 } 37 38 func (self *SWire) GetId() string { 39 return fmt.Sprintf("%s/%s/%s", self.zone.region.GetGlobalId(), self.zone.region.client.subscriptionId, self.vpc.GetName()) 40 } 41 42 func (self *SWire) GetGlobalId() string { 43 return strings.ToLower(self.GetId()) 44 } 45 46 func (self *SWire) GetName() string { 47 return fmt.Sprintf("%s-%s", self.zone.region.client.cpcfg.Name, self.vpc.GetName()) 48 } 49 50 func (self *SWire) IsEmulated() bool { 51 return true 52 } 53 54 func (self *SWire) GetStatus() string { 55 return api.WIRE_STATUS_AVAILABLE 56 } 57 58 func (self *SRegion) CreateNetwork(vpcId, name string, cidr string, desc string) (*SNetwork, error) { 59 params := map[string]interface{}{ 60 "Name": name, 61 "Properties": map[string]interface{}{ 62 "AddressPrefix": cidr, 63 }, 64 } 65 resource := fmt.Sprintf("%s/subnets/%s", vpcId, name) 66 network := &SNetwork{} 67 resp, err := self.put(resource, jsonutils.Marshal(params)) 68 if err != nil { 69 return nil, errors.Wrapf(err, "put(%s)", resource) 70 } 71 err = resp.Unmarshal(network) 72 if err != nil { 73 return nil, errors.Wrapf(err, "resp.Unmarshal") 74 } 75 return network, nil 76 } 77 78 func (self *SWire) CreateINetwork(opts *cloudprovider.SNetworkCreateOptions) (cloudprovider.ICloudNetwork, error) { 79 network, err := self.zone.region.CreateNetwork(self.vpc.ID, opts.Name, opts.Cidr, opts.Desc) 80 if err != nil { 81 return nil, err 82 } 83 network.wire = self 84 return network, nil 85 } 86 87 func (self *SWire) GetBandwidth() int { 88 return 10000 89 } 90 91 func (self *SWire) GetINetworkById(netid string) (cloudprovider.ICloudNetwork, error) { 92 networks, err := self.GetINetworks() 93 if err != nil { 94 return nil, err 95 } 96 for i := 0; i < len(networks); i += 1 { 97 if networks[i].GetGlobalId() == strings.ToLower(netid) { 98 return networks[i], nil 99 } 100 } 101 return nil, errors.Wrapf(cloudprovider.ErrNotFound, netid) 102 } 103 104 func (self *SWire) GetINetworks() ([]cloudprovider.ICloudNetwork, error) { 105 ret := []cloudprovider.ICloudNetwork{} 106 networks := self.vpc.GetNetworks() 107 for i := range networks { 108 networks[i].wire = self 109 ret = append(ret, &networks[i]) 110 } 111 return ret, nil 112 } 113 114 func (self *SWire) GetIVpc() cloudprovider.ICloudVpc { 115 return self.vpc 116 } 117 118 func (self *SWire) GetIZone() cloudprovider.ICloudZone { 119 return self.zone 120 }