Skip to content

Commit

Permalink
replace hard coded http status code (#255)
Browse files Browse the repository at this point in the history
  • Loading branch information
priyakumarank authored Oct 2, 2019
1 parent c38eb72 commit 2792ae9
Show file tree
Hide file tree
Showing 19 changed files with 75 additions and 46 deletions.
5 changes: 3 additions & 2 deletions controllers/eventhub_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package controllers
import (
"context"
"fmt"
"net/http"
"time"

"github.com/Azure/azure-service-operator/pkg/resourcemanager/config"
Expand Down Expand Up @@ -136,12 +137,12 @@ func (r *EventhubReconciler) reapply(instance *azurev1.Eventhub) error {
secretName := instance.Spec.SecretName

result, _ := r.EventHubManager.GetHub(ctx, resourcegroup, eventhubNamespace, eventhubName)
if result.Response.StatusCode == 404 {
if result.Response.StatusCode == http.StatusNotFound {
r.reconcileExternal(instance)
r.Recorder.Event(instance, "Normal", "Updated", "Resource does not exist in azure, reapplied it")
}

if result.Response.StatusCode == 200 {
if result.Response.StatusCode == http.StatusOK {
//get secret for eventhub
err = r.getEventhubSecrets(secretName, instance)
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions controllers/keyvault_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"context"
"fmt"
"log"
"net/http"
"strings"
"time"

Expand Down Expand Up @@ -72,7 +73,7 @@ var _ = Describe("KeyVault Controller", func() {
// verify key vault exists in Azure
Eventually(func() bool {
result, _ := tc.keyVaultManager.GetVault(context.Background(), tc.resourceGroupName, keyVaultInstance.Name)
return result.Response.StatusCode == 200
return result.Response.StatusCode == http.StatusOK
}, tc.timeout,
).Should(BeTrue())

Expand All @@ -93,7 +94,7 @@ var _ = Describe("KeyVault Controller", func() {
// confirm key vault is gone from Azure
Eventually(func() bool {
result, _ := tc.keyVaultManager.GetVault(context.Background(), tc.resourceGroupName, keyVaultInstance.Name)
return result.Response.StatusCode == 404
return result.Response.StatusCode == http.StatusNotFound
}, tc.timeout, poll,
).Should(BeTrue())
})
Expand Down
3 changes: 2 additions & 1 deletion pkg/helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/md5"
"fmt"
"io"
"net/http"
"regexp"
"strings"

Expand Down Expand Up @@ -36,7 +37,7 @@ func IgnoreKubernetesResourceNotFound(err error) error {
}

func IgnoreAzureResourceNotFound(err error) error {
if err.(autorest.DetailedError).StatusCode.(int) == 404 {
if err.(autorest.DetailedError).StatusCode.(int) == http.StatusNotFound {
return nil
}
return err
Expand Down
5 changes: 3 additions & 2 deletions pkg/resourcemanager/eventhubs/consumergroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package eventhubs

import (
"context"
"net/http"
"time"

helpers "github.com/Azure/azure-service-operator/pkg/helpers"
Expand Down Expand Up @@ -71,7 +72,7 @@ var _ = Describe("ConsumerGroup", func() {

Eventually(func() bool {
result, _ := consumerGroupManager.GetConsumerGroup(context.Background(), rgName, eventhubNamespaceName, eventhubName, consumerGroupName)
return result.Response.StatusCode == 200
return result.Response.StatusCode == http.StatusOK
}, timeout,
).Should(BeTrue())

Expand All @@ -80,7 +81,7 @@ var _ = Describe("ConsumerGroup", func() {

Eventually(func() bool {
result, _ := consumerGroupManager.GetConsumerGroup(context.Background(), rgName, eventhubNamespaceName, eventhubName, consumerGroupName)
return result.Response.StatusCode == 404
return result.Response.StatusCode == http.StatusNotFound
}, timeout,
).Should(BeTrue())

Expand Down
7 changes: 4 additions & 3 deletions pkg/resourcemanager/eventhubs/hub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package eventhubs

import (
"context"
"net/http"
"time"

model "github.com/Azure/azure-sdk-for-go/services/eventhub/mgmt/2017-04-01/eventhub"
Expand Down Expand Up @@ -71,7 +72,7 @@ var _ = Describe("Eventhub", func() {

Eventually(func() bool {
result, _ := eventHubManager.GetHub(context.Background(), rgName, eventhubNamespaceName, eventhubName)
return result.Response.StatusCode == 200
return result.Response.StatusCode == http.StatusOK
}, timeout,
).Should(BeTrue())

Expand All @@ -88,7 +89,7 @@ var _ = Describe("Eventhub", func() {

Eventually(func() bool {
result, _ := eventHubManager.ListKeys(context.Background(), rgName, eventhubNamespaceName, eventhubName, authorizationRuleName)
return result.Response.StatusCode == 200
return result.Response.StatusCode == http.StatusOK
}, timeout,
).Should(BeTrue())

Expand All @@ -97,7 +98,7 @@ var _ = Describe("Eventhub", func() {

Eventually(func() bool {
result, _ := eventHubManager.GetHub(context.Background(), rgName, eventhubNamespaceName, eventhubName)
return result.Response.StatusCode == 404
return result.Response.StatusCode == http.StatusNotFound
}, timeout,
).Should(BeTrue())

Expand Down
5 changes: 3 additions & 2 deletions pkg/resourcemanager/eventhubs/namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package eventhubs

import (
"context"
"net/http"
"time"

helpers "github.com/Azure/azure-service-operator/pkg/helpers"
Expand Down Expand Up @@ -59,7 +60,7 @@ var _ = Describe("Namespace", func() {

Eventually(func() bool {
result, _ := eventHubNamespaceManager.GetNamespace(context.Background(), rgName, eventhubNamespaceName)
return result.Response.StatusCode == 200
return result.Response.StatusCode == http.StatusOK
}, timeout,
).Should(BeTrue())

Expand All @@ -68,7 +69,7 @@ var _ = Describe("Namespace", func() {

Eventually(func() bool {
result, _ := eventHubNamespaceManager.GetNamespace(context.Background(), rgName, eventhubNamespaceName)
return result.Response.StatusCode == 404 || *result.ProvisioningState == "Deleting"
return result.Response.StatusCode == http.StatusNotFound || *result.ProvisioningState == "Deleting"
}, timeout,
).Should(BeTrue())

Expand Down
6 changes: 4 additions & 2 deletions pkg/resourcemanager/mock/eventhubs/consumergroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package eventhubs
import (
"context"
"errors"
"net/http"

"github.com/Azure/azure-sdk-for-go/services/eventhub/mgmt/2017-04-01/eventhub"
"github.com/Azure/azure-service-operator/pkg/resourcemanager/mock/helpers"
"github.com/Azure/go-autorest/autorest"
Expand Down Expand Up @@ -81,12 +83,12 @@ func (manager *mockConsumerGroupManager) DeleteConsumerGroup(ctx context.Context
})

if index == -1 {
return helpers.GetRestResponse(404), errors.New("consumer group not found")
return helpers.GetRestResponse(http.StatusNotFound), errors.New("consumer group not found")
}

manager.consumerGroupResources = append(groups[:index], groups[index+1:]...)

return helpers.GetRestResponse(200), nil
return helpers.GetRestResponse(http.StatusOK), nil
}

func (manager *mockConsumerGroupManager) GetConsumerGroup(ctx context.Context, resourceGroupName string, namespaceName string, eventHubName string, consumerGroupName string) (eventhub.ConsumerGroup, error) {
Expand Down
8 changes: 5 additions & 3 deletions pkg/resourcemanager/mock/eventhubs/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package eventhubs
import (
"context"
"errors"
"net/http"

"github.com/Azure/azure-sdk-for-go/services/eventhub/mgmt/2017-04-01/eventhub"
pkghelpers "github.com/Azure/azure-service-operator/pkg/helpers"
"github.com/Azure/azure-service-operator/pkg/resourcemanager/mock/helpers"
Expand Down Expand Up @@ -71,12 +73,12 @@ func (manager *mockEventHubManager) DeleteHub(ctx context.Context, resourceGroup
})

if index == -1 {
return helpers.GetRestResponse(404), errors.New("eventhub not found")
return helpers.GetRestResponse(http.StatusNotFound), errors.New("eventhub not found")
}

manager.eventHubResources = append(hubs[:index], hubs[index+1:]...)

return helpers.GetRestResponse(200), nil
return helpers.GetRestResponse(http.StatusOK), nil
}

func (manager *mockEventHubManager) CreateHub(ctx context.Context, resourceGroupName string, namespaceName string, eventHubName string, messageRetentionInDays int32, partitionCount int32, captureDescription *eventhub.CaptureDescription) (eventhub.Model, error) {
Expand Down Expand Up @@ -142,7 +144,7 @@ func (manager *mockEventHubManager) CreateOrUpdateAuthorizationRule(ctx context.
hub.eventHubAccesses = append(hub.eventHubAccesses, eventHubAccess{
rule: parameters,
keys: eventhub.AccessKeys{
Response: helpers.GetRestResponse(200),
Response: helpers.GetRestResponse(http.StatusOK),
PrimaryConnectionString: to.StringPtr(pkghelpers.RandomString(40)),
SecondaryConnectionString: to.StringPtr(pkghelpers.RandomString(40)),
AliasPrimaryConnectionString: to.StringPtr(pkghelpers.RandomString(40)),
Expand Down
6 changes: 4 additions & 2 deletions pkg/resourcemanager/mock/eventhubs/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package eventhubs
import (
"context"
"errors"
"net/http"

"github.com/Azure/azure-sdk-for-go/services/eventhub/mgmt/2017-04-01/eventhub"
"github.com/Azure/azure-service-operator/pkg/resourcemanager/mock/helpers"
"github.com/Azure/go-autorest/autorest"
Expand Down Expand Up @@ -70,12 +72,12 @@ func (manager *mockEventHubNamespaceManager) DeleteNamespace(ctx context.Context
})

if index == -1 {
return helpers.GetRestResponse(404), errors.New("eventhub namespace not found")
return helpers.GetRestResponse(http.StatusNotFound), errors.New("eventhub namespace not found")
}

manager.eventHubNamespaceResources = append(namespaces[:index], namespaces[index+1:]...)

return helpers.GetRestResponse(200), nil
return helpers.GetRestResponse(http.StatusOK), nil
}

func (manager *mockEventHubNamespaceManager) GetNamespace(ctx context.Context, resourceGroupName string, namespaceName string) (*eventhub.EHNamespace, error) {
Expand Down
10 changes: 6 additions & 4 deletions pkg/resourcemanager/mock/keyvaults/keyvault.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package keyvaults
import (
"context"
"errors"
"net/http"

"github.com/Azure/azure-sdk-for-go/services/keyvault/mgmt/2018-02-14/keyvault"
pkghelpers "github.com/Azure/azure-service-operator/pkg/helpers"
"github.com/Azure/azure-service-operator/pkg/resourcemanager/mock/helpers"
Expand Down Expand Up @@ -48,7 +50,7 @@ func findKeyVault(res []keyVaultResource, predicate func(keyVaultResource) bool)
// CreateVault creates a new key vault
func (manager *MockKeyVaultManager) CreateVault(ctx context.Context, groupName string, vaultName string, location string) (keyvault.Vault, error) {
v := keyvault.Vault{
Response: helpers.GetRestResponse(200),
Response: helpers.GetRestResponse(http.StatusOK),
Properties: &keyvault.VaultProperties{},
ID: to.StringPtr(pkghelpers.RandomString(10)),
Name: to.StringPtr(vaultName),
Expand Down Expand Up @@ -77,12 +79,12 @@ func (manager *MockKeyVaultManager) DeleteVault(ctx context.Context, groupName s
})

if index == -1 {
return helpers.GetRestResponse(404), errors.New("key vault not found")
return helpers.GetRestResponse(http.StatusNotFound), errors.New("key vault not found")
}

manager.keyVaultResources = append(vaults[:index], vaults[index+1:]...)

return helpers.GetRestResponse(200), nil
return helpers.GetRestResponse(http.StatusOK), nil
}

// Returns an existing keyvault instance
Expand All @@ -96,7 +98,7 @@ func (manager *MockKeyVaultManager) GetVault(ctx context.Context, groupName stri

if index == -1 {
return keyvault.Vault{
Response: helpers.GetRestResponse(404),
Response: helpers.GetRestResponse(http.StatusNotFound),
}, errors.New("key vault not found")
}

Expand Down
10 changes: 6 additions & 4 deletions pkg/resourcemanager/mock/resourcegroups/resourcegroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package resourcegroups
import (
"context"
"errors"
"net/http"

"github.com/Azure/azure-service-operator/pkg/resourcemanager/mock/helpers"

"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2017-05-10/resources"
Expand Down Expand Up @@ -60,12 +62,12 @@ func (manager *MockResourceGroupManager) DeleteGroup(ctx context.Context, groupN
})

if index == -1 {
return helpers.GetRestResponse(404), errors.New("resource group not found")
return helpers.GetRestResponse(http.StatusNotFound), errors.New("resource group not found")
}

manager.resourceGroups = append(groups[:index], groups[index+1:]...)

return helpers.GetRestResponse(200), nil
return helpers.GetRestResponse(http.StatusOK), nil
}

func (manager *MockResourceGroupManager) DeleteGroupAsync(ctx context.Context, groupName string) (resources.GroupsDeleteFuture, error) {
Expand All @@ -81,8 +83,8 @@ func (manager *MockResourceGroupManager) CheckExistence(ctx context.Context, gro
})

if index == -1 {
return helpers.GetRestResponse(404), errors.New("resource group not found")
return helpers.GetRestResponse(http.StatusNotFound), errors.New("resource group not found")
}

return helpers.GetRestResponse(204), nil
return helpers.GetRestResponse(http.StatusNoContent), nil
}
6 changes: 4 additions & 2 deletions pkg/resourcemanager/mock/storages/blob_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package storages
import (
"context"
"errors"
"net/http"

"github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2019-04-01/storage"
"github.com/Azure/azure-service-operator/pkg/resourcemanager/mock/helpers"
"github.com/Azure/go-autorest/autorest"
Expand Down Expand Up @@ -96,10 +98,10 @@ func (manager *mockBlobContainerManager) DeleteBlobContainer(ctx context.Context
})

if index == -1 {
return helpers.GetRestResponse(404), errors.New("blob container not found")
return helpers.GetRestResponse(http.StatusNotFound), errors.New("blob container not found")
}

manager.blobContainerResource = append(containers[:index], containers[index+1:]...)

return helpers.GetRestResponse(200), nil
return helpers.GetRestResponse(http.StatusOK), nil
}
6 changes: 4 additions & 2 deletions pkg/resourcemanager/mock/storages/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package storages
import (
"context"
"errors"
"net/http"

"github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2019-04-01/storage"
apiv1 "github.com/Azure/azure-service-operator/api/v1"
"github.com/Azure/azure-service-operator/pkg/resourcemanager/mock/helpers"
Expand Down Expand Up @@ -103,10 +105,10 @@ func (manager *mockStorageManager) DeleteStorage(ctx context.Context, resourceGr
})

if index == -1 {
return helpers.GetRestResponse(404), errors.New("storage account not found")
return helpers.GetRestResponse(http.StatusNotFound), errors.New("storage account not found")
}

manager.storageResource = append(groups[:index], groups[index+1:]...)

return helpers.GetRestResponse(200), nil
return helpers.GetRestResponse(http.StatusOK), nil
}
8 changes: 5 additions & 3 deletions pkg/resourcemanager/resourcegroups/resourcegroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ package resourcegroups

import (
"context"
"github.com/Azure/azure-service-operator/pkg/resourcemanager/config"
"net/http"
"time"

"github.com/Azure/azure-service-operator/pkg/resourcemanager/config"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

Expand Down Expand Up @@ -59,7 +61,7 @@ var _ = Describe("ResourceGroups", func() {
Eventually(func() bool {
result, _ := resourceGroupManager.CheckExistence(context.Background(), resourcegroupName)

return result.Response.StatusCode == 204
return result.Response.StatusCode == http.StatusNoContent
}, timeout,
).Should(BeTrue())

Expand All @@ -68,7 +70,7 @@ var _ = Describe("ResourceGroups", func() {

Eventually(func() bool {
result, _ := GetGroup(context.Background(), resourcegroupName)
return result.Response.StatusCode == 404 || *result.Properties.ProvisioningState == "Deleting"
return result.Response.StatusCode == http.StatusNotFound || *result.Properties.ProvisioningState == "Deleting"
}, timeout,
).Should(BeTrue())

Expand Down
Loading

0 comments on commit 2792ae9

Please sign in to comment.