-
Notifications
You must be signed in to change notification settings - Fork 418
/
warehouse.go
191 lines (177 loc) · 5.84 KB
/
warehouse.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package resources
import (
"database/sql"
"log"
"strings"
"github.com/chanzuckerberg/terraform-provider-snowflake/pkg/snowflake"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
// warehouseCreateProperties are only available via the CREATE statement
var warehouseCreateProperties = []string{"initially_suspended", "wait_for_provisioning", "statement_timeout_in_seconds"}
var warehouseProperties = []string{
"comment", "warehouse_size", "max_cluster_count", "min_cluster_count",
"scaling_policy", "auto_suspend", "auto_resume",
"resource_monitor",
}
var warehouseSchema = map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"comment": {
Type: schema.TypeString,
Optional: true,
Default: "",
},
"warehouse_size": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringInSlice([]string{
"XSMALL", "X-SMALL", "SMALL", "MEDIUM", "LARGE", "XLARGE",
"X-LARGE", "XXLARGE", "X2LARGE", "2X-LARGE", "XXXLARGE", "X3LARGE",
"3X-LARGE", "X4LARGE", "4X-LARGE",
}, true),
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
normalize := func(s string) string {
return strings.ToUpper(strings.Replace(s, "-", "", -1))
}
return normalize(old) == normalize(new)
},
},
"max_cluster_count": {
Type: schema.TypeInt,
Description: "Specifies the maximum number of server clusters for the warehouse.",
Optional: true,
Computed: true,
ValidateFunc: validation.IntBetween(1, 10),
},
"min_cluster_count": {
Type: schema.TypeInt,
Description: "Specifies the minimum number of server clusters for the warehouse (only applies to multi-cluster warehouses).",
Optional: true,
Computed: true,
ValidateFunc: validation.IntBetween(1, 10),
},
"scaling_policy": {
Type: schema.TypeString,
Description: "Specifies the policy for automatically starting and shutting down clusters in a multi-cluster warehouse running in Auto-scale mode.",
Optional: true,
Computed: true,
ValidateFunc: validation.StringInSlice([]string{"STANDARD", "ECONOMY"}, true),
},
"auto_suspend": {
Type: schema.TypeInt,
Description: "Specifies the number of seconds of inactivity after which a warehouse is automatically suspended.",
Optional: true,
Computed: true,
ValidateFunc: validation.IntAtLeast(1),
},
// @TODO add a disable_auto_suspend property that sets the value of auto_suspend to NULL
"auto_resume": {
Type: schema.TypeBool,
Description: "Specifies whether to automatically resume a warehouse when a SQL statement (e.g. query) is submitted to it.",
Optional: true,
Computed: true,
},
"initially_suspended": {
Type: schema.TypeBool,
Description: "Specifies whether the warehouse is created initially in the ‘Suspended’ state.",
Optional: true,
},
"resource_monitor": {
Type: schema.TypeString,
Description: "Specifies the name of a resource monitor that is explicitly assigned to the warehouse.",
Optional: true,
Computed: true,
},
"wait_for_provisioning": {
Type: schema.TypeBool,
Description: "Specifies whether the warehouse, after being resized, waits for all the servers to provision before executing any queued or new queries.",
Optional: true,
},
"statement_timeout_in_seconds": {
Type: schema.TypeInt,
Optional: true,
Default: 0,
ForceNew: false,
Description: "Specifies the time, in seconds, after which a running SQL statement (query, DDL, DML, etc.) is canceled by the system",
},
}
// Warehouse returns a pointer to the resource representing a warehouse
func Warehouse() *schema.Resource {
return &schema.Resource{
Create: CreateWarehouse,
Read: ReadWarehouse,
Delete: DeleteWarehouse,
Update: UpdateWarehouse,
Schema: warehouseSchema,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
}
}
// CreateWarehouse implements schema.CreateFunc
func CreateWarehouse(d *schema.ResourceData, meta interface{}) error {
props := append(warehouseProperties, warehouseCreateProperties...)
return CreateResource("warehouse", props, warehouseSchema, snowflake.Warehouse, ReadWarehouse)(d, meta)
}
// ReadWarehouse implements schema.ReadFunc
func ReadWarehouse(d *schema.ResourceData, meta interface{}) error {
db := meta.(*sql.DB)
stmt := snowflake.Warehouse(d.Id()).Show()
row := snowflake.QueryRow(db, stmt)
w, err := snowflake.ScanWarehouse(row)
if err == sql.ErrNoRows {
// If not found, mark resource to be removed from statefile during apply or refresh
log.Printf("[DEBUG] warehouse (%s) not found", d.Id())
d.SetId("")
return nil
}
if err != nil {
return err
}
err = d.Set("name", w.Name)
if err != nil {
return err
}
err = d.Set("comment", w.Comment)
if err != nil {
return err
}
err = d.Set("warehouse_size", w.Size)
if err != nil {
return err
}
err = d.Set("max_cluster_count", w.MaxClusterCount)
if err != nil {
return err
}
err = d.Set("min_cluster_count", w.MinClusterCount)
if err != nil {
return err
}
err = d.Set("scaling_policy", w.ScalingPolicy)
if err != nil {
return err
}
err = d.Set("auto_suspend", w.AutoSuspend)
if err != nil {
return err
}
err = d.Set("auto_resume", w.AutoResume)
if err != nil {
return err
}
err = d.Set("resource_monitor", w.ResourceMonitor)
return err
}
// UpdateWarehouse implements schema.UpdateFunc
func UpdateWarehouse(d *schema.ResourceData, meta interface{}) error {
return UpdateResource("warehouse", warehouseProperties, warehouseSchema, snowflake.Warehouse, ReadWarehouse)(d, meta)
}
// DeleteWarehouse implements schema.DeleteFunc
func DeleteWarehouse(d *schema.ResourceData, meta interface{}) error {
return DeleteResource("warehouse", snowflake.Warehouse)(d, meta)
}