Merge pull request #4097 from ioito/bugfix/zstack-api-query-encode

fix: 避免zstack参数含有中文,未编码导致请求失败
This commit is contained in:
yunion-ci-robot
2019-12-12 11:00:38 +08:00
committed by GitHub
21 changed files with 113 additions and 80 deletions

View File

@@ -14,6 +14,8 @@
package zstack
import "net/url"
type SCluster struct {
ZStackBasic
Description string `json:"description"`
@@ -26,9 +28,9 @@ type SCluster struct {
func (region *SRegion) GetClusters() ([]SCluster, error) {
clusters := []SCluster{}
params := []string{}
params := url.Values{}
if SkipEsxi {
params = append(params, "q=type!=vmware")
params.Set("q", "type!=vmware")
}
return clusters, region.client.listAll("clusters", params, &clusters)
}

View File

@@ -14,6 +14,8 @@
package zstack
import "net/url"
type SConfiguration struct {
Name string
Category string
@@ -24,5 +26,5 @@ type SConfiguration struct {
func (region *SRegion) GetConfigrations() ([]SConfiguration, error) {
configrations := []SConfiguration{}
return configrations, region.client.listAll("global-configurations", []string{}, &configrations)
return configrations, region.client.listAll("global-configurations", url.Values{}, &configrations)
}

View File

@@ -17,6 +17,7 @@ package zstack
import (
"context"
"fmt"
"net/url"
"strings"
"time"
@@ -119,15 +120,17 @@ func (region *SRegion) GetDiskWithStorage(diskId string) (*SDisk, error) {
func (region *SRegion) GetDisks(storageId string, diskIds []string, diskType string) ([]SDisk, error) {
disks := []SDisk{}
params := []string{"q=status!=Deleted", "q=status!=NotInstantiated"}
params := url.Values{}
params.Add("q", "status!=Deleted")
params.Add("q", "status!=NotInstantiated")
if len(storageId) > 0 {
params = append(params, "q=primaryStorageUuid="+storageId)
params.Add("q", "primaryStorageUuid="+storageId)
}
if len(diskIds) > 0 {
params = append(params, "q=uuid?="+strings.Join(diskIds, ","))
params.Add("q", "uuid?="+strings.Join(diskIds, ","))
}
if len(diskType) > 0 {
params = append(params, "q=type="+diskType)
params.Add("q", "type="+diskType)
}
return disks, region.client.listAll("volumes", params, &disks)
}

View File

@@ -16,6 +16,7 @@ package zstack
import (
"fmt"
"net/url"
"yunion.io/x/jsonutils"
)
@@ -30,9 +31,9 @@ type SDiskOffering struct {
func (region *SRegion) GetDiskOfferings(diskSizeGB int) ([]SDiskOffering, error) {
offerings := []SDiskOffering{}
params := []string{}
params := url.Values{}
if diskSizeGB != 0 {
params = append(params, "q=diskSize="+fmt.Sprintf("%d", diskSizeGB*1024*1024*1024))
params.Add("q", "diskSize="+fmt.Sprintf("%d", diskSizeGB*1024*1024*1024))
}
return offerings, region.client.listAll("disk-offerings", params, &offerings)
}

View File

@@ -16,6 +16,7 @@ package zstack
import (
"fmt"
"net/url"
"time"
"yunion.io/x/jsonutils"
@@ -43,12 +44,12 @@ func (region *SRegion) GetEip(eipId string) (*SEipAddress, error) {
func (region *SRegion) GetEips(eipId, instanceId string) ([]SEipAddress, error) {
eips := []SEipAddress{}
params := []string{}
params := url.Values{}
if len(eipId) > 0 {
params = append(params, "q=uuid="+eipId)
params.Add("q", "uuid="+eipId)
}
if len(instanceId) > 0 {
params = append(params, "q=vmNic.vmInstanceUuid="+instanceId)
params.Add("q", "vmNic.vmInstanceUuid="+instanceId)
}
err := region.client.listAll("eips", params, &eips)
if err != nil {

View File

@@ -16,6 +16,7 @@ package zstack
import (
"fmt"
"net/url"
"strings"
"github.com/pkg/errors"
@@ -52,15 +53,15 @@ type SHost struct {
func (region *SRegion) GetHosts(zoneId string, hostId string) ([]SHost, error) {
hosts := []SHost{}
params := []string{}
params := url.Values{}
if len(zoneId) > 0 {
params = append(params, "q=zone.uuid="+zoneId)
params.Add("q", "zone.uuid="+zoneId)
}
if len(hostId) > 0 {
params = append(params, "q=uuid="+hostId)
params.Add("q", "uuid="+hostId)
}
if SkipEsxi {
params = append(params, "q=hypervisorType!=ESX")
params.Add("q", "hypervisorType!=ESX")
}
return hosts, region.client.listAll("hosts", params, &hosts)
}

View File

@@ -19,6 +19,7 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"sort"
"time"
@@ -197,15 +198,16 @@ func (region *SRegion) GetImage(imageId string) (*SImage, error) {
func (region *SRegion) GetImages(zoneId string, imageId string) ([]SImage, error) {
images := []SImage{}
params := []string{"q=system=false"}
params := url.Values{}
params.Add("q", "system=false")
if len(zoneId) > 0 {
params = append(params, "q=backupStorage.zone.uuid="+zoneId)
params.Add("q", "backupStorage.zone.uuid="+zoneId)
}
if len(imageId) > 0 {
params = append(params, "q=uuid="+imageId)
params.Add("q", "uuid="+imageId)
}
if SkipEsxi {
params = append(params, "q=type!=vmware")
params.Add("q", "type!=vmware")
}
return images, region.client.listAll("images", params, &images)
}

View File

@@ -14,6 +14,8 @@
package zstack
import "net/url"
type ImageServers []SImageServer
type SImageServer struct {
@@ -49,15 +51,17 @@ func (v ImageServers) Less(i, j int) bool {
func (region *SRegion) GetImageServers(zoneId, serverId string) ([]SImageServer, error) {
servers := []SImageServer{}
params := []string{"q=state=Enabled", "q=status=Connected"}
params := url.Values{}
params.Add("q", "state=Enabled")
params.Add("q", "status=Connected")
if SkipEsxi {
params = append(params, "q=type!=VCenter")
params.Add("q", "type!=VCenter")
}
if len(zoneId) > 0 {
params = append(params, "q=zone.uuid="+zoneId)
params.Add("q", "zone.uuid="+zoneId)
}
if len(serverId) > 0 {
params = append(params, "q=uuid="+serverId)
params.Add("q", "uuid="+serverId)
}
return servers, region.client.listAll("backup-storage", params, &servers)
}

View File

@@ -74,18 +74,20 @@ func (region *SRegion) GetInstance(instanceId string) (*SInstance, error) {
func (region *SRegion) GetInstances(hostId string, instanceId string, nicId string) ([]SInstance, error) {
instance := []SInstance{}
params := []string{"q=type=UserVm", "q=state!=Destroyed"}
params := url.Values{}
params.Add("q", "type=UserVm")
params.Add("q", "state!=Destroyed")
if len(hostId) > 0 {
params = append(params, "q=lastHostUuid="+hostId)
params.Add("q", "lastHostUuid="+hostId)
}
if len(instanceId) > 0 {
params = append(params, "q=uuid="+instanceId)
params.Add("q", "uuid="+instanceId)
}
if len(nicId) > 0 {
params = append(params, "q=vmNics.uuid="+nicId)
params.Add("q", "vmNics.uuid="+nicId)
}
if SkipEsxi {
params = append(params, "q=hypervisorType!=ESX")
params.Add("q", "hypervisorType!=ESX")
}
return instance, region.client.listAll("vm-instances", params, &instance)
}

View File

@@ -16,6 +16,7 @@ package zstack
import (
"fmt"
"net/url"
"yunion.io/x/jsonutils"
"yunion.io/x/pkg/util/netutils"
@@ -87,15 +88,15 @@ func (region *SRegion) GetL3Network(l3Id string) (*SL3Network, error) {
func (region *SRegion) GetL3Networks(zoneId string, wireId string, l3Id string) ([]SL3Network, error) {
l3Networks := []SL3Network{}
params := []string{}
params := url.Values{}
if len(zoneId) > 0 {
params = append(params, "q=zone.uuid="+zoneId)
params.Add("q", "zone.uuid="+zoneId)
}
if len(wireId) > 0 {
params = append(params, "q=l2NetworkUuid="+wireId)
params.Add("q", "l2NetworkUuid="+wireId)
}
if len(l3Id) > 0 {
params = append(params, "q=uuid="+l3Id)
params.Add("q", "uuid="+l3Id)
}
return l3Networks, region.client.listAll("l3-networks", params, &l3Networks)
}

View File

@@ -16,6 +16,7 @@ package zstack
import (
"fmt"
"net/url"
"yunion.io/x/jsonutils"
"yunion.io/x/log"
@@ -63,21 +64,21 @@ func (region *SRegion) GetNetworkServices() (*SNetworkService, error) {
func (region *SRegion) GetNetworkServiceProviders(Type string) ([]SNetworkServiceProvider, error) {
providers := []SNetworkServiceProvider{}
params := []string{}
params := url.Values{}
if len(Type) > 0 {
params = append(params, "q=type="+Type)
params.Add("q", "type="+Type)
}
return providers, region.client.listAll("network-services/providers", params, &providers)
}
func (region *SRegion) GetNetworkServiceRef(l3Id string, Type string) ([]SNetworkServiceRef, error) {
refs := []SNetworkServiceRef{}
params := []string{}
params := url.Values{}
if len(l3Id) > 0 {
params = append(params, "q=l3NetworkUuid="+l3Id)
params.Add("q", "l3NetworkUuid="+l3Id)
}
if len(Type) > 0 {
params = append(params, "q=networkServiceType="+Type)
params.Add("q", "networkServiceType="+Type)
}
return refs, region.client.listAll("l3-networks/network-services/refs", params, &refs)
}

View File

@@ -16,6 +16,7 @@ package zstack
import (
"fmt"
"net/url"
"github.com/pkg/errors"
@@ -85,18 +86,20 @@ func (region *SRegion) CreateInstanceOffering(name string, cpu int, memoryMb int
func (region *SRegion) GetInstanceOfferings(offerId string, name string, cpu int, memorySizeMb int) ([]SInstanceOffering, error) {
offerings := []SInstanceOffering{}
params := []string{"q=type=UserVM", "q=state=Enabled"}
params := url.Values{}
params.Add("q", "type=UserVM")
params.Add("q", "state=Enabled")
if len(offerId) > 0 {
params = append(params, "q=uuid="+offerId)
params.Add("q", "uid="+offerId)
}
if len(name) > 0 {
params = append(params, "q=name="+name)
params.Add("q", "name="+name)
}
if cpu != 0 {
params = append(params, fmt.Sprintf("q=cpuNum=%d", cpu))
params.Add("q", fmt.Sprintf("cpuNum=%d", cpu))
}
if memorySizeMb != 0 {
params = append(params, fmt.Sprintf("q=memorySize=%d", memorySizeMb*1024*1024))
params.Add("q", fmt.Sprintf("memorySize=%d", memorySizeMb*1024*1024))
}
if err := region.client.listAll("instance-offerings", params, &offerings); err != nil {
return nil, err

View File

@@ -16,6 +16,7 @@ package zstack
import (
"fmt"
"net/url"
"strings"
"github.com/pkg/errors"
@@ -184,9 +185,9 @@ func (region *SRegion) GetZone(zoneId string) (*SZone, error) {
func (region *SRegion) GetZones(zoneId string) ([]SZone, error) {
zones := []SZone{}
params := []string{}
params := url.Values{}
if len(zoneId) > 0 {
params = append(params, "q=uuid="+zoneId)
params.Add("q", "uuid="+zoneId)
}
err := region.client.listAll("zones", params, &zones)
if err != nil {

View File

@@ -17,6 +17,7 @@ package zstack
import (
"fmt"
"net"
"net/url"
"sort"
"strings"
@@ -81,15 +82,15 @@ func (region *SRegion) GetSecurityGroup(secgroupId string) (*SSecurityGroup, err
func (region *SRegion) GetSecurityGroups(secgroupId string, instanceId string, name string) ([]SSecurityGroup, error) {
secgroups := []SSecurityGroup{}
params := []string{}
params := url.Values{}
if len(secgroupId) > 0 {
params = append(params, "q=uuid="+secgroupId)
params.Add("q", "uuid="+secgroupId)
}
if len(instanceId) > 0 {
params = append(params, "q=vmNic.vmInstanceUuid="+instanceId)
params.Add("q", "vmNic.vmInstanceUuid="+instanceId)
}
if len(name) > 0 {
params = append(params, "q=name="+name)
params.Add("q", "name="+name)
}
err := region.client.listAll("security-groups", params, &secgroups)
if err != nil {

View File

@@ -16,6 +16,7 @@ package zstack
import (
"fmt"
"net/url"
"yunion.io/x/jsonutils"
"yunion.io/x/log"
@@ -95,12 +96,12 @@ func (region *SRegion) GetSnapshot(snapshotId string) (*SSnapshot, error) {
func (region *SRegion) GetSnapshots(snapshotId string, diskId string) ([]SSnapshot, error) {
snapshots := []SSnapshot{}
params := []string{}
params := url.Values{}
if len(snapshotId) > 0 {
params = append(params, "q=uuid="+snapshotId)
params.Add("q", "uuid="+snapshotId)
}
if len(diskId) > 0 {
params = append(params, "q=volumeUuid="+diskId)
params.Add("q", "volumeUuid="+diskId)
}
if err := region.client.listAll("volume-snapshots", params, &snapshots); err != nil {
return nil, err

View File

@@ -16,6 +16,7 @@ package zstack
import (
"fmt"
"net/url"
"strings"
"yunion.io/x/jsonutils"
@@ -118,18 +119,18 @@ func (region *SRegion) GetStorage(storageId string) (*SStorage, error) {
func (region *SRegion) GetStorages(zoneId, clusterId, storageId string) ([]SStorage, error) {
storages := []SStorage{}
params := []string{}
params := url.Values{}
if len(zoneId) > 0 {
params = append(params, "q=zone.uuid="+zoneId)
params.Add("q", "zone.uuid="+zoneId)
}
if len(clusterId) > 0 {
params = append(params, "q=cluster.uuid="+clusterId)
params.Add("q", "cluster.uuid="+clusterId)
}
if SkipEsxi {
params = append(params, "q=type!=VCenter")
params.Add("q", "type!=VCenter")
}
if len(storageId) > 0 {
params = append(params, "q=uuid="+storageId)
params.Add("q", "uuid="+storageId)
}
return storages, region.client.listAll("primary-storage", params, &storages)
}

View File

@@ -16,6 +16,7 @@ package zstack
import (
"fmt"
"net/url"
"strings"
"yunion.io/x/jsonutils"
@@ -55,9 +56,9 @@ func (region *SRegion) GetLocalStorage(storageId string, hostId string) (*SLocal
func (region *SRegion) GetLocalStorages(storageId string, hostId string) ([]SLocalStorage, error) {
localStorage := []SLocalStorage{}
params := []string{}
params := url.Values{}
if len(hostId) > 0 {
params = append(params, "hostUuid="+hostId)
params.Set("hostUuid", hostId)
}
err := region.client.listAll(fmt.Sprintf("primary-storage/local-storage/%s/capacities", storageId), params, &localStorage)
if err != nil {

View File

@@ -14,6 +14,8 @@
package zstack
import "net/url"
type SSysTag struct {
ZStackTime
Inherent bool `json:"inherent"`
@@ -26,18 +28,18 @@ type SSysTag struct {
func (region *SRegion) GetSysTags(tagId string, resourceType string, resourceId string, tag string) ([]SSysTag, error) {
tags := []SSysTag{}
params := []string{}
params := url.Values{}
if len(tagId) > 0 {
params = append(params, "q=uuid="+tagId)
params.Add("q", "uuid="+tagId)
}
if len(resourceType) > 0 {
params = append(params, "q=resourceType="+resourceType)
params.Add("q", "resourceType="+resourceType)
}
if len(resourceId) > 0 {
params = append(params, "q=resourceUuid="+resourceId)
params.Add("q", "resourceUuid="+resourceId)
}
if len(tag) > 0 {
params = append(params, "q=tag="+tag)
params.Add("q", "tag="+tag)
}
return tags, region.client.listAll("system-tags", params, &tags)
}

View File

@@ -16,6 +16,7 @@ package zstack
import (
"fmt"
"net/url"
"github.com/pkg/errors"
@@ -132,9 +133,9 @@ func (region *SRegion) GetNetworkId(vip *SVirtualIP) string {
func (region *SRegion) GetVirtualIPs(vipId string) ([]SVirtualIP, error) {
vips := []SVirtualIP{}
params := []string{}
params := url.Values{}
if len(vipId) > 0 {
params = append(params, "q=uuid="+vipId)
params.Add("q", "uuid="+vipId)
}
return vips, region.client.listAll("vips", params, &vips)
}

View File

@@ -16,6 +16,7 @@ package zstack
import (
"fmt"
"net/url"
"strings"
"yunion.io/x/jsonutils"
@@ -48,15 +49,16 @@ func (region *SRegion) GetWires(zoneId string, wireId string, clusterId string)
if err != nil {
return nil, err
}
params := []string{"q=attachedClusterUuids?=" + strings.Join(clusterIds, ",")}
params := url.Values{}
params.Add("q", "attachedClusterUuids?="+strings.Join(clusterIds, ","))
if len(clusterId) > 0 {
params = []string{"q=attachedClusterUuids?=" + clusterId}
params.Set("q", "attachedClusterUuids?="+clusterId)
}
if len(zoneId) > 0 {
params = append(params, "q=zone.uuid="+zoneId)
params.Add("q", "zone.uuid="+zoneId)
}
if len(wireId) > 0 {
params = append(params, "q=uuid="+wireId)
params.Add("q", "uuid="+wireId)
}
err = region.client.listAll("l2-networks", params, &wires)
if err != nil {

View File

@@ -120,13 +120,13 @@ func (cli *SZStackClient) GetIRegionById(id string) (cloudprovider.ICloudRegion,
return nil, cloudprovider.ErrNotFound
}
func (cli *SZStackClient) getRequestURL(resource string, params []string) string {
return cli.authURL + fmt.Sprintf("/zstack/%s/%s", ZSTACK_API_VERSION, resource) + "?" + strings.Join(params, "&")
func (cli *SZStackClient) getRequestURL(resource string, params url.Values) string {
return cli.authURL + fmt.Sprintf("/zstack/%s/%s", ZSTACK_API_VERSION, resource) + "?" + params.Encode()
}
func (cli *SZStackClient) testAccessKey() error {
zones := []SZone{}
err := cli.listAll("zones", []string{}, &zones)
err := cli.listAll("zones", url.Values{}, &zones)
if err != nil {
return errors.Wrap(err, "testAccessKey")
}
@@ -155,7 +155,7 @@ func (cli *SZStackClient) connect() error {
return fmt.Errorf("password auth has been deprecated, please using ak sk auth")
}
func (cli *SZStackClient) listAll(resource string, params []string, retVal interface{}) error {
func (cli *SZStackClient) listAll(resource string, params url.Values, retVal interface{}) error {
result := []jsonutils.JSONObject{}
start, limit := 0, 50
for {
@@ -193,18 +193,18 @@ func (cli *SZStackClient) sign(uri, method string, header http.Header) error {
return nil
}
func (cli *SZStackClient) _list(resource string, start int, limit int, params []string) (jsonutils.JSONObject, error) {
func (cli *SZStackClient) _list(resource string, start int, limit int, params url.Values) (jsonutils.JSONObject, error) {
client := httputils.GetDefaultClient()
header := http.Header{}
if params == nil {
params = []string{}
params = url.Values{}
}
params = append(params, "replyWithCount=true")
params = append(params, fmt.Sprintf("start=%d", start))
params.Set("replyWithCount", "true")
params.Set("start", fmt.Sprintf("%d", start))
if limit == 0 {
limit = 50
}
params = append(params, fmt.Sprintf("limit=%d", limit))
params.Set("limit", fmt.Sprintf("%d", limit))
requestURL := cli.getRequestURL(resource, params)
err := cli.sign(requestURL, "GET", header)
if err != nil {
@@ -404,7 +404,7 @@ func (cli *SZStackClient) _post(resource string, params jsonutils.JSONObject) (j
return resp, nil
}
func (cli *SZStackClient) list(baseURL string, start int, limit int, params []string, retVal interface{}) error {
func (cli *SZStackClient) list(baseURL string, start int, limit int, params url.Values, retVal interface{}) error {
resp, err := cli._list(baseURL, start, limit, params)
if err != nil {
return err