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

[ISSUE-3258] add warehouse constraint as an optional variable #3260

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
33 changes: 32 additions & 1 deletion pkg/resources/warehouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ var warehouseSchema = map[string]*schema.Schema{
Description: "Specifies the maximum scale factor for leasing compute resources for query acceleration. The scale factor is used as a multiplier based on warehouse size.",
Default: IntDefault,
},
"resource_constraint": {
Type: schema.TypeString,
Optional: true,
ValidateDiagFunc: sdkValidation(sdk.ToResourceConstraint),
DiffSuppressFunc: SuppressIfAny(NormalizeAndCompare(sdk.ToResourceConstraint), IgnoreChangeToCurrentSnowflakeValueInShow("resource_constraint")),
Description: fmt.Sprintf("Specifies memory and architecture type. Valid values are (case-insensitive): %s.", possibleValuesListed(sdk.ValidResourceConstraintString)),

},
strings.ToLower(string(sdk.ObjectParameterMaxConcurrencyLevel)): {
Type: schema.TypeInt,
Optional: true,
Expand Down Expand Up @@ -206,7 +214,7 @@ func Warehouse() *schema.Resource {
},

CustomizeDiff: TrackingCustomDiffWrapper(resources.Warehouse, customdiff.All(
ComputedIfAnyAttributeChanged(warehouseSchema, ShowOutputAttributeName, "name", "warehouse_type", "warehouse_size", "max_cluster_count", "min_cluster_count", "scaling_policy", "auto_suspend", "auto_resume", "resource_monitor", "comment", "enable_query_acceleration", "query_acceleration_max_scale_factor"),
ComputedIfAnyAttributeChanged(warehouseSchema, ShowOutputAttributeName, "name", "warehouse_type", "warehouse_size", "max_cluster_count", "min_cluster_count", "scaling_policy", "auto_suspend", "auto_resume", "resource_monitor", "comment", "enable_query_acceleration", "query_acceleration_max_scale_factor", "resource_constraint"),
ComputedIfAnyAttributeChanged(warehouseSchema, ParametersAttributeName, strings.ToLower(string(sdk.ObjectParameterMaxConcurrencyLevel)), strings.ToLower(string(sdk.ObjectParameterStatementQueuedTimeoutInSeconds)), strings.ToLower(string(sdk.ObjectParameterStatementTimeoutInSeconds))),
ComputedIfAnyAttributeChanged(warehouseSchema, FullyQualifiedNameAttributeName, "name"),

Expand Down Expand Up @@ -279,6 +287,9 @@ func ImportWarehouse(ctx context.Context, d *schema.ResourceData, meta any) ([]*
if err = d.Set("query_acceleration_max_scale_factor", w.QueryAccelerationMaxScaleFactor); err != nil {
return nil, err
}
if err = d.Set("resource_constraint", w.ResourceConstraint); err != nil {
return nil, err
}

return []*schema.ResourceData{d}, nil
}
Expand Down Expand Up @@ -347,6 +358,13 @@ func CreateWarehouse(ctx context.Context, d *schema.ResourceData, meta any) diag
if v := d.Get("query_acceleration_max_scale_factor").(int); v != IntDefault {
createOptions.QueryAccelerationMaxScaleFactor = sdk.Int(v)
}
if v, ok := d.GetOk("resource_constraint"); ok {
resourceConstraint, err := sdk.ToResourceConstraint(v.(string))
if err != nil {
return diag.FromErr(err)
}
createOptions.ResourceConstraint = &resourceconstraint
}
if v := GetConfigPropertyAsPointerAllowingZeroValue[int](d, "max_concurrency_level"); v != nil {
createOptions.MaxConcurrencyLevel = v
}
Expand Down Expand Up @@ -403,6 +421,7 @@ func GetReadWarehouseFunc(withExternalChangesMarking bool) schema.ReadContextFun
outputMapping{"resource_monitor", "resource_monitor", w.ResourceMonitor.Name(), w.ResourceMonitor.Name(), nil},
outputMapping{"enable_query_acceleration", "enable_query_acceleration", w.EnableQueryAcceleration, fmt.Sprintf("%t", w.EnableQueryAcceleration), nil},
outputMapping{"query_acceleration_max_scale_factor", "query_acceleration_max_scale_factor", w.QueryAccelerationMaxScaleFactor, w.QueryAccelerationMaxScaleFactor, nil},
outputMapping{"resource_constraint", "resource_constraint", string(w.ResourceConstraint), w.ResourceConstraint, nil},
); err != nil {
return diag.FromErr(err)
}
Expand All @@ -425,6 +444,7 @@ func GetReadWarehouseFunc(withExternalChangesMarking bool) schema.ReadContextFun
"resource_monitor",
"enable_query_acceleration",
"query_acceleration_max_scale_factor",
"resource_constraint",
}); err != nil {
return diag.FromErr(err)
}
Expand Down Expand Up @@ -586,6 +606,17 @@ func UpdateWarehouse(ctx context.Context, d *schema.ResourceData, meta any) diag
unset.StatementQueuedTimeoutInSeconds = sdk.Bool(true)
}
}
if d.HasChange("resource_constraint") {
if v, ok := d.GetOk("resource_constraint"); ok {
resourceConstraint, err := sdk.ToResourceConstraint(v.(string))
if err != nil {
return diag.FromErr(err)
}
set.ResourceConstraint = &resourceConstraint
} else {
set.ResourceConstraint = sdk.Pointer(sdk.ResourceConstraint)
}
}

