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
3 changes: 3 additions & 0 deletions docs/resources/rdb_instance.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ are of the form `{region}/{id}`, e.g. `fr-par/11111111-1111-1111-1111-1111111111
- `port` - Port in the Private Network.
- `name` - Name of the endpoint.
- `hostname` - Hostname of the endpoint.
- `private_ip` - The private IPv4 address associated with the resource.
- `id` - The ID of the IPv4 address resource.
- `address` - The private IPv4 address.
- `certificate` - Certificate of the Database Instance.
- `organization_id` - The organization ID the Database Instance is associated with.

Expand Down
500 changes: 299 additions & 201 deletions internal/services/ipam/testdata/data-source-ipamiprdb.cassette.yaml

Large diffs are not rendered by default.

60 changes: 59 additions & 1 deletion internal/services/rdb/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import (
"fmt"
"io"

"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"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/rdb/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/cdf"
Expand All @@ -18,6 +20,7 @@ import (
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
"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 @@ -322,6 +325,25 @@ func ResourceInstance() *schema.Resource {
Optional: true,
Description: "Enable or disable encryption at rest for the database instance",
},
"private_ip": {
Type: schema.TypeList,
Computed: true,
Description: "The private IPv4 address 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",
},
},
},
},
// Common
"region": regional.Schema(),
"organization_id": account.OrganizationIDSchema(),
Expand Down Expand Up @@ -640,15 +662,51 @@ func ResourceRdbInstanceRead(ctx context.Context, d *schema.ResourceData, m inte
_ = d.Set("logs_policy", flattenInstanceLogsPolicy(res.LogsPolicy))

// set endpoints
privateIPs := make([]map[string]interface{}, 0, 1)
diags := diag.Diagnostics{}

if pnI, pnExist := flattenPrivateNetwork(res.Endpoints); pnExist {
_ = d.Set("private_network", pnI)

for _, endpoint := range res.Endpoints {
if endpoint.PrivateNetwork == nil {
continue
}

if endpoint.PrivateNetwork.ProvisioningMode == rdb.EndpointPrivateNetworkDetailsProvisioningModeIpam {
resourceType := ipamAPI.ResourceTypeRdbInstance
opts := &ipam.GetResourcePrivateIPsOptions{
ResourceID: &res.ID,
ResourceType: &resourceType,
PrivateNetworkID: &endpoint.PrivateNetwork.PrivateNetworkID,
}

endpointPrivateIPs, 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_ip"),
})
}

privateIPs = append(privateIPs, endpointPrivateIPs...)
}
}
}

_ = d.Set("private_ip", privateIPs)

if lbI, lbExists := flattenLoadBalancer(res.Endpoints); lbExists {
_ = d.Set("load_balancer", lbI)
}

return nil
return diags
}

//gocyclo:ignore
Expand Down
2 changes: 2 additions & 0 deletions internal/services/rdb/instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,8 @@ func TestAccInstance_Endpoints(t *testing.T) {
resource.TestCheckResourceAttr("scaleway_rdb_instance.test_endpoints", "load_balancer.#", "0"),
resource.TestCheckResourceAttr("scaleway_rdb_instance.test_endpoints", "endpoint_ip", ""), // Deprecated attribute, might be deleted later
resource.TestCheckResourceAttr("scaleway_rdb_instance.test_endpoints", "endpoint_port", "0"), // Deprecated attribute, might be deleted later
resource.TestCheckResourceAttrSet("scaleway_rdb_instance.test_endpoints", "private_ip.0.id"),
resource.TestCheckResourceAttrSet("scaleway_rdb_instance.test_endpoints", "private_ip.0.address"),
),
},
{
Expand Down
1,316 changes: 805 additions & 511 deletions internal/services/rdb/testdata/instance-endpoints.cassette.yaml

Large diffs are not rendered by default.

Loading
Loading