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/mongodb_instance.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ In addition to all arguments above, the following attributes are exported:
- `ips` - List of IP addresses for your endpoint.
- `port` - TCP port of the endpoint.
- `dns_records` - List of DNS records for your endpoint.
- `private_ip` - The private IPv4 address associated with the instance.
- `id` - The ID of the IPv4 address resource.
- `address` - The private IPv4 address.
- `public_network` - Private Network endpoints of the Database Instance.
- `id` - The ID of the endpoint.
- `port` - TCP port of the endpoint.
Expand Down
75 changes: 74 additions & 1 deletion internal/services/mongodb/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ package mongodb
import (
"context"
"errors"
"fmt"
"time"

"github.com/hashicorp/go-cty/cty"
"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"
mongodb "github.com/scaleway/scaleway-sdk-go/api/mongodb/v1alpha1"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/dsf"
Expand All @@ -16,6 +19,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 @@ -152,6 +156,26 @@ func ResourceInstance() *schema.Resource {
},
},
// Computed
"private_ip": {
Type: schema.TypeList,
Computed: true,
Optional: 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",
},
},
},
},
"public_network": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -362,10 +386,59 @@ func ResourceInstanceRead(ctx context.Context, d *schema.ResourceData, m interfa
_ = d.Set("public_network", publicNetworkEndpoint)
}

diags := diag.Diagnostics{}
privateIPs := []map[string]interface{}(nil)
authorized := true

privateNetworkEndpoint, privateNetworkExists := flattenPrivateNetwork(instance.Endpoints)

if privateNetworkExists {
_ = d.Set("private_network", privateNetworkEndpoint)

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

resourceType := ipamAPI.ResourceTypeMgdbInstance
opts := &ipam.GetResourcePrivateIPsOptions{
ResourceID: &instance.ID,
ResourceType: &resourceType,
PrivateNetworkID: &endpoint.PrivateNetwork.PrivateNetworkID,
ProjectID: &instance.ProjectID,
}

endpointPrivateIPs, err := ipam.GetResourcePrivateIPs(ctx, m, region, opts)

switch {
case err == nil:
privateIPs = append(privateIPs, endpointPrivateIPs...)
case httperrors.Is403(err):
authorized = false

diags = append(diags, diag.Diagnostic{
Severity: diag.Warning,
Summary: "Unauthorized to read MongoDB Instance's private IP, please check your IAM permissions",
Detail: err.Error(),
AttributePath: cty.GetAttrPath("private_ip"),
})
default:
diags = append(diags, diag.Diagnostic{
Severity: diag.Warning,
Summary: fmt.Sprintf("Unable to get private IP for instance %q", instance.Name),
Detail: err.Error(),
AttributePath: cty.GetAttrPath("private_ip"),
})
}

if !authorized {
break
}
}
}

if authorized {
_ = d.Set("private_ip", privateIPs)
}

if len(instance.Settings) > 0 {
Expand All @@ -377,7 +450,7 @@ func ResourceInstanceRead(ctx context.Context, d *schema.ResourceData, m interfa
_ = d.Set("settings", settingsMap)
}

return nil
return diags
}

func ResourceInstanceUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
Expand Down
6 changes: 6 additions & 0 deletions internal/services/mongodb/instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ func TestAccMongoDBInstance_WithPrivateNetwork(t *testing.T) {
resource.TestCheckResourceAttrSet("scaleway_mongodb_instance.main", "private_network.0.id"),
resource.TestCheckResourceAttrSet("scaleway_mongodb_instance.main", "private_network.0.port"),
resource.TestCheckResourceAttrSet("scaleway_mongodb_instance.main", "private_network.0.dns_records.0"),
resource.TestCheckResourceAttrSet("scaleway_mongodb_instance.main", "private_ip.0.id"),
resource.TestCheckResourceAttrSet("scaleway_mongodb_instance.main", "private_ip.0.address"),
),
},
},
Expand Down Expand Up @@ -309,6 +311,8 @@ func TestAccMongoDBInstance_UpdatePrivateNetwork(t *testing.T) {
resource.TestCheckResourceAttr("scaleway_mongodb_instance.main", "private_network.#", "1"),
resource.TestCheckResourceAttrSet("scaleway_mongodb_instance.main", "private_network.0.pn_id"),
resource.TestCheckResourceAttrSet("scaleway_mongodb_instance.main", "private_network.0.id"),
resource.TestCheckResourceAttrSet("scaleway_mongodb_instance.main", "private_ip.0.id"),
resource.TestCheckResourceAttrSet("scaleway_mongodb_instance.main", "private_ip.0.address"),
),
},
{
Expand Down Expand Up @@ -341,6 +345,8 @@ func TestAccMongoDBInstance_UpdatePrivateNetwork(t *testing.T) {
resource.TestCheckResourceAttr("scaleway_mongodb_instance.main", "private_network.#", "1"),
resource.TestCheckResourceAttrSet("scaleway_mongodb_instance.main", "private_network.0.pn_id"),
resource.TestCheckResourceAttrSet("scaleway_mongodb_instance.main", "private_network.0.id"),
resource.TestCheckResourceAttrSet("scaleway_mongodb_instance.main", "private_ip.0.id"),
resource.TestCheckResourceAttrSet("scaleway_mongodb_instance.main", "private_ip.0.address"),
),
},
{
Expand Down
Loading
Loading