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 all commits
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
4 changes: 3 additions & 1 deletion pkg/resources/helper_expansion.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package resources

import "slices"
import (
"slices"
)

// borrowed from https://github.com/terraform-providers/terraform-provider-aws/blob/master/aws/structure.go#L924:6

Expand Down
92 changes: 92 additions & 0 deletions pkg/resources/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/snowflake"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

Expand Down Expand Up @@ -128,3 +129,94 @@ func GetPropertyAsPointer[T any](d *schema.ResourceData, property string) *T {
}
return &typedValue
}

type tags []tag

func (t tags) toSnowflakeTagValues() []snowflake.TagValue {
sT := make([]snowflake.TagValue, len(t))
for i, tag := range t {
sT[i] = tag.toSnowflakeTagValue()
}
return sT
}

func (t tag) toSnowflakeTagValue() snowflake.TagValue {
return snowflake.TagValue{
Name: t.name,
Value: t.value,
Database: t.database,
Schema: t.schema,
}
}

func (t tags) getNewIn(new tags) (added tags) {
added = tags{}
for _, t0 := range t {
found := false
for _, cN := range new {
if t0.name == cN.name {
found = true
break
}
}
if !found {
added = append(added, t0)
}
}
return
}

func (t tags) getChangedTagProperties(new tags) (changed tags) {
changed = tags{}
for _, t0 := range t {
for _, tN := range new {
if t0.name == tN.name && t0.value != tN.value {
changed = append(changed, tN)
}
}
}
return
}

func (t tags) diffs(new tags) (removed tags, added tags, changed tags) {
return t.getNewIn(new), new.getNewIn(t), t.getChangedTagProperties(new)
}

func (t columns) getNewIn(new columns) (added columns) {
added = columns{}
for _, cO := range t {
found := false
for _, cN := range new {
if cO.name == cN.name {
found = true
break
}
}
if !found {
added = append(added, cO)
}
}
return
}

type tag struct {
name string
value string
database string
schema string
}

func getTags(from interface{}) (to tags) {
tags := from.([]interface{})
to = make([]tag, len(tags))
for i, t := range tags {
v := t.(map[string]interface{})
to[i] = tag{
name: v["name"].(string),
value: v["value"].(string),
database: v["database"].(string),
schema: v["schema"].(string),
}
}
return to
}
Loading
Loading