Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: tag identifiers problems #2534

Merged
merged 9 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions MIGRATION_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ This document is meant to help you migrate your Terraform config to the new newe
describe deprecations or breaking changes and help you to change your configuration to keep the same (or similar) behavior
across different versions.

## v0.87.0 ➞ v0.88.0
### snowflake_tag_association resource changes
#### *(behavior change)* `object_name`
No longer deprecated.
#### *(behavior change)* `object_identifier`
No longer required.
sfc-gh-jcieslak marked this conversation as resolved.
Show resolved Hide resolved

## v0.86.0 ➞ v0.87.0
### snowflake_failover_group resource changes
#### *(bug fix)* ACCOUNT PARAMETERS is returned as PARAMETERS from SHOW FAILOVER GROUPS
Expand Down
4 changes: 2 additions & 2 deletions docs/resources/tag_association.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ resource "snowflake_tag_association" "table_association" {

### Required

- `object_identifier` (Block List, Min: 1) Specifies the object identifier for the tag association. (see [below for nested schema](#nestedblock--object_identifier))
- `object_type` (String) Specifies the type of object to add a tag to. ex: 'ACCOUNT', 'COLUMN', 'DATABASE', etc. For more information: https://docs.snowflake.com/en/user-guide/object-tagging.html#supported-objects
- `tag_id` (String) Specifies the identifier for the tag. Note: format must follow: "databaseName"."schemaName"."tagName" or "databaseName.schemaName.tagName" or "databaseName|schemaName.tagName" (snowflake_tag.tag.id)
- `tag_value` (String) Specifies the value of the tag, (e.g. 'finance' or 'engineering')

### Optional

- `object_name` (String, Deprecated) Specifies the object identifier for the tag association.
- `object_identifier` (Block List) Specifies the object identifier for the tag association. (see [below for nested schema](#nestedblock--object_identifier))
- `object_name` (String) Specifies the object identifier for the tag association.
- `skip_validation` (Boolean) If true, skips validation of the tag association.
- `timeouts` (Block, Optional) (see [below for nested schema](#nestedblock--timeouts))

Expand Down
70 changes: 40 additions & 30 deletions pkg/resources/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"fmt"
"slices"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -119,27 +120,25 @@ func ReadContextTag(ctx context.Context, d *schema.ResourceData, meta interface{
client := sdk.NewClientFromDB(db)

id := helpers.DecodeSnowflakeID(d.Id()).(sdk.SchemaObjectIdentifier)
request := sdk.NewShowTagRequest().WithIn(&sdk.In{Schema: sdk.NewDatabaseObjectIdentifier(id.DatabaseName(), id.SchemaName())}).WithLike(id.Name())
tags, err := client.Tags.Show(ctx, request)

tag, err := client.Tags.ShowByID(ctx, id)
if err != nil {
return diag.FromErr(err)
}
for _, item := range tags {
if err := d.Set("name", item.Name); err != nil {
return diag.FromErr(err)
}
if err := d.Set("database", item.DatabaseName); err != nil {
return diag.FromErr(err)
}
if err := d.Set("schema", item.SchemaName); err != nil {
return diag.FromErr(err)
}
if err := d.Set("comment", item.Comment); err != nil {
return diag.FromErr(err)
}
if err := d.Set("allowed_values", item.AllowedValues); err != nil {
return diag.FromErr(err)
}
if err := d.Set("name", tag.Name); err != nil {
return diag.FromErr(err)
}
if err := d.Set("database", tag.DatabaseName); err != nil {
return diag.FromErr(err)
}
if err := d.Set("schema", tag.SchemaName); err != nil {
return diag.FromErr(err)
}
if err := d.Set("comment", tag.Comment); err != nil {
return diag.FromErr(err)
}
if err := d.Set("allowed_values", tag.AllowedValues); err != nil {
return diag.FromErr(err)
}
return diags
}
Expand All @@ -164,21 +163,32 @@ func UpdateContextTag(ctx context.Context, d *schema.ResourceData, meta interfac
}
}
if d.HasChange("allowed_values") {
v, ok := d.GetOk("allowed_values")
if ok {
allowedValues := expandAllowedValues(v)
// unset the allowed values
unset := sdk.NewTagUnsetRequest().WithAllowedValues(true)
if err := client.Tags.Alter(ctx, sdk.NewAlterTagRequest(id).WithUnset(unset)); err != nil {
return diag.FromErr(err)
old, new := d.GetChange("allowed_values")
oldAllowedValues := expandStringList(old.([]interface{}))
newAllowedValues := expandStringList(new.([]interface{}))
var allowedValuesToAdd, allowedValuesToRemove []string

for _, oldAllowedValue := range oldAllowedValues {
if !slices.Contains(newAllowedValues, oldAllowedValue) {
allowedValuesToRemove = append(allowedValuesToRemove, oldAllowedValue)
}
}

for _, newAllowedValue := range newAllowedValues {
if !slices.Contains(oldAllowedValues, newAllowedValue) {
allowedValuesToAdd = append(allowedValuesToAdd, newAllowedValue)
}
// add the allowed values
if err := client.Tags.Alter(ctx, sdk.NewAlterTagRequest(id).WithAdd(allowedValues)); err != nil {
}

if len(allowedValuesToAdd) > 0 {
if err := client.Tags.Alter(ctx, sdk.NewAlterTagRequest(id).WithAdd(allowedValuesToAdd)); err != nil {
return diag.FromErr(err)
}
} else {
unset := sdk.NewTagUnsetRequest().WithAllowedValues(true)
if err := client.Tags.Alter(ctx, sdk.NewAlterTagRequest(id).WithUnset(unset)); err != nil {
}

if len(allowedValuesToRemove) > 0 {
req := sdk.NewAlterTagRequest(id).WithUnset(sdk.NewTagUnsetRequest().WithAllowedValues(true))
if err := client.Tags.Alter(ctx, req); err != nil {
return diag.FromErr(err)
}
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/resources/tag_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ func TestAcc_Tag(t *testing.T) {
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.RequireAbove(tfversion.Version1_5_0),
},
CheckDestroy: testAccCheckFunctionDestroy,
// todo: implement CheckDestroy (SNOW-1165865)
CheckDestroy: nil,
Steps: []resource.TestStep{
{
ConfigDirectory: acc.ConfigurationDirectory("TestAcc_Tag/basic"),
Expand All @@ -41,6 +42,7 @@ func TestAcc_Tag(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "schema", acc.TestSchemaName),
resource.TestCheckResourceAttr(resourceName, "allowed_values.#", "2"),
resource.TestCheckResourceAttr(resourceName, "allowed_values.0", "alv1"),
sfc-gh-jcieslak marked this conversation as resolved.
Show resolved Hide resolved
resource.TestCheckResourceAttr(resourceName, "allowed_values.1", "alv2"),
resource.TestCheckResourceAttr(resourceName, "comment", "Terraform acceptance test"),
),
},
Expand Down
3 changes: 1 addition & 2 deletions pkg/resources/tag_association.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@ var tagAssociationSchema = map[string]*schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "Specifies the object identifier for the tag association.",
Deprecated: "Use `object_identifier` instead",
ForceNew: true,
},
"object_identifier": {
Type: schema.TypeList,
Required: true,
Optional: true,
MinItems: 1,
Description: "Specifies the object identifier for the tag association.",
Elem: &schema.Resource{
Expand Down
Loading
Loading