Skip to content

Commit e798959

Browse files
committed
add tags
1 parent 9905a08 commit e798959

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

mmv1/third_party/terraform/services/resourcemanager/resource_google_project.go

+12
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,14 @@ func ResourceGoogleProject() *schema.Resource {
133133
Description: `All of labels (key/value pairs) present on the resource in GCP, including the labels configured through Terraform, other clients and services.`,
134134
Elem: &schema.Schema{Type: schema.TypeString},
135135
},
136+
137+
"tags": {
138+
Type: schema.TypeMap,
139+
Optional: true,
140+
ForceNew: true,
141+
Elem: &schema.Schema{Type: schema.TypeString},
142+
Description: `A map of resource manager tags. Resource manager tag keys and values have the same definition as resource manager tags. Keys must be in the format tagKeys/{tag_key_id}, and values are in the format tagValues/456. The field is ignored when empty.`,
143+
},
136144
},
137145
UseJSONNumber: true,
138146
}
@@ -166,6 +174,10 @@ func resourceGoogleProjectCreate(d *schema.ResourceData, meta interface{}) error
166174
project.Labels = tpgresource.ExpandEffectiveLabels(d)
167175
}
168176

177+
if _, ok := d.GetOk("tags"); ok {
178+
project.Tags = tpgresource.ExpandStringMap(d, "tags")
179+
}
180+
169181
var op *cloudresourcemanager.Operation
170182
err = transport_tpg.Retry(transport_tpg.RetryOptions{
171183
RetryFunc: func() (reqErr error) {

mmv1/third_party/terraform/services/resourcemanager/resource_google_project_test.go

+70
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,41 @@ func TestAccProject_migrateParent(t *testing.T) {
234234
})
235235
}
236236

237+
// Test that a Project resource can be created with tags
238+
func TestAccProject_tags(t *testing.T) {
239+
t.Parallel()
240+
241+
org := envvar.GetTestOrgFromEnv(t)
242+
pid := fmt.Sprintf("%s-%d", TestPrefix, acctest.RandInt(t))
243+
acctest.VcrTest(t, resource.TestCase{
244+
PreCheck: func() { acctest.AccTestPreCheck(t) },
245+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
246+
Steps: []resource.TestStep{
247+
{
248+
Config: testAccProject_tags(pid, org, map[string]string{org + "/env": "test"}),
249+
Check: resource.ComposeTestCheckFunc(
250+
testAccCheckGoogleProjectExists("google_project.acceptance", pid),
251+
),
252+
},
253+
// Make sure import supports tags
254+
{
255+
ResourceName: "google_project.acceptance",
256+
ImportState: true,
257+
ImportStateVerify: true,
258+
ImportStateVerifyIgnore: []string{"tags", "deletion_policy"}, // we don't read tags back
259+
},
260+
// Update tags tries to replace project but fails due to deletion protection
261+
{
262+
Config: testAccProject_tags(pid, org, map[string]string{org + "/env": "staging"}),
263+
ExpectError: regexp.MustCompile("deletion_policy"),
264+
},
265+
{
266+
Config: testAccProject_tagsAllowDestroy(pid, org, map[string]string{org + "/env": "test"}),
267+
},
268+
},
269+
})
270+
}
271+
237272
func testAccCheckGoogleProjectExists(r, pid string) resource.TestCheckFunc {
238273
return func(s *terraform.State) error {
239274
rs, ok := s.RootModule().Resources[r]
@@ -549,3 +584,38 @@ resource "google_folder" "folder1" {
549584
}
550585
`, pid, pid, org, folderName, org)
551586
}
587+
588+
func testAccProject_tags(pid, org string, tags map[string]string) string {
589+
r := fmt.Sprintf(`
590+
resource "google_project" "acceptance" {
591+
project_id = "%s"
592+
name = "%s"
593+
org_id = "%s"
594+
tags = {`, pid, pid, org)
595+
596+
l := ""
597+
for key, value := range tags {
598+
l += fmt.Sprintf("%q = %q\n", key, value)
599+
}
600+
601+
l += fmt.Sprintf("}\n}")
602+
return r + l
603+
}
604+
605+
func testAccProject_tagsAllowDestroy(pid, org string, tags map[string]string) string {
606+
r := fmt.Sprintf(`
607+
resource "google_project" "acceptance" {
608+
project_id = "%s"
609+
name = "%s"
610+
org_id = "%s"
611+
deletion_policy = "DELETE"
612+
tags = {`, pid, pid, org)
613+
614+
l := ""
615+
for key, value := range tags {
616+
l += fmt.Sprintf("%q = %q\n", key, value)
617+
}
618+
619+
l += fmt.Sprintf("}\n}")
620+
return r + l
621+
}

0 commit comments

Comments
 (0)