Skip to content

Commit 7dc697c

Browse files
committed
feat(datawarehouse): make public endpoints required and create them with deployment
1 parent dfddc5b commit 7dc697c

File tree

6 files changed

+932
-5375
lines changed

6 files changed

+932
-5375
lines changed

internal/services/datawarehouse/deployment.go

Lines changed: 38 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -74,80 +74,40 @@ func ResourceDeployment() *schema.Resource {
7474
Optional: true,
7575
Description: "Password for the first user of the deployment",
7676
},
77-
"private_network": {
78-
Type: schema.TypeList,
79-
Optional: true,
80-
Computed: true,
81-
MaxItems: 1,
82-
Description: "If set, creates a private endpoint. Provide pn_id.",
83-
AtLeastOneOf: []string{"private_network", "public_network"},
84-
Elem: &schema.Resource{
85-
Schema: map[string]*schema.Schema{
86-
// Input: private network ID
87-
"pn_id": {
88-
Type: schema.TypeString,
89-
Optional: true,
90-
Description: "UUID of the private network for the endpoint",
91-
},
92-
// Computed fields returned from API:
93-
"id": {
94-
Type: schema.TypeString,
95-
Computed: true,
96-
Description: "ID of the private endpoint (computed)",
97-
},
98-
"dns_record": {
99-
Type: schema.TypeString,
100-
Computed: true,
101-
Description: "DNS record assigned to the private endpoint (computed)",
102-
},
103-
"protocol": {
104-
Type: schema.TypeString,
105-
Computed: true,
106-
Description: "Service protocol (e.g. \"tcp\", \"https\", \"mysql\") for the private endpoint",
107-
},
108-
"port": {
109-
Type: schema.TypeInt,
110-
Computed: true,
111-
Description: "TCP port number for the private endpoint",
112-
},
113-
},
114-
},
115-
},
116-
117-
// Computed block: public_network (max 1). Only returned if a public endpoint exists.
118-
"public_network": {
119-
Type: schema.TypeList,
120-
Optional: true,
121-
Computed: true,
122-
MaxItems: 1,
123-
Description: "Details of the public endpoint (computed). At least one of private_network or public_network must be specified.",
124-
AtLeastOneOf: []string{"private_network", "public_network"},
125-
Elem: &schema.Resource{
126-
Schema: map[string]*schema.Schema{
127-
"id": {
128-
Type: schema.TypeString,
129-
Computed: true,
130-
Description: "ID of the public endpoint (computed)",
131-
},
132-
"dns_record": {
133-
Type: schema.TypeString,
134-
Computed: true,
135-
Description: "DNS record for the public endpoint (computed)",
136-
},
137-
"protocol": {
138-
Type: schema.TypeString,
139-
Computed: true,
140-
Description: "Service protocol (e.g. \"tcp\", \"https\", \"mysql\") for the public endpoint",
141-
},
142-
"port": {
143-
Type: schema.TypeInt,
144-
Computed: true,
145-
Description: "TCP port number for the public endpoint",
77+
// Public endpoint (optional). If not specified, a public endpoint is created by default.
78+
// Private endpoints are not yet supported by the API.
79+
"public_network": {
80+
Type: schema.TypeList,
81+
Optional: true,
82+
Computed: true,
83+
MaxItems: 1,
84+
Description: "Public endpoint configuration. If not specified, a public endpoint is created by default.",
85+
Elem: &schema.Resource{
86+
Schema: map[string]*schema.Schema{
87+
"id": {
88+
Type: schema.TypeString,
89+
Computed: true,
90+
Description: "ID of the public endpoint (computed)",
91+
},
92+
"dns_record": {
93+
Type: schema.TypeString,
94+
Computed: true,
95+
Description: "DNS record for the public endpoint (computed)",
96+
},
97+
"protocol": {
98+
Type: schema.TypeString,
99+
Computed: true,
100+
Description: "Service protocol (e.g. \"tcp\", \"https\", \"mysql\") for the public endpoint",
101+
},
102+
"port": {
103+
Type: schema.TypeInt,
104+
Computed: true,
105+
Description: "TCP port number for the public endpoint",
106+
},
146107
},
147108
},
148109
},
149110
},
150-
},
151111
}
152112
}
153113

