Skip to content

Commit 6820278

Browse files
committed
feat(datawarehouse): add schema validation, expose all services, and fix timeouts
1 parent e234026 commit 6820278

File tree

5 files changed

+75
-3427
lines changed

5 files changed

+75
-3427
lines changed

internal/services/datawarehouse/deployment.go

Lines changed: 66 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package datawarehouse
22

33
import (
44
"context"
5+
"fmt"
56
"time"
67

78
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
810
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
911
datawarehouseapi "github.com/scaleway/scaleway-sdk-go/api/datawarehouse/v1beta1"
1012
"github.com/scaleway/scaleway-sdk-go/scw"
@@ -28,6 +30,18 @@ func ResourceDeployment() *schema.Resource {
2830
Update: schema.DefaultTimeout(30 * time.Minute),
2931
Delete: schema.DefaultTimeout(30 * time.Minute),
3032
},
33+
CustomizeDiff: customdiff.All(
34+
func(_ context.Context, diff *schema.ResourceDiff, _ any) error {
35+
cpuMin := diff.Get("cpu_min").(int)
36+
cpuMax := diff.Get("cpu_max").(int)
37+
38+
if cpuMin > cpuMax {
39+
return fmt.Errorf("cpu_min (%d) must be <= cpu_max (%d)", cpuMin, cpuMax)
40+
}
41+
42+
return nil
43+
},
44+
),
3145
Schema: map[string]*schema.Schema{
3246
"region": regional.Schema(),
3347
"project_id": account.ProjectIDSchema(),
@@ -45,6 +59,7 @@ func ResourceDeployment() *schema.Resource {
4559
"version": {
4660
Type: schema.TypeString,
4761
Required: true,
62+
ForceNew: true,
4863
Description: "ClickHouse version to use",
4964
},
5065
"replica_count": {
@@ -71,37 +86,64 @@ func ResourceDeployment() *schema.Resource {
7186
Type: schema.TypeString,
7287
Sensitive: true,
7388
Optional: true,
89+
ForceNew: true,
7490
Description: "Password for the first user of the deployment",
7591
},
7692
"public_network": {
7793
Type: schema.TypeList,
7894
Computed: true,
95+
MaxItems: 1,
7996
Description: "Public endpoint configuration. A public endpoint is created by default.",
8097
Elem: &schema.Resource{
8198
Schema: map[string]*schema.Schema{
8299
"id": {
83100
Type: schema.TypeString,
84101
Computed: true,
85-
Description: "ID of the public endpoint (computed)",
102+
Description: "ID of the public endpoint",
86103
},
87104
"dns_record": {
88105
Type: schema.TypeString,
89106
Computed: true,
90-
Description: "DNS record for the public endpoint (computed)",
91-
},
92-
"protocol": {
93-
Type: schema.TypeString,
94-
Computed: true,
95-
Description: "Service protocol (e.g. \"tcp\", \"https\", \"mysql\") for the public endpoint",
107+
Description: "DNS record for the public endpoint",
96108
},
97-
"port": {
98-
Type: schema.TypeInt,
109+
"services": {
110+
Type: schema.TypeList,
99111
Computed: true,
100-
Description: "TCP port number for the public endpoint",
112+
Description: "List of services exposed on the public endpoint",
113+
Elem: &schema.Resource{
114+
Schema: map[string]*schema.Schema{
115+
"protocol": {
116+
Type: schema.TypeString,
117+
Computed: true,
118+
Description: "Service protocol (e.g. \"tcp\", \"https\", \"mysql\")",
119+
},
120+
"port": {
121+
Type: schema.TypeInt,
122+
Computed: true,
123+
Description: "TCP port number",
124+
},
125+
},
126+
},
101127
},
102128
},
103129
},
104130
},
131+
// Computed
132+
"status": {
133+
Type: schema.TypeString,
134+
Computed: true,
135+
Description: "The status of the deployment",
136+
},
137+
"created_at": {
138+
Type: schema.TypeString,
139+
Computed: true,
140+
Description: "Date and time of deployment creation (RFC 3339 format)",
141+
},
142+
"updated_at": {
143+
Type: schema.TypeString,
144+
Computed: true,
145+
Description: "Date and time of deployment last update (RFC 3339 format)",
146+
},
105147
},
106148
}
107149
}
@@ -153,7 +195,7 @@ func resourceDeploymentRead(ctx context.Context, d *schema.ResourceData, meta in
153195
region := scw.Region(d.Get("region").(string))
154196
id := d.Id()
155197

156-
_, err := waitForDatawarehouseDeployment(ctx, api, region, id, d.Timeout(schema.TimeoutCreate))
198+
_, err := waitForDatawarehouseDeployment(ctx, api, region, id, d.Timeout(schema.TimeoutRead))
157199
if err != nil {
158200
return diag.FromErr(err)
159201
}
@@ -172,13 +214,18 @@ func resourceDeploymentRead(ctx context.Context, d *schema.ResourceData, meta in
172214
return diag.FromErr(err)
173215
}
174216

217+
_ = d.Set("region", string(region))
218+
_ = d.Set("project_id", deployment.ProjectID)
175219
_ = d.Set("name", deployment.Name)
176220
_ = d.Set("tags", flattenStringList(deployment.Tags))
177221
_ = d.Set("version", deployment.Version)
178222
_ = d.Set("replica_count", int(deployment.ReplicaCount))
179223
_ = d.Set("cpu_min", int(deployment.CPUMin))
180224
_ = d.Set("cpu_max", int(deployment.CPUMax))
181225
_ = d.Set("ram_per_cpu", int(deployment.RAMPerCPU))
226+
_ = d.Set("status", string(deployment.Status))
227+
_ = d.Set("created_at", deployment.CreatedAt.Format(time.RFC3339))
228+
_ = d.Set("updated_at", deployment.UpdatedAt.Format(time.RFC3339))
182229

183230
publicBlock, hasPublic := flattenPublicNetwork(deployment.Endpoints)
184231
if hasPublic {
@@ -198,7 +245,7 @@ func resourceDeploymentUpdate(ctx context.Context, d *schema.ResourceData, meta
198245
region := scw.Region(d.Get("region").(string))
199246
id := d.Id()
200247

201-
_, err := waitForDatawarehouseDeployment(ctx, api, region, id, d.Timeout(schema.TimeoutCreate))
248+
_, err := waitForDatawarehouseDeployment(ctx, api, region, id, d.Timeout(schema.TimeoutUpdate))
202249
if err != nil {
203250
return diag.FromErr(err)
204251
}
@@ -256,6 +303,11 @@ func resourceDeploymentUpdate(ctx context.Context, d *schema.ResourceData, meta
256303
if err != nil {
257304
return diag.FromErr(err)
258305
}
306+
307+
_, err = waitForDatawarehouseDeployment(ctx, api, region, id, d.Timeout(schema.TimeoutUpdate))
308+
if err != nil {
309+
return diag.FromErr(err)
310+
}
259311
}
260312

261313
readDiags := resourceDeploymentRead(ctx, d, meta)
@@ -269,7 +321,7 @@ func resourceDeploymentDelete(ctx context.Context, d *schema.ResourceData, meta
269321
region := scw.Region(d.Get("region").(string))
270322
id := d.Id()
271323

272-
_, err := waitForDatawarehouseDeployment(ctx, api, region, id, d.Timeout(schema.TimeoutCreate))
324+
_, err := waitForDatawarehouseDeployment(ctx, api, region, id, d.Timeout(schema.TimeoutDelete))
273325
if err != nil {
274326
return diag.FromErr(err)
275327
}
@@ -282,12 +334,10 @@ func resourceDeploymentDelete(ctx context.Context, d *schema.ResourceData, meta
282334
return diag.FromErr(err)
283335
}
284336

285-
_, err = waitForDatawarehouseDeployment(ctx, api, region, id, d.Timeout(schema.TimeoutCreate))
337+
_, err = waitForDatawarehouseDeployment(ctx, api, region, id, d.Timeout(schema.TimeoutDelete))
286338
if err != nil && !httperrors.Is404(err) {
287339
return diag.FromErr(err)
288340
}
289341

290-
d.SetId("")
291-
292342
return nil
293343
}

0 commit comments

Comments
 (0)