if updateParamDiags := handleWarehouseParametersChanges(d, &set, &unset); len(updateParamDiags) > 0 {
return updateParamDiags
Expand Down
30 changes: 30 additions & 0 deletions pkg/sdk/warehouses.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,36 @@ func ToWarehouseSize(s string) (WarehouseSize, error) {
}
}

type ResourceConstraint string

const (
ResourceConstraintSize1X ConstraintSize = "MEMORY_1X"
ResourceConstraintSize1XX86 ConstraintSize = "MEMORY_1X_X86"
ResourceConstraintSize16X ConstraintSize = "MEMORY_16X"
ResourceConstraintSize16XX86 ConstraintSize = "MEMORY_16X_X86"
ResourceConstraintSize64X ConstraintSize = "MEMORY_64X"
ResourceConstraintSize64XX86 ConstraintSize = "MEMORY_64X_X86"
)

func ToResourceConstraint(s string) (ResourceConstraint, error) {
switch strings.ToUpper(s) {
case string(ResourceConstraintSize1X):
return ResourceConstraintSize1X, nil
case string(ResourceConstraintSize1XX86):
return ResourceConstraintSize1XX86, nil
case string(ResourceConstraintSize16X):
return ResourceConstraintSize16X, nil
case string(ResourceConstraintSize16XX86):
return ResourceConstraintSize16XX86, nil
case string(ResourceConstraintSize64X):
return ResourceConstraintSize64X, nil
case string(ResourceConstraintSize64XX86):
return ResourceConstraintSize64XX86, nil
default:
return "", fmt.Errorf("invalid constraint type: %s", s)
}
}

type ScalingPolicy string

const (
Expand Down
10 changes: 10 additions & 0 deletions pkg/sdk/warehouses_validations.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,13 @@ var WarehouseParameters = []ObjectParameter{
ObjectParameterStatementQueuedTimeoutInSeconds,
ObjectParameterStatementTimeoutInSeconds,
}

// ValidWarehouseResourceConstraint is based on https://docs.snowflake.com/en/sql-reference/sql/create-warehouse#optional-properties-objectproperties
var ValidWarehouseResourceConstraint = []string{
string(ResourceConstraintSize1X),
string(ResourceConstraintSize1XX86),
string(ResourceConstraintSize16X),
string(ResourceConstraintSize16XX86),
string(ResourceConstraintSize64X),
string(ResourceConstraintSize64XX86),
}