mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-01 15:07:17 +08:00
optimize: 分离各个资源的request请求
This commit is contained in:
@@ -17,6 +17,7 @@ package google
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
@@ -28,14 +29,13 @@ import (
|
||||
|
||||
type SDisk struct {
|
||||
storage *SStorage
|
||||
SResourceBase
|
||||
|
||||
Id string
|
||||
CreationTimestamp time.Time
|
||||
Name string
|
||||
SizeGB int
|
||||
Zone string
|
||||
Status string
|
||||
SelfLink string
|
||||
Type string
|
||||
SourceImage string
|
||||
LastAttachTimestamp time.Time
|
||||
@@ -56,7 +56,7 @@ func (region *SRegion) GetDisks(zone string, storageType string, maxResults int,
|
||||
}
|
||||
params := map[string]string{}
|
||||
if len(storageType) > 0 {
|
||||
params["filter"] = fmt.Sprintf(`type="%s/zones/%s/diskTypes/%s"`, region.GetUrlPrefixWithProjectId(), zone, storageType)
|
||||
params["filter"] = fmt.Sprintf(`type="%s/%s/projects/%s/zones/%s/diskTypes/%s"`, GOOGLE_COMPUTE_DOMAIN, GOOGLE_API_VERSION, region.GetProjectId(), zone, storageType)
|
||||
}
|
||||
return disks, region.List(fmt.Sprintf("zones/%s/disks", zone), params, maxResults, pageToken, &disks)
|
||||
}
|
||||
@@ -66,18 +66,6 @@ func (region *SRegion) GetDisk(id string) (*SDisk, error) {
|
||||
return disk, region.Get(id, disk)
|
||||
}
|
||||
|
||||
func (disk *SDisk) GetId() string {
|
||||
return disk.SelfLink
|
||||
}
|
||||
|
||||
func (disk *SDisk) GetGlobalId() string {
|
||||
return getGlobalId(disk.SelfLink)
|
||||
}
|
||||
|
||||
func (disk *SDisk) GetName() string {
|
||||
return disk.Name
|
||||
}
|
||||
|
||||
func (disk *SDisk) GetStatus() string {
|
||||
switch disk.Status {
|
||||
case "READY":
|
||||
@@ -194,7 +182,8 @@ func (disk *SDisk) GetISnapshot(id string) (cloudprovider.ICloudSnapshot, error)
|
||||
func (disk *SDisk) GetExtSnapshotPolicyIds() ([]string, error) {
|
||||
result := []string{}
|
||||
for _, policy := range disk.ResourcePolicies {
|
||||
result = append(result, getGlobalId(policy))
|
||||
globalId := strings.TrimPrefix(policy, fmt.Sprintf("%s/%s/", GOOGLE_COMPUTE_DOMAIN, GOOGLE_API_VERSION))
|
||||
result = append(result, globalId)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ package google
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
@@ -28,6 +29,7 @@ import (
|
||||
|
||||
type SAddress struct {
|
||||
region *SRegion
|
||||
SResourceBase
|
||||
|
||||
Id string
|
||||
CreationTimestamp time.Time
|
||||
@@ -58,18 +60,6 @@ func (region *SRegion) GetEip(id string) (*SAddress, error) {
|
||||
return eip, region.Get(id, eip)
|
||||
}
|
||||
|
||||
func (addr *SAddress) GetId() string {
|
||||
return addr.SelfLink
|
||||
}
|
||||
|
||||
func (addr *SAddress) GetName() string {
|
||||
return addr.Name
|
||||
}
|
||||
|
||||
func (addr *SAddress) GetGlobalId() string {
|
||||
return getGlobalId(addr.SelfLink)
|
||||
}
|
||||
|
||||
func (addr *SAddress) GetStatus() string {
|
||||
switch addr.Status {
|
||||
case "RESERVING":
|
||||
@@ -143,7 +133,7 @@ func (addr *SAddress) GetAssociationType() string {
|
||||
|
||||
func (addr *SAddress) GetAssociationExternalId() string {
|
||||
if len(addr.Users) > 0 {
|
||||
return getGlobalId(addr.Users[0])
|
||||
return strings.TrimPrefix(addr.Users[0], fmt.Sprintf("%s/%s/", GOOGLE_COMPUTE_DOMAIN, GOOGLE_API_VERSION))
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
@@ -14,12 +14,18 @@
|
||||
|
||||
package google
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"yunion.io/x/pkg/errors"
|
||||
)
|
||||
|
||||
type SGlobalNetwork struct {
|
||||
Id string
|
||||
//CreationTimestamp time.Time
|
||||
Name string
|
||||
SResourceBase
|
||||
|
||||
Id string
|
||||
CreationTimestamp time.Time
|
||||
Description string
|
||||
SelfLink string
|
||||
AutoCreateSubnetworks bool
|
||||
Subnetworks []string
|
||||
RoutingConfig map[string]string
|
||||
@@ -28,12 +34,29 @@ type SGlobalNetwork struct {
|
||||
|
||||
func (cli *SGoogleClient) GetGlobalNetwork(id string) (*SGlobalNetwork, error) {
|
||||
net := &SGlobalNetwork{}
|
||||
return net, cli.get(id, net)
|
||||
return net, cli.ecsGet(id, net)
|
||||
}
|
||||
|
||||
func (cli *SGoogleClient) GetGlobalNetworks(maxResults int, pageToken string) ([]SGlobalNetwork, error) {
|
||||
networks := []SGlobalNetwork{}
|
||||
params := map[string]string{}
|
||||
resource := "global/networks"
|
||||
return networks, cli.list(resource, params, maxResults, pageToken, &networks)
|
||||
if maxResults == 0 && len(pageToken) == 0 {
|
||||
err := cli.ecsListAll(resource, params, &networks)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "ecsListAll")
|
||||
}
|
||||
return networks, nil
|
||||
}
|
||||
resp, err := cli.ecsList(resource, params)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "ecsList")
|
||||
}
|
||||
if resp.Contains("items") {
|
||||
err = resp.Unmarshal(&networks, "items")
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "resp.Unmarshal")
|
||||
}
|
||||
}
|
||||
return networks, nil
|
||||
}
|
||||
|
||||
@@ -39,7 +39,11 @@ const (
|
||||
|
||||
GOOGLE_DEFAULT_REGION = "asia-east1"
|
||||
|
||||
GOOGLE_API_VERSION = "v1"
|
||||
GOOGLE_COMPUTE_DOMAIN = "https://www.googleapis.com/compute"
|
||||
GOOGLE_MANAGER_DOMAIN = "https://cloudresourcemanager.googleapis.com"
|
||||
|
||||
GOOGLE_API_VERSION = "v1"
|
||||
GOOGLE_MANAGER_API_VERSION = "v1"
|
||||
)
|
||||
|
||||
type SGoogleClient struct {
|
||||
@@ -94,7 +98,7 @@ func (self *SGoogleClient) GetAccountId() string {
|
||||
|
||||
func (self *SGoogleClient) fetchRegions() error {
|
||||
regions := []SRegion{}
|
||||
err := self.listAll("regions", nil, ®ions)
|
||||
err := self.ecsListAll("regions", nil, ®ions)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -107,87 +111,75 @@ func (self *SGoogleClient) fetchRegions() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *SGoogleClient) get(id string, retval interface{}) error {
|
||||
if !strings.HasPrefix(id, getUrlPrefix()) {
|
||||
id = getUrlPrefix() + id
|
||||
func jsonRequest(client *http.Client, method httputils.THttpMethod, domain, apiVersion, resource string, params map[string]string, body jsonutils.JSONObject, debug bool) (jsonutils.JSONObject, error) {
|
||||
resource = strings.TrimPrefix(resource, fmt.Sprintf("%s/%s/", domain, apiVersion))
|
||||
_url := fmt.Sprintf("%s/%s/%s", domain, apiVersion, resource)
|
||||
values := url.Values{}
|
||||
for k, v := range params {
|
||||
values.Set(k, v)
|
||||
}
|
||||
data, err := jsonRequest(self.client, "GET", id, nil, self.Debug)
|
||||
if len(values) > 0 {
|
||||
_url = fmt.Sprintf("%s?%s", _url, values.Encode())
|
||||
}
|
||||
return _jsonRequest(client, method, _url, body, debug)
|
||||
}
|
||||
|
||||
func (self *SGoogleClient) ecsGet(resource string, retval interface{}) error {
|
||||
resp, err := jsonRequest(self.client, "GET", GOOGLE_COMPUTE_DOMAIN, GOOGLE_API_VERSION, resource, nil, nil, self.Debug)
|
||||
if err != nil {
|
||||
if strings.Index(err.Error(), "not found") > 0 {
|
||||
return cloudprovider.ErrNotFound
|
||||
return errors.Wrap(err, "jsonRequest")
|
||||
}
|
||||
if retval != nil {
|
||||
err = resp.Unmarshal(retval)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "resp.Unmarshal")
|
||||
}
|
||||
return errors.Wrap(err, "JSONRequest")
|
||||
}
|
||||
err = data.Unmarshal(retval)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "Unmarshal")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *SGoogleClient) listAll(resource string, params map[string]string, retval interface{}) error {
|
||||
var (
|
||||
items *jsonutils.JSONArray = jsonutils.NewArray()
|
||||
_items []jsonutils.JSONObject = []jsonutils.JSONObject{}
|
||||
func (self *SGoogleClient) ecsList(resource string, params map[string]string) (jsonutils.JSONObject, error) {
|
||||
resource = fmt.Sprintf("projects/%s/%s", self.projectId, resource)
|
||||
return jsonRequest(self.client, "GET", GOOGLE_COMPUTE_DOMAIN, GOOGLE_API_VERSION, resource, params, nil, self.Debug)
|
||||
}
|
||||
|
||||
maxResults int = 500
|
||||
nextPageToken string = ""
|
||||
err error = nil
|
||||
)
|
||||
func (self *SGoogleClient) managerList(resource string, params map[string]string) (jsonutils.JSONObject, error) {
|
||||
return jsonRequest(self.client, "GET", GOOGLE_MANAGER_DOMAIN, GOOGLE_MANAGER_API_VERSION, resource, params, nil, self.Debug)
|
||||
}
|
||||
|
||||
func (self *SGoogleClient) managerGet(resource string) (jsonutils.JSONObject, error) {
|
||||
return jsonRequest(self.client, "GET", GOOGLE_MANAGER_DOMAIN, GOOGLE_MANAGER_API_VERSION, resource, nil, nil, self.Debug)
|
||||
}
|
||||
|
||||
func (self *SGoogleClient) ecsListAll(resource string, params map[string]string, retval interface{}) error {
|
||||
if params == nil {
|
||||
params = map[string]string{}
|
||||
}
|
||||
items := jsonutils.NewArray()
|
||||
nextPageToken := ""
|
||||
params["maxResults"] = "500"
|
||||
for {
|
||||
_items, nextPageToken, err = self._listAll(resource, params, maxResults, nextPageToken)
|
||||
params["pageToken"] = nextPageToken
|
||||
resp, err := self.ecsList(resource, params)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, `_listAll("%s")`, resource)
|
||||
return errors.Wrap(err, "ecsList")
|
||||
}
|
||||
items.Add(_items...)
|
||||
if len(nextPageToken) == 0 || len(_items) == 0 {
|
||||
if resp.Contains("items") {
|
||||
_items, err := resp.GetArray("items")
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "resp.GetArray")
|
||||
}
|
||||
items.Add(_items...)
|
||||
}
|
||||
nextPageToken, _ = resp.GetString("nextPageToken")
|
||||
if len(nextPageToken) == 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
return items.Unmarshal(retval)
|
||||
}
|
||||
|
||||
func (self *SGoogleClient) _listAll(resource string, params map[string]string, maxResults int, pageToken string) ([]jsonutils.JSONObject, string, error) {
|
||||
if params == nil {
|
||||
params = map[string]string{}
|
||||
}
|
||||
params["maxResults"] = fmt.Sprintf("%d", maxResults)
|
||||
params["pageToken"] = pageToken
|
||||
data, err := self._list(resource, params)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
items := []jsonutils.JSONObject{}
|
||||
if data.Contains("items") {
|
||||
items, err = data.GetArray("items")
|
||||
if err != nil {
|
||||
return nil, "", errors.Wrap(err, "data.GetArray")
|
||||
}
|
||||
}
|
||||
nextPageToken, _ := data.GetString("nextPageToken")
|
||||
return items, nextPageToken, nil
|
||||
}
|
||||
|
||||
func (self *SGoogleClient) list(resource string, params map[string]string, maxResults int, pageToken string, retval interface{}) error {
|
||||
if maxResults == 0 && len(pageToken) == 0 {
|
||||
return self.listAll(resource, params, retval)
|
||||
}
|
||||
params["maxResults"] = fmt.Sprintf("%d", maxResults)
|
||||
params["pageToken"] = pageToken
|
||||
data, err := self._list(resource, params)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "_list(%s)", resource)
|
||||
}
|
||||
if data.Contains("items") {
|
||||
err := data.Unmarshal(retval, "items")
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "data.Unmarshal")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func jsonRequest(client *http.Client, method httputils.THttpMethod, url string, body jsonutils.JSONObject, debug bool) (jsonutils.JSONObject, error) {
|
||||
func _jsonRequest(client *http.Client, method httputils.THttpMethod, url string, body jsonutils.JSONObject, debug bool) (jsonutils.JSONObject, error) {
|
||||
_, data, err := httputils.JSONRequest(client, context.Background(), method, url, nil, body, debug)
|
||||
if err != nil {
|
||||
if strings.Index(err.Error(), "not found") > 0 {
|
||||
@@ -198,18 +190,6 @@ func jsonRequest(client *http.Client, method httputils.THttpMethod, url string,
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func (self *SGoogleClient) _list(resource string, params map[string]string) (jsonutils.JSONObject, error) {
|
||||
baseUrl := fmt.Sprintf("%s%s/%s", getUrlPrefix(), self.projectId, resource)
|
||||
values := url.Values{}
|
||||
for k, v := range params {
|
||||
values.Set(k, v)
|
||||
}
|
||||
if len(values) > 0 {
|
||||
baseUrl = fmt.Sprintf("%s?%s", baseUrl, values.Encode())
|
||||
}
|
||||
return jsonRequest(self.client, "GET", baseUrl, nil, self.Debug)
|
||||
}
|
||||
|
||||
func (self *SGoogleClient) GetRegion(regionId string) *SRegion {
|
||||
if len(regionId) == 0 {
|
||||
regionId = GOOGLE_DEFAULT_REGION
|
||||
@@ -288,11 +268,3 @@ func (self *SGoogleClient) GetIProjects() ([]cloudprovider.ICloudProject, error)
|
||||
}
|
||||
return iprojects, nil
|
||||
}
|
||||
|
||||
func getUrlPrefix() string {
|
||||
return fmt.Sprintf("https://www.googleapis.com/compute/%s/projects/", GOOGLE_API_VERSION)
|
||||
}
|
||||
|
||||
func getGlobalId(id string) string {
|
||||
return strings.TrimPrefix(id, getUrlPrefix())
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ func (self *SHost) GetMetadata() *jsonutils.JSONDict {
|
||||
}
|
||||
|
||||
func (host *SHost) GetId() string {
|
||||
return getGlobalId(host.zone.GetId())
|
||||
return host.zone.GetId()
|
||||
}
|
||||
|
||||
func (host *SHost) GetGlobalId() string {
|
||||
|
||||
@@ -39,10 +39,10 @@ type SDeprecated struct {
|
||||
|
||||
type SImage struct {
|
||||
storagecache *SStoragecache
|
||||
SResourceBase
|
||||
|
||||
Id string
|
||||
CreationTimestamp time.Time
|
||||
Name string
|
||||
Description string
|
||||
SourceType string
|
||||
RawDisk map[string]string
|
||||
@@ -52,7 +52,6 @@ type SImage struct {
|
||||
DiskSizeGb int
|
||||
Licenses []string
|
||||
Family string
|
||||
SelfLink string
|
||||
LabelFingerprint string
|
||||
GuestOsFeatures []GuestOsFeature
|
||||
LicenseCodes []string
|
||||
@@ -109,18 +108,6 @@ func (region *SRegion) GetImage(id string) (*SImage, error) {
|
||||
return image, region.Get(id, image)
|
||||
}
|
||||
|
||||
func (image *SImage) GetId() string {
|
||||
return image.SelfLink
|
||||
}
|
||||
|
||||
func (image *SImage) GetGlobalId() string {
|
||||
return getGlobalId(image.SelfLink)
|
||||
}
|
||||
|
||||
func (image *SImage) GetName() string {
|
||||
return image.Name
|
||||
}
|
||||
|
||||
func (image *SImage) GetMetadata() *jsonutils.JSONDict {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -68,10 +68,10 @@ type SInstanceTag struct {
|
||||
type SInstance struct {
|
||||
multicloud.SInstanceBase
|
||||
host *SHost
|
||||
SResourceBase
|
||||
|
||||
Id string
|
||||
CreationTimestamp time.Time
|
||||
Name string
|
||||
Description string
|
||||
Tags SInstanceTag
|
||||
MachineType string
|
||||
@@ -82,7 +82,6 @@ type SInstance struct {
|
||||
Disks []InstanceDisk
|
||||
Metadata map[string]string
|
||||
ServiceAccounts []ServiceAccount
|
||||
SelfLink string
|
||||
Scheduling map[string]interface{}
|
||||
CpuPlatform string
|
||||
LabelFingerprint string
|
||||
@@ -110,18 +109,6 @@ func (region *SRegion) GetInstance(id string) (*SInstance, error) {
|
||||
return instance, region.Get(id, instance)
|
||||
}
|
||||
|
||||
func (instance *SInstance) GetId() string {
|
||||
return instance.SelfLink
|
||||
}
|
||||
|
||||
func (instnace *SInstance) GetGlobalId() string {
|
||||
return getGlobalId(instnace.SelfLink)
|
||||
}
|
||||
|
||||
func (instance *SInstance) GetName() string {
|
||||
return instance.Name
|
||||
}
|
||||
|
||||
func (instnace *SInstance) IsEmulated() bool {
|
||||
return false
|
||||
}
|
||||
@@ -238,6 +225,7 @@ func (instance *SInstance) GetIEIP() (cloudprovider.ICloudEIP, error) {
|
||||
region: instance.host.zone.region,
|
||||
SelfLink: instance.SelfLink,
|
||||
Id: instance.SelfLink,
|
||||
Status: "IN_USE",
|
||||
Address: conf.NatIP,
|
||||
}
|
||||
return eip, nil
|
||||
|
||||
@@ -49,7 +49,7 @@ func (region *SRegion) GetMachineTypes(zone string, maxResults int, pageToken st
|
||||
|
||||
func (region *SRegion) GetMachineType(id string) (*SMachineType, error) {
|
||||
machine := &SMachineType{}
|
||||
err := region.client.get(id, machine)
|
||||
err := region.client.ecsGet(id, machine)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -28,15 +28,14 @@ import (
|
||||
|
||||
type SNetwork struct {
|
||||
wire *SWire
|
||||
SResourceBase
|
||||
|
||||
Id string
|
||||
CreationTimestamp time.Time
|
||||
Name string
|
||||
Network string
|
||||
IpCidrRange string
|
||||
Region string
|
||||
GatewayAddress string
|
||||
SelfLink string
|
||||
Status string
|
||||
AvailableCpuPlatforms []string
|
||||
PrivateIpGoogleAccess bool
|
||||
@@ -60,14 +59,6 @@ func (region *SRegion) GetNetwork(id string) (*SNetwork, error) {
|
||||
return network, region.Get(id, network)
|
||||
}
|
||||
|
||||
func (network *SNetwork) GetId() string {
|
||||
return network.SelfLink
|
||||
}
|
||||
|
||||
func (network *SNetwork) GetName() string {
|
||||
return network.Name
|
||||
}
|
||||
|
||||
func (network *SNetwork) GetMetadata() *jsonutils.JSONDict {
|
||||
return nil
|
||||
}
|
||||
@@ -92,10 +83,6 @@ func (network *SNetwork) GetStatus() string {
|
||||
return api.NETWORK_INTERFACE_STATUS_AVAILABLE
|
||||
}
|
||||
|
||||
func (network *SNetwork) GetGlobalId() string {
|
||||
return getGlobalId(network.SelfLink)
|
||||
}
|
||||
|
||||
func (network *SNetwork) Delete() error {
|
||||
return cloudprovider.ErrNotImplemented
|
||||
}
|
||||
|
||||
@@ -15,7 +15,6 @@
|
||||
package google
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
@@ -33,31 +32,38 @@ type SProject struct {
|
||||
|
||||
func (cli *SGoogleClient) GetProject(id string) (*SProject, error) {
|
||||
project := &SProject{}
|
||||
return project, cli.get(id, project)
|
||||
resp, err := cli.managerGet(id)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "managerGet")
|
||||
}
|
||||
err = resp.Unmarshal(project)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "resp.Unmarshal")
|
||||
}
|
||||
return project, nil
|
||||
}
|
||||
|
||||
func (cli *SGoogleClient) GetProjects() ([]SProject, error) {
|
||||
baseUrl := "https://cloudresourcemanager.googleapis.com/v1/projects"
|
||||
nextPageToken := ""
|
||||
params := map[string]string{}
|
||||
result := []SProject{}
|
||||
for {
|
||||
url := baseUrl
|
||||
if len(nextPageToken) > 0 {
|
||||
url = fmt.Sprintf("%s?pageToken=%s", baseUrl, nextPageToken)
|
||||
params["pageToken"] = nextPageToken
|
||||
}
|
||||
data, err := jsonRequest(cli.client, "GET", url, nil, cli.Debug)
|
||||
resp, err := cli.managerList("projects", params)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "JSONRequest")
|
||||
return nil, errors.Wrap(err, "managerList")
|
||||
}
|
||||
_result := []SProject{}
|
||||
if data.Contains("projects") {
|
||||
err = data.Unmarshal(&_result, "projects")
|
||||
if resp.Contains("projects") {
|
||||
err = resp.Unmarshal(&_result, "projects")
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "data.Unmarshal")
|
||||
}
|
||||
}
|
||||
result = append(result, _result...)
|
||||
nextPageToken, _ = data.GetString("nextPageToken")
|
||||
nextPageToken, _ = resp.GetString("nextPageToken")
|
||||
if len(nextPageToken) == 0 || len(_result) == 0 {
|
||||
break
|
||||
}
|
||||
|
||||
@@ -140,10 +140,6 @@ func (region *SRegion) GetIVpcById(id string) (cloudprovider.ICloudVpc, error) {
|
||||
return nil, cloudprovider.ErrNotFound
|
||||
}
|
||||
|
||||
func (region *SRegion) GetUrlPrefixWithProjectId() string {
|
||||
return getUrlPrefix() + region.GetProjectId()
|
||||
}
|
||||
|
||||
func (region *SRegion) GetProjectId() string {
|
||||
return region.client.projectId
|
||||
}
|
||||
@@ -245,15 +241,33 @@ func (region *SRegion) GetISnapshotById(id string) (cloudprovider.ICloudSnapshot
|
||||
}
|
||||
|
||||
func (region *SRegion) ListAll(resource string, params map[string]string, retval interface{}) error {
|
||||
return region.client.listAll(resource, params, retval)
|
||||
return region.client.ecsListAll(resource, params, retval)
|
||||
}
|
||||
|
||||
func (region *SRegion) List(resource string, params map[string]string, maxResults int, pageToken string, retval interface{}) error {
|
||||
return region.client.list(resource, params, maxResults, pageToken, retval)
|
||||
if maxResults == 0 && len(pageToken) == 0 {
|
||||
return region.ListAll(resource, params, retval)
|
||||
}
|
||||
if params == nil {
|
||||
params = map[string]string{}
|
||||
}
|
||||
params["maxResults"] = fmt.Sprintf("%d", maxResults)
|
||||
params["pageToken"] = pageToken
|
||||
resp, err := region.client.ecsList(resource, params)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "ecsList")
|
||||
}
|
||||
if resp.Contains("items") && retval != nil {
|
||||
err = resp.Unmarshal(retval, "items")
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "resp.Unmarshal")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (region *SRegion) Get(id string, retval interface{}) error {
|
||||
return region.client.get(id, retval)
|
||||
return region.client.ecsGet(id, retval)
|
||||
}
|
||||
|
||||
func (region *SRegion) fetchResourcePolicies() ([]SResourcePolicy, error) {
|
||||
|
||||
@@ -41,7 +41,7 @@ func (region *SRegion) GetRegionDisks(storageType string, maxResults int, pageTo
|
||||
disks := []SRegionDisk{}
|
||||
params := map[string]string{}
|
||||
if len(storageType) > 0 {
|
||||
params["filter"] = fmt.Sprintf(`type="%s/regions/%s/diskTypes/%s"`, region.GetUrlPrefixWithProjectId(), region.Name, storageType)
|
||||
params["filter"] = fmt.Sprintf(`type="%s/%s/projects/%s/regions/%s/diskTypes/%s"`, GOOGLE_COMPUTE_DOMAIN, GOOGLE_API_VERSION, region.GetProjectId(), region.Name, storageType)
|
||||
}
|
||||
resource := fmt.Sprintf("regions/%s/disks", region.Name)
|
||||
return disks, region.List(resource, params, maxResults, pageToken, &disks)
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
// Copyright 2019 Yunion
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package google
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type SResourceBase struct {
|
||||
Name string
|
||||
SelfLink string
|
||||
}
|
||||
|
||||
func (r *SResourceBase) GetId() string {
|
||||
return r.SelfLink
|
||||
}
|
||||
|
||||
func (r *SResourceBase) GetGlobalId() string {
|
||||
return strings.TrimPrefix(r.SelfLink, fmt.Sprintf("%s/%s/", GOOGLE_COMPUTE_DOMAIN, GOOGLE_API_VERSION))
|
||||
}
|
||||
|
||||
func (r *SResourceBase) GetName() string {
|
||||
return r.Name
|
||||
}
|
||||
@@ -73,13 +73,12 @@ type SSnapshotSchedulePolicy struct {
|
||||
|
||||
type SResourcePolicy struct {
|
||||
region *SRegion
|
||||
SResourceBase
|
||||
|
||||
Id string
|
||||
|
||||
CreationTimestamp time.Time
|
||||
SelfLink string
|
||||
Region string
|
||||
Name string
|
||||
Status string
|
||||
Kind string
|
||||
SnapshotSchedulePolicy SSnapshotSchedulePolicy `json:"snapshotSchedulePolicy"`
|
||||
@@ -97,18 +96,6 @@ func (region *SRegion) GetResourcePolicy(id string) (*SResourcePolicy, error) {
|
||||
return policy, region.Get(id, policy)
|
||||
}
|
||||
|
||||
func (policy *SResourcePolicy) GetId() string {
|
||||
return getGlobalId(policy.SelfLink)
|
||||
}
|
||||
|
||||
func (policy *SResourcePolicy) GetGlobalId() string {
|
||||
return policy.GetId()
|
||||
}
|
||||
|
||||
func (policy *SResourcePolicy) GetName() string {
|
||||
return policy.Name
|
||||
}
|
||||
|
||||
func (policy *SResourcePolicy) GetStatus() string {
|
||||
switch policy.Status {
|
||||
case "READY":
|
||||
|
||||
@@ -82,6 +82,9 @@ func (region *SRegion) GetFirewalls(network string, maxResults int, pageToken st
|
||||
firewalls := []SFirewall{}
|
||||
params := map[string]string{"filter": "disabled = false"}
|
||||
resource := "global/firewalls"
|
||||
if len(network) > 0 {
|
||||
params["filter"] = fmt.Sprintf(`(disabled = false) AND (network="%s")`, network)
|
||||
}
|
||||
return firewalls, region.List(resource, params, maxResults, pageToken, &firewalls)
|
||||
}
|
||||
|
||||
@@ -166,7 +169,7 @@ func (firewall *SFirewall) toRules() ([]secrules.SecurityRule, error) {
|
||||
}
|
||||
|
||||
func (secgroup *SSecurityGroup) GetId() string {
|
||||
return getGlobalId(secgroup.vpc.globalnetwork.SelfLink)
|
||||
return secgroup.vpc.globalnetwork.GetGlobalId()
|
||||
}
|
||||
|
||||
func (secgroup *SSecurityGroup) GetGlobalId() string {
|
||||
|
||||
@@ -26,10 +26,10 @@ import (
|
||||
|
||||
type SSnapshot struct {
|
||||
region *SRegion
|
||||
SResourceBase
|
||||
|
||||
Id string
|
||||
CreationTimestamp time.Time
|
||||
Name string
|
||||
Status string
|
||||
SourceDisk string
|
||||
SourceDiskId string
|
||||
@@ -37,7 +37,6 @@ type SSnapshot struct {
|
||||
StorageBytes int
|
||||
StorageBytesStatus string
|
||||
Licenses []string
|
||||
SelfLink string
|
||||
LabelFingerprint string
|
||||
LicenseCodes []string
|
||||
StorageLocations []string
|
||||
@@ -59,18 +58,6 @@ func (region *SRegion) GetSnapshot(id string) (*SSnapshot, error) {
|
||||
return snapshot, region.Get(id, snapshot)
|
||||
}
|
||||
|
||||
func (snapshot *SSnapshot) GetId() string {
|
||||
return snapshot.SelfLink
|
||||
}
|
||||
|
||||
func (snapshot *SSnapshot) GetGlobalId() string {
|
||||
return getGlobalId(snapshot.SelfLink)
|
||||
}
|
||||
|
||||
func (snapshot *SSnapshot) GetName() string {
|
||||
return snapshot.Name
|
||||
}
|
||||
|
||||
//CREATING, DELETING, FAILED, READY, or UPLOADING
|
||||
func (snapshot *SSnapshot) GetStatus() string {
|
||||
switch snapshot.Status {
|
||||
|
||||
@@ -26,6 +26,7 @@ import (
|
||||
|
||||
type SStorage struct {
|
||||
zone *SZone
|
||||
SResourceBase
|
||||
|
||||
CreationTimestamp time.Time
|
||||
Name string
|
||||
@@ -52,14 +53,6 @@ func (region *SRegion) GetStorage(id string) (*SStorage, error) {
|
||||
return storage, region.Get(id, storage)
|
||||
}
|
||||
|
||||
func (storage *SStorage) GetId() string {
|
||||
return getGlobalId(storage.SelfLink)
|
||||
}
|
||||
|
||||
func (storage *SStorage) GetGlobalId() string {
|
||||
return storage.GetId()
|
||||
}
|
||||
|
||||
func (storage *SStorage) GetName() string {
|
||||
return storage.Description
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ func (vpc *SVpc) GetName() string {
|
||||
}
|
||||
|
||||
func (vpc *SVpc) GetId() string {
|
||||
return getGlobalId(vpc.globalnetwork.SelfLink)
|
||||
return vpc.globalnetwork.GetGlobalId()
|
||||
}
|
||||
|
||||
func (vpc *SVpc) GetGlobalId() string {
|
||||
|
||||
@@ -31,7 +31,7 @@ func (wire *SWire) GetId() string {
|
||||
}
|
||||
|
||||
func (wire *SWire) GetGlobalId() string {
|
||||
return fmt.Sprintf("%s-%s", getGlobalId(wire.GetId()), wire.vpc.region.Name)
|
||||
return fmt.Sprintf("%s-%s", wire.GetId(), wire.vpc.region.Name)
|
||||
}
|
||||
|
||||
func (wire *SWire) GetName() string {
|
||||
|
||||
@@ -45,7 +45,7 @@ func (region *SRegion) GetZones(regionId string, maxResults int, pageToken strin
|
||||
zones := []SZone{}
|
||||
params := map[string]string{}
|
||||
if len(regionId) > 0 {
|
||||
params["filter"] = fmt.Sprintf(`region="%s/regions/%s"`, region.GetUrlPrefixWithProjectId(), regionId)
|
||||
params["filter"] = fmt.Sprintf(`region="%s/%s/projects/%s/regions/%s"`, GOOGLE_COMPUTE_DOMAIN, GOOGLE_API_VERSION, region.GetProjectId(), regionId)
|
||||
}
|
||||
resource := "zones"
|
||||
return zones, region.List(resource, params, maxResults, pageToken, &zones)
|
||||
|
||||
Reference in New Issue
Block a user