Skip to content

feat(baremetal): read private ips #3071

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 13, 2025
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/baremetal_server.md
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,9 @@ In addition to all arguments above, the following attributes are exported:
- `status` - The private network status.
- `created_at` - The date and time of the creation of the private network.
- `updated_at` - The date and time of the last update of the private network.
- `private_ips` - The list of private IPv4 and IPv6 addresses associated with the resource.
- `id` - The ID of the IP address resource.
- `address` - The private IP address.
- `ips` - (List of) The IPs of the server.
- `id` - The ID of the IP.
- `address` - The address of the IP.
Expand Down
59 changes: 58 additions & 1 deletion internal/services/baremetal/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/scaleway/scaleway-sdk-go/api/baremetal/v1"
baremetalV3 "github.com/scaleway/scaleway-sdk-go/api/baremetal/v3"
ipamAPI "github.com/scaleway/scaleway-sdk-go/api/ipam/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
sdkValidation "github.com/scaleway/scaleway-sdk-go/validation"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/cdf"
Expand All @@ -21,6 +22,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 @@ -268,6 +270,25 @@ If this behaviour is wanted, please set 'reinstall_on_ssh_key_changes' argument
Optional: true,
Description: "The partitioning schema in json format",
},
"private_ips": {
Type: schema.TypeList,
Computed: true,
Description: "List of private IPv4 and IPv6 addresses associated with the resource",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: "The ID of the IP address resource",
},
"address": {
Type: schema.TypeString,
Computed: true,
Description: "The private IP address",
},
},
},
},
},
CustomizeDiff: customdiff.Sequence(
customDiffOffer(),
Expand Down Expand Up @@ -508,7 +529,43 @@ func ResourceServerRead(ctx context.Context, d *schema.ResourceData, m interface

_ = d.Set("private_network", flattenPrivateNetworks(pnRegion, listPrivateNetworks.ServerPrivateNetworks))

return nil
privateNetworkIDs := make([]string, 0, listPrivateNetworks.TotalCount)
for _, pn := range listPrivateNetworks.ServerPrivateNetworks {
privateNetworkIDs = append(privateNetworkIDs, pn.PrivateNetworkID)
}

allPrivateIPs := make([]map[string]interface{}, 0, listPrivateNetworks.TotalCount)
diags := diag.Diagnostics{}

for _, privateNetworkID := range privateNetworkIDs {
resourceType := ipamAPI.ResourceTypeBaremetalPrivateNic
opts := &ipam.GetResourcePrivateIPsOptions{
ResourceType: &resourceType,
PrivateNetworkID: &privateNetworkID,
}

privateIPs, err := ipam.GetResourcePrivateIPs(ctx, m, pnRegion, 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 IPs 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)

return diags
}

//gocyclo:ignore
Expand Down
2 changes: 2 additions & 0 deletions internal/services/baremetal/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,8 @@ func TestAccServer_CreateServerWithPrivateNetwork(t *testing.T) {
testAccCheckBaremetalServerExists(tt, "scaleway_baremetal_server.base"),
testAccCheckBaremetalServerHasPrivateNetwork(tt, "scaleway_baremetal_server.base"),
resource.TestCheckResourceAttrPair("scaleway_baremetal_server.base", "private_network.0.id", "scaleway_vpc_private_network.pn", "id"),
resource.TestCheckResourceAttrSet("scaleway_baremetal_server.base", "private_ips.0.id"),
resource.TestCheckResourceAttrSet("scaleway_baremetal_server.base", "private_ips.0.address"),
),
},
},
Expand Down

Large diffs are not rendered by default.

3,240 changes: 1,963 additions & 1,277 deletions internal/services/baremetal/testdata/server-add-private-network.cassette.yaml

Large diffs are not rendered by default.

Large diffs are not rendered by default.

3,814 changes: 2,103 additions & 1,711 deletions internal/services/baremetal/testdata/server-with-ipam-private-network.cassette.yaml

Large diffs are not rendered by default.

67 changes: 67 additions & 0 deletions internal/services/ipam/helpers.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package ipam

import (
"context"
"fmt"
"net"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
ipam "github.com/scaleway/scaleway-sdk-go/api/ipam/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/meta"
)
Expand Down Expand Up @@ -65,3 +68,67 @@ func diffSuppressFuncStandaloneIPandCIDR(_, oldValue, newValue string, _ *schema

return false
}

type GetResourcePrivateIPsOptions struct {
ResourceType *ipam.ResourceType
ResourceID *string
ResourceName *string
PrivateNetworkID *string
}

// GetResourcePrivateIPs fetches the private IP addresses of a resource in a private network.
func GetResourcePrivateIPs(ctx context.Context, m interface{}, region scw.Region, opts *GetResourcePrivateIPsOptions) ([]map[string]interface{}, error) {
ipamAPI := ipam.NewAPI(meta.ExtractScwClient(m))

req := &ipam.ListIPsRequest{
Region: region,
}

if opts != nil {
if opts.PrivateNetworkID != nil {
req.PrivateNetworkID = opts.PrivateNetworkID
}

if opts.ResourceID != nil {
req.ResourceID = opts.ResourceID
}

if opts.ResourceName != nil {
req.ResourceName = opts.ResourceName
}

if opts.ResourceType != nil {
req.ResourceType = *opts.ResourceType
}
}

resp, err := ipamAPI.ListIPs(req, scw.WithContext(ctx))
if err != nil {
if httperrors.Is403(err) {
return nil, err
}

return nil, fmt.Errorf("error fetching IPs from IPAM: %w", err)
}

if len(resp.IPs) == 0 {
return nil, nil
}

ipList := make([]map[string]interface{}, 0, len(resp.IPs))

for _, ip := range resp.IPs {
ipNet := ip.Address
if ipNet.IP == nil {
continue
}

ipMap := map[string]interface{}{
"id": regional.NewIDString(region, ip.ID),
"address": ipNet.IP.String(),
}
ipList = append(ipList, ipMap)
}

return ipList, nil
}
Loading