Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/resources/redis_cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ the form `{zone}/{id}`, e.g. `fr-par-1/11111111-1111-1111-1111-111111111111`
- `endpoint_id` - The ID of the endpoint.
- `zone` - The zone of the Private Network.

- `private_ips` - The list of private IPv4 addresses associated with the resource.
- `id` - The ID of the IPv4 address resource.
- `address` - The private IPv4 address.

- `created_at` - The date and time of creation of the Redis™ cluster.
- `updated_at` - The date and time of the last update of the Redis™ cluster.
- `certificate` - The PEM of the certificate used by redis, only when `tls_enabled` is true
Expand Down

Large diffs are not rendered by default.

69 changes: 68 additions & 1 deletion internal/services/redis/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import (
"strings"
"time"

"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
ipamAPI "github.com/scaleway/scaleway-sdk-go/api/ipam/v1"
"github.com/scaleway/scaleway-sdk-go/api/redis/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/cdf"
Expand All @@ -19,6 +21,7 @@ import (
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/zonal"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/account"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/ipam"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
)
Expand Down Expand Up @@ -193,6 +196,25 @@ func ResourceCluster() *schema.Resource {
},
},
},
"private_ips": {
Type: schema.TypeList,
Computed: true,
Description: "List of private IPv4 addresses associated with the resource",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: "The ID of the IPv4 address resource",
},
"address": {
Type: schema.TypeString,
Computed: true,
Description: "The private IPv4 address",
},
},
},
},
"certificate": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -344,11 +366,56 @@ func ResourceClusterRead(ctx context.Context, d *schema.ResourceData, m interfac
}

// set endpoints
allPrivateIPs := []map[string]interface{}(nil)
diags := diag.Diagnostics{}

pnI, pnExists := flattenPrivateNetwork(cluster.Endpoints)
if pnExists {
_ = d.Set("private_network", pnI)

privateNetworkIDs := []string(nil)

for _, endpoint := range cluster.Endpoints {
if endpoint.PrivateNetwork != nil {
privateNetworkIDs = append(privateNetworkIDs, endpoint.PrivateNetwork.ID)
}
}

resourceType := ipamAPI.ResourceTypeRedisCluster

region, err := zone.Region()
if err != nil {
return diag.FromErr(err)
}

for _, privateNetworkID := range privateNetworkIDs {
opts := &ipam.GetResourcePrivateIPsOptions{
ResourceType: &resourceType,
PrivateNetworkID: &privateNetworkID,
ResourceID: &cluster.ID,
}

privateIPs, err := ipam.GetResourcePrivateIPs(ctx, m, region, opts)
if err != nil {
if !httperrors.Is403(err) {
return diag.FromErr(err)
}

diags = append(diags, diag.Diagnostic{
Severity: diag.Warning,
Summary: err.Error(),
Detail: "Got 403 while reading private IP from IPAM API, please check your IAM permissions",
AttributePath: cty.GetAttrPath("private_ips"),
})
}

if privateIPs != nil {
allPrivateIPs = append(allPrivateIPs, privateIPs...)
}
}
}

_ = d.Set("private_ips", allPrivateIPs)
_ = d.Set("public_network", flattenPublicNetwork(cluster.Endpoints))

if cluster.TLSEnabled {
Expand All @@ -370,7 +437,7 @@ func ResourceClusterRead(ctx context.Context, d *schema.ResourceData, m interfac
_ = d.Set("certificate", "")
}

return nil
return diags
}

func ResourceClusterUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
Expand Down
6 changes: 6 additions & 0 deletions internal/services/redis/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,12 @@ func TestAccCluster_MigrateClusterSizeWithIPAMEndpoint(t *testing.T) {
resource.TestCheckResourceAttr("scaleway_redis_cluster.main", "tls_enabled", "true"),
resource.TestCheckResourceAttrPair("scaleway_redis_cluster.main", "private_network.0.id", "scaleway_vpc_private_network.private_network", "id"),
acctest.CheckResourceIDChanged("scaleway_redis_cluster.main", &clusterID),
resource.TestCheckResourceAttrSet("scaleway_redis_cluster.main", "private_ips.0.id"),
resource.TestCheckResourceAttrSet("scaleway_redis_cluster.main", "private_ips.0.address"),
resource.TestCheckResourceAttrSet("scaleway_redis_cluster.main", "private_ips.1.id"),
resource.TestCheckResourceAttrSet("scaleway_redis_cluster.main", "private_ips.1.address"),
resource.TestCheckResourceAttrSet("scaleway_redis_cluster.main", "private_ips.2.id"),
resource.TestCheckResourceAttrSet("scaleway_redis_cluster.main", "private_ips.2.address"),
),
},
},
Expand Down
Loading
Loading