@@ -171,6 +131,14 @@ func resourceDeploymentCreate(ctx context.Context, d *schema.ResourceData, meta
171131
req.Tags = expandStringList(v.([]interface{}))
172132
}
173133

134+
// Build public endpoint for deployment creation (required)
135+
// Note: Private endpoints are not yet supported by the API
136+
req.Endpoints = []*datawarehouseapi.EndpointSpec{
137+
{
138+
Public: &datawarehouseapi.EndpointSpecPublicDetails{},
139+
},
140+
}
141+
174142
deployment, err := api.CreateDeployment(req, scw.WithContext(ctx))
175143
if err != nil {
176144
return diag.FromErr(err)
@@ -181,40 +149,6 @@ func resourceDeploymentCreate(ctx context.Context, d *schema.ResourceData, meta
181149
return diag.FromErr(err)
182150
}
183151

184-
// Create endpoints after deployment is ready
185-
if v, ok := d.GetOk("private_network"); ok {
186-
blk := v.([]interface{})[0].(map[string]interface{})
187-
if pnID, _ := blk["pn_id"].(string); pnID != "" {
188-
spec := &datawarehouseapi.EndpointSpec{
189-
PrivateNetwork: &datawarehouseapi.EndpointSpecPrivateNetworkDetails{
190-
PrivateNetworkID: pnID,
191-
},
192-
}
193-
_, err := api.CreateEndpoint(&datawarehouseapi.CreateEndpointRequest{
194-
Region: region,
195-
DeploymentID: deployment.ID,
196-
Endpoint: spec,
197-
}, scw.WithContext(ctx))
198-
if err != nil {
199-
return diag.FromErr(err)
200-
}
201-
}
202-
}
203-
204-
if _, ok := d.GetOk("public_network"); ok {
205-
spec := &datawarehouseapi.EndpointSpec{
206-
Public: &datawarehouseapi.EndpointSpecPublicDetails{},
207-
}
208-
_, err := api.CreateEndpoint(&datawarehouseapi.CreateEndpointRequest{
209-
Region: region,
210-
DeploymentID: deployment.ID,
211-
Endpoint: spec,
212-
}, scw.WithContext(ctx))
213-
if err != nil {
214-
return diag.FromErr(err)
215-
}
216-
}
217-
218152
d.SetId(deployment.ID)
219153

220154
return resourceDeploymentRead(ctx, d, meta)
@@ -259,13 +193,6 @@ func resourceDeploymentRead(ctx context.Context, d *schema.ResourceData, meta in
259193
_ = d.Set("public_network", nil)
260194
}
261195

262-
privateBlock, hasPrivate := flattenPrivateNetwork(deployment.Endpoints)
263-
if hasPrivate {
264-
_ = d.Set("private_network", privateBlock.([]map[string]interface{}))
265-
} else {
266-
_ = d.Set("private_network", nil)
267-
}
268-
269196
return nil
270197
}
271198

internal/services/datawarehouse/deployment_test.go

Lines changed: 0 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ resource "scaleway_datawarehouse_deployment" "main" {
5959

6060
// Public endpoint is present
6161
resource.TestCheckResourceAttr("scaleway_datawarehouse_deployment.main", "public_network.#", "1"),
62-
resource.TestCheckResourceAttr("scaleway_datawarehouse_deployment.main", "private_network.#", "0"),
6362
),
6463
},
6564

