diff --git a/pkg/multicloud/qcloud/host.go b/pkg/multicloud/qcloud/host.go index 60e5f72f2..8c92e09a6 100644 --- a/pkg/multicloud/qcloud/host.go +++ b/pkg/multicloud/qcloud/host.go @@ -77,13 +77,7 @@ func (self *SHost) _createVM(name, hostname string, imgId string, sysDisk cloudp publicIpBw int, publicIpChargeType cloudprovider.TElasticipChargeType, tags map[string]string, osType string, ) (string, error) { - net := self.zone.getNetworkById(networkId) - if net == nil { - return "", fmt.Errorf("invalid network ID %s", networkId) - } - var err error - keypair := "" if len(publicKey) > 0 { keypair, err = self.zone.region.syncKeypair(publicKey) diff --git a/pkg/multicloud/qcloud/instance.go b/pkg/multicloud/qcloud/instance.go index 7379db0d0..25a9d1579 100644 --- a/pkg/multicloud/qcloud/instance.go +++ b/pkg/multicloud/qcloud/instance.go @@ -214,7 +214,7 @@ func (self *SInstance) GetInstanceType() string { } func (self *SInstance) getVpc() (*SVpc, error) { - return self.host.zone.region.getVpc(self.VirtualPrivateCloud.VpcId) + return self.host.zone.region.GetVpc(self.VirtualPrivateCloud.VpcId) } func (self *SInstance) GetIDisks() ([]cloudprovider.ICloudDisk, error) { diff --git a/pkg/multicloud/qcloud/qcloud.go b/pkg/multicloud/qcloud/qcloud.go index 03fc04013..7a8039abb 100644 --- a/pkg/multicloud/qcloud/qcloud.go +++ b/pkg/multicloud/qcloud/qcloud.go @@ -18,7 +18,7 @@ import ( "bytes" "context" "fmt" - "io/ioutil" + "io" "net/http" "net/url" "strconv" @@ -436,11 +436,11 @@ func (client *SQcloudClient) getSdkClient(regionId string) (*common.Client, erro httpClient := client.cpcfg.AdaptiveTimeoutHttpClient() ts, _ := httpClient.Transport.(*http.Transport) cli.WithHttpTransport(cloudprovider.GetCheckTransport(ts, func(req *http.Request) (func(resp *http.Response) error, error) { - body, err := ioutil.ReadAll(req.Body) + body, err := io.ReadAll(req.Body) if err != nil { return nil, errors.Wrapf(err, "ioutil.ReadAll") } - req.Body = ioutil.NopCloser(bytes.NewBuffer(body)) + req.Body = io.NopCloser(bytes.NewBuffer(body)) params, err := url.ParseQuery(string(body)) if err != nil { return nil, errors.Wrapf(err, "ParseQuery(%s)", string(body)) @@ -807,7 +807,7 @@ func (client *SQcloudClient) fetchBuckets() error { } name := bInfo.Name[:slashPos] region, err := client.getIRegionByRegionId(bInfo.Region) - var zone cloudprovider.ICloudZone + var zone *SZone = nil if err != nil { log.Errorf("fail to find region %s", bInfo.Region) // possibly a zone, try zone @@ -824,10 +824,12 @@ func (client *SQcloudClient) fetchBuckets() error { continue } zoneId := bInfo.Region - zone, _ = region.(*SRegion).getZoneById(bInfo.Region) - if zone != nil { - zoneId = zone.GetId() + zone, err = region.(*SRegion).getZoneById(bInfo.Region) + if err != nil { + log.Errorf("fail to find zone %s", zoneId) + continue } + zoneId = zone.GetId() log.Debugf("find zonal bucket %s", zoneId) } b := SBucket{ @@ -838,7 +840,7 @@ func (client *SQcloudClient) fetchBuckets() error { CreateDate: createAt, } if zone != nil { - b.zone = zone.(*SZone) + b.zone = zone } ret = append(ret, &b) } diff --git a/pkg/multicloud/qcloud/region.go b/pkg/multicloud/qcloud/region.go index 24af630cf..49b0618a8 100644 --- a/pkg/multicloud/qcloud/region.go +++ b/pkg/multicloud/qcloud/region.go @@ -38,9 +38,6 @@ type SRegion struct { client *SQcloudClient - izones []cloudprovider.ICloudZone - ivpcs []cloudprovider.ICloudVpc - storageCache *SStoragecache instanceTypes []SInstanceType @@ -110,11 +107,11 @@ func (self *SRegion) CreateIVpc(opts *cloudprovider.VpcCreateOptions) (cloudprov if err != nil { return nil, err } - err = self.fetchInfrastructure() + vpc, err := self.GetVpc(vpcId) if err != nil { return nil, err } - return self.GetIVpcById(vpcId) + return vpc, nil } func (self *SRegion) GetCosClient(bucket *SBucket) (*cos.Client, error) { @@ -286,27 +283,28 @@ func (self *SRegion) GetIVpcById(id string) (cloudprovider.ICloudVpc, error) { } func (self *SRegion) getZoneById(id string) (*SZone, error) { - izones, err := self.GetIZones() + zones, err := self.GetZones() if err != nil { return nil, err } - for i := 0; i < len(izones); i += 1 { - zone := izones[i].(*SZone) - if zone.Zone == id { - return zone, nil + for i := 0; i < len(zones); i += 1 { + if zones[i].Zone == id { + return &zones[i], nil } } - return nil, fmt.Errorf("no such zone %s", id) + return nil, errors.Wrapf(cloudprovider.ErrNotFound, id) } func (self *SRegion) GetIVpcs() ([]cloudprovider.ICloudVpc, error) { - if self.ivpcs == nil { - err := self.fetchInfrastructure() - if err != nil { - return nil, err - } + vpcs, err := self.GetVpcs(nil) + if err != nil { + return nil, errors.Wrapf(err, "GetVpcs") } - return self.ivpcs, nil + ret := []cloudprovider.ICloudVpc{} + for i := range vpcs { + ret = append(ret, &vpcs[i]) + } + return ret, nil } func (self *SRegion) GetIZoneById(id string) (cloudprovider.ICloudZone, error) { @@ -323,123 +321,85 @@ func (self *SRegion) GetIZoneById(id string) (cloudprovider.ICloudZone, error) { } func (self *SRegion) GetIZones() ([]cloudprovider.ICloudZone, error) { - if self.izones == nil { - var err error - err = self.fetchInfrastructure() - if err != nil { - return nil, err - } + zones, err := self.GetZones() + if err != nil { + return nil, err + } + ret := []cloudprovider.ICloudZone{} + for i := range zones { + ret = append(ret, &zones[i]) } - return self.izones, nil + return ret, nil } -func (self *SRegion) _fetchZones() error { - params := make(map[string]string) - zones := make([]SZone, 0) - body, err := self.cvmRequest("DescribeZones", params, true) +func (self *SRegion) GetZones() ([]SZone, error) { + body, err := self.cvmRequest("DescribeZones", map[string]string{}, true) if err != nil { - return err + return nil, err } + zones := make([]SZone, 0) err = body.Unmarshal(&zones, "ZoneSet") if err != nil { - return err + return nil, err } - self.izones = make([]cloudprovider.ICloudZone, len(zones)) for i := 0; i < len(zones); i++ { zones[i].region = self - self.izones[i] = &zones[i] } - return nil -} - -func (self *SRegion) fetchInfrastructure() error { - err := self._fetchZones() - if err != nil { - return err - } - err = self.fetchIVpcs() - if err != nil { - return err - } - for i := 0; i < len(self.ivpcs); i += 1 { - for j := 0; j < len(self.izones); j += 1 { - zone := self.izones[j].(*SZone) - vpc := self.ivpcs[i].(*SVpc) - wire := SWire{zone: zone, vpc: vpc} - zone.addWire(&wire) - vpc.addWire(&wire) - } - } - return nil + return zones, nil } func (self *SRegion) DeleteVpc(vpcId string) error { params := make(map[string]string) params["VpcId"] = vpcId - _, err := self.vpcRequest("DeleteVpc", params) return err } -func (self *SRegion) getVpc(vpcId string) (*SVpc, error) { - vpcs, total, err := self.GetVpcs([]string{vpcId}, 0, 1) +func (self *SRegion) GetVpc(vpcId string) (*SVpc, error) { + vpcs, err := self.GetVpcs([]string{vpcId}) if err != nil { return nil, err } - if total > 1 { - return nil, cloudprovider.ErrDuplicateId - } - if total == 0 { - return nil, cloudprovider.ErrNotFound + for i := range vpcs { + if vpcs[i].VpcId == vpcId { + vpcs[i].region = self + return &vpcs[i], nil + } } - vpcs[0].region = self - return &vpcs[0], nil + return nil, errors.Wrapf(err, "GetVpc(%s)", vpcId) } -func (self *SRegion) fetchIVpcs() error { - vpcs := make([]SVpc, 0) +func (self *SRegion) GetVpcs(vpcIds []string) ([]SVpc, error) { + params := map[string]string{ + "Limit": "100", + } + for index, vpcId := range vpcIds { + params[fmt.Sprintf("VpcIds.%d", index)] = vpcId + } + ret := []SVpc{} for { - part, total, err := self.GetVpcs(nil, len(vpcs), 50) + resp, err := self.vpcRequest("DescribeVpcs", params) if err != nil { - return err + return nil, err } - vpcs = append(vpcs, part...) - if len(vpcs) >= total { - break + part := struct { + VpcSet []SVpc + TotalCount float64 + }{} + err = resp.Unmarshal(&part) + if err != nil { + return nil, errors.Wrapf(err, "Unmarshal") } - } - self.ivpcs = make([]cloudprovider.ICloudVpc, len(vpcs)) - for i := 0; i < len(vpcs); i += 1 { - vpcs[i].region = self - self.ivpcs[i] = &vpcs[i] - } - return nil -} - -func (self *SRegion) GetVpcs(vpcIds []string, offset int, limit int) ([]SVpc, int, error) { - if limit > 50 || limit <= 0 { - limit = 50 - } - params := make(map[string]string) - params["Limit"] = fmt.Sprintf("%d", limit) - params["Offset"] = fmt.Sprintf("%d", offset) - if vpcIds != nil && len(vpcIds) > 0 { - for index, vpcId := range vpcIds { - params[fmt.Sprintf("VpcIds.%d", index)] = vpcId + for i := range part.VpcSet { + part.VpcSet[i].region = self + ret = append(ret, part.VpcSet[i]) } + if len(ret) >= int(part.TotalCount) || len(part.VpcSet) == 0 { + break + } + params["Offset"] = fmt.Sprintf("%d", len(ret)) } - body, err := self.vpcRequest("DescribeVpcs", params) - if err != nil { - return nil, 0, err - } - vpcs := make([]SVpc, 0) - err = body.Unmarshal(&vpcs, "VpcSet") - if err != nil { - log.Errorf("Unmarshal vpc fail %s", err) - return nil, 0, err - } - total, _ := body.Float("TotalCount") - return vpcs, int(total), nil + return ret, nil } func (self *SRegion) GetGeographicInfo() cloudprovider.SGeographicInfo { @@ -543,53 +503,58 @@ func (self *SRegion) memcachedRequest(apiName string, params map[string]string) return self.client.memcachedRequest(apiName, params) } -func (self *SRegion) GetNetworks(ids []string, vpcId string, offset int, limit int) ([]SNetwork, int, error) { - if limit > 50 || limit <= 0 { - limit = 50 +func (self *SRegion) GetNetworks(ids []string, vpcId, zone string) ([]SNetwork, error) { + params := map[string]string{ + "Limit": "100", } - params := make(map[string]string) - params["Limit"] = fmt.Sprintf("%d", limit) - params["Offset"] = fmt.Sprintf("%d", offset) - base := 0 - if ids != nil && len(ids) > 0 { - for index, networkId := range ids { - params[fmt.Sprintf("SubnetIds.%d", index)] = networkId - } - base += len(ids) + for index, networkId := range ids { + params[fmt.Sprintf("SubnetIds.%d", index)] = networkId } + base := 0 if len(vpcId) > 0 { - params["Filters.0.Name"] = "vpc-id" - params["Filters.0.Values.0"] = vpcId + params[fmt.Sprintf("Filters.%d.Name", base)] = "vpc-id" + params[fmt.Sprintf("Filters.%d.Values.0", base)] = vpcId + base++ } - - body, err := self.vpcRequest("DescribeSubnets", params) - if err != nil { - log.Errorf("DescribeSubnets fail %s", err) - return nil, 0, err + if len(zone) > 0 { + params[fmt.Sprintf("Filters.%d.Name", base)] = "zone" + params[fmt.Sprintf("Filters.%d.Values.0", base)] = zone + base++ } - - networks := make([]SNetwork, 0) - err = body.Unmarshal(&networks, "SubnetSet") - if err != nil { - log.Errorf("Unmarshal network fail %s", err) - return nil, 0, err + ret := []SNetwork{} + for { + resp, err := self.vpcRequest("DescribeSubnets", params) + if err != nil { + return nil, err + } + part := struct { + SubnetSet []SNetwork + TotalCount float64 + }{} + err = resp.Unmarshal(&part) + if err != nil { + return nil, err + } + ret = append(ret, part.SubnetSet...) + if len(ret) >= int(part.TotalCount) || len(part.SubnetSet) == 0 { + break + } + params["Offset"] = fmt.Sprintf("%d", len(ret)) } - total, _ := body.Float("TotalCount") - return networks, int(total), nil + return ret, nil } -func (self *SRegion) GetNetwork(networkId string) (*SNetwork, error) { - networks, total, err := self.GetNetworks([]string{networkId}, "", 0, 1) +func (self *SRegion) GetNetwork(id string) (*SNetwork, error) { + networks, err := self.GetNetworks([]string{id}, "", "") if err != nil { return nil, err } - if total > 1 { - return nil, cloudprovider.ErrDuplicateId - } - if total == 0 { - return nil, cloudprovider.ErrNotFound + for i := range networks { + if networks[i].SubnetId == id { + return &networks[i], nil + } } - return &networks[0], nil + return nil, errors.Wrapf(cloudprovider.ErrNotFound, id) } func (self *SRegion) getStoragecache() *SStoragecache { @@ -629,39 +594,43 @@ func (self *SRegion) GetMatchInstanceTypes(cpu int, memMB int, gpu int, zoneId s } func (self *SRegion) CreateInstanceSimple(name string, imgId string, cpu int, memGB int, storageType string, dataDiskSizesGB []int, networkId string, passwd string, publicKey string, secgroup string, tags map[string]string) (*SInstance, error) { - izones, err := self.GetIZones() + zones, err := self.GetZones() if err != nil { return nil, err } - for i := 0; i < len(izones); i += 1 { - z := izones[i].(*SZone) - log.Debugf("Search in zone %s", z.Zone) - net := z.getNetworkById(networkId) - if net != nil { - desc := &cloudprovider.SManagedVMCreateConfig{ - Name: name, - ExternalImageId: imgId, - SysDisk: cloudprovider.SDiskInfo{SizeGB: 0, StorageType: storageType}, - Cpu: cpu, - MemoryMB: memGB * 1024, - ExternalNetworkId: networkId, - Password: passwd, - DataDisks: []cloudprovider.SDiskInfo{}, - PublicKey: publicKey, - - Tags: tags, - - ExternalSecgroupIds: []string{secgroup}, - } - for _, sizeGB := range dataDiskSizesGB { - desc.DataDisks = append(desc.DataDisks, cloudprovider.SDiskInfo{SizeGB: sizeGB, StorageType: storageType}) - } - inst, err := z.getHost().CreateVM(desc) - if err != nil { - return nil, err - } - return inst.(*SInstance), nil + net, err := self.GetNetwork(networkId) + if err != nil { + return nil, err + } + for i := 0; i < len(zones); i += 1 { + zone := zones[i] + log.Debugf("Search in zone %s", zone.Zone) + if zone.ZoneName != net.Zone { + continue + } + desc := &cloudprovider.SManagedVMCreateConfig{ + Name: name, + ExternalImageId: imgId, + SysDisk: cloudprovider.SDiskInfo{SizeGB: 0, StorageType: storageType}, + Cpu: cpu, + MemoryMB: memGB * 1024, + ExternalNetworkId: networkId, + Password: passwd, + DataDisks: []cloudprovider.SDiskInfo{}, + PublicKey: publicKey, + + Tags: tags, + + ExternalSecgroupIds: []string{secgroup}, + } + for _, sizeGB := range dataDiskSizesGB { + desc.DataDisks = append(desc.DataDisks, cloudprovider.SDiskInfo{SizeGB: sizeGB, StorageType: storageType}) + } + inst, err := zone.getHost().CreateVM(desc) + if err != nil { + return nil, err } + return inst.(*SInstance), nil } return nil, fmt.Errorf("cannot find network %s", networkId) } diff --git a/pkg/multicloud/qcloud/shell/network.go b/pkg/multicloud/qcloud/shell/network.go index a417a9573..190696d62 100644 --- a/pkg/multicloud/qcloud/shell/network.go +++ b/pkg/multicloud/qcloud/shell/network.go @@ -24,15 +24,16 @@ import ( func init() { type NetworkListOptions struct { - Limit int `help:"page size"` - Offset int `help:"page offset"` + Ids []string + VpcId string + Zone string } shellutils.R(&NetworkListOptions{}, "network-list", "List networks", func(cli *qcloud.SRegion, args *NetworkListOptions) error { - networks, total, e := cli.GetNetworks(nil, "", args.Offset, args.Limit) - if e != nil { - return e + networks, err := cli.GetNetworks(args.Ids, args.VpcId, args.Zone) + if err != nil { + return err } - printList(networks, total, args.Offset, args.Limit, []string{}) + printList(networks, 0, 0, 0, []string{}) return nil }) diff --git a/pkg/multicloud/qcloud/shell/vpc.go b/pkg/multicloud/qcloud/shell/vpc.go index 492b93ebb..d2377089c 100644 --- a/pkg/multicloud/qcloud/shell/vpc.go +++ b/pkg/multicloud/qcloud/shell/vpc.go @@ -23,15 +23,14 @@ import ( func init() { type VpcListOptions struct { - Limit int `help:"page size"` - Offset int `help:"page offset"` + Ids []string } shellutils.R(&VpcListOptions{}, "vpc-list", "List vpcs", func(cli *qcloud.SRegion, args *VpcListOptions) error { - vpcs, total, err := cli.GetVpcs(nil, args.Offset, args.Limit) + vpcs, err := cli.GetVpcs(args.Ids) if err != nil { return err } - printList(vpcs, total, args.Offset, args.Limit, []string{}) + printList(vpcs, 0, 0, 0, []string{}) return nil }) diff --git a/pkg/multicloud/qcloud/vpc.go b/pkg/multicloud/qcloud/vpc.go index a78e4100b..1361f7b94 100644 --- a/pkg/multicloud/qcloud/vpc.go +++ b/pkg/multicloud/qcloud/vpc.go @@ -31,8 +31,6 @@ type SVpc struct { region *SRegion - iwires []cloudprovider.ICloudWire - CidrBlock string Ipv6CidrBlock string CreatedTime time.Time @@ -116,16 +114,6 @@ func (self *SVpc) GetIRouteTableById(routeTableId string) (cloudprovider.ICloudR return &routetables[0], nil } -func (self *SVpc) getWireByZoneId(zoneId string) *SWire { - for i := 0; i <= len(self.iwires); i++ { - wire := self.iwires[i].(*SWire) - if wire.zone.Zone == zoneId { - return wire - } - } - return nil -} - func (self *SVpc) GetINatGateways() ([]cloudprovider.ICloudNatGateway, error) { nats := []SNatGateway{} for { @@ -146,48 +134,30 @@ func (self *SVpc) GetINatGateways() ([]cloudprovider.ICloudNatGateway, error) { return inats, nil } -func (self *SVpc) fetchNetworks() error { - networks, total, err := self.region.GetNetworks(nil, self.VpcId, 0, 50) - if err != nil { - return err - } - if total > len(networks) { - networks, _, err = self.region.GetNetworks(nil, self.VpcId, 0, total) - if err != nil { - return err - } - } - for i := 0; i < len(networks); i += 1 { - wire := self.getWireByZoneId(networks[i].Zone) - networks[i].wire = wire - wire.addNetwork(&networks[i]) - } - return nil -} - func (self *SVpc) GetIWireById(wireId string) (cloudprovider.ICloudWire, error) { - if self.iwires == nil { - err := self.fetchNetworks() - if err != nil { - return nil, err - } + wires, err := self.GetIWires() + if err != nil { + return nil, err } - for i := 0; i < len(self.iwires); i += 1 { - if self.iwires[i].GetGlobalId() == wireId { - return self.iwires[i], nil + for i := 0; i < len(wires); i += 1 { + if wires[i].GetGlobalId() == wireId { + return wires[i], nil } } return nil, cloudprovider.ErrNotFound } func (self *SVpc) GetIWires() ([]cloudprovider.ICloudWire, error) { - if self.iwires == nil { - err := self.fetchNetworks() - if err != nil { - return nil, err - } + zones, err := self.region.GetZones() + if err != nil { + return nil, err + } + ret := []cloudprovider.ICloudWire{} + for i := range zones { + wire := &SWire{zone: &zones[i], vpc: self} + ret = append(ret, wire) } - return self.iwires, nil + return ret, nil } func (self *SVpc) GetRegion() cloudprovider.ICloudRegion { @@ -195,18 +165,11 @@ func (self *SVpc) GetRegion() cloudprovider.ICloudRegion { } func (self *SVpc) Refresh() error { - new, err := self.region.getVpc(self.VpcId) + vpc, err := self.region.GetVpc(self.VpcId) if err != nil { return err } - return jsonutils.Update(self, new) -} - -func (self *SVpc) addWire(wire *SWire) { - if self.iwires == nil { - self.iwires = make([]cloudprovider.ICloudWire, 0) - } - self.iwires = append(self.iwires, wire) + return jsonutils.Update(self, vpc) } func (self *SVpc) GetAuthorityOwnerId() string { diff --git a/pkg/multicloud/qcloud/wire.go b/pkg/multicloud/qcloud/wire.go index 8feee936d..504ea593f 100644 --- a/pkg/multicloud/qcloud/wire.go +++ b/pkg/multicloud/qcloud/wire.go @@ -17,7 +17,7 @@ package qcloud import ( "fmt" - "yunion.io/x/log" + "yunion.io/x/pkg/errors" api "yunion.io/x/cloudmux/pkg/apis/compute" "yunion.io/x/cloudmux/pkg/cloudprovider" @@ -29,8 +29,6 @@ type SWire struct { QcloudTags zone *SZone vpc *SVpc - - inetworks []cloudprovider.ICloudNetwork } func (self *SWire) GetId() string { @@ -69,88 +67,37 @@ func (self *SWire) GetBandwidth() int { return 10000 } -func (self *SWire) GetNetworkById(networkId string) *SNetwork { - networks, err := self.GetINetworks() - if err != nil { - return nil - } - log.Debugf("search for networks %d", len(networks)) - for i := 0; i < len(networks); i += 1 { - log.Debugf("search %s", networks[i].GetName()) - network := networks[i].(*SNetwork) - if network.SubnetId == networkId { - return network - } - } - return nil -} - func (self *SWire) CreateINetwork(opts *cloudprovider.SNetworkCreateOptions) (cloudprovider.ICloudNetwork, error) { networkId, err := self.zone.region.CreateNetwork(self.zone.Zone, self.vpc.VpcId, opts.Name, opts.Cidr, opts.Desc) if err != nil { - log.Errorf("CreateNetwork error %s", err) - return nil, err + return nil, errors.Wrapf(err, "CreateNetwork") } - self.inetworks = nil - network := self.GetNetworkById(networkId) - if network == nil { - log.Errorf("cannot find vswitch after create????") - return nil, cloudprovider.ErrNotFound + network, err := self.vpc.region.GetNetwork(networkId) + if err != nil { + return nil, errors.Wrapf(err, "GetNetwork") } + network.wire = self return network, nil } func (self *SWire) GetINetworkById(netid string) (cloudprovider.ICloudNetwork, error) { - networks, err := self.GetINetworks() + network, err := self.zone.region.GetNetwork(netid) if err != nil { return nil, err } - for i := 0; i < len(networks); i += 1 { - if networks[i].GetGlobalId() == netid { - return networks[i], nil - } - } - return nil, cloudprovider.ErrNotFound -} - -func (self *SWire) getNetworkById(networkId string) *SNetwork { - networks, err := self.GetINetworks() - if err != nil { - return nil - } - log.Debugf("search for networks %d", len(networks)) - for i := 0; i < len(networks); i += 1 { - log.Debugf("search %s", networks[i].GetName()) - network := networks[i].(*SNetwork) - if network.SubnetId == networkId { - return network - } - } - return nil + network.wire = self + return network, nil } func (self *SWire) GetINetworks() ([]cloudprovider.ICloudNetwork, error) { - if self.inetworks == nil { - err := self.vpc.fetchNetworks() - if err != nil { - return nil, err - } - } - return self.inetworks, nil -} - -func (self *SWire) addNetwork(network *SNetwork) { - if self.inetworks == nil { - self.inetworks = make([]cloudprovider.ICloudNetwork, 0) - } - find := false - for i := 0; i < len(self.inetworks); i += 1 { - if self.inetworks[i].GetId() == network.SubnetId { - find = true - break - } + networks, err := self.zone.region.GetNetworks(nil, self.vpc.VpcId, self.zone.Zone) + if err != nil { + return nil, err } - if !find { - self.inetworks = append(self.inetworks, network) + ret := []cloudprovider.ICloudNetwork{} + for i := range networks { + networks[i].wire = self + ret = append(ret, &networks[i]) } + return ret, nil } diff --git a/pkg/multicloud/qcloud/zone.go b/pkg/multicloud/qcloud/zone.go index 4e9f3e5c1..17885587e 100644 --- a/pkg/multicloud/qcloud/zone.go +++ b/pkg/multicloud/qcloud/zone.go @@ -42,8 +42,6 @@ type SZone struct { QcloudTags region *SRegion - iwires []cloudprovider.ICloudWire - host *SHost istorages []cloudprovider.ICloudStorage @@ -247,28 +245,17 @@ func (self *SZone) GetIStorageById(id string) (cloudprovider.ICloudStorage, erro return nil, cloudprovider.ErrNotFound } -func (self *SZone) addWire(wire *SWire) { - if self.iwires == nil { - self.iwires = make([]cloudprovider.ICloudWire, 0) - } - self.iwires = append(self.iwires, wire) -} - func (self *SZone) GetIWires() ([]cloudprovider.ICloudWire, error) { - return self.iwires, nil -} - -func (self *SZone) getNetworkById(networkId string) *SNetwork { - log.Debugf("Search in wires %d", len(self.iwires)) - for i := 0; i < len(self.iwires); i += 1 { - log.Debugf("Search in wire %s", self.iwires[i].GetName()) - wire := self.iwires[i].(*SWire) - net := wire.getNetworkById(networkId) - if net != nil { - return net - } + vpcs, err := self.region.GetVpcs(nil) + if err != nil { + return nil, err } - return nil + ret := []cloudprovider.ICloudWire{} + for i := range vpcs { + wire := &SWire{vpc: &vpcs[i], zone: self} + ret = append(ret, wire) + } + return ret, nil } func (self *SZone) fetchInstanceTypes() {