Skip to content

Commit 5f7333f

Browse files
Merge pull request #4116 from Azure/fix-comparison
Start apihelpers package
2 parents 499d1e8 + 8f58753 commit 5f7333f

File tree

7 files changed

+57
-21
lines changed

7 files changed

+57
-21
lines changed

backend/pkg/controllers/controllerutils/service_provider_cluster.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/Azure/ARO-HCP/internal/api"
2525
"github.com/Azure/ARO-HCP/internal/database"
2626
"github.com/Azure/ARO-HCP/internal/utils"
27+
"github.com/Azure/ARO-HCP/internal/utils/apihelpers"
2728
)
2829

2930
// newInitialServiceProviderCluster returns a new ServiceProviderCluster with
@@ -46,7 +47,7 @@ func newInitialServiceProviderCluster(clusterResourceID *azcorearm.ResourceID) *
4647
func GetOrCreateServiceProviderCluster(
4748
ctx context.Context, dbClient database.DBClient, clusterResourceID *azcorearm.ResourceID,
4849
) (*api.ServiceProviderCluster, error) {
49-
if clusterResourceID.ResourceType.String() != api.ClusterResourceType.String() {
50+
if !apihelpers.ResourceTypeEqual(clusterResourceID.ResourceType, api.ClusterResourceType) {
5051
return nil, utils.TrackError(fmt.Errorf("expected resource type %s, got %s", api.ClusterResourceType, clusterResourceID.ResourceType))
5152
}
5253

backend/pkg/controllers/controllerutils/service_provider_nodepool.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/Azure/ARO-HCP/internal/api"
2525
"github.com/Azure/ARO-HCP/internal/database"
2626
"github.com/Azure/ARO-HCP/internal/utils"
27+
"github.com/Azure/ARO-HCP/internal/utils/apihelpers"
2728
)
2829

2930
// newInitialServiceProviderNodePool returns a new ServiceProviderNodePool with
@@ -46,7 +47,7 @@ func newInitialServiceProviderNodePool(npResourceID *azcorearm.ResourceID) *api.
4647
func GetOrCreateServiceProviderNodePool(
4748
ctx context.Context, dbClient database.DBClient, nodePoolResourceID *azcorearm.ResourceID,
4849
) (*api.ServiceProviderNodePool, error) {
49-
if nodePoolResourceID.ResourceType.String() != api.NodePoolResourceType.String() {
50+
if !apihelpers.ResourceTypeEqual(nodePoolResourceID.ResourceType, api.NodePoolResourceType) {
5051
return nil, utils.TrackError(fmt.Errorf("expected resource type %s, got %s", api.NodePoolResourceType, nodePoolResourceID.ResourceType))
5152
}
5253

internal/api/arm/error.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import (
2222
"k8s.io/apimachinery/pkg/util/validation/field"
2323

2424
azcorearm "github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
25+
26+
"github.com/Azure/ARO-HCP/internal/utils/apihelpers"
2527
)
2628

2729
// CloudError codes
@@ -240,13 +242,13 @@ func NewResourceNotFoundError(resourceID *azcorearm.ResourceID) *CloudError {
240242
var code string
241243
var message string
242244

243-
switch resourceID.ResourceType.String() {
244-
case azcorearm.SubscriptionResourceType.String():
245+
switch {
246+
case apihelpers.ResourceTypeEqual(resourceID.ResourceType, azcorearm.SubscriptionResourceType):
245247
code = CloudErrorCodeSubscriptionNotFound
246248
message = fmt.Sprintf(
247249
"The subscription '%s' was not found.",
248250
resourceID.SubscriptionID)
249-
case azcorearm.ResourceGroupResourceType.String():
251+
case apihelpers.ResourceTypeEqual(resourceID.ResourceType, azcorearm.ResourceGroupResourceType):
250252
code = CloudErrorCodeResourceGroupNotFound
251253
message = fmt.Sprintf(
252254
"The resource group '%s' under subscription '%s' was not found.",

internal/databasetesting/mock_dbclient_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/Azure/ARO-HCP/internal/api"
2727
"github.com/Azure/ARO-HCP/internal/api/arm"
2828
"github.com/Azure/ARO-HCP/internal/database"
29+
"github.com/Azure/ARO-HCP/internal/utils/apihelpers"
2930
)
3031

3132
func TestMockDBClient_LoadFromDirectory(t *testing.T) {
@@ -61,13 +62,13 @@ func TestMockDBClient_LoadFromDirectory(t *testing.T) {
6162
}
6263

6364
switch {
64-
case typedDoc.ResourceType == api.ClusterResourceType.String():
65+
case apihelpers.ResourceTypeStringEqual(typedDoc.ResourceType, api.ClusterResourceType):
6566
foundCluster = true
66-
case typedDoc.ResourceType == api.NodePoolResourceType.String():
67+
case apihelpers.ResourceTypeStringEqual(typedDoc.ResourceType, api.NodePoolResourceType):
6768
foundNodePool = true
68-
case typedDoc.ResourceType == azcorearm.SubscriptionResourceType.String():
69+
case apihelpers.ResourceTypeStringEqual(typedDoc.ResourceType, azcorearm.SubscriptionResourceType):
6970
foundSubscription = true
70-
case typedDoc.ResourceType == api.OperationStatusResourceType.String():
71+
case apihelpers.ResourceTypeStringEqual(typedDoc.ResourceType, api.OperationStatusResourceType):
7172
foundOperation = true
7273
}
7374
}
@@ -478,7 +479,7 @@ func TestMockDBClient_UntypedCRUD(t *testing.T) {
478479
t.Fatalf("Failed to get cluster via untyped CRUD: %v", err)
479480
}
480481

481-
if retrieved.ResourceType != api.ClusterResourceType.String() {
482+
if !apihelpers.ResourceTypeStringEqual(retrieved.ResourceType, api.ClusterResourceType) {
482483
t.Errorf("Expected resource type %s, got %s", api.ClusterResourceType.String(), retrieved.ResourceType)
483484
}
484485
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2026 Microsoft Corporation
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package apihelpers
16+
17+
import (
18+
"strings"
19+
20+
azcorearm "github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
21+
)
22+
23+
func ResourceTypeEqual(lhs, rhs azcorearm.ResourceType) bool {
24+
return strings.EqualFold(lhs.String(), rhs.String())
25+
}
26+
27+
func ResourceTypeStringEqual(s string, rt azcorearm.ResourceType) bool {
28+
return strings.EqualFold(s, rt.String())
29+
}

test-integration/utils/integrationutils/cosmos_testinfo.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import (
4040
"github.com/Azure/ARO-HCP/internal/api"
4141
"github.com/Azure/ARO-HCP/internal/database"
4242
"github.com/Azure/ARO-HCP/internal/utils"
43+
"github.com/Azure/ARO-HCP/internal/utils/apihelpers"
4344
)
4445

4546
type CosmosIntegrationTestInfo struct {
@@ -115,17 +116,17 @@ func LoadCosmosContent(ctx context.Context, cosmosContainer *azcosmos.ContainerC
115116

116117
var err error
117118
switch {
118-
case strings.EqualFold(contentMap["resourceType"].(string), api.OperationStatusResourceType.String()),
119-
strings.EqualFold(contentMap["resourceType"].(string), api.ClusterResourceType.String()),
120-
strings.EqualFold(contentMap["resourceType"].(string), api.NodePoolResourceType.String()),
121-
strings.EqualFold(contentMap["resourceType"].(string), api.ExternalAuthResourceType.String()),
122-
strings.EqualFold(contentMap["resourceType"].(string), api.ClusterControllerResourceType.String()),
123-
strings.EqualFold(contentMap["resourceType"].(string), api.NodePoolControllerResourceType.String()),
124-
strings.EqualFold(contentMap["resourceType"].(string), api.ExternalAuthControllerResourceType.String()):
119+
case apihelpers.ResourceTypeStringEqual(contentMap["resourceType"].(string), api.OperationStatusResourceType),
120+
apihelpers.ResourceTypeStringEqual(contentMap["resourceType"].(string), api.ClusterResourceType),
121+
apihelpers.ResourceTypeStringEqual(contentMap["resourceType"].(string), api.NodePoolResourceType),
122+
apihelpers.ResourceTypeStringEqual(contentMap["resourceType"].(string), api.ExternalAuthResourceType),
123+
apihelpers.ResourceTypeStringEqual(contentMap["resourceType"].(string), api.ClusterControllerResourceType),
124+
apihelpers.ResourceTypeStringEqual(contentMap["resourceType"].(string), api.NodePoolControllerResourceType),
125+
apihelpers.ResourceTypeStringEqual(contentMap["resourceType"].(string), api.ExternalAuthControllerResourceType):
125126
partitionKey := azcosmos.NewPartitionKeyString(contentMap["partitionKey"].(string))
126127
_, err = cosmosContainer.CreateItem(ctx, partitionKey, content, nil)
127128

128-
case strings.EqualFold(contentMap["resourceType"].(string), azcorearm.SubscriptionResourceType.String()):
129+
case apihelpers.ResourceTypeStringEqual(contentMap["resourceType"].(string), azcorearm.SubscriptionResourceType):
129130
partitionKey := azcosmos.NewPartitionKeyString(contentMap["partitionKey"].(string))
130131
_, err = cosmosContainer.CreateItem(ctx, partitionKey, content, nil)
131132

@@ -280,12 +281,12 @@ func saveContainerContent(ctx context.Context, documentLister DocumentLister, ou
280281
armResourceID.Name+".json",
281282
)
282283

283-
case strings.EqualFold(resourceType.(string), azcorearm.SubscriptionResourceType.String()):
284+
case apihelpers.ResourceTypeStringEqual(resourceType.(string), azcorearm.SubscriptionResourceType):
284285
filename = filepath.Join(
285286
"subscriptions",
286287
fmt.Sprintf("subscription_%s.json", docMap["id"].(string)))
287288

288-
case strings.EqualFold(resourceType.(string), api.OperationStatusResourceType.String()):
289+
case apihelpers.ResourceTypeStringEqual(resourceType.(string), api.OperationStatusResourceType):
289290
externalID := properties["externalId"].(string)
290291
if clusterResourceID, _ := azcorearm.ParseResourceID(externalID); clusterResourceID != nil {
291292
clusterDir := resourceIDToDir(clusterResourceID)

test-integration/utils/integrationutils/frontend_testinfo.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"github.com/Azure/ARO-HCP/internal/api"
3131
"github.com/Azure/ARO-HCP/internal/api/arm"
3232
"github.com/Azure/ARO-HCP/internal/database"
33+
"github.com/Azure/ARO-HCP/internal/utils/apihelpers"
3334
hcpsdk20240610preview "github.com/Azure/ARO-HCP/test/sdk/v20240610preview/resourcemanager/redhatopenshifthcp/armredhatopenshifthcp"
3435
)
3536

@@ -120,7 +121,7 @@ func resourceIDToDir(resourceID *azcorearm.ResourceID) string {
120121
)
121122

122123
default:
123-
if resourceID.Parent.ResourceType.String() == "Microsoft.Resources/resourceGroups" {
124+
if apihelpers.ResourceTypeEqual(resourceID.Parent.ResourceType, azcorearm.ResourceGroupResourceType) {
124125
return filepath.Join(
125126
startingDir,
126127
resourceID.ResourceType.String(),

0 commit comments

Comments
 (0)