|
6 | 6 |
|
7 | 7 | "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
|
8 | 8 | "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
|
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" |
9 | 10 | redis "github.com/scaleway/scaleway-sdk-go/api/redis/v1alpha1"
|
10 | 11 | "github.com/scaleway/scaleway-sdk-go/scw"
|
11 | 12 | )
|
@@ -74,16 +75,6 @@ func resourceScalewayRedisCluster() *schema.Resource {
|
74 | 75 | Description: "Whether or not TLS is enabled.",
|
75 | 76 | ForceNew: true,
|
76 | 77 | },
|
77 |
| - "created_at": { |
78 |
| - Type: schema.TypeString, |
79 |
| - Computed: true, |
80 |
| - Description: "The date and time of the creation of the Redis cluster", |
81 |
| - }, |
82 |
| - "updated_at": { |
83 |
| - Type: schema.TypeString, |
84 |
| - Computed: true, |
85 |
| - Description: "The date and time of the last update of the Redis cluster", |
86 |
| - }, |
87 | 78 | "acl": {
|
88 | 79 | Type: schema.TypeList,
|
89 | 80 | Description: "List of acl rules.",
|
@@ -117,6 +108,74 @@ func resourceScalewayRedisCluster() *schema.Resource {
|
117 | 108 | Type: schema.TypeString,
|
118 | 109 | },
|
119 | 110 | },
|
| 111 | + "private_network": { |
| 112 | + Type: schema.TypeSet, |
| 113 | + Optional: true, |
| 114 | + Description: "Private network specs details", |
| 115 | + Elem: &schema.Resource{ |
| 116 | + Schema: map[string]*schema.Schema{ |
| 117 | + "endpoint_id": { |
| 118 | + Type: schema.TypeString, |
| 119 | + Computed: true, |
| 120 | + Description: "UUID of the endpoint to be connected to the cluster", |
| 121 | + }, |
| 122 | + "id": { |
| 123 | + Type: schema.TypeString, |
| 124 | + Required: true, |
| 125 | + ValidateFunc: validationUUIDorUUIDWithLocality(), |
| 126 | + Description: "UUID of the private network to be connected to the cluster", |
| 127 | + }, |
| 128 | + "service_ips": { |
| 129 | + Type: schema.TypeList, |
| 130 | + Required: true, |
| 131 | + Elem: &schema.Schema{ |
| 132 | + Type: schema.TypeString, |
| 133 | + ValidateFunc: validation.IsCIDR, |
| 134 | + }, |
| 135 | + Description: "List of IPv4 addresses of the private network with a CIDR notation", |
| 136 | + }, |
| 137 | + "zone": zoneSchema(), |
| 138 | + }, |
| 139 | + }, |
| 140 | + }, |
| 141 | + // Computed |
| 142 | + "public_network": { |
| 143 | + Type: schema.TypeList, |
| 144 | + Optional: true, |
| 145 | + Computed: true, |
| 146 | + MaxItems: 1, |
| 147 | + Description: "Public network specs details", |
| 148 | + Elem: &schema.Resource{ |
| 149 | + Schema: map[string]*schema.Schema{ |
| 150 | + "id": { |
| 151 | + Type: schema.TypeString, |
| 152 | + Computed: true, |
| 153 | + }, |
| 154 | + "port": { |
| 155 | + Type: schema.TypeInt, |
| 156 | + Computed: true, |
| 157 | + Description: "TCP port of the endpoint", |
| 158 | + }, |
| 159 | + "ips": { |
| 160 | + Type: schema.TypeList, |
| 161 | + Elem: &schema.Schema{ |
| 162 | + Type: schema.TypeString, |
| 163 | + }, |
| 164 | + Computed: true, |
| 165 | + }, |
| 166 | + }, |
| 167 | + }, |
| 168 | + }, |
| 169 | + "created_at": { |
| 170 | + Type: schema.TypeString, |
| 171 | + Computed: true, |
| 172 | + Description: "The date and time of the creation of the Redis cluster", |
| 173 | + }, |
| 174 | + "updated_at": { |
| 175 | + Type: schema.TypeString, |
| 176 | + Computed: true, |
| 177 | + Description: "The date and time of the last update of the Redis cluster", |
| 178 | + }, |
120 | 179 | // Common
|
121 | 180 | "zone": zoneSchema(),
|
122 | 181 | "project_id": projectIDSchema(),
|
@@ -165,6 +224,15 @@ func resourceScalewayRedisClusterCreate(ctx context.Context, d *schema.ResourceD
|
165 | 224 | createReq.ClusterSettings = expandRedisSettings(settings)
|
166 | 225 | }
|
167 | 226 |
|
| 227 | + privN, privNExists := d.GetOk("private_network") |
| 228 | + if privNExists { |
| 229 | + pnSpecs, err := expandRedisPrivateNetwork(privN.(*schema.Set).List()) |
| 230 | + if err != nil { |
| 231 | + return diag.FromErr(err) |
| 232 | + } |
| 233 | + createReq.Endpoints = pnSpecs |
| 234 | + } |
| 235 | + |
168 | 236 | res, err := redisAPI.CreateCluster(createReq, scw.WithContext(ctx))
|
169 | 237 | if err != nil {
|
170 | 238 | return diag.FromErr(err)
|
@@ -216,6 +284,13 @@ func resourceScalewayRedisClusterRead(ctx context.Context, d *schema.ResourceDat
|
216 | 284 | _ = d.Set("tags", cluster.Tags)
|
217 | 285 | }
|
218 | 286 |
|
| 287 | + // set endpoints |
| 288 | + pnI, pnExists := flattenRedisPrivateNetwork(cluster.Endpoints) |
| 289 | + if pnExists { |
| 290 | + _ = d.Set("private_network", pnI) |
| 291 | + } |
| 292 | + _ = d.Set("public_network", flattenRedisPublicNetwork(cluster.Endpoints)) |
| 293 | + |
219 | 294 | return nil
|
220 | 295 | }
|
221 | 296 |
|
@@ -303,6 +378,13 @@ func resourceScalewayRedisClusterUpdate(ctx context.Context, d *schema.ResourceD
|
303 | 378 | }
|
304 | 379 | }
|
305 | 380 |
|
| 381 | + if d.HasChanges("private_network") { |
| 382 | + diagnostics := resourceScalewayRedisClusterUpdateEndpoints(ctx, d, redisAPI, zone, ID) |
| 383 | + if diagnostics != nil { |
| 384 | + return diagnostics |
| 385 | + } |
| 386 | + } |
| 387 | + |
306 | 388 | _, err = waitForRedisCluster(ctx, redisAPI, zone, ID, d.Timeout(schema.TimeoutUpdate))
|
307 | 389 | if err != nil {
|
308 | 390 | return diag.FromErr(err)
|
@@ -344,6 +426,42 @@ func resourceScalewayRedisClusterUpdateSettings(ctx context.Context, d *schema.R
|
344 | 426 | return nil
|
345 | 427 | }
|
346 | 428 |
|
| 429 | +func resourceScalewayRedisClusterUpdateEndpoints(ctx context.Context, d *schema.ResourceData, redisAPI *redis.API, zone scw.Zone, clusterID string) diag.Diagnostics { |
| 430 | + // retrieve state |
| 431 | + cluster, err := waitForRedisCluster(ctx, redisAPI, zone, clusterID, d.Timeout(schema.TimeoutUpdate)) |
| 432 | + if err != nil { |
| 433 | + return diag.FromErr(err) |
| 434 | + } |
| 435 | + |
| 436 | + // get new desired state of endpoints |
| 437 | + rawNewEndpoints := d.Get("private_network") |
| 438 | + newEndpoints, err := expandRedisPrivateNetwork(rawNewEndpoints.(*schema.Set).List()) |
| 439 | + if err != nil { |
| 440 | + return diag.FromErr(err) |
| 441 | + } |
| 442 | + if len(newEndpoints) == 0 { |
| 443 | + newEndpoints = append(newEndpoints, &redis.EndpointSpec{ |
| 444 | + PublicNetwork: &redis.EndpointSpecPublicNetworkSpec{}, |
| 445 | + }) |
| 446 | + } |
| 447 | + // send request |
| 448 | + _, err = redisAPI.SetEndpoints(&redis.SetEndpointsRequest{ |
| 449 | + Zone: cluster.Zone, |
| 450 | + ClusterID: cluster.ID, |
| 451 | + Endpoints: newEndpoints, |
| 452 | + }, scw.WithContext(ctx)) |
| 453 | + if err != nil { |
| 454 | + return diag.FromErr(err) |
| 455 | + } |
| 456 | + |
| 457 | + _, err = waitForRedisCluster(ctx, redisAPI, zone, clusterID, d.Timeout(schema.TimeoutUpdate)) |
| 458 | + if err != nil { |
| 459 | + return diag.FromErr(err) |
| 460 | + } |
| 461 | + |
| 462 | + return nil |
| 463 | +} |
| 464 | + |
347 | 465 | func resourceScalewayRedisClusterDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
348 | 466 | redisAPI, zone, ID, err := redisAPIWithZoneAndID(meta, d.Id())
|
349 | 467 | if err != nil {
|
|
0 commit comments