Skip to content

Commit

Permalink
Add tags field to Filestore Backups for TagsR2401 (#12442) (#20718)
Browse files Browse the repository at this point in the history
[upstream:c8d6edea53f0cfb14c1ea6250cccbda7044759bc]

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Dec 17, 2024
1 parent de2f3e1 commit c6c5589
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .changelog/12442.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
filestore: added `tags` field to `filstore_backup` to allow setting tags for backups at creation time
```
27 changes: 27 additions & 0 deletions google/services/filestore/resource_filestore_backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
},
},
})
Expand Down
70 changes: 70 additions & 0 deletions google/services/filestore/resource_filestore_backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
7 changes: 7 additions & 0 deletions website/docs/r/filestore_backup.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down

0 comments on commit c6c5589

Please sign in to comment.