Skip to content

Commit 00245c3

Browse files
committed
feat(datawarehouse): add support for deployments, databases and users
1 parent 97801bb commit 00245c3

23 files changed

+5205
-2
lines changed

cmd/tftemplate/go.mod

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ go 1.22
44

55
toolchain go1.22.2
66

7-
require github.com/AlecAivazis/survey/v2 v2.3.7
7+
require (
8+
github.com/AlecAivazis/survey/v2 v2.3.7
9+
golang.org/x/text v0.15.0
10+
)
811

912
require (
1013
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
@@ -14,5 +17,4 @@ require (
1417
github.com/stretchr/testify v1.7.0 // indirect
1518
golang.org/x/sys v0.20.0 // indirect
1619
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
17-
golang.org/x/text v0.15.0 // indirect
1820
)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
subcategory: "Data Warehouse"
3+
page_title: "Scaleway: scaleway_datawarehouse_database"
4+
---
5+
6+
# Resource: scaleway_datawarehouse_database
7+
8+
Creates and manages Scaleway Data Warehouse databases within a deployment.
9+
For more information refer to the [product documentation](https://www.scaleway.com/en/docs/data-warehouse/).
10+
11+
## Example Usage
12+
13+
### Basic
14+
15+
```terraform
16+
resource "scaleway_datawarehouse_deployment" "main" {
17+
name = "my-datawarehouse"
18+
version = "v25"
19+
replica_count = 1
20+
cpu_min = 2
21+
cpu_max = 4
22+
ram_per_cpu = 4
23+
password = "thiZ_is_v&ry_s3cret"
24+
}
25+
26+
resource "scaleway_datawarehouse_database" "main" {
27+
deployment_id = scaleway_datawarehouse_deployment.main.id
28+
name = "my_database"
29+
}
30+
```
31+
32+
## Argument Reference
33+
34+
The following arguments are supported:
35+
36+
- `deployment_id` - (Required) ID of the Data Warehouse deployment to which this database belongs.
37+
- `name` - (Required) Name of the database.
38+
- `region` - (Defaults to [provider](../index.md#region) `region`) The [region](../guides/regions_and_zones.md#regions) in which the database should be created.
39+
40+
## Attributes Reference
41+
42+
In addition to all arguments above, the following attributes are exported:
43+
44+
- `id` - The ID of the database (format: `{region}/{deployment_id}/{name}`).
45+
- `size` - Size of the database in GB.
46+
47+
## Import
48+
49+
Data Warehouse databases can be imported using the `{region}/{deployment_id}/{name}`, e.g.
50+
51+
```bash
52+
terraform import scaleway_datawarehouse_database.main fr-par/11111111-1111-1111-1111-111111111111/my_database
53+
```
54+
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
---
2+
subcategory: "Data Warehouse"
3+
page_title: "Scaleway: scaleway_datawarehouse_deployment"
4+
---
5+
6+
# Resource: scaleway_datawarehouse_deployment
7+
8+
Creates and manages Scaleway Data Warehouse deployments.
9+
For more information refer to the [product documentation](https://www.scaleway.com/en/docs/data-warehouse/).
10+
11+
## Example Usage
12+
13+
### Basic
14+
15+
```terraform
16+
resource "scaleway_datawarehouse_deployment" "main" {
17+
name = "my-datawarehouse"
18+
version = "v25"
19+
replica_count = 1
20+
cpu_min = 2
21+
cpu_max = 4
22+
ram_per_cpu = 4
23+
password = "thiZ_is_v&ry_s3cret"
24+
}
25+
```
26+
27+
### With Tags
28+
29+
```terraform
30+
resource "scaleway_datawarehouse_deployment" "main" {
31+
name = "my-datawarehouse"
32+
version = "v25"
33+
replica_count = 1
34+
cpu_min = 2
35+
cpu_max = 4
36+
ram_per_cpu = 4
37+
password = "thiZ_is_v&ry_s3cret"
38+
tags = ["production", "analytics"]
39+
}
40+
```
41+
42+
## Argument Reference
43+
44+
The following arguments are supported:
45+
46+
- `name` - (Required) Name of the Data Warehouse deployment.
47+
- `version` - (Required, Forces new resource) ClickHouse version to use (e.g., "v25"). Changing this forces recreation of the deployment.
48+
- `replica_count` - (Required) Number of replicas.
49+
- `cpu_min` - (Required) Minimum CPU count. Must be less than or equal to `cpu_max`.
50+
- `cpu_max` - (Required) Maximum CPU count. Must be greater than or equal to `cpu_min`.
51+
- `ram_per_cpu` - (Required) RAM per CPU in GB.
52+
- `password` - (Optional) Password for the first user of the deployment. If not specified, a random password will be generated. Note: password is only used during deployment creation.
53+
- `tags` - (Optional) List of tags to apply to the deployment.
54+
- `region` - (Defaults to [provider](../index.md#region) `region`) The [region](../guides/regions_and_zones.md#regions) in which the deployment should be created.
55+
- `project_id` - (Defaults to [provider](../index.md#project_id) `project_id`) The ID of the project the deployment is associated with.
56+
57+
~> **Important:** Private endpoints are not yet supported by the API. A public endpoint is always created automatically.
58+
59+
~> **Note:** During the private beta phase, modifying `cpu_min`, `cpu_max`, and `replica_count` has no effect until the feature is launched in general availability.
60+
61+
## Attributes Reference
62+
63+
In addition to all arguments above, the following attributes are exported:
64+
65+
- `id` - The ID of the deployment.
66+
- `status` - The status of the deployment (e.g., "ready", "provisioning").
67+
- `created_at` - Date and time of deployment creation (RFC 3339 format).
68+
- `updated_at` - Date and time of deployment last update (RFC 3339 format).
69+
- `public_network` - Public endpoint information (always created automatically).
70+
- `id` - The ID of the public endpoint.
71+
- `dns_record` - DNS record for the public endpoint.
72+
- `services` - List of services exposed on the public endpoint.
73+
- `protocol` - Service protocol (e.g., "tcp", "https", "mysql").
74+
- `port` - TCP port number.
75+
76+
## Import
77+
78+
Data Warehouse deployments can be imported using the `{region}/{id}`, e.g.
79+
80+
```bash
81+
terraform import scaleway_datawarehouse_deployment.main fr-par/11111111-1111-1111-1111-111111111111
82+
```
83+
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
---
2+
subcategory: "Data Warehouse"
3+
page_title: "Scaleway: scaleway_datawarehouse_user"
4+
---
5+
6+
# Resource: scaleway_datawarehouse_user
7+
8+
Creates and manages Scaleway Data Warehouse users within a deployment.
9+
For more information refer to the [product documentation](https://www.scaleway.com/en/docs/data-warehouse/).
10+
11+
## Example Usage
12+
13+
### Basic
14+
15+
```terraform
16+
resource "scaleway_datawarehouse_deployment" "main" {
17+
name = "my-datawarehouse"
18+
version = "v25"
19+
replica_count = 1
20+
cpu_min = 2
21+
cpu_max = 4
22+
ram_per_cpu = 4
23+
password = "thiZ_is_v&ry_s3cret"
24+
}
25+
26+
resource "scaleway_datawarehouse_user" "main" {
27+
deployment_id = scaleway_datawarehouse_deployment.main.id
28+
name = "my_user"
29+
password = "user_password_123"
30+
}
31+
```
32+
33+
### Admin User
34+
35+
```terraform
36+
resource "scaleway_datawarehouse_deployment" "main" {
37+
name = "my-datawarehouse"
38+
version = "v25"
39+
replica_count = 1
40+
cpu_min = 2
41+
cpu_max = 4
42+
ram_per_cpu = 4
43+
password = "thiZ_is_v&ry_s3cret"
44+
}
45+
46+
resource "scaleway_datawarehouse_user" "admin" {
47+
deployment_id = scaleway_datawarehouse_deployment.main.id
48+
name = "admin_user"
49+
password = "admin_password_456"
50+
is_admin = true
51+
}
52+
```
53+
54+
## Argument Reference
55+
56+
The following arguments are supported:
57+
58+
- `deployment_id` - (Required) ID of the Data Warehouse deployment to which this user belongs.
59+
- `name` - (Required) Name of the ClickHouse user.
60+
- `password` - (Required) Password for the ClickHouse user.
61+
- `is_admin` - (Optional) Whether the user has administrator privileges. Defaults to `false`.
62+
- `region` - (Defaults to [provider](../index.md#region) `region`) The [region](../guides/regions_and_zones.md#regions) in which the user should be created.
63+
64+
## Attributes Reference
65+
66+
In addition to all arguments above, the following attributes are exported:
67+
68+
- `id` - The ID of the user (format: `{region}/{deployment_id}/{name}`).
69+
70+
## Import
71+
72+
Data Warehouse users can be imported using the `{region}/{deployment_id}/{name}`, e.g.
73+
74+
```bash
75+
terraform import scaleway_datawarehouse_user.main fr-par/11111111-1111-1111-1111-111111111111/my_user
76+
```
77+
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
package datawarehouse
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"strings"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
datawarehouseapi "github.com/scaleway/scaleway-sdk-go/api/datawarehouse/v1beta1"
11+
"github.com/scaleway/scaleway-sdk-go/scw"
12+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
13+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
14+
)
15+
16+
func ResourceDatabase() *schema.Resource {
17+
return &schema.Resource{
18+
CreateContext: resourceDatabaseCreate,
19+
ReadContext: resourceDatabaseRead,
20+
DeleteContext: resourceDatabaseDelete,
21+
Importer: &schema.ResourceImporter{
22+
StateContext: schema.ImportStatePassthroughContext,
23+
},
24+
Schema: map[string]*schema.Schema{
25+
"region": regional.Schema(),
26+
"deployment_id": {
27+
Type: schema.TypeString,
28+
Required: true,
29+
ForceNew: true,
30+
Description: "ID of the Datawarehouse deployment to which this database belongs.",
31+
},
32+
"name": {
33+
Type: schema.TypeString,
34+
Required: true,
35+
ForceNew: true,
36+
Description: "Name of the database.",
37+
},
38+
"size": {
39+
Type: schema.TypeInt,
40+
Computed: true,
41+
Description: "Size of the database (in GB).",
42+
},
43+
},
44+
}
45+
}
46+
47+
func resourceDatabaseCreate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
48+
api := NewAPI(meta)
49+
50+
region := scw.Region(d.Get("region").(string))
51+
deploymentID := d.Get("deployment_id").(string)
52+
name := d.Get("name").(string)
53+
54+
req := &datawarehouseapi.CreateDatabaseRequest{
55+
Region: region,
56+
DeploymentID: deploymentID,
57+
Name: name,
58+
}
59+
60+
_, err := api.CreateDatabase(req, scw.WithContext(ctx))
61+
if err != nil {
62+
return diag.FromErr(err)
63+
}
64+
65+
d.SetId(ResourceDatabaseID(region, deploymentID, name))
66+
67+
return resourceDatabaseRead(ctx, d, meta)
68+
}
69+
70+
func resourceDatabaseRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
71+
api := NewAPI(meta)
72+
73+
region, deploymentID, name, err := ResourceDatabaseParseID(d.Id())
74+
if err != nil {
75+
return diag.FromErr(err)
76+
}
77+
78+
resp, err := api.ListDatabases(&datawarehouseapi.ListDatabasesRequest{
79+
Region: region,
80+
DeploymentID: deploymentID,
81+
Name: scw.StringPtr(name),
82+
}, scw.WithContext(ctx))
83+
if err != nil {
84+
if httperrors.Is404(err) {
85+
d.SetId("")
86+
87+
return nil
88+
}
89+
90+
return diag.FromErr(err)
91+
}
92+
93+
var found *datawarehouseapi.Database
94+
95+
for _, db := range resp.Databases {
96+
if db.Name == name {
97+
found = db
98+
99+
break
100+
}
101+
}
102+
103+
if found == nil {
104+
d.SetId("")
105+
106+
return nil
107+
}
108+
109+
_ = d.Set("deployment_id", deploymentID)
110+
_ = d.Set("name", found.Name)
111+
_ = d.Set("size", int(found.Size))
112+
113+
return nil
114+
}
115+
116+
func resourceDatabaseDelete(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
117+
api := NewAPI(meta)
118+
119+
region, deploymentID, name, err := ResourceDatabaseParseID(d.Id())
120+
if err != nil {
121+
return diag.FromErr(err)
122+
}
123+
124+
err = api.DeleteDatabase(&datawarehouseapi.DeleteDatabaseRequest{
125+
Region: region,
126+
DeploymentID: deploymentID,
127+
Name: name,
128+
}, scw.WithContext(ctx))
129+
if err != nil && !httperrors.Is404(err) {
130+
return diag.FromErr(err)
131+
}
132+
133+
d.SetId("")
134+
135+
return nil
136+
}
137+
138+
func ResourceDatabaseID(region scw.Region, deploymentID, name string) string {
139+
return fmt.Sprintf("%s/%s/%s", region, deploymentID, name)
140+
}
141+
142+
func ResourceDatabaseParseID(id string) (scw.Region, string, string, error) {
143+
parts := strings.Split(id, "/")
144+
if len(parts) != 3 {
145+
return "", "", "", fmt.Errorf("unexpected format of ID (%s), expected region/deployment_id/name", id)
146+
}
147+
148+
return scw.Region(parts[0]), parts[1], parts[2], nil
149+
}

0 commit comments

Comments
 (0)