Merge pull request #8068 from ioito/hotfix/qx-openstack-keypair

fix: avoid compare err with cloudprovider.ErrNotFound direct
This commit is contained in:
Zexi Li
2020-09-25 01:38:15 +08:00
committed by GitHub
24 changed files with 51 additions and 39 deletions
+3 -3
View File
@@ -1019,7 +1019,7 @@ func (bucket *SBucket) PerformUpload(
if inc.SizeBytes < 0 {
inc.SizeBytes = 0
}
} else if err == cloudprovider.ErrNotFound {
} else if errors.Cause(err) == cloudprovider.ErrNotFound {
// new upload
inc.SizeBytes = sizeBytes
inc.ObjectCount = 1
@@ -1246,7 +1246,7 @@ func (bucket *SBucket) GetDetailsAcl(
} else {
object, err := cloudprovider.GetIObject(iBucket, objKey)
if err != nil {
if err == cloudprovider.ErrNotFound {
if errors.Cause(err) == cloudprovider.ErrNotFound {
return output, httperrors.NewNotFoundError("object %s not found", objKey)
} else {
return output, httperrors.NewInternalServerError("iBucket.GetIObjects error %s", err)
@@ -1455,7 +1455,7 @@ func (bucket *SBucket) processObjectsActionInput(input api.BucketObjectsActionIn
} else {
object, err := cloudprovider.GetIObject(iBucket, key)
if err != nil {
if err == cloudprovider.ErrNotFound {
if errors.Cause(err) == cloudprovider.ErrNotFound {
return nil, nil, httperrors.NewResourceNotFoundError("object %s not found", key)
} else {
return nil, nil, httperrors.NewInternalServerError("iBucket.GetIObject error %s", err)
+1 -5
View File
@@ -2536,11 +2536,7 @@ func (self *SHost) GetIHostAndProvider() (cloudprovider.ICloudHost, cloudprovide
}
ihost, err := iregion.GetIHostById(self.ExternalId)
if err != nil {
if err == cloudprovider.ErrNotFound {
return nil, nil, cloudprovider.ErrNotFound
}
log.Errorf("fail to find ihost by id %s %s", self.ExternalId, err)
return nil, nil, fmt.Errorf("fail to find ihost by id %s", err)
return nil, nil, errors.Wrapf(err, "iregion.GetIHostById(%s)", self.ExternalId)
}
return ihost, provider, nil
}
+1 -1
View File
@@ -484,7 +484,7 @@ func (spc *SSnapshotPolicyCache) DeleteCloudSnapshotPolicy() error {
return err
}
cloudSp, err := iregion.GetISnapshotPolicyById(spc.ExternalId)
if err == cloudprovider.ErrNotFound {
if errors.Cause(err) == cloudprovider.ErrNotFound {
return nil
}
if err != nil {
+2 -1
View File
@@ -21,6 +21,7 @@ import (
"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/pkg/errors"
api "yunion.io/x/onecloud/pkg/apis/compute"
"yunion.io/x/onecloud/pkg/cloudcommon/db"
@@ -64,7 +65,7 @@ func (self *SNatDEntryDeleteTask) OnInit(ctx context.Context, obj db.IStandalone
}
cloudNatDEntry, err := cloudNatGateway.GetINatDEntryByID(dnatEntry.ExternalId)
if err == cloudprovider.ErrNotFound {
if errors.Cause(err) == cloudprovider.ErrNotFound {
// already delete
} else if err != nil {
self.TaskFailed(ctx, dnatEntry, jsonutils.NewString(fmt.Sprintf("Get DNat Entry by ID '%s' failed", dnatEntry.ExternalId)))
+2 -1
View File
@@ -21,6 +21,7 @@ import (
"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/pkg/errors"
api "yunion.io/x/onecloud/pkg/apis/compute"
"yunion.io/x/onecloud/pkg/cloudcommon/db"
@@ -64,7 +65,7 @@ func (self *SNatSEntryDeleteTask) OnInit(ctx context.Context, obj db.IStandalone
return
}
cloudNatSEntry, err := cloudNatGateway.GetINatSEntryByID(snatEntry.ExternalId)
if err == cloudprovider.ErrNotFound {
if errors.Cause(err) == cloudprovider.ErrNotFound {
//already delete
} else if err != nil {
self.TaskFailed(ctx, snatEntry, jsonutils.NewString(fmt.Sprintf("Get SNat Entry by ID '%s' failed: %s", snatEntry.ExternalId, err)))
+2 -1
View File
@@ -19,6 +19,7 @@ import (
"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/pkg/errors"
api "yunion.io/x/onecloud/pkg/apis/compute"
"yunion.io/x/onecloud/pkg/cloudcommon/db"
@@ -57,7 +58,7 @@ func (self *NetworkDeleteTask) OnInit(ctx context.Context, obj db.IStandaloneMod
self.taskFailed(ctx, network, jsonutils.NewString(err.Error()))
return
}
} else if err == cloudprovider.ErrNotFound {
} else if errors.Cause(err) == cloudprovider.ErrNotFound {
// already deleted, do nothing
} else {
self.taskFailed(ctx, network, jsonutils.NewString(err.Error()))
+2 -1
View File
@@ -21,6 +21,7 @@ import (
"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/pkg/errors"
"yunion.io/x/pkg/utils"
api "yunion.io/x/onecloud/pkg/apis/compute"
@@ -130,7 +131,7 @@ func (self *SDisk) GetId() string {
func (self *SDisk) Delete(ctx context.Context) error {
_, err := self.storage.zone.region.getDisk(self.DiskId)
if err != nil {
if err == cloudprovider.ErrNotFound {
if errors.Cause(err) == cloudprovider.ErrNotFound {
// 未找到disk, 说明disk已经被删除了. 避免回收站中disk-delete循环删除失败
return nil
}
+2 -1
View File
@@ -26,6 +26,7 @@ import (
"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/pkg/errors"
"yunion.io/x/onecloud/pkg/cloudprovider"
"yunion.io/x/onecloud/pkg/compute/options"
@@ -198,7 +199,7 @@ func (self *SStoragecache) uploadImage(ctx context.Context, userCred mcclient.To
for {
_, err = self.region.GetImageByName(imageName)
if err != nil {
if err == cloudprovider.ErrNotFound {
if errors.Cause(err) == cloudprovider.ErrNotFound {
break
} else {
return "", err
+1 -1
View File
@@ -809,7 +809,7 @@ func (self *SRegion) CreateILoadBalancerCertificate(cert *cloudprovider.SLoadbal
return true, nil
}
if err == cloudprovider.ErrNotFound {
if errors.Cause(err) == cloudprovider.ErrNotFound {
return false, nil
} else {
return false, err
+1 -1
View File
@@ -205,7 +205,7 @@ func (self *SStoragecache) uploadImage(ctx context.Context, userCred mcclient.To
for {
_, err = self.region.GetImageByName(imageName, ImageOwnerSelf)
if err != nil {
if err == cloudprovider.ErrNotFound {
if errors.Cause(err) == cloudprovider.ErrNotFound {
break
} else {
return "", err
+3 -3
View File
@@ -583,15 +583,15 @@ func (e *AzureError) Error() string {
func (self *SAzureClient) getUniqName(cli *autorest.Client, resourceGroup string, resourceType, name string, body jsonutils.JSONObject) (string, string, error) {
url := fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/%s/%s", self.subscriptionId, resourceGroup, resourceType, name)
if _, err := jsonRequest(cli, "GET", self.domain, url, self.subscriptionId, "", DefaultResource); err != nil {
if err == cloudprovider.ErrNotFound {
if errors.Cause(err) == cloudprovider.ErrNotFound {
return url, body.String(), nil
}
return "", "", err
}
for i := 0; i < 20; i++ {
url = fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/%s/%s-%d", self.subscriptionId, resourceGroup, resourceType, name, i)
if _, err := jsonRequest(cli, "GET", self.domain, url, self.subscriptionId, "", DefaultResource); err == cloudprovider.ErrNotFound {
if err == cloudprovider.ErrNotFound {
if _, err := jsonRequest(cli, "GET", self.domain, url, self.subscriptionId, "", DefaultResource); err != nil {
if errors.Cause(err) == cloudprovider.ErrNotFound {
data := body.(*jsonutils.JSONDict)
data.Set("name", jsonutils.NewString(fmt.Sprintf("%s-%d", name, i)))
return url, body.String(), nil
+2 -1
View File
@@ -21,6 +21,7 @@ import (
"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/pkg/errors"
"yunion.io/x/pkg/util/osprofile"
"yunion.io/x/pkg/utils"
@@ -92,7 +93,7 @@ func (self *SHost) CreateVM(desc *cloudprovider.SManagedVMCreateConfig) (cloudpr
}
nic, err := self.searchNetorkInterface(desc.IpAddr, net.GetId(), desc.ExternalSecgroupId)
if err != nil {
if err == cloudprovider.ErrNotFound {
if errors.Cause(err) == cloudprovider.ErrNotFound {
nic, err = self.zone.region.CreateNetworkInterface(desc.ProjectId, fmt.Sprintf("%s-ipconfig", desc.Name), desc.IpAddr, net.GetId(), desc.ExternalSecgroupId)
if err != nil {
return nil, err
+2 -2
View File
@@ -267,7 +267,7 @@ func (self *SRegion) CreateStorageAccount(storageAccount string) (*SStorageAccou
if err == nil {
return account, nil
}
if err == cloudprovider.ErrNotFound {
if errors.Cause(err) == cloudprovider.ErrNotFound {
uniqName := self.GetUniqStorageAccountName()
stoargeaccount := SStorageAccount{
region: self,
@@ -1145,7 +1145,7 @@ func (b *SStorageAccount) AbortMultipartUpload(ctx context.Context, key string,
}
container, err := b.getOrCreateContainer(containerName, false)
if err != nil {
if err == cloudprovider.ErrNotFound {
if errors.Cause(err) == cloudprovider.ErrNotFound {
return nil
}
return errors.Wrap(err, "getOrCreateContainer")
+9 -4
View File
@@ -21,6 +21,7 @@ import (
"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/pkg/errors"
billing_api "yunion.io/x/onecloud/pkg/apis/billing"
api "yunion.io/x/onecloud/pkg/apis/compute"
@@ -326,10 +327,14 @@ func (self *SDisk) GetAccessPath() string {
}
func (self *SDisk) Delete(ctx context.Context) error {
if disk, err := self.storage.zone.region.GetDisk(self.GetId()); err == cloudprovider.ErrNotFound {
log.Errorf("Failed to find disk %s when delete", self.GetId())
return nil
} else if disk.Status != "deleting" {
disk, err := self.storage.zone.region.GetDisk(self.GetId())
if err != nil {
if errors.Cause(err) == cloudprovider.ErrNotFound {
return nil
}
return err
}
if disk.Status != "deleting" {
// 等待硬盘ready
cloudprovider.WaitStatus(self, api.DISK_READY, 5*time.Second, 60*time.Second)
err := self.storage.zone.region.DeleteDisk(self.GetId())
@@ -298,7 +298,7 @@ func (self *SElbBackendGroup) AddBackendServer(serverId string, weight int, port
func (self *SElbBackendGroup) RemoveBackendServer(backendId string, weight int, port int) error {
ibackend, err := self.GetILoadbalancerBackendById(backendId)
if err != nil {
if err == cloudprovider.ErrNotFound {
if errors.Cause(err) == cloudprovider.ErrNotFound {
return nil
}
+1 -1
View File
@@ -215,7 +215,7 @@ func (self *SStoragecache) uploadImage(ctx context.Context, userCred mcclient.To
for {
_, err = self.region.GetImageByName(imageName)
if err != nil {
if err == cloudprovider.ErrNotFound {
if errors.Cause(err) == cloudprovider.ErrNotFound {
break
} else {
return "", err
+8 -4
View File
@@ -21,6 +21,7 @@ import (
"yunion.io/x/pkg/errors"
"yunion.io/x/onecloud/pkg/cloudprovider"
"yunion.io/x/onecloud/pkg/util/rand"
)
type SKeypair struct {
@@ -112,13 +113,16 @@ func (region *SRegion) syncKeypair(namePrefix, publicKey string) (string, error)
return keypair.Keypair.Name, nil
}
}
for i := 0; i < 10; i++ {
name := fmt.Sprintf("%s-%d", namePrefix, i)
randomString := func(prefix string, length int) string {
return fmt.Sprintf("%s-%s", prefix, rand.String(length))
}
for i := 1; i < 10; i++ {
name := randomString(namePrefix, i)
if _, err := region.GetKeypair(name); err != nil {
if err == cloudprovider.ErrNotFound {
if errors.Cause(err) == cloudprovider.ErrNotFound {
keypair, err := region.CreateKeypair(name, publicKey, "ssh")
if err != nil {
return "", err
return "", errors.Wrapf(err, "CreateKeypair")
}
return keypair.Keypair.Name, nil
}
+2 -1
View File
@@ -21,6 +21,7 @@ import (
"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/pkg/errors"
api "yunion.io/x/onecloud/pkg/apis/compute"
"yunion.io/x/onecloud/pkg/cloudprovider"
@@ -132,7 +133,7 @@ func (cache *SStoragecache) uploadImage(ctx context.Context, userCred mcclient.T
for {
_, err = cache.region.GetImageByName(imageName)
if err != nil {
if err == cloudprovider.ErrNotFound {
if errors.Cause(err) == cloudprovider.ErrNotFound {
break
} else {
return "", err
+1 -1
View File
@@ -621,7 +621,7 @@ func (self *SRegion) StopVM(instanceId string, isForce bool) error {
func (self *SRegion) DeleteVM(instanceId string) error {
status, err := self.GetInstanceStatus(instanceId)
if err != nil {
if err == cloudprovider.ErrNotFound {
if errors.Cause(err) == cloudprovider.ErrNotFound {
return nil
}
log.Errorf("Fail to get instance status on DeleteVM: %s", err)
@@ -231,7 +231,7 @@ func (self *SLBBackendGroup) AddBackendServer(serverId string, weight int, port
// https://cloud.tencent.com/document/product/214/31794
func (self *SLBBackendGroup) RemoveBackendServer(serverId string, weight int, port int) error {
_, err := self.lb.region.GetInstance(serverId)
if err == cloudprovider.ErrNotFound {
if err != nil && errors.Cause(err) == cloudprovider.ErrNotFound {
return nil
}
+1 -1
View File
@@ -208,7 +208,7 @@ func (self *SStoragecache) uploadImage(ctx context.Context, userCred mcclient.To
for {
_, err = self.region.GetImageByName(imageName)
if err != nil {
if err == cloudprovider.ErrNotFound {
if errors.Cause(err) == cloudprovider.ErrNotFound {
break
} else {
return "", err
+1 -1
View File
@@ -127,7 +127,7 @@ func (self *SSnapshot) Delete() error {
idisk, err := self.region.GetDisk(self.UDiskID)
if err == nil {
zoneId = idisk.Zone
} else if err == cloudprovider.ErrNotFound {
} else if errors.Cause(err) == cloudprovider.ErrNotFound {
zones, err := self.region.GetIZones()
if err != nil {
return errors.Wrap(err, "snapshot.Delete GetIZones")
+1 -1
View File
@@ -214,7 +214,7 @@ func (self *SStoragecache) uploadImage(ctx context.Context, userCred mcclient.To
for {
_, err = self.region.GetImageByName(imageName)
if err != nil {
if err == cloudprovider.ErrNotFound {
if errors.Cause(err) == cloudprovider.ErrNotFound {
break
} else {
return "", err
+1 -1
View File
@@ -392,7 +392,7 @@ func (region *SRegion) _createVM(desc *cloudprovider.SManagedVMCreateConfig, zon
if len(desc.InstanceType) > 0 {
offering, err := region.GetInstanceOfferingByType(desc.InstanceType)
if err != nil {
if err == cloudprovider.ErrNotFound {
if errors.Cause(err) == cloudprovider.ErrNotFound {
offering, err = region.CreateInstanceOffering(desc.InstanceType, desc.Cpu, desc.MemoryMB, "UserVm")
if err != nil {
return nil, err