Skip to content

Commit

Permalink
Task: Remove functions used in errhelp.go across controllers (#1044)
Browse files Browse the repository at this point in the history
* first

* removed tests to be in sync with master

Co-authored-by: William Mortl <wmortl@Williams-MacBook-Pro.local>
Co-authored-by: Janani Vasudevan <49576785+jananivMS@users.noreply.github.com>
  • Loading branch information
3 people authored May 19, 2020
1 parent 210028e commit 5a08846
Show file tree
Hide file tree
Showing 25 changed files with 314 additions and 191 deletions.
35 changes: 0 additions & 35 deletions pkg/errhelp/errhelp.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,6 @@ import (
"strings"
)

// IsParentNotFound checks if the error is about a parent resrouce not existing
func IsParentNotFound(err error) bool {
return strings.Contains(err.Error(), "ParentResourceNotFound")
}

// IsGroupNotFound checks if error is about resource group not existing
func IsGroupNotFound(err error) bool {
return strings.Contains(err.Error(), "ResourceGroupNotFound")
}

// IsNotActive checks if error is mentioning a non active resource
func IsNotActive(err error) bool {
return strings.Contains(err.Error(), "not active")
}

// IsAsynchronousOperationNotComplete checks if error reports an asynchronous operation not completed
func IsAsynchronousOperationNotComplete(err error) bool {
return strings.Contains(err.Error(), "asynchronous operation has not completed")
}

// IsStatusCode204 checks if the error reports a status code 204 failure to respond to request
func IsStatusCode204(err error) bool {
return strings.Contains(err.Error(), "StatusCode=204")
}

// IsStatusCode404 checks if the error reports a status code 404 resource not found
func IsStatusCode404(err error) bool {
return strings.Contains(err.Error(), "StatusCode=404")
}

// IsResourceNotFound checks if error reports that a referenced resource is not found
func IsResourceNotFound(err error) bool {
return strings.Contains(err.Error(), "ResourceNotFound")
}

// StripErrorIDs takes an error and returns its string representation after filtering some common ID patterns
func StripErrorIDs(err error) string {
patterns := []string{
Expand Down
13 changes: 11 additions & 2 deletions pkg/resourcemanager/apim/apimgmt/apimgmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/Azure/azure-sdk-for-go/services/apimanagement/mgmt/2019-01-01/apimanagement"
"github.com/Azure/azure-service-operator/api/v1alpha1"
"github.com/Azure/azure-service-operator/pkg/errhelp"
"github.com/Azure/azure-service-operator/pkg/helpers"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
Expand Down Expand Up @@ -69,7 +70,11 @@ var _ = Describe("API Management", func() {
APIETag)
if err != nil {
fmt.Println(err.Error())
if !errhelp.IsAsynchronousOperationNotComplete(err) {
ignore := []string{
errhelp.AsyncOpIncompleteError,
}
azerr := errhelp.NewAzureErrorAzureError(err)
if !helpers.ContainsString(ignore, azerr.Type) {
fmt.Println("error occured")
return false
}
Expand All @@ -90,7 +95,11 @@ var _ = Describe("API Management", func() {
_, err = APIManager.DeleteAPI(ctx, tc.ResourceGroupName, APIServiceName, *contract.ID, *contract.APIRevision, true)
if err != nil {
fmt.Println(err.Error())
if !errhelp.IsAsynchronousOperationNotComplete(err) {
ignore := []string{
errhelp.AsyncOpIncompleteError,
}
azerr := errhelp.NewAzureErrorAzureError(err)
if !helpers.ContainsString(ignore, azerr.Type) {
fmt.Println("error occured")
return false
}
Expand Down
17 changes: 15 additions & 2 deletions pkg/resourcemanager/appinsights/appinsights.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,22 @@ func (m *Manager) Delete(ctx context.Context, obj runtime.Object, opts ...resour

response, err := m.DeleteAppInsights(ctx, i.Spec.ResourceGroup, i.Name)
if err != nil {
if !errhelp.IsAsynchronousOperationNotComplete(err) {
return true, err
catch := []string{
errhelp.AsyncOpIncompleteError,
}
gone := []string{
errhelp.ResourceGroupNotFoundErrorCode,
errhelp.ParentNotFoundErrorCode,
errhelp.NotFoundErrorCode,
errhelp.ResourceNotFound,
}
azerr := errhelp.NewAzureErrorAzureError(err)
if helpers.ContainsString(catch, azerr.Type) {
return true, nil
} else if helpers.ContainsString(gone, azerr.Type) {
return false, nil
}
return true, err
}
i.Status.State = response.Status

Expand Down
12 changes: 10 additions & 2 deletions pkg/resourcemanager/appinsights/appinsights_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ var _ = Describe("App Insights", func() {
_, err = AppInsightsManager.CreateAppInsights(ctx, rgName, "web", "other", location, appinsightsInstance)
if err != nil {
fmt.Println(err.Error())
if !errhelp.IsAsynchronousOperationNotComplete(err) {
ignore := []string{
errhelp.AsyncOpIncompleteError,
}
azerr := errhelp.NewAzureErrorAzureError(err)
if !helpers.ContainsString(ignore, azerr.Type) {
fmt.Println("error occured")
return false
}
Expand All @@ -64,7 +68,11 @@ var _ = Describe("App Insights", func() {
_, err = AppInsightsManager.DeleteAppInsights(ctx, psqlServer, appinsightsInstance)
if err != nil {
fmt.Println(err.Error())
if !errhelp.IsAsynchronousOperationNotComplete(err) {
ignore := []string{
errhelp.AsyncOpIncompleteError,
}
azerr := errhelp.NewAzureErrorAzureError(err)
if !helpers.ContainsString(ignore, azerr.Type) {
fmt.Println("error occured")
return false
}
Expand Down
16 changes: 12 additions & 4 deletions pkg/resourcemanager/appinsights/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ var _ = BeforeSuite(func() {
var _ = AfterSuite(func() {
By("tearing down the test environment")
_, err := tc.ResourceGroupManager.DeleteGroup(ctx, tc.ResourceGroupName)
if !errhelp.IsAsynchronousOperationNotComplete(err) {
ignore := []string{
errhelp.AsyncOpIncompleteError,
}
azerr := errhelp.NewAzureErrorAzureError(err)
if !helpers.ContainsString(ignore, azerr.Type) {
log.Println("Delete RG failed")
return
}
Expand All @@ -88,9 +92,13 @@ var _ = AfterSuite(func() {
if err == nil {
log.Println("waiting for resource group to be deleted")
} else {
if errhelp.IsGroupNotFound(err) {
log.Println("resource group deleted")
break
catch := []string{
errhelp.ResourceGroupNotFoundErrorCode,
}
azerr := errhelp.NewAzureErrorAzureError(err)
if helpers.ContainsString(catch, azerr.Type) {
log.Println("Delete RG failed")
return
} else {
log.Println(fmt.Sprintf("cannot delete resource group: %v", err))
return
Expand Down
16 changes: 13 additions & 3 deletions pkg/resourcemanager/azuresql/azuresqldb/azuresqldb_reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,21 @@ func (db *AzureSqlDbManager) Delete(ctx context.Context, obj runtime.Object, opt

_, err = db.DeleteDB(ctx, groupName, server, dbName)
if err != nil {
if errhelp.IsStatusCode204(err) {
// Database does not exist
catch := []string{
errhelp.AsyncOpIncompleteError,
}
gone := []string{
errhelp.ResourceGroupNotFoundErrorCode,
errhelp.ParentNotFoundErrorCode,
errhelp.NotFoundErrorCode,
errhelp.ResourceNotFound,
}
azerr := errhelp.NewAzureErrorAzureError(err)
if helpers.ContainsString(catch, azerr.Type) {
return true, nil
} else if helpers.ContainsString(gone, azerr.Type) {
return false, nil
}

return true, fmt.Errorf("AzureSqlDb delete error %v", err)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,23 @@ func (fw *AzureSqlFirewallRuleManager) Delete(ctx context.Context, obj runtime.O

err = fw.DeleteSQLFirewallRule(ctx, groupName, server, ruleName)
if err != nil {
if errhelp.IsStatusCode204(err) {
// firewall does not exist
return true, nil
catch := []string{
errhelp.AsyncOpIncompleteError,
}
if errhelp.IsStatusCode404(err) {
gone := []string{
errhelp.ResourceGroupNotFoundErrorCode,
errhelp.ParentNotFoundErrorCode,
errhelp.NotFoundErrorCode,
errhelp.ResourceNotFound,
}
azerr := errhelp.NewAzureErrorAzureError(err)
if helpers.ContainsString(catch, azerr.Type) {
return true, nil
} else if helpers.ContainsString(gone, azerr.Type) {
return false, nil
}
instance.Status.Message = fmt.Sprintf("AzureSqlFirewallRule Delete failed with %s", err.Error())
return false, err
return true, err
}
instance.Status.Message = fmt.Sprintf("Delete AzureSqlFirewallRule succeeded")
return false, nil
Expand Down
17 changes: 15 additions & 2 deletions pkg/resourcemanager/keyvaults/keyvault.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,9 +486,22 @@ func (k *azureKeyVaultManager) Delete(ctx context.Context, obj runtime.Object, o
if err == nil {
_, err := k.DeleteVault(ctx, instance.Spec.ResourceGroup, instance.Name)
if err != nil {
if !errhelp.IsAsynchronousOperationNotComplete(err) {
return true, err
catch := []string{
errhelp.AsyncOpIncompleteError,
}
gone := []string{
errhelp.ResourceGroupNotFoundErrorCode,
errhelp.ParentNotFoundErrorCode,
errhelp.NotFoundErrorCode,
errhelp.ResourceNotFound,
}
azerr := errhelp.NewAzureErrorAzureError(err)
if helpers.ContainsString(catch, azerr.Type) {
return true, nil
} else if helpers.ContainsString(gone, azerr.Type) {
return false, nil
}
return true, err
}
return true, nil
}
Expand Down
12 changes: 10 additions & 2 deletions pkg/resourcemanager/keyvaults/keyvault_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ var _ = Describe("KeyVault Resource Manager test", func() {
)
if err != nil {
fmt.Println(err.Error())
if !errhelp.IsAsynchronousOperationNotComplete(err) {
ignore := []string{
errhelp.AsyncOpIncompleteError,
}
azerr := errhelp.NewAzureErrorAzureError(err)
if !helpers.ContainsString(ignore, azerr.Type) {
fmt.Println("error occured")
return false
}
Expand All @@ -97,7 +101,11 @@ var _ = Describe("KeyVault Resource Manager test", func() {
_, err := keyVaultManager.DeleteVault(ctx, rgName, keyvaultName)
if err != nil {
fmt.Println(err.Error())
if !errhelp.IsAsynchronousOperationNotComplete(err) {
ignore := []string{
errhelp.AsyncOpIncompleteError,
}
azerr := errhelp.NewAzureErrorAzureError(err)
if !helpers.ContainsString(ignore, azerr.Type) {
fmt.Println("error occured")
return false
}
Expand Down
13 changes: 11 additions & 2 deletions pkg/resourcemanager/keyvaults/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,15 @@ var _ = AfterSuite(func() {
By("tearing down the test environment")
// delete the resource group and contained resources
_, err := tc.ResourceGroupManager.DeleteGroup(ctx, tc.ResourceGroupName)
if !errhelp.IsAsynchronousOperationNotComplete(err) {
ignore := []string{
errhelp.AsyncOpIncompleteError,
}
azerr := errhelp.NewAzureErrorAzureError(err)
if !helpers.ContainsString(ignore, azerr.Type) {
log.Println("Delete RG failed")
return
}

polling := time.Second * 10
Eventually(func() bool {
_, err := resourcegroupsresourcemanager.GetGroup(ctx, tc.ResourceGroupName)
Expand All @@ -93,7 +98,11 @@ var _ = AfterSuite(func() {
return false
}

if errhelp.IsGroupNotFound(err) {
catch := []string{
errhelp.ResourceGroupNotFoundErrorCode,
}
azerr := errhelp.NewAzureErrorAzureError(err)
if helpers.ContainsString(catch, azerr.Type) {
log.Println("resource group deleted")
return true
} else {
Expand Down
17 changes: 15 additions & 2 deletions pkg/resourcemanager/loadbalancer/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,22 @@ func (g *AzureLoadBalancerClient) Delete(ctx context.Context, obj runtime.Object
resourceGroup,
)
if err != nil {
if !errhelp.IsAsynchronousOperationNotComplete(err) {
return true, err
catch := []string{
errhelp.AsyncOpIncompleteError,
}
gone := []string{
errhelp.ResourceGroupNotFoundErrorCode,
errhelp.ParentNotFoundErrorCode,
errhelp.NotFoundErrorCode,
errhelp.ResourceNotFound,
}
azerr := errhelp.NewAzureErrorAzureError(err)
if helpers.ContainsString(catch, azerr.Type) {
return true, nil
} else if helpers.ContainsString(gone, azerr.Type) {
return false, nil
}
return true, err
}

if err == nil {
Expand Down
23 changes: 17 additions & 6 deletions pkg/resourcemanager/mysql/database/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,26 @@ func (m *MySQLDatabaseClient) Delete(ctx context.Context, obj runtime.Object, op

status, err := m.DeleteDatabase(ctx, instance.Name, instance.Spec.Server, instance.Spec.ResourceGroup)
if err != nil {
if !errhelp.IsAsynchronousOperationNotComplete(err) {
return true, err
catch := []string{
errhelp.AsyncOpIncompleteError,
}
}

if err == nil {
if status != "InProgress" {
gone := []string{
errhelp.ResourceGroupNotFoundErrorCode,
errhelp.ParentNotFoundErrorCode,
errhelp.NotFoundErrorCode,
errhelp.ResourceNotFound,
}
azerr := errhelp.NewAzureErrorAzureError(err)
if helpers.ContainsString(catch, azerr.Type) {
return true, nil
} else if helpers.ContainsString(gone, azerr.Type) {
return false, nil
}
return true, err
}

if status != "InProgress" {
return false, nil
}

return true, nil
Expand Down
17 changes: 15 additions & 2 deletions pkg/resourcemanager/mysql/firewallrule/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,22 @@ func (m *MySQLFirewallRuleClient) Delete(ctx context.Context, obj runtime.Object

status, err := m.DeleteFirewallRule(ctx, instance.Spec.ResourceGroup, instance.Spec.Server, instance.Name)
if err != nil {
if !errhelp.IsAsynchronousOperationNotComplete(err) {
return true, err
catch := []string{
errhelp.AsyncOpIncompleteError,
}
gone := []string{
errhelp.ResourceGroupNotFoundErrorCode,
errhelp.ParentNotFoundErrorCode,
errhelp.NotFoundErrorCode,
errhelp.ResourceNotFound,
}
azerr := errhelp.NewAzureErrorAzureError(err)
if helpers.ContainsString(catch, azerr.Type) {
return true, nil
} else if helpers.ContainsString(gone, azerr.Type) {
return false, nil
}
return true, err
}
instance.Status.State = status

Expand Down
17 changes: 15 additions & 2 deletions pkg/resourcemanager/mysql/server/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,22 @@ func (m *MySQLServerClient) Delete(ctx context.Context, obj runtime.Object, opts

status, err := m.DeleteServer(ctx, instance.Spec.ResourceGroup, instance.Name)
if err != nil {
if !errhelp.IsAsynchronousOperationNotComplete(err) {
return true, err
catch := []string{
errhelp.AsyncOpIncompleteError,
}
gone := []string{
errhelp.ResourceGroupNotFoundErrorCode,
errhelp.ParentNotFoundErrorCode,
errhelp.NotFoundErrorCode,
errhelp.ResourceNotFound,
}
azerr := errhelp.NewAzureErrorAzureError(err)
if helpers.ContainsString(catch, azerr.Type) {
return true, nil
} else if helpers.ContainsString(gone, azerr.Type) {
return false, nil
}
return true, err
}
instance.Status.State = status

Expand Down
Loading

0 comments on commit 5a08846

Please sign in to comment.