diff --git a/.changelog/12442.txt b/.changelog/12442.txt new file mode 100644 index 00000000000..c90e4ce1d5e --- /dev/null +++ b/.changelog/12442.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +filestore: added `tags` field to `filstore_backup` to allow setting tags for backups at creation time +``` \ No newline at end of file diff --git a/google/services/filestore/resource_filestore_backup.go b/google/services/filestore/resource_filestore_backup.go index 8272d88cd5e..054c99da08f 100644 --- a/google/services/filestore/resource_filestore_backup.go +++ b/google/services/filestore/resource_filestore_backup.go @@ -100,6 +100,16 @@ character, which cannot be a dash.`, Please refer to the field 'effective_labels' for all of the labels present on the resource.`, Elem: &schema.Schema{Type: schema.TypeString}, }, + "tags": { + Type: schema.TypeMap, + Optional: true, + ForceNew: true, + 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/{tag_value_id}. +The field is ignored (both PUT & PATCH) when empty.`, + Elem: &schema.Schema{Type: schema.TypeString}, + }, "capacity_gb": { Type: schema.TypeString, Computed: true, @@ -185,6 +195,12 @@ func resourceFilestoreBackupCreate(d *schema.ResourceData, meta interface{}) err } else if v, ok := d.GetOkExists("source_file_share"); !tpgresource.IsEmptyValue(reflect.ValueOf(sourceFileShareProp)) && (ok || !reflect.DeepEqual(v, sourceFileShareProp)) { obj["sourceFileShare"] = sourceFileShareProp } + tagsProp, err := expandFilestoreBackupTags(d.Get("tags"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("tags"); !tpgresource.IsEmptyValue(reflect.ValueOf(tagsProp)) && (ok || !reflect.DeepEqual(v, tagsProp)) { + obj["tags"] = tagsProp + } labelsProp, err := expandFilestoreBackupEffectiveLabels(d.Get("effective_labels"), d, config) if err != nil { return err @@ -628,6 +644,17 @@ func expandFilestoreBackupSourceFileShare(v interface{}, d tpgresource.Terraform return v, nil } +func expandFilestoreBackupTags(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (map[string]string, error) { + if v == nil { + return map[string]string{}, nil + } + m := make(map[string]string) + for k, val := range v.(map[string]interface{}) { + m[k] = val.(string) + } + return m, nil +} + func expandFilestoreBackupEffectiveLabels(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (map[string]string, error) { if v == nil { return map[string]string{}, nil diff --git a/google/services/filestore/resource_filestore_backup_generated_test.go b/google/services/filestore/resource_filestore_backup_generated_test.go index 445d8375c2b..a80331846df 100644 --- a/google/services/filestore/resource_filestore_backup_generated_test.go +++ b/google/services/filestore/resource_filestore_backup_generated_test.go @@ -49,7 +49,7 @@ func TestAccFilestoreBackup_filestoreBackupBasicExample(t *testing.T) { ResourceName: "google_filestore_backup.backup", ImportState: true, ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"labels", "location", "name", "terraform_labels"}, + ImportStateVerifyIgnore: []string{"labels", "location", "name", "tags", "terraform_labels"}, }, }, }) diff --git a/google/services/filestore/resource_filestore_backup_test.go b/google/services/filestore/resource_filestore_backup_test.go index 3ba6250409b..74982269132 100644 --- a/google/services/filestore/resource_filestore_backup_test.go +++ b/google/services/filestore/resource_filestore_backup_test.go @@ -8,6 +8,7 @@ import ( "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-provider-google/google/acctest" + "github.com/hashicorp/terraform-provider-google/google/envvar" ) func TestAccFilestoreBackup_update(t *testing.T) { @@ -115,3 +116,72 @@ resource "google_filestore_backup" "backup" { `, instName, bkupName) } + +func TestAccFilestoreBackup_tags(t *testing.T) { + t.Parallel() + + org := envvar.GetTestOrgFromEnv(t) + instanceName := fmt.Sprintf("tf-fs-inst-%d", acctest.RandInt(t)) + backupName := fmt.Sprintf("tf-fs-bkup-%d", acctest.RandInt(t)) + tagKey := acctest.BootstrapSharedTestTagKey(t, "filestore-backups-tagkey") + tagValue := acctest.BootstrapSharedTestTagValue(t, "filestore-backups-tagvalue", tagKey) + + acctest.VcrTest(t, resource.TestCase{ + PreCheck: func() { acctest.AccTestPreCheck(t) }, + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), + CheckDestroy: testAccCheckFilestoreBackupDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testAccFilestoreBackupTags(instanceName, backupName, map[string]string{org + "/" + tagKey: tagValue}), + }, + { + ResourceName: "google_filestore_backup.backup", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"labels", "terraform_labels", "description", "location", "tags"}, + }, + }, + }) +} + +func testAccFilestoreBackupTags(instanceName string, backupName string, tags map[string]string) string { + + r := fmt.Sprintf(` + resource "google_filestore_instance" "instance" { + name = "%s" + location = "us-central1-b" + tier = "BASIC_HDD" + + file_shares { + capacity_gb = 1024 + name = "share1" + } + + networks { + network = "default" + modes = ["MODE_IPV4"] + connect_mode = "DIRECT_PEERING" + } + } + + resource "google_filestore_backup" "backup" { + name = "%s" + location = "us-central1" + description = "This is a filestore backup for the test instance" + source_instance = google_filestore_instance.instance.id + source_file_share = "share1" + + labels = { + "files":"label1", + "other-label": "label2" + } + tags = {`, instanceName, backupName) + + l := "" + for key, value := range tags { + l += fmt.Sprintf("%q = %q\n", key, value) + } + + l += fmt.Sprintf("}\n}") + return r + l +} diff --git a/website/docs/r/filestore_backup.html.markdown b/website/docs/r/filestore_backup.html.markdown index 5c437474587..57abdcf3a82 100644 --- a/website/docs/r/filestore_backup.html.markdown +++ b/website/docs/r/filestore_backup.html.markdown @@ -111,6 +111,13 @@ The following arguments are supported: **Note**: This field is non-authoritative, and will only manage the labels present in your configuration. Please refer to the field `effective_labels` for all of the labels present on the resource. +* `tags` - + (Optional) + 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/{tag_value_id}. + The field is ignored (both PUT & PATCH) when empty. + * `project` - (Optional) The ID of the project in which the resource belongs. If it is not provided, the provider project is used.