Skip to content

Commit c7c58af

Browse files
authored
chore: use shared constants (#654)
Replace repeated hard-coded literals with named constants to centralize values and reduce duplication. Signed-off-by: Ryan Johnson <ryan@tenthirtyam.org>
1 parent 3e9b9dd commit c7c58af

91 files changed

Lines changed: 2632 additions & 2232 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

internal/authctx/helper.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ var RefreshUserAuthContext = func(config *TanzuContext, refreshCondition func(er
121121
refreshSMUserAuthCtx(config)
122122
return
123123
}
124+
124125
refreshSaaSUserAuthCtx(config)
125126
}
126127
}

internal/helper/request_url_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"github.com/stretchr/testify/require"
1212
)
1313

14+
const testMultiPathWithQueryParams = "p1/p2/p3?k1=v1&k2=v2"
15+
1416
func TestConstructRequestURL(t *testing.T) {
1517
t.Parallel()
1618

@@ -75,7 +77,7 @@ func TestAppendQueryParams(t *testing.T) {
7577
"k1": []string{"v1"},
7678
"k2": []string{"v2"},
7779
},
78-
expected: "p1/p2/p3?k1=v1&k2=v2",
80+
expected: testMultiPathWithQueryParams,
7981
},
8082
}
8183

@@ -103,8 +105,8 @@ func TestString(t *testing.T) {
103105
},
104106
{
105107
name: "case for multiple paths and query parameters",
106-
requestURL: "p1/p2/p3?k1=v1&k2=v2",
107-
expected: "p1/p2/p3?k1=v1&k2=v2",
108+
requestURL: testMultiPathWithQueryParams,
109+
expected: testMultiPathWithQueryParams,
108110
},
109111
}
110112

internal/resources/akscluster/helpers_test.go

Lines changed: 49 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,21 @@ import (
2222
"github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/akscluster"
2323
)
2424

25+
const (
26+
test127001 = "127.0.0.1"
27+
testAzure = "azure"
28+
testEastus = "eastus"
29+
testEnable = "enable"
30+
testName = "name"
31+
testResourceGroup = "resource-group"
32+
testSubId = "sub-id"
33+
testTestCluster = "test-cluster"
34+
testTestCred = "test-cred"
35+
testVal = "val"
36+
37+
test127002 = "127.0.0.2"
38+
)
39+
2540
func dataDiffFrom(t *testing.T, original map[string]any, updated map[string]any) *schema.ResourceData {
2641
originalData := schema.TestResourceDataRaw(t, akscluster.ClusterSchema, original)
2742
originalData.SetId("test-uid")
@@ -36,10 +51,10 @@ func dataDiffFrom(t *testing.T, original map[string]any, updated map[string]any)
3651

3752
func expectedFullName() *models.VmwareTanzuManageV1alpha1AksclusterFullName {
3853
return &models.VmwareTanzuManageV1alpha1AksclusterFullName{
39-
CredentialName: "test-cred",
40-
SubscriptionID: "sub-id",
41-
ResourceGroupName: "resource-group",
42-
Name: "test-cluster",
54+
CredentialName: testTestCred,
55+
SubscriptionID: testSubId,
56+
ResourceGroupName: testResourceGroup,
57+
Name: testTestCluster,
4358
}
4459
}
4560

@@ -89,18 +104,18 @@ func enableCSI(c *models.VmwareTanzuManageV1alpha1AksCluster) {
89104
func aTestCluster(w ...clusterWither) *models.VmwareTanzuManageV1alpha1AksCluster {
90105
c := &models.VmwareTanzuManageV1alpha1AksCluster{
91106
FullName: &models.VmwareTanzuManageV1alpha1AksclusterFullName{
92-
CredentialName: "test-cred",
93-
ResourceGroupName: "resource-group",
94-
SubscriptionID: "sub-id",
95-
Name: "test-cluster",
107+
CredentialName: testTestCred,
108+
ResourceGroupName: testResourceGroup,
109+
SubscriptionID: testSubId,
110+
Name: testTestCluster,
96111
},
97112
Meta: &objectmetamodel.VmwareTanzuCoreV1alpha1ObjectMeta{
98113
UID: "test-uid",
99114
},
100115
Spec: &models.VmwareTanzuManageV1alpha1AksclusterSpec{
101116
ClusterGroupName: "my-cluster-group",
102117
Config: &models.VmwareTanzuManageV1alpha1AksclusterClusterConfig{
103-
Location: "eastus",
118+
Location: testEastus,
104119
Version: "1.26.0",
105120
NodeResourceGroupName: "my-node-group",
106121
DiskEncryptionSetID: "disk-encryption-set-id",
@@ -120,7 +135,7 @@ func aTestCluster(w ...clusterWither) *models.VmwareTanzuManageV1alpha1AksCluste
120135
EnableRbac: true,
121136
},
122137
APIServerAccessConfig: &models.VmwareTanzuManageV1alpha1AksclusterAPIServerAccessConfig{
123-
AuthorizedIPRanges: []string{"127.0.0.1", "127.0.0.2"},
138+
AuthorizedIPRanges: []string{test127001, test127002},
124139
EnablePrivateCluster: true,
125140
},
126141
LinuxConfig: &models.VmwareTanzuManageV1alpha1AksclusterLinuxConfig{
@@ -129,10 +144,10 @@ func aTestCluster(w ...clusterWither) *models.VmwareTanzuManageV1alpha1AksCluste
129144
},
130145
NetworkConfig: &models.VmwareTanzuManageV1alpha1AksclusterNetworkConfig{
131146
DNSPrefix: "net-prefix",
132-
DNSServiceIP: "127.0.0.1",
133-
DockerBridgeCidr: "127.0.0.2",
147+
DNSServiceIP: test127001,
148+
DockerBridgeCidr: test127002,
134149
LoadBalancerSku: "load-balancer",
135-
NetworkPlugin: "azure",
150+
NetworkPlugin: testAzure,
136151
NetworkPolicy: "policy",
137152
ServiceCidrs: []string{"127.0.0.4"},
138153
},
@@ -261,7 +276,7 @@ func withNodepools(nps []any) mapWither {
261276

262277
func withName(name string) mapWither {
263278
return func(m map[string]any) {
264-
m["name"] = name
279+
m[testName] = name
265280
}
266281
}
267282

@@ -307,24 +322,24 @@ func withPodSubnetID(podSubnetID string) mapWither {
307322

308323
func aTestClusterDataMap(w ...mapWither) map[string]any {
309324
m := map[string]any{
310-
"credential_name": "test-cred",
311-
"subscription_id": "sub-id",
312-
"resource_group": "resource-group",
313-
"name": "test-cluster",
325+
"credential_name": testTestCred,
326+
"subscription_id": testSubId,
327+
"resource_group": testResourceGroup,
328+
testName: testTestCluster,
314329
"spec": []any{map[string]any{
315330
"cluster_group": "my-cluster-group",
316331
"proxy": "my-proxy",
317332
"config": []any{map[string]any{
318-
"location": "eastus",
333+
"location": testEastus,
319334
"kubernetes_version": "1.26.0",
320335
"node_resource_group_name": "my-node-group",
321336
"disk_encryption_set": "disk-encryption-set-id",
322337
"tags": map[string]any{
323338
"custom-tag": "tag-data",
324339
},
325340
"sku": []any{map[string]any{
326-
"name": "BASIC",
327-
"tier": "FREE",
341+
testName: "BASIC",
342+
"tier": "FREE",
328343
}},
329344
"access_config": []any{map[string]any{
330345
"enable_rbac": true,
@@ -337,7 +352,7 @@ func aTestClusterDataMap(w ...mapWither) map[string]any {
337352
}},
338353
}},
339354
"api_server_access_config": []any{map[string]any{
340-
"authorized_ip_ranges": []any{"127.0.0.1", "127.0.0.2"},
355+
"authorized_ip_ranges": []any{test127001, test127002},
341356
"enable_private_cluster": true,
342357
}},
343358
"linux_config": []any{map[string]any{
@@ -346,11 +361,11 @@ func aTestClusterDataMap(w ...mapWither) map[string]any {
346361
}},
347362
"network_config": []any{map[string]any{
348363
"load_balancer_sku": "load-balancer",
349-
"network_plugin": "azure",
364+
"network_plugin": testAzure,
350365
"network_plugin_mode": "",
351366
"network_policy": "policy",
352-
"dns_service_ip": "127.0.0.1",
353-
"docker_bridge_cidr": "127.0.0.2",
367+
"dns_service_ip": test127001,
368+
"docker_bridge_cidr": test127002,
354369
"pod_cidr": nil,
355370
"service_cidr": []any{"127.0.0.4"},
356371
"dns_prefix": "net-prefix",
@@ -363,16 +378,16 @@ func aTestClusterDataMap(w ...mapWither) map[string]any {
363378
}},
364379
"addon_config": []any{map[string]any{
365380
"azure_keyvault_secrets_provider_addon_config": []any{map[string]any{
366-
"enable": true,
381+
testEnable: true,
367382
"enable_secret_rotation": true,
368383
"rotation_poll_interval": "5m",
369384
}},
370385
"monitor_addon_config": []any{map[string]any{
371-
"enable": true,
386+
testEnable: true,
372387
"log_analytics_workspace_id": "workspace-id",
373388
}},
374389
"azure_policy_addon_config": []any{map[string]any{
375-
"enable": true,
390+
testEnable: true,
376391
}},
377392
}},
378393
"auto_upgrade_config": []any{map[string]any{
@@ -457,7 +472,7 @@ func aTestNodePool(w ...nodepoolWither) *models.VmwareTanzuManageV1alpha1Aksclus
457472
},
458473
EnableNodePublicIP: true,
459474
MaxPods: 110,
460-
NodeLabels: map[string]string{"label": "val"},
475+
NodeLabels: map[string]string{"label": testVal},
461476
NodeTaints: []*models.VmwareTanzuManageV1alpha1AksclusterNodepoolTaint{{
462477
Effect: models.VmwareTanzuManageV1alpha1AksclusterNodepoolTaintEffectNOSCHEDULE.Pointer(),
463478
Key: "tkey",
@@ -466,7 +481,7 @@ func aTestNodePool(w ...nodepoolWither) *models.VmwareTanzuManageV1alpha1Aksclus
466481
OsDiskSizeGb: 30,
467482
OsDiskType: models.VmwareTanzuManageV1alpha1AksclusterNodepoolOsDiskTypeEPHEMERAL.Pointer(),
468483
OsType: models.VmwareTanzuManageV1alpha1AksclusterNodepoolOsTypeLINUX.Pointer(),
469-
Tags: map[string]string{"tmc.node.tag": "val"},
484+
Tags: map[string]string{"tmc.node.tag": testVal},
470485
UpgradeConfig: &models.VmwareTanzuManageV1alpha1AksclusterNodepoolUpgradeConfig{
471486
MaxSurge: "50%",
472487
},
@@ -484,7 +499,7 @@ func aTestNodePool(w ...nodepoolWither) *models.VmwareTanzuManageV1alpha1Aksclus
484499

485500
func aTestNodepoolDataMap(w ...mapWither) map[string]any {
486501
m := map[string]any{
487-
"name": "system-np",
502+
testName: "system-np",
488503
"spec": []any{map[string]any{
489504
"mode": "SYSTEM",
490505
"node_image_version": "v1",
@@ -509,10 +524,10 @@ func aTestNodepoolDataMap(w ...mapWither) map[string]any {
509524
},
510525
"vnet_subnet_id": "vnet-1/subnets/subnet-1",
511526
"pod_subnet_id": "vnet-1/subnets/subnet-2",
512-
"node_labels": map[string]any{"label": "val"},
513-
"tags": map[string]any{"tmc.node.tag": "val"},
527+
"node_labels": map[string]any{"label": testVal},
528+
"tags": map[string]any{"tmc.node.tag": testVal},
514529
"auto_scaling_config": []any{map[string]any{
515-
"enable": true,
530+
testEnable: true,
516531
"min_count": 1,
517532
"max_count": 10,
518533
}},

internal/resources/akscluster/resource_akscluster_acc_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -394,10 +394,10 @@ func testAKSCluster(fn *aksmodel.VmwareTanzuManageV1alpha1AksclusterFullName) st
394394
name = "%s"
395395
spec {
396396
config {
397-
location = "eastus"
397+
location = "%s"
398398
kubernetes_version = "%s"
399399
network_config {
400-
network_plugin = "azure"
400+
network_plugin = "%s"
401401
dns_prefix = "dns-tf-test"
402402
}
403403
storage_config {
@@ -420,7 +420,7 @@ func testAKSCluster(fn *aksmodel.VmwareTanzuManageV1alpha1AksclusterFullName) st
420420
}
421421
}
422422
}
423-
}`, fn.Name, fn.CredentialName, fn.SubscriptionID, fn.Name, aksKubernetesVersion)
423+
}`, fn.Name, fn.CredentialName, fn.SubscriptionID, fn.Name, testEastus, aksKubernetesVersion, testAzure)
424424
}
425425

426426
func testAKSClusterEnableCSI(fn *aksmodel.VmwareTanzuManageV1alpha1AksclusterFullName) string {
@@ -431,10 +431,10 @@ func testAKSClusterEnableCSI(fn *aksmodel.VmwareTanzuManageV1alpha1AksclusterFul
431431
name = "%s"
432432
spec {
433433
config {
434-
location = "eastus"
434+
location = "%s"
435435
kubernetes_version = "%s"
436436
network_config {
437-
network_plugin = "azure"
437+
network_plugin = "%s"
438438
dns_prefix = "dns-tf-test"
439439
}
440440
storage_config {
@@ -457,7 +457,7 @@ func testAKSClusterEnableCSI(fn *aksmodel.VmwareTanzuManageV1alpha1AksclusterFul
457457
}
458458
}
459459
}
460-
}`, fn.Name, fn.CredentialName, fn.SubscriptionID, fn.Name, aksKubernetesVersion)
460+
}`, fn.Name, fn.CredentialName, fn.SubscriptionID, fn.Name, testEastus, aksKubernetesVersion, testAzure)
461461
}
462462

463463
func testAKSClusterAddUserNodepool(fn *aksmodel.VmwareTanzuManageV1alpha1AksclusterFullName) string {
@@ -468,10 +468,10 @@ func testAKSClusterAddUserNodepool(fn *aksmodel.VmwareTanzuManageV1alpha1Aksclus
468468
name = "%s"
469469
spec {
470470
config {
471-
location = "eastus"
471+
location = "%s"
472472
kubernetes_version = "%s"
473473
network_config {
474-
network_plugin = "azure"
474+
network_plugin = "%s"
475475
dns_prefix = "dns-tf-test"
476476
}
477477
storage_config {
@@ -505,7 +505,7 @@ func testAKSClusterAddUserNodepool(fn *aksmodel.VmwareTanzuManageV1alpha1Aksclus
505505
}
506506
}
507507
}
508-
}`, fn.Name, fn.CredentialName, fn.SubscriptionID, fn.Name, aksKubernetesVersion)
508+
}`, fn.Name, fn.CredentialName, fn.SubscriptionID, fn.Name, testEastus, aksKubernetesVersion, testAzure)
509509
}
510510

511511
func testAKSClusterRemoveUserNodepool(fn *aksmodel.VmwareTanzuManageV1alpha1AksclusterFullName) string {
@@ -522,9 +522,9 @@ func mockCluster(w ...clusterWither) *aksmodel.VmwareTanzuManageV1alpha1AksClust
522522
AgentName: "",
523523
ClusterGroupName: "default",
524524
Config: &aksmodel.VmwareTanzuManageV1alpha1AksclusterClusterConfig{
525-
Location: "eastus",
525+
Location: testEastus,
526526
NetworkConfig: &aksmodel.VmwareTanzuManageV1alpha1AksclusterNetworkConfig{
527-
NetworkPlugin: "azure",
527+
NetworkPlugin: testAzure,
528528
DNSPrefix: "dns-tf-test",
529529
},
530530
StorageConfig: &aksmodel.VmwareTanzuManageV1alpha1AksclusterStorageConfig{

internal/resources/cluster/cluster_flatten_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ func TestFlattenClusterFullname(t *testing.T) {
2929
description: "normal scenario with complete cluster full name",
3030
input: &clustermodel.VmwareTanzuManageV1alpha1ClusterFullName{
3131
Name: "dummy",
32-
ManagementClusterName: "attached",
33-
ProvisionerName: "attached",
32+
ManagementClusterName: attachedValue,
33+
ProvisionerName: attachedValue,
3434
},
3535
expected: []interface{}{
3636
map[string]interface{}{
3737
NameKey: "dummy",
38-
ManagementClusterNameKey: "attached",
39-
ProvisionerNameKey: "attached",
38+
ManagementClusterNameKey: attachedValue,
39+
ProvisionerNameKey: attachedValue,
4040
},
4141
},
4242
},

internal/resources/cluster/cluster_spec_flatten_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ func TestFlattenSpec(t *testing.T) {
2828
{
2929
description: "normal scenario with cluster group",
3030
input: &clustermodel.VmwareTanzuManageV1alpha1ClusterSpec{
31-
ClusterGroupName: "default",
31+
ClusterGroupName: clusterGroupDefaultValue,
3232
},
3333
expected: []interface{}{
3434
map[string]interface{}{
35-
clusterGroupKey: "default",
35+
clusterGroupKey: clusterGroupDefaultValue,
3636
proxyNameKey: "",
3737
imageRegistryNameKey: "",
3838
},
@@ -41,28 +41,28 @@ func TestFlattenSpec(t *testing.T) {
4141
{
4242
description: "normal scenario with cluster group and proxy",
4343
input: &clustermodel.VmwareTanzuManageV1alpha1ClusterSpec{
44-
ClusterGroupName: "default",
45-
ProxyName: "proxy",
44+
ClusterGroupName: clusterGroupDefaultValue,
45+
ProxyName: proxyNameKey,
4646
},
4747
expected: []interface{}{
4848
map[string]interface{}{
49-
clusterGroupKey: "default",
50-
proxyNameKey: "proxy",
49+
clusterGroupKey: clusterGroupDefaultValue,
50+
proxyNameKey: proxyNameKey,
5151
imageRegistryNameKey: "",
5252
},
5353
},
5454
},
5555
{
5656
description: "normal scenario with cluster group, proxy and image registry",
5757
input: &clustermodel.VmwareTanzuManageV1alpha1ClusterSpec{
58-
ClusterGroupName: "default",
59-
ProxyName: "proxy",
58+
ClusterGroupName: clusterGroupDefaultValue,
59+
ProxyName: proxyNameKey,
6060
ImageRegistry: "image-registry",
6161
},
6262
expected: []interface{}{
6363
map[string]interface{}{
64-
clusterGroupKey: "default",
65-
proxyNameKey: "proxy",
64+
clusterGroupKey: clusterGroupDefaultValue,
65+
proxyNameKey: proxyNameKey,
6666
imageRegistryNameKey: "image-registry",
6767
},
6868
},

0 commit comments

Comments
 (0)