Skip to content

Commit 50b92ef

Browse files
Fix TestAccContainerNodePool_resourceManagerTags (#13994) (#22951)
[upstream:c722c3acb2e53f0d0c7b2aa4f1568504c27d2d9b] Signed-off-by: Modular Magician <[email protected]>
1 parent 421b7d5 commit 50b92ef

File tree

7 files changed

+93
-148
lines changed

7 files changed

+93
-148
lines changed

google/acctest/bootstrap_test_utils.go

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1753,11 +1753,30 @@ func BootstrapSharedCodeRepositoryIndex(t *testing.T, codeRepositoryIndexId, loc
17531753
}
17541754

17551755
const sharedTagKeyPrefix = "tf-bootstrap-tagkey"
1756+
const sharedTagKeyParentErr = "Parent %q is not valid. Should be in format: 'organizations/123' OR 'projects/123'."
17561757

1757-
func BootstrapSharedTestTagKey(t *testing.T, testId string) string {
1758+
func BootstrapSharedTestProjectTagKey(t *testing.T, testId string, obj map[string]interface{}) string {
1759+
pid := envvar.GetTestProjectFromEnv()
1760+
return bootstrapSharedTestTagKey(t, testId, "projects/"+pid, obj)
1761+
}
1762+
1763+
func BootstrapSharedTestOrganizationTagKey(t *testing.T, testId string, obj map[string]interface{}) string {
17581764
org := envvar.GetTestOrgFromEnv(t)
1765+
return bootstrapSharedTestTagKey(t, testId, "organizations/"+org, obj)
1766+
}
1767+
1768+
// parent should be in format: {"organization" OR "projects"}/{id}
1769+
func bootstrapSharedTestTagKey(t *testing.T, testId, parent string, obj map[string]interface{}) string {
17591770
sharedTagKey := fmt.Sprintf("%s-%s", sharedTagKeyPrefix, testId)
1760-
tagKeyName := fmt.Sprintf("%s/%s", org, sharedTagKey)
1771+
1772+
parentSplit := strings.Split(parent, "/")
1773+
if len(parentSplit) < 2 || (parentSplit[0] != "organizations" && parentSplit[0] != "projects") {
1774+
parentErr := fmt.Sprintf(sharedTagKeyParentErr, parent)
1775+
t.Fatalf("Error bootstrapping shared tag key %q: %s", sharedTagKey, parentErr)
1776+
}
1777+
1778+
parentId := parentSplit[1]
1779+
tagKeyName := fmt.Sprintf("%s/%s", parentId, sharedTagKey)
17611780

17621781
config := BootstrapConfig(t)
17631782
if config == nil {
@@ -1777,10 +1796,13 @@ func BootstrapSharedTestTagKey(t *testing.T, testId string) string {
17771796
if err != nil && transport_tpg.IsGoogleApiErrorWithCode(err, 403) {
17781797
log.Printf("[DEBUG] TagKey %q not found, bootstrapping", sharedTagKey)
17791798
tagKeyObj := map[string]interface{}{
1780-
"parent": "organizations/" + org,
1799+
"parent": parent,
17811800
"shortName": sharedTagKey,
17821801
"description": "Bootstrapped tag key for Terraform Acceptance testing",
17831802
}
1803+
if obj != nil {
1804+
maps.Insert(tagKeyObj, maps.All(obj))
1805+
}
17841806

17851807
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
17861808
Config: config,
@@ -1825,10 +1847,19 @@ func BootstrapSharedTestTagKey(t *testing.T, testId string) string {
18251847

18261848
const sharedTagValuePrefix = "tf-bootstrap-tagvalue"
18271849

1828-
func BootstrapSharedTestTagValue(t *testing.T, testId string, tagKey string) string {
1850+
func BootstrapSharedTestProjectTagValue(t *testing.T, testId string, tagKey string) string {
1851+
pid := envvar.GetTestProjectFromEnv()
1852+
return BootstrapSharedTestTagValue(t, testId, tagKey, pid)
1853+
}
1854+
1855+
func BootstrapSharedTestOrganizationTagValue(t *testing.T, testId string, tagKey string) string {
18291856
org := envvar.GetTestOrgFromEnv(t)
1857+
return BootstrapSharedTestTagValue(t, testId, tagKey, org)
1858+
}
1859+
1860+
func BootstrapSharedTestTagValue(t *testing.T, testId string, tagKey, parentId string) string {
18301861
sharedTagValue := fmt.Sprintf("%s-%s", sharedTagValuePrefix, testId)
1831-
tagKeyName := fmt.Sprintf("%s/%s", org, tagKey)
1862+
tagKeyName := fmt.Sprintf("%s/%s", parentId, tagKey)
18321863
tagValueName := fmt.Sprintf("%s/%s", tagKeyName, sharedTagValue)
18331864

18341865
config := BootstrapConfig(t)

google/services/container/resource_container_node_pool_test.go

Lines changed: 47 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,27 @@ func TestAccContainerNodePool_resourceManagerTags(t *testing.T) {
5757
t.Parallel()
5858
pid := envvar.GetTestProjectFromEnv()
5959

60-
randomSuffix := acctest.RandString(t, 10)
61-
clusterName := fmt.Sprintf("tf-test-cluster-%s", randomSuffix)
62-
6360
networkName := acctest.BootstrapSharedTestNetwork(t, "gke-cluster")
64-
subnetworkName := acctest.BootstrapSubnet(t, "gke-cluster", networkName)
61+
tagData := map[string]interface{}{
62+
"purpose": "GCE_FIREWALL",
63+
"purpose_data": map[string]interface{}{
64+
"network": pid + "/" + networkName,
65+
},
66+
}
67+
tagKey1 := acctest.BootstrapSharedTestProjectTagKey(t, "resourceManagerTags1", tagData)
68+
tagKey2 := acctest.BootstrapSharedTestProjectTagKey(t, "resourceManagerTags2", tagData)
69+
70+
context := map[string]interface{}{
71+
"pid": pid,
72+
"org": envvar.GetTestOrgFromEnv(t),
73+
"network": networkName,
74+
"subnet": acctest.BootstrapSubnet(t, "gke-cluster", networkName),
75+
"tagKey1": tagKey1,
76+
"tagValue1": acctest.BootstrapSharedTestProjectTagValue(t, "resourceManagerTags1", tagKey1),
77+
"tagKey2": tagKey2,
78+
"tagValue2": acctest.BootstrapSharedTestProjectTagValue(t, "resourceManagerTags2", tagKey2),
79+
"random_suffix": acctest.RandString(t, 10),
80+
}
6581

6682
bootstrapGkeTagManagerServiceAgents(t)
6783

@@ -74,7 +90,7 @@ func TestAccContainerNodePool_resourceManagerTags(t *testing.T) {
7490
CheckDestroy: testAccCheckContainerClusterDestroyProducer(t),
7591
Steps: []resource.TestStep{
7692
{
77-
Config: testAccContainerNodePool_resourceManagerTags(pid, clusterName, networkName, subnetworkName, randomSuffix),
93+
Config: testAccContainerNodePool_resourceManagerTags(context),
7894
Check: resource.ComposeTestCheckFunc(
7995
resource.TestCheckResourceAttrSet("google_container_node_pool.primary_nodes", "node_config.0.resource_manager_tags.%"),
8096
),
@@ -86,7 +102,7 @@ func TestAccContainerNodePool_resourceManagerTags(t *testing.T) {
86102
ImportStateVerifyIgnore: []string{"min_master_version", "cluster"},
87103
},
88104
{
89-
Config: testAccContainerNodePool_resourceManagerTagsUpdate1(pid, clusterName, networkName, subnetworkName, randomSuffix),
105+
Config: testAccContainerNodePool_resourceManagerTagsUpdate1(context),
90106
Check: resource.ComposeTestCheckFunc(
91107
resource.TestCheckResourceAttrSet("google_container_node_pool.primary_nodes", "node_config.0.resource_manager_tags.%"),
92108
),
@@ -98,7 +114,7 @@ func TestAccContainerNodePool_resourceManagerTags(t *testing.T) {
98114
ImportStateVerifyIgnore: []string{"min_master_version", "cluster"},
99115
},
100116
{
101-
Config: testAccContainerNodePool_resourceManagerTagsUpdate2(pid, clusterName, networkName, subnetworkName, randomSuffix),
117+
Config: testAccContainerNodePool_resourceManagerTagsUpdate2(context),
102118
},
103119
{
104120
ResourceName: "google_container_node_pool.primary_nodes",
@@ -4653,52 +4669,18 @@ resource "google_container_node_pool" "without_confidential_boot_disk" {
46534669
`, cluster, networkName, subnetworkName, np)
46544670
}
46554671

4656-
func testAccContainerNodePool_resourceManagerTags(projectID, clusterName, networkName, subnetworkName, randomSuffix string) string {
4657-
return fmt.Sprintf(`
4672+
func testAccContainerNodePool_resourceManagerTags(context map[string]interface{}) string {
4673+
return acctest.Nprintf(`
46584674
data "google_project" "project" {
4659-
project_id = "%[1]s"
4660-
}
4661-
4662-
resource "google_tags_tag_key" "key1" {
4663-
parent = "projects/%[1]s"
4664-
short_name = "foobarbaz1-%[2]s"
4665-
description = "For foo/bar1 resources"
4666-
purpose = "GCE_FIREWALL"
4667-
purpose_data = {
4668-
network = "%[1]s/%[4]s"
4669-
}
4670-
}
4671-
4672-
resource "google_tags_tag_value" "value1" {
4673-
parent = google_tags_tag_key.key1.id
4674-
short_name = "foo1-%[2]s"
4675-
description = "For foo1 resources"
4676-
}
4677-
4678-
resource "google_tags_tag_key" "key2" {
4679-
parent = "projects/%[1]s"
4680-
short_name = "foobarbaz2-%[2]s"
4681-
description = "For foo/bar2 resources"
4682-
purpose = "GCE_FIREWALL"
4683-
purpose_data = {
4684-
network = "%[1]s/%[4]s"
4685-
}
4686-
4687-
depends_on = [google_tags_tag_key.key1]
4688-
}
4689-
4690-
resource "google_tags_tag_value" "value2" {
4691-
parent = google_tags_tag_key.key2.id
4692-
short_name = "foo2-%[2]s"
4693-
description = "For foo2 resources"
4675+
project_id = "%{pid}"
46944676
}
46954677
46964678
data "google_container_engine_versions" "uscentral1a" {
46974679
location = "us-central1-a"
46984680
}
46994681
47004682
resource "google_container_cluster" "primary" {
4701-
name = "%[3]s"
4683+
name = "tf-test-cluster-%{random_suffix}"
47024684
location = "us-central1-a"
47034685
min_master_version = data.google_container_engine_versions.uscentral1a.release_channel_latest_version["STABLE"]
47044686
@@ -4709,8 +4691,8 @@ resource "google_container_cluster" "primary" {
47094691
initial_node_count = 1
47104692
47114693
deletion_protection = false
4712-
network = "%[4]s"
4713-
subnetwork = "%[5]s"
4694+
network = "%{network}"
4695+
subnetwork = "%{subnet}"
47144696
47154697
timeouts {
47164698
create = "30m"
@@ -4732,59 +4714,25 @@ resource "google_container_node_pool" "primary_nodes" {
47324714
disk_size_gb = 15
47334715
47344716
resource_manager_tags = {
4735-
(google_tags_tag_key.key1.id) = google_tags_tag_value.value1.id
4717+
"%{pid}/%{tagKey1}" = "%{tagValue1}"
47364718
}
47374719
}
47384720
}
4739-
`, projectID, randomSuffix, clusterName, networkName, subnetworkName)
4721+
`, context)
47404722
}
47414723

4742-
func testAccContainerNodePool_resourceManagerTagsUpdate1(projectID, clusterName, networkName, subnetworkName, randomSuffix string) string {
4743-
return fmt.Sprintf(`
4724+
func testAccContainerNodePool_resourceManagerTagsUpdate1(context map[string]interface{}) string {
4725+
return acctest.Nprintf(`
47444726
data "google_project" "project" {
4745-
project_id = "%[1]s"
4746-
}
4747-
4748-
resource "google_tags_tag_key" "key1" {
4749-
parent = "projects/%[1]s"
4750-
short_name = "foobarbaz1-%[2]s"
4751-
description = "For foo/bar1 resources"
4752-
purpose = "GCE_FIREWALL"
4753-
purpose_data = {
4754-
network = "%[1]s/%[4]s"
4755-
}
4756-
}
4757-
4758-
resource "google_tags_tag_value" "value1" {
4759-
parent = google_tags_tag_key.key1.id
4760-
short_name = "foo1-%[2]s"
4761-
description = "For foo1 resources"
4762-
}
4763-
4764-
resource "google_tags_tag_key" "key2" {
4765-
parent = "projects/%[1]s"
4766-
short_name = "foobarbaz2-%[2]s"
4767-
description = "For foo/bar2 resources"
4768-
purpose = "GCE_FIREWALL"
4769-
purpose_data = {
4770-
network = "%[1]s/%[4]s"
4771-
}
4772-
4773-
depends_on = [google_tags_tag_key.key1]
4774-
}
4775-
4776-
resource "google_tags_tag_value" "value2" {
4777-
parent = google_tags_tag_key.key2.id
4778-
short_name = "foo2-%[2]s"
4779-
description = "For foo2 resources"
4727+
project_id = "%{pid}"
47804728
}
47814729
47824730
data "google_container_engine_versions" "uscentral1a" {
47834731
location = "us-central1-a"
47844732
}
47854733
47864734
resource "google_container_cluster" "primary" {
4787-
name = "%[3]s"
4735+
name = "tf-test-cluster-%{random_suffix}"
47884736
location = "us-central1-a"
47894737
min_master_version = data.google_container_engine_versions.uscentral1a.release_channel_latest_version["STABLE"]
47904738
@@ -4795,8 +4743,8 @@ resource "google_container_cluster" "primary" {
47954743
initial_node_count = 1
47964744
47974745
deletion_protection = false
4798-
network = "%[4]s"
4799-
subnetwork = "%[5]s"
4746+
network = "%{network}"
4747+
subnetwork = "%{subnet}"
48004748
48014749
timeouts {
48024750
create = "30m"
@@ -4818,60 +4766,26 @@ resource "google_container_node_pool" "primary_nodes" {
48184766
disk_size_gb = 15
48194767
48204768
resource_manager_tags = {
4821-
(google_tags_tag_key.key1.id) = google_tags_tag_value.value1.id
4822-
(google_tags_tag_key.key2.id) = google_tags_tag_value.value2.id
4769+
"%{pid}/%{tagKey1}" = "%{tagValue1}"
4770+
"%{pid}/%{tagKey2}" = "%{tagValue2}"
48234771
}
48244772
}
48254773
}
4826-
`, projectID, randomSuffix, clusterName, networkName, subnetworkName)
4774+
`, context)
48274775
}
48284776

4829-
func testAccContainerNodePool_resourceManagerTagsUpdate2(projectID, clusterName, networkName, subnetworkName, randomSuffix string) string {
4830-
return fmt.Sprintf(`
4777+
func testAccContainerNodePool_resourceManagerTagsUpdate2(context map[string]interface{}) string {
4778+
return acctest.Nprintf(`
48314779
data "google_project" "project" {
4832-
project_id = "%[1]s"
4833-
}
4834-
4835-
resource "google_tags_tag_key" "key1" {
4836-
parent = "projects/%[1]s"
4837-
short_name = "foobarbaz1-%[2]s"
4838-
description = "For foo/bar1 resources"
4839-
purpose = "GCE_FIREWALL"
4840-
purpose_data = {
4841-
network = "%[1]s/%[4]s"
4842-
}
4843-
}
4844-
4845-
resource "google_tags_tag_value" "value1" {
4846-
parent = google_tags_tag_key.key1.id
4847-
short_name = "foo1-%[2]s"
4848-
description = "For foo1 resources"
4849-
}
4850-
4851-
resource "google_tags_tag_key" "key2" {
4852-
parent = "projects/%[1]s"
4853-
short_name = "foobarbaz2-%[2]s"
4854-
description = "For foo/bar2 resources"
4855-
purpose = "GCE_FIREWALL"
4856-
purpose_data = {
4857-
network = "%[1]s/%[4]s"
4858-
}
4859-
4860-
depends_on = [google_tags_tag_key.key1]
4861-
}
4862-
4863-
resource "google_tags_tag_value" "value2" {
4864-
parent = google_tags_tag_key.key2.id
4865-
short_name = "foo2-%[2]s"
4866-
description = "For foo2 resources"
4780+
project_id = "%{pid}"
48674781
}
48684782
48694783
data "google_container_engine_versions" "uscentral1a" {
48704784
location = "us-central1-a"
48714785
}
48724786
48734787
resource "google_container_cluster" "primary" {
4874-
name = "%[3]s"
4788+
name = "tf-test-cluster-%{random_suffix}"
48754789
location = "us-central1-a"
48764790
min_master_version = data.google_container_engine_versions.uscentral1a.release_channel_latest_version["STABLE"]
48774791
@@ -4882,8 +4796,8 @@ resource "google_container_cluster" "primary" {
48824796
initial_node_count = 1
48834797
48844798
deletion_protection = false
4885-
network = "%[4]s"
4886-
subnetwork = "%[5]s"
4799+
network = "%{network}"
4800+
subnetwork = "%{subnet}"
48874801
48884802
timeouts {
48894803
create = "30m"
@@ -4905,7 +4819,7 @@ resource "google_container_node_pool" "primary_nodes" {
49054819
disk_size_gb = 15
49064820
}
49074821
}
4908-
`, projectID, randomSuffix, clusterName, networkName, subnetworkName)
4822+
`, context)
49094823
}
49104824

49114825
func TestAccContainerNodePool_privateRegistry(t *testing.T) {

google/services/datafusion/resource_data_fusion_instance_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,11 @@ resource "google_data_fusion_instance" "foobar" {
173173
func TestAccDatafusionInstance_tags(t *testing.T) {
174174
t.Parallel()
175175

176-
tagKey := acctest.BootstrapSharedTestTagKey(t, "datafusion-instances-tagkey")
176+
tagKey := acctest.BootstrapSharedTestOrganizationTagKey(t, "datafusion-instances-tagkey", nil)
177177
context := map[string]interface{}{
178178
"org": envvar.GetTestOrgFromEnv(t),
179179
"tagKey": tagKey,
180-
"tagValue": acctest.BootstrapSharedTestTagValue(t, "datafusion-instances-tagvalue", tagKey),
180+
"tagValue": acctest.BootstrapSharedTestOrganizationTagValue(t, "datafusion-instances-tagvalue", tagKey),
181181
"random_suffix": acctest.RandString(t, 10),
182182
}
183183

google/services/filestore/resource_filestore_backup_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,11 @@ resource "google_filestore_backup" "backup" {
134134
func TestAccFilestoreBackup_tags(t *testing.T) {
135135
t.Parallel()
136136

137-
tagKey := acctest.BootstrapSharedTestTagKey(t, "filestore-backups-tagkey")
137+
tagKey := acctest.BootstrapSharedTestOrganizationTagKey(t, "filestore-backups-tagkey", nil)
138138
context := map[string]interface{}{
139139
"org": envvar.GetTestOrgFromEnv(t),
140140
"tagKey": tagKey,
141-
"tagValue": acctest.BootstrapSharedTestTagValue(t, "filestore-backups-tagvalue", tagKey),
141+
"tagValue": acctest.BootstrapSharedTestOrganizationTagValue(t, "filestore-backups-tagvalue", tagKey),
142142
"random_suffix": acctest.RandString(t, 10),
143143
}
144144

0 commit comments

Comments
 (0)