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

feat: Resource monitor v1 readiness #3052

Merged
merged 8 commits into from
Sep 11, 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
20 changes: 20 additions & 0 deletions pkg/acceptance/bettertestspoc/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package config
import (
"encoding/json"
"fmt"
"reflect"
"strings"
"testing"

tfconfig "github.com/hashicorp/terraform-plugin-testing/config"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/resources"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -96,6 +99,23 @@ func FromModel(t *testing.T, model ResourceModel) string {
return s
}

func ConfigVariablesFromModel(t *testing.T, model ResourceModel) tfconfig.Variables {
t.Helper()
variables := make(tfconfig.Variables)
rType := reflect.TypeOf(model).Elem()
rValue := reflect.ValueOf(model).Elem()
for i := 0; i < rType.NumField(); i++ {
field := rType.Field(i)
if jsonTag, ok := field.Tag.Lookup("json"); ok {
name := strings.Split(jsonTag, ",")[0]
if fieldValue, ok := rValue.Field(i).Interface().(tfconfig.Variable); ok {
variables[name] = fieldValue
}
}
}
return variables
}

type nullVariable struct{}

// MarshalJSON returns the JSON encoding of nullVariable.
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/acceptance/bettertestspoc/config/model/gen/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func ModelFromResourceSchemaDetails(resourceSchemaDetails genhelpers.ResourceSch
Attributes: attributes,
PreambleModel: PreambleModel{
PackageName: packageWithGenerateDirective,
AdditionalStandardImports: []string{"reflect", "strings"},
AdditionalStandardImports: []string{},
},
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,3 @@ func {{ .Name }}WithDefaultMeta(
{{- end -}}
return {{ $modelVar }}
}

func (r *{{ $modelName }}) ToConfigVariables() tfconfig.Variables {
variables := make(tfconfig.Variables)
rType := reflect.TypeOf(r).Elem()
rValue := reflect.ValueOf(r).Elem()
for i := 0; i < rType.NumField(); i++ {
field := rType.Field(i)
if jsonTag, ok := field.Tag.Lookup("json"); ok {
name := strings.Split(jsonTag, ",")[0]
if fieldValue, ok := rValue.Field(i).Interface().(tfconfig.Variable); ok {
variables[name] = fieldValue
}
}
}
return variables
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 0 additions & 19 deletions pkg/acceptance/bettertestspoc/config/model/user_model_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 0 additions & 19 deletions pkg/acceptance/bettertestspoc/config/model/view_model_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 0 additions & 19 deletions pkg/acceptance/bettertestspoc/config/model/warehouse_model_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions pkg/acceptance/helpers/resource_monitor_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ func (c *ResourceMonitorClient) CreateResourceMonitorWithOptions(t *testing.T, o
return resourceMonitor, c.DropResourceMonitorFunc(t, id)
}

func (c *ResourceMonitorClient) Alter(t *testing.T, id sdk.AccountObjectIdentifier, opts *sdk.AlterResourceMonitorOptions) {
t.Helper()
ctx := context.Background()
err := c.client().Alter(ctx, id, opts)
require.NoError(t, err)
}

func (c *ResourceMonitorClient) DropResourceMonitorFunc(t *testing.T, id sdk.AccountObjectIdentifier) func() {
t.Helper()
ctx := context.Background()
Expand Down
9 changes: 5 additions & 4 deletions pkg/internal/genhelpers/struct_details_extractor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
//
// TODO [SNOW-1501905]: test type of slice fields
func Test_ExtractStructDetails(t *testing.T) {
type testIntEnum int
type testStruct struct {
unexportedString string
unexportedInt int
Expand All @@ -31,8 +32,8 @@ func Test_ExtractStructDetails(t *testing.T) {

unexportedStringEnum sdk.WarehouseType
unexportedStringEnumPtr *sdk.WarehouseType
unexportedIntEnum sdk.ResourceMonitorLevel
unexportedIntEnumPtr *sdk.ResourceMonitorLevel
unexportedIntEnum testIntEnum
unexportedIntEnumPtr *testIntEnum

unexportedAccountIdentifier sdk.AccountIdentifier
unexportedExternalObjectIdentifier sdk.ExternalObjectIdentifier
Expand Down Expand Up @@ -90,8 +91,8 @@ func Test_ExtractStructDetails(t *testing.T) {

assertFieldExtracted(structDetails.Fields[10], "unexportedStringEnum", "sdk.WarehouseType", "string")
assertFieldExtracted(structDetails.Fields[11], "unexportedStringEnumPtr", "*sdk.WarehouseType", "*string")
assertFieldExtracted(structDetails.Fields[12], "unexportedIntEnum", "sdk.ResourceMonitorLevel", "int")
assertFieldExtracted(structDetails.Fields[13], "unexportedIntEnumPtr", "*sdk.ResourceMonitorLevel", "*int")
assertFieldExtracted(structDetails.Fields[12], "unexportedIntEnum", "genhelpers.testIntEnum", "int")
assertFieldExtracted(structDetails.Fields[13], "unexportedIntEnumPtr", "*genhelpers.testIntEnum", "*int")

assertFieldExtracted(structDetails.Fields[14], "unexportedAccountIdentifier", "sdk.AccountIdentifier", "struct")
assertFieldExtracted(structDetails.Fields[15], "unexportedExternalObjectIdentifier", "sdk.ExternalObjectIdentifier", "struct")
Expand Down
30 changes: 9 additions & 21 deletions pkg/resources/resource_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,14 @@ var resourceMonitorSchema = map[string]*schema.Schema{
Optional: true,
Description: "Represents a numeric value specified as a percentage of the credit quota. Values over 100 are supported. After reaching this value, all assigned warehouses while allowing currently running queries to complete will be suspended. No new queries can be executed by the warehouses until the credit quota for the resource monitor is increased. In addition, this action sends a notification to all users who have enabled notifications for themselves.",
ValidateDiagFunc: validation.ToDiagFunc(validation.IntAtLeast(1)),
DiffSuppressFunc: IgnoreChangeToCurrentSnowflakeValueInShow("suspend_at"),
},
"suspend_immediate_trigger": {
Type: schema.TypeInt,
Optional: true,
Description: "Represents a numeric value specified as a percentage of the credit quota. Values over 100 are supported. After reaching this value, all assigned warehouses immediately cancel any currently running queries or statements. In addition, this action sends a notification to all users who have enabled notifications for themselves.",
ValidateDiagFunc: validation.ToDiagFunc(validation.IntAtLeast(1)),
DiffSuppressFunc: IgnoreChangeToCurrentSnowflakeValueInShow("suspend_immediately_at"),
},
ShowOutputAttributeName: {
Type: schema.TypeList,
Expand Down Expand Up @@ -306,25 +308,20 @@ func UpdateResourceMonitor(ctx context.Context, d *schema.ResourceData, meta any
return diag.FromErr(err)
}

var runSetStatement bool
var runUnsetStatement bool
opts := sdk.AlterResourceMonitorOptions{}
set := sdk.ResourceMonitorSet{}
unset := sdk.ResourceMonitorUnset{}

if d.HasChange("credit_quota") {
runSetStatement = true
if v, ok := d.GetOk("credit_quota"); ok {
set.CreditQuota = sdk.Pointer(v.(int))
} else {
runUnsetStatement = true
unset.CreditQuota = sdk.Bool(true)
}
}

if (d.HasChange("frequency") || d.HasChange("start_timestamp")) &&
(d.Get("frequency").(string) != "" && d.Get("start_timestamp").(string) != "") {
runSetStatement = true
frequency, err := sdk.ToResourceMonitorFrequency(d.Get("frequency").(string))
if err != nil {
return diag.FromErr(err)
Expand All @@ -335,18 +332,15 @@ func UpdateResourceMonitor(ctx context.Context, d *schema.ResourceData, meta any

if d.HasChange("end_timestamp") {
if v, ok := d.GetOk("end_timestamp"); ok {
runSetStatement = true
set.EndTimestamp = sdk.Pointer(v.(string))
} else {
runUnsetStatement = true
unset.EndTimestamp = sdk.Bool(true)
}
}

if d.HasChange("notify_users") {
userIds := expandStringList(d.Get("notify_users").(*schema.Set).List())
if len(userIds) > 0 {
runSetStatement = true
users := make([]sdk.NotifiedUser, len(userIds))
for i, userId := range userIds {
users[i] = sdk.NotifiedUser{
Expand All @@ -357,7 +351,6 @@ func UpdateResourceMonitor(ctx context.Context, d *schema.ResourceData, meta any
Users: users,
}
} else {
runUnsetStatement = true
unset.NotifyUsers = sdk.Bool(true)
}
}
Expand Down Expand Up @@ -403,31 +396,26 @@ func UpdateResourceMonitor(ctx context.Context, d *schema.ResourceData, meta any

// This is to prevent SQL compilation errors from Snowflake, because you cannot only alter triggers.
// It's going to set credit quota to the same value as before making it pass SQL compilation stage.
if len(opts.Triggers) > 0 && !runSetStatement && !runUnsetStatement {
if len(opts.Triggers) > 0 && (set == (sdk.ResourceMonitorSet{})) && (unset == (sdk.ResourceMonitorUnset{})) {
if creditQuota, ok := d.GetOk("credit_quota"); ok {
runSetStatement = true
set.CreditQuota = sdk.Pointer(creditQuota.(int))
} else {
runUnsetStatement = true
unset.CreditQuota = sdk.Bool(true)
}
}

if runSetStatement {
if set != (sdk.ResourceMonitorSet{}) {
opts.Set = &set
}

if set != (sdk.ResourceMonitorSet{}) {
opts.Set = &set
if err := client.ResourceMonitors.Alter(ctx, id, &opts); err != nil {
sfc-gh-asawicki marked this conversation as resolved.
Show resolved Hide resolved
d.Partial(true)
return diag.FromErr(err)
}
}

if runUnsetStatement {
if unset != (sdk.ResourceMonitorUnset{}) {
opts.Unset = &unset
}
if unset != (sdk.ResourceMonitorUnset{}) {
opts.Unset = &unset
if err := client.ResourceMonitors.Alter(ctx, id, &opts); err != nil {
d.Partial(true)
return diag.FromErr(err)
}
}
Expand Down
Loading
Loading