Skip to content

Commit 45658de

Browse files
authored
fix(rdb): update endpoints handling (#2308)
* fix(rdb): update endpoints handling * record conflicting cassettes * update doc with examples * update conflicting cassettes
1 parent 0ccefa7 commit 45658de

34 files changed

+13665
-7158
lines changed

docs/resources/rdb_instance.md

Lines changed: 60 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,6 @@ resource "scaleway_rdb_instance" "main" {
2424
}
2525
```
2626

27-
### Example With IPAM
28-
29-
```hcl
30-
resource "scaleway_vpc_private_network" "pn" {}
31-
32-
resource "scaleway_rdb_instance" "main" {
33-
name = "test-rdb"
34-
node_type = "DB-DEV-S"
35-
engine = "PostgreSQL-11"
36-
is_ha_cluster = true
37-
disable_backup = true
38-
user_name = "my_initial_user"
39-
password = "thiZ_is_v&ry_s3cret"
40-
private_network {
41-
pn_id = scaleway_vpc_private_network.pn.id
42-
}
43-
}
44-
```
45-
4627
### Example with Settings
4728

4829
```hcl
@@ -79,38 +60,57 @@ resource "scaleway_rdb_instance" "main" {
7960
}
8061
```
8162

82-
### Example with custom private network
63+
### Examples of endpoints configuration
64+
65+
RDB Instances can have a maximum of 1 public endpoint and 1 private endpoint. It can have both, or none.
66+
67+
#### 1 static private network endpoint
8368

8469
```hcl
85-
# VPC PRIVATE NETWORK
8670
resource "scaleway_vpc_private_network" "pn" {
87-
name = "my_private_network"
8871
ipv4_subnet {
8972
subnet = "172.16.20.0/22"
9073
}
9174
}
9275
93-
# RDB INSTANCE CONNECTED ON A CUSTOM PRIVATE NETWORK
9476
resource "scaleway_rdb_instance" "main" {
95-
name = "test-rdb"
9677
node_type = "db-dev-s"
9778
engine = "PostgreSQL-11"
98-
is_ha_cluster = false
99-
disable_backup = true
100-
user_name = "my_initial_user"
101-
password = "thiZ_is_v&ry_s3cret"
102-
region = "fr-par"
103-
tags = ["terraform-test", "scaleway_rdb_instance", "volume", "rdb_pn"]
104-
volume_type = "bssd"
105-
volume_size_in_gb = 10
10679
private_network {
107-
ip_net = "172.16.20.4/22" # IP address within a given IP network
10880
pn_id = scaleway_vpc_private_network.pn.id
81+
ip_net = "172.16.20.4/22" # IP address within a given IP network
82+
(enable_ipam = false)
10983
}
110-
disable_public_endpoint = true
11184
}
11285
```
11386

87+
#### 1 IPAM private network endpoint + 1 public endpoint
88+
89+
```hcl
90+
resource "scaleway_vpc_private_network" "pn" {}
91+
92+
resource "scaleway_rdb_instance" "main" {
93+
node_type = "DB-DEV-S"
94+
engine = "PostgreSQL-11"
95+
private_network {
96+
pn_id = scaleway_vpc_private_network.pn.id
97+
(enable_ipam = true)
98+
}
99+
load_balancer {}
100+
}
101+
```
102+
103+
#### Default: 1 public endpoint
104+
105+
```hcl
106+
resource "scaleway_rdb_instance" "main" {
107+
node_type = "db-dev-s"
108+
engine = "PostgreSQL-11"
109+
}
110+
```
111+
112+
-> If nothing is defined, your instance will have a default public load-balancer endpoint
113+
114114
## Arguments Reference
115115

116116
The following arguments are supported:
@@ -145,6 +145,16 @@ and if you are using `bssd` storage, you should increase the volume size before
145145

146146
- `name` - (Optional) The name of the Database Instance.
147147

148+
- `tags` - (Optional) The tags associated with the Database Instance.
149+
150+
- `region` - (Defaults to [provider](../index.md#arguments-reference) `region`) The [region](../guides/regions_and_zones.md#regions)
151+
in which the Database Instance should be created.
152+
153+
- `project_id` - (Defaults to [provider](../index.md#arguments-reference) `project_id`) The ID of the project the Database
154+
Instance is associated with.
155+
156+
### Backups
157+
148158
- `disable_backup` - (Optional) Disable automated backup for the database instance.
149159

150160
- `backup_schedule_frequency` - (Optional) Backup schedule frequency in hours.
@@ -153,40 +163,32 @@ and if you are using `bssd` storage, you should increase the volume size before
153163

154164
- `backup_same_region` - (Optional) Boolean to store logical backups in the same region as the database instance.
155165

156-
- `init_settings` - (Optional) Map of engine settings to be set at database initialisation.
157-
158-
~> **Important:** Updates to `init_settings` will recreate the Database Instance.
166+
### Settings
159167

160168
- `settings` - (Optional) Map of engine settings to be set. Using this option will override default config.
161169

162-
- `tags` - (Optional) The tags associated with the Database Instance.
163-
164-
- `disable_public_endpoint` - (Optional) Disable the default public endpoint
165-
166-
- `region` - (Defaults to [provider](../index.md#region) `region`) The [region](../guides/regions_and_zones.md#regions)
167-
in which the Database Instance should be created.
168-
169-
- `project_id` - (Defaults to [provider](../index.md#project_id) `project_id`) The ID of the project the Database
170-
Instance is associated with.
170+
- `init_settings` - (Optional) Map of engine settings to be set at database initialisation.
171171

172-
## Settings
172+
~> **Important:** Updates to `init_settings` will recreate the Database Instance.
173173

174-
Please consult
175-
the [GoDoc](https://pkg.go.dev/github.com/scaleway/[email protected]/api/rdb/v1#EngineVersion) to list all
176-
available `settings` and `init_settings` on your `node_type` of your convenient.
174+
Please consult the [GoDoc](https://pkg.go.dev/github.com/scaleway/[email protected]/api/rdb/v1#EngineVersion) to list all available `settings` and `init_settings` for the `node_type` of your convenience.
177175

178-
## Private Network
176+
### Endpoints
179177

180-
~> **Important:** Updates to `private_network` will recreate the attachment Instance.
178+
- `private_network` - List of private networks endpoints of the database instance.
181179

182-
~> **NOTE:** Please calculate your host IP.
183-
using [cirhost](https://developer.hashicorp.com/terraform/language/functions/cidrhost). Otherwise, lets IPAM service
180+
- `pn_id` - (Required) The ID of the private network.
181+
- `enable_ipam` - (Optional) Whether the endpoint should be configured with IPAM. Defaults to `false` if `ip_net` is defined, `true` otherwise.
182+
- `ip_net` - (Optional) The IP network address within the private subnet. This must be an IPv4 address with a CIDR notation.
183+
The IP network address within the private subnet is determined by the IP Address Management (IPAM) service if not set.
184+
185+
~> **NOTE:** Please calculate your host IP using [cidrhost](https://developer.hashicorp.com/terraform/language/functions/cidrhost). Otherwise, let IPAM service
184186
handle the host IP on the network.
185187

186-
- `ip_net` - (Optional) The IP network address within the private subnet. This must be an IPv4 address with a
187-
CIDR notation. The IP network address within the private subnet is determined by the IP Address Management (IPAM)
188-
service if not set.
189-
- `pn_id` - (Required) The ID of the private network.
188+
~> **Important:** Updates to `private_network` will recreate the Instance's endpoint
189+
190+
- `load_balancer` - (Optional) List of load balancer endpoints of the database instance. A load-balancer endpoint will be set by default if no private network is.
191+
This block must be defined if you want a public endpoint in addition to your private endpoint.
190192

191193
## Attributes Reference
192194

scaleway/data_source_ipam_ip_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func TestAccScalewayDataSourceIPAMIP_RDB(t *testing.T) {
139139
}
140140
141141
resource scaleway_rdb_instance main {
142-
name = "test-rdb"
142+
name = "test-ipam-ip-rdb"
143143
node_type = "db-dev-s"
144144
engine = "PostgreSQL-14"
145145
is_ha_cluster = false

scaleway/helpers_rdb.go

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/hashicorp/go-cty/cty"
1010
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1111
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12+
"github.com/scaleway/scaleway-sdk-go/api/ipam/v1"
1213
"github.com/scaleway/scaleway-sdk-go/api/rdb/v1"
1314
"github.com/scaleway/scaleway-sdk-go/scw"
1415
)
@@ -108,28 +109,30 @@ func waitForRDBReadReplica(ctx context.Context, api *rdb.API, region scw.Region,
108109
}, scw.WithContext(ctx))
109110
}
110111

111-
func expandPrivateNetwork(data interface{}, exist bool) ([]*rdb.EndpointSpec, error) {
112+
func expandPrivateNetwork(data interface{}, exist bool, enableIpam bool) ([]*rdb.EndpointSpec, error) {
112113
if data == nil || !exist {
113114
return nil, nil
114115
}
115116

116117
var res []*rdb.EndpointSpec
117118
for _, pn := range data.([]interface{}) {
118119
r := pn.(map[string]interface{})
119-
ipNet := r["ip_net"].(string)
120120
spec := &rdb.EndpointSpec{
121121
PrivateNetwork: &rdb.EndpointSpecPrivateNetwork{
122122
PrivateNetworkID: expandID(r["pn_id"].(string)),
123123
},
124124
}
125-
if len(ipNet) > 0 {
126-
ip, err := expandIPNet(r["ip_net"].(string))
127-
if err != nil {
128-
return res, err
129-
}
130-
spec.PrivateNetwork.ServiceIP = &ip
131-
} else {
125+
if enableIpam {
132126
spec.PrivateNetwork.IpamConfig = &rdb.EndpointSpecPrivateNetworkIpamConfig{}
127+
} else {
128+
ipNet := r["ip_net"].(string)
129+
if len(ipNet) > 0 {
130+
ip, err := expandIPNet(r["ip_net"].(string))
131+
if err != nil {
132+
return res, err
133+
}
134+
spec.PrivateNetwork.ServiceIP = &ip
135+
}
133136
}
134137
res = append(res, spec)
135138
}
@@ -143,7 +146,7 @@ func expandLoadBalancer() *rdb.EndpointSpec {
143146
}
144147
}
145148

146-
func flattenPrivateNetwork(endpoints []*rdb.Endpoint) (interface{}, bool) {
149+
func flattenPrivateNetwork(endpoints []*rdb.Endpoint, enableIpam bool) (interface{}, bool) {
147150
pnI := []map[string]interface{}(nil)
148151
for _, endpoint := range endpoints {
149152
if endpoint.PrivateNetwork != nil {
@@ -165,6 +168,7 @@ func flattenPrivateNetwork(endpoints []*rdb.Endpoint) (interface{}, bool) {
165168
"ip_net": serviceIP,
166169
"pn_id": pnRegionalID,
167170
"hostname": flattenStringPtr(endpoint.Hostname),
171+
"enable_ipam": enableIpam,
168172
})
169173
return pnI, true
170174
}
@@ -317,3 +321,26 @@ func rdbPrivilegeUpgradeV1SchemaType() cty.Type {
317321
"id": cty.String,
318322
})
319323
}
324+
325+
func isIpamEndpoint(instance *rdb.Instance, meta interface{}) (bool, error) {
326+
ipamAPI := ipam.NewAPI(meta.(*Meta).scwClient)
327+
328+
ips, err := ipamAPI.ListIPs(&ipam.ListIPsRequest{
329+
Region: instance.Region,
330+
ResourceID: &instance.ID,
331+
ResourceType: "rdb_instance",
332+
IsIPv6: scw.BoolPtr(false),
333+
}, scw.WithAllPages())
334+
if err != nil {
335+
return false, fmt.Errorf("could not list IPs: %w", err)
336+
}
337+
338+
switch ips.TotalCount {
339+
case 1:
340+
return true, nil
341+
case 0:
342+
return false, nil
343+
default:
344+
return false, fmt.Errorf("expected no more than 1 IP for instance, got %d", ips.TotalCount)
345+
}
346+
}

0 commit comments

Comments
 (0)