@@ -87,105 +86,6 @@ resource "scaleway_datawarehouse_deployment" "main" {
8786

8887
// Public network still present
8988
resource.TestCheckResourceAttr("scaleway_datawarehouse_deployment.main", "public_network.#", "1"),
90-
// Private network still absent
91-
resource.TestCheckResourceAttr("scaleway_datawarehouse_deployment.main", "private_network.#", "0"),
92-
),
93-
},
94-
},
95-
})
96-
}
97-
98-
// The private-endpoint test is now written, assuming you have changed your schema to:
99-
//
100-
// resource "scaleway_vpc_private_network" "pn" {
101-
// name = "tf-test-pn"
102-
// }
103-
//
104-
// resource "scaleway_datawarehouse_deployment" "main" {
105-
// name = "tf-test-deploy-private"
106-
// version = "<latestVersion>"
107-
// replica_count = 1
108-
// cpu_min = 2
109-
// cpu_max = 4
110-
// ram_per_cpu = 4
111-
// password = "password@1234567"
112-
//
113-
// private_network {
114-
// pn_id = scaleway_vpc_private_network.pn.id
115-
// }
116-
// }
117-
func TestAccDeployment_PrivateEndpoint(t *testing.T) {
118-
tt := acctest.NewTestTools(t)
119-
defer tt.Cleanup()
120-
121-
// Fetch latest ClickHouse version
122-
api := datawarehouse.NewAPI(tt.Meta)
123-
versionsResp, err := api.ListVersions(&datawarehouseSDK.ListVersionsRequest{}, scw.WithAllPages())
124-
if err != nil {
125-
t.Fatalf("unable to fetch datawarehouse versions: %s", err)
126-
}
127-
if len(versionsResp.Versions) == 0 {
128-
t.Fatal("no datawarehouse versions available")
129-
}
130-
latestVersion := versionsResp.Versions[0].Version
131-
132-
resource.ParallelTest(t, resource.TestCase{
133-
PreCheck: func() { acctest.PreCheck(t) },
134-
ProviderFactories: tt.ProviderFactories,
135-
CheckDestroy: isDeploymentDestroyed(tt),
136-
Steps: []resource.TestStep{
137-
{
138-
Config: fmt.Sprintf(`
139-
resource "scaleway_vpc_private_network" "pn" {
140-
name = "tf-test-pn"
141-
}
142-
143-
resource "scaleway_datawarehouse_deployment" "main" {
144-
name = "tf-test-deploy-private"
145-
version = "%s"
146-
replica_count = 1
147-
cpu_min = 2
148-
cpu_max = 4
149-
ram_per_cpu = 4
150-
password = "password@1234567"
151-
152-
private_network {
153-
pn_id = scaleway_vpc_private_network.pn.id
154-
}
155-
}
156-
`, latestVersion),
157-
Check: resource.ComposeTestCheckFunc(
158-
isDeploymentPresent(tt, "scaleway_datawarehouse_deployment.main"),
159-
// Exactly one private_network block...
160-
resource.TestCheckResourceAttr("scaleway_datawarehouse_deployment.main", "private_network.#", "1"),
161-
// ...and no public_network
162-
resource.TestCheckResourceAttr("scaleway_datawarehouse_deployment.main", "public_network.#", "0"),
163-
164-
// Check that Terraform stored the private_network.pn_id correctly
165-
resource.TestCheckResourceAttrPair(
166-
"scaleway_datawarehouse_deployment.main", "private_network.0.pn_id",
167-
"scaleway_vpc_private_network.pn", "id",
168-
),
169-
170-
// Ensure the computed fields (id, dns_record, protocol, port) are non‐empty:
171-
func(s *terraform.State) error {
172-
rs := s.RootModule().Resources["scaleway_datawarehouse_deployment.main"]
173-
attrs := rs.Primary.Attributes
174-
175-
if attrs["private_network.0.id"] == "" {
176-
return fmt.Errorf("expected private_network.0.id to be set, got empty")
177-
}
178-
if attrs["private_network.0.dns_record"] == "" {
179-
return fmt.Errorf("expected private_network.0.dns_record to be set, got empty")
180-
}
181-
if attrs["private_network.0.protocol"] == "" {
182-
return fmt.Errorf("expected private_network.0.protocol to be set, got empty")
183-
}
184-
if attrs["private_network.0.port"] == "" {
185-
return fmt.Errorf("expected private_network.0.port to be set, got empty")
186-
}
187-
return nil
188-
},
18989
),
19090
},
19191
},

0 commit comments

Comments
 (0)