44 "context"
55 "fmt"
66
7- "github.com/hashicorp/terraform-plugin-log/tflog"
87 "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
98 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
109 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
@@ -99,8 +98,15 @@ func resourceScalewayRdbReadReplica() *schema.Resource {
9998 Description : "The IP network address within the private subnet" ,
10099 Optional : true ,
101100 Computed : true ,
101+ AtLeastOneOf : []string {"private_network.0.enable_ipam" },
102102 ValidateFunc : validation .IsCIDR ,
103103 },
104+ "enable_ipam" : {
105+ Type : schema .TypeBool ,
106+ Optional : true ,
107+ AtLeastOneOf : []string {"private_network.0.service_ip" },
108+ Description : "Whether or not the private network endpoint should be configured with IPAM" ,
109+ },
104110 "zone" : {
105111 Type : schema .TypeString ,
106112 Description : "Private network zone" ,
@@ -152,7 +158,11 @@ func resourceScalewayRdbReadReplicaCreate(ctx context.Context, d *schema.Resourc
152158 if directAccess := expandReadReplicaEndpointsSpecDirectAccess (d .Get ("direct_access" )); directAccess != nil {
153159 endpointSpecs = append (endpointSpecs , directAccess )
154160 }
155- if pn , err := expandReadReplicaEndpointsSpecPrivateNetwork (d .Get ("private_network" )); err != nil || pn != nil {
161+ enableIpam := true
162+ if _ , ipSet := d .GetOk ("private_network.0.service_ip" ); ipSet {
163+ enableIpam = false
164+ }
165+ if pn , err := expandReadReplicaEndpointsSpecPrivateNetwork (d .Get ("private_network" ), enableIpam ); err != nil || pn != nil {
156166 if err != nil {
157167 return diag .FromErr (err )
158168 }
@@ -194,7 +204,11 @@ func resourceScalewayRdbReadReplicaRead(ctx context.Context, d *schema.ResourceD
194204 return diag .FromErr (err )
195205 }
196206
197- directAccess , privateNetwork := flattenReadReplicaEndpoints (rr .Endpoints )
207+ enableIpam , err := isIpamEndpoint (rr , meta )
208+ if err != nil {
209+ return diag .FromErr (err )
210+ }
211+ directAccess , privateNetwork := flattenReadReplicaEndpoints (rr .Endpoints , enableIpam )
198212 _ = d .Set ("direct_access" , directAccess )
199213 _ = d .Set ("private_network" , privateNetwork )
200214
@@ -214,7 +228,7 @@ func resourceScalewayRdbReadReplicaUpdate(ctx context.Context, d *schema.Resourc
214228 }
215229
216230 // verify resource is ready
217- _ , err = waitForRDBReadReplica (ctx , rdbAPI , region , ID , d .Timeout (schema .TimeoutRead ))
231+ rr , err : = waitForRDBReadReplica (ctx , rdbAPI , region , ID , d .Timeout (schema .TimeoutRead ))
218232 if err != nil {
219233 if is404Error (err ) {
220234 d .SetId ("" )
@@ -226,35 +240,59 @@ func resourceScalewayRdbReadReplicaUpdate(ctx context.Context, d *schema.Resourc
226240 newEndpoints := []* rdb.ReadReplicaEndpointSpec (nil )
227241
228242 if d .HasChange ("direct_access" ) {
229- _ , directAccessExists := d .GetOk ("direct_access" )
230- tflog .Debug (ctx , "direct_access" , map [string ]interface {}{
231- "exists" : directAccessExists ,
232- })
233- if ! directAccessExists {
234- err := rdbAPI .DeleteEndpoint (& rdb.DeleteEndpointRequest {
235- Region : region ,
236- EndpointID : expandID (d .Get ("direct_access.0.endpoint_id" )),
237- }, scw .WithContext (ctx ))
238- if err != nil {
239- return diag .FromErr (err )
243+ // delete old endpoint
244+ for _ , e := range rr .Endpoints {
245+ if e .DirectAccess != nil {
246+ err := rdbAPI .DeleteEndpoint (& rdb.DeleteEndpointRequest {
247+ Region : region ,
248+ EndpointID : e .ID ,
249+ }, scw .WithContext (ctx ))
250+ if err != nil {
251+ return diag .FromErr (err )
252+ }
240253 }
241- } else {
242- newEndpoints = append (newEndpoints , expandReadReplicaEndpointsSpecDirectAccess (d .Get ("direct_access" )))
254+ }
255+ // retrieve state
256+ rr , err = waitForRDBReadReplica (ctx , rdbAPI , region , ID , d .Timeout (schema .TimeoutRead ))
257+ if err != nil {
258+ return diag .FromErr (err )
259+ }
260+ // create a new one if defined
261+ if directAccess , directAccessExists := d .GetOk ("direct_access" ); directAccessExists {
262+ newEndpoints = append (newEndpoints , expandReadReplicaEndpointsSpecDirectAccess (directAccess ))
243263 }
244264 }
245265
246266 if d .HasChange ("private_network" ) {
247- _ , privateNetworkExists := d .GetOk ("private_network" )
248- if ! privateNetworkExists {
249- err := rdbAPI .DeleteEndpoint (& rdb.DeleteEndpointRequest {
250- Region : region ,
251- EndpointID : expandID (d .Get ("private_network.0.endpoint_id" )),
252- }, scw .WithContext (ctx ))
253- if err != nil {
254- return diag .FromErr (err )
267+ // delete old endpoint
268+ for _ , e := range rr .Endpoints {
269+ if e .PrivateNetwork != nil {
270+ err := rdbAPI .DeleteEndpoint (& rdb.DeleteEndpointRequest {
271+ Region : region ,
272+ EndpointID : e .ID ,
273+ }, scw .WithContext (ctx ))
274+ if err != nil {
275+ return diag .FromErr (err )
276+ }
277+ }
278+ }
279+ // retrieve state
280+ _ , err = waitForRDBReadReplica (ctx , rdbAPI , region , ID , d .Timeout (schema .TimeoutRead ))
281+ if err != nil {
282+ return diag .FromErr (err )
283+ }
284+ // create a new one if defined
285+ if pn , pnExists := d .GetOk ("private_network" ); pnExists {
286+ // "enable_ipam" is not readable from the API, so we just read the user's config
287+ enableIpam := true
288+ if rawConfig := d .GetRawConfig (); ! rawConfig .IsNull () {
289+ pnRawConfig := rawConfig .AsValueMap ()["private_network" ].AsValueSlice ()[0 ].AsValueMap ()
290+ if ! pnRawConfig ["enable_ipam" ].IsNull () && pnRawConfig ["enable_ipam" ].False () ||
291+ ! pnRawConfig ["service_ip" ].IsNull () {
292+ enableIpam = false
293+ }
255294 }
256- } else {
257- pnEndpoint , err := expandReadReplicaEndpointsSpecPrivateNetwork (d .Get ("private_network" ))
295+ pnEndpoint , err := expandReadReplicaEndpointsSpecPrivateNetwork (pn , enableIpam )
258296 if err != nil {
259297 return diag .FromErr (err )
260298 }
0 commit comments