diff --git a/pkg/multicloud/apsara/apsara.go b/pkg/multicloud/apsara/apsara.go index c098986738..fc9cd41d34 100644 --- a/pkg/multicloud/apsara/apsara.go +++ b/pkg/multicloud/apsara/apsara.go @@ -226,13 +226,20 @@ func _jsonRequest(client *sdk.Client, domain string, version string, apiName str req.Domain = domain req.Version = version req.ApiName = apiName + id := "" if params != nil { for k, v := range params { req.QueryParams[k] = v + if strings.ToLower(k) != "regionid" && strings.HasSuffix(k, "Id") { + id = v + } } } req.Scheme = "http" req.GetHeaders()["User-Agent"] = "vendor/yunion-OneCloud@" + v.Get().GitVersion + if strings.HasPrefix(apiName, "Describe") && len(id) > 0 { + req.GetHeaders()["x-acs-instanceId"] = id + } resp, err := processCommonRequest(client, req) if err != nil { @@ -250,6 +257,9 @@ func _jsonRequest(client *sdk.Client, domain string, version string, apiName str return nil, fmt.Errorf(body.String()) } } + if body.Contains("errorKey") { + return nil, errors.Errorf(body.String()) + } return body, nil } diff --git a/pkg/multicloud/apsara/image.go b/pkg/multicloud/apsara/image.go index 11177ac5e4..f2b6b4c7be 100644 --- a/pkg/multicloud/apsara/image.go +++ b/pkg/multicloud/apsara/image.go @@ -274,25 +274,29 @@ func (self *SRegion) ImportImage(name string, osArch string, osType string, osDi } func (self *SRegion) GetImage(imageId string) (*SImage, error) { - images, _, err := self.GetImages("", "", []string{imageId}, "", 0, 1) - if err != nil { - return nil, err + for _, owner := range []ImageOwnerType{ImageOwnerSelf, ImageOwnerSystem} { + images, _, err := self.GetImages("", owner, []string{imageId}, "", 0, 1) + if err != nil { + return nil, err + } + if len(images) == 1 { + return &images[0], nil + } } - if len(images) == 0 { - return nil, cloudprovider.ErrNotFound - } - return &images[0], nil + return nil, cloudprovider.ErrNotFound } func (self *SRegion) GetImageByName(name string) (*SImage, error) { - images, _, err := self.GetImages("", "", nil, name, 0, 1) - if err != nil { - return nil, err + for _, owner := range []ImageOwnerType{ImageOwnerSelf, ImageOwnerSystem} { + images, _, err := self.GetImages("", owner, nil, name, 0, 1) + if err != nil { + return nil, err + } + if len(images) == 1 { + return &images[0], nil + } } - if len(images) == 0 { - return nil, cloudprovider.ErrNotFound - } - return &images[0], nil + return nil, cloudprovider.ErrNotFound } func (self *SRegion) GetImagesBySnapshot(snapshotId string, offset int, limit int) ([]SImage, int, error) { diff --git a/pkg/multicloud/apsara/storagecache.go b/pkg/multicloud/apsara/storagecache.go index 26e250ecd4..5ff18aa8c3 100644 --- a/pkg/multicloud/apsara/storagecache.go +++ b/pkg/multicloud/apsara/storagecache.go @@ -69,18 +69,30 @@ func (self *SStoragecache) IsEmulated() bool { return false } -func (self *SStoragecache) fetchImages() error { +func (self *SStoragecache) getImages(owner ImageOwnerType) ([]SImage, error) { images := make([]SImage, 0) for { - parts, total, err := self.region.GetImages(ImageStatusType(""), "", nil, "", len(images), 50) + parts, total, err := self.region.GetImages(ImageStatusType(""), owner, nil, "", len(images), 50) if err != nil { - return err + return nil, err } images = append(images, parts...) if len(images) >= total { break } } + return images, nil +} + +func (self *SStoragecache) fetchImages() error { + images := make([]SImage, 0) + for _, owner := range []ImageOwnerType{ImageOwnerSelf, ImageOwnerSystem} { + _images, err := self.getImages(owner) + if err != nil { + return errors.Wrapf(err, "GetImage(%s)", owner) + } + images = append(images, _images...) + } self.iimages = make([]cloudprovider.ICloudImage, len(images)) for i := 0; i < len(images); i += 1 { images[i].storageCache = self