-
Notifications
You must be signed in to change notification settings - Fork 31
/
test_helper.go
197 lines (172 loc) · 5.63 KB
/
test_helper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
Copyright © 2023 VMware, Inc. All Rights Reserved.
SPDX-License-Identifier: MPL-2.0
*/
package commonscope
import (
"fmt"
"log"
"os"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
clusterresource "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/cluster"
clustergroupresource "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/clustergroup"
testhelper "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/testing"
workspaceresource "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/workspace"
)
const (
// Cluster.
clusterResource = clusterresource.ResourceName
clusterResourceVar = "test_cluster"
managementClusterName = AttachedValue
provisionerName = AttachedValue
clusterNamePrefix = "tf-attach-test"
clusterGroupNameForCluster = "default"
// ClusterGroup.
clusterGroupResource = clustergroupresource.ResourceName
clusterGroupResourceVar = "test_cluster_group"
clusterGroupNamePrefix = "tf-cg-test"
// Workspace.
workspaceResource = workspaceresource.ResourceName
workspaceResourceVar = "test_workspace"
workspaceNamePrefix = "tf-workspace-test"
)
type Cluster struct {
Resource string
ResourceVar string
ResourceName string
KubeConfigPath string
Name string
ClusterGroupName string
ManagementClusterName string
ProvisionerName string
}
type ClusterGroup struct {
ResourceName string
Resource string
ResourceVar string
Name string
}
type Workspace struct {
ResourceName string
Resource string
ResourceVar string
Name string
}
type ScopeHelperResources struct {
Meta string
Cluster *Cluster
ClusterGroup *ClusterGroup
Workspace *Workspace
OrgID string
}
func NewScopeHelperResources() *ScopeHelperResources {
return &ScopeHelperResources{
Meta: testhelper.MetaTemplate,
Cluster: &Cluster{
Resource: clusterResource,
ResourceVar: clusterResourceVar,
ResourceName: fmt.Sprintf("%s.%s", clusterResource, clusterResourceVar),
KubeConfigPath: os.Getenv("KUBECONFIG"),
Name: acctest.RandomWithPrefix(clusterNamePrefix),
ClusterGroupName: clusterGroupNameForCluster,
ManagementClusterName: managementClusterName,
ProvisionerName: provisionerName,
},
ClusterGroup: &ClusterGroup{
ResourceName: fmt.Sprintf("%s.%s", clusterGroupResource, clusterGroupResourceVar),
Resource: clusterGroupResource,
ResourceVar: clusterGroupResourceVar,
Name: acctest.RandomWithPrefix(clusterGroupNamePrefix),
},
Workspace: &Workspace{
ResourceName: fmt.Sprintf("%s.%s", workspaceResource, workspaceResourceVar),
Resource: workspaceResource,
ResourceVar: workspaceResourceVar,
Name: acctest.RandomWithPrefix(workspaceNamePrefix),
},
OrgID: os.Getenv("ORG_ID"),
}
}
func (shr *ScopeHelperResources) getTestResourceWorkspaceConfigValue() string {
return fmt.Sprintf(`
resource "%s" "%s" {
name = "%s"
%s
}
`, shr.Workspace.Resource, shr.Workspace.ResourceVar, shr.Workspace.Name, shr.Meta)
}
// GetTestResourceHelperAndScope builds the helper resource and scope blocks for git repository resource based on a scope type.
func (shr *ScopeHelperResources) GetTestResourceHelperAndScope(scopeType Scope, scopesAllowed []string) (string, string) {
var (
helperBlock string
scopeBlock string
)
switch scopeType {
case ClusterScope:
helperBlock = shr.getTestResourceClusterConfigValue()
scopeBlock = fmt.Sprintf(`
scope {
cluster {
management_cluster_name = %[1]s.management_cluster_name
provisioner_name = %[1]s.provisioner_name
name = %[1]s.name
}
}
`, shr.Cluster.ResourceName)
case ClusterGroupScope:
helperBlock = shr.getTestResourceClusterGroupConfigValue()
scopeBlock = fmt.Sprintf(`
scope {
cluster_group {
name = %s.name
}
}
`, shr.ClusterGroup.ResourceName)
case WorkspaceScope:
helperBlock = shr.getTestResourceWorkspaceConfigValue()
scopeBlock = fmt.Sprintf(`
scope {
workspace {
workspace = %s.name
}
}
`, shr.Workspace.ResourceName)
case UnknownScope:
log.Printf("[ERROR]: No valid scope type block found: minimum one valid scope type block is required among: %v. Please check the schema.", strings.Join(scopesAllowed, `, `))
}
return helperBlock, scopeBlock
}
func (shr *ScopeHelperResources) getTestResourceClusterConfigValue() string {
return fmt.Sprintf(`
resource "%s" "%s" {
management_cluster_name = "%s"
provisioner_name = "%s"
name = "%s"
%s
attach_k8s_cluster {
kubeconfig_file = "%s"
}
spec {
cluster_group = "%s"
}
ready_wait_timeout = "3m"
}
`, shr.Cluster.Resource, shr.Cluster.ResourceVar, shr.Cluster.ManagementClusterName, shr.Cluster.ProvisionerName, shr.Cluster.Name, shr.Meta, shr.Cluster.KubeConfigPath, shr.Cluster.ClusterGroupName)
}
func (shr *ScopeHelperResources) getTestResourceClusterGroupConfigValue() string {
return fmt.Sprintf(`
resource "%s" "%s" {
name = "%s"
%s
}
`, shr.ClusterGroup.Resource, shr.ClusterGroup.ResourceVar, shr.ClusterGroup.Name, shr.Meta)
}
func MetaResourceAttributeCheck(resourceName string) []resource.TestCheckFunc {
return []resource.TestCheckFunc{
resource.TestCheckResourceAttr(resourceName, "meta.#", "1"),
resource.TestCheckResourceAttrSet(resourceName, "meta.0.uid"),
resource.TestCheckResourceAttrSet(resourceName, "meta.0.resource_version"),
}
}