-
Notifications
You must be signed in to change notification settings - Fork 130
feat(ipam): show private ips on resources #2759
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
base: master
Are you sure you want to change the base?
Changes from all commits
0b33647
58c737d
7595a71
7ae769b
0f1f66e
c75938c
644b8ee
d04b097
9619f51
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ import ( | |
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" | ||
"github.com/scaleway/scaleway-sdk-go/api/baremetal/v1" | ||
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" | ||
|
@@ -18,6 +19,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" | ||
) | ||
|
@@ -244,6 +246,25 @@ If this behaviour is wanted, please set 'reinstall_on_ssh_key_changes' argument | |
}, | ||
}, | ||
}, | ||
"private_ip": { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would plural make sense here if it is a list? |
||
Type: schema.TypeList, | ||
Computed: true, | ||
Description: "List of private IP 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", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would precise that it is an IPv4 |
||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
CustomizeDiff: customdiff.Sequence( | ||
cdf.LocalityCheck("private_network.#.id"), | ||
|
@@ -464,12 +485,35 @@ func ResourceServerRead(ctx context.Context, d *schema.ResourceData, m interface | |
if err != nil { | ||
return diag.FromErr(fmt.Errorf("failed to list server's private networks: %w", err)) | ||
} | ||
|
||
pnRegion, err := server.Zone.Region() | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
_ = d.Set("private_network", flattenPrivateNetworks(pnRegion, listPrivateNetworks.ServerPrivateNetworks)) | ||
|
||
privateNetworkIDs := make([]string, 0, len(listPrivateNetworks.ServerPrivateNetworks)) | ||
for _, pn := range listPrivateNetworks.ServerPrivateNetworks { | ||
privateNetworkIDs = append(privateNetworkIDs, pn.PrivateNetworkID) | ||
} | ||
|
||
var allPrivateIPs []map[string]interface{} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Considering server will have at least 1 IP per connected private network. This should be allocated for the minimum size. |
||
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 { | ||
return diag.FromErr(err) | ||
} | ||
if privateIPs != nil { | ||
allPrivateIPs = append(allPrivateIPs, privateIPs...) | ||
} | ||
} | ||
_ = d.Set("private_ip", allPrivateIPs) | ||
|
||
return nil | ||
} | ||
|
||
|
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,13 +6,15 @@ import ( | |
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/scaleway/scaleway-sdk-go/api/instance/v1" | ||
ipamAPI "github.com/scaleway/scaleway-sdk-go/api/ipam/v1" | ||
"github.com/scaleway/scaleway-sdk-go/scw" | ||
"github.com/scaleway/terraform-provider-scaleway/v2/internal/cdf" | ||
"github.com/scaleway/terraform-provider-scaleway/v2/internal/dsf" | ||
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors" | ||
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality" | ||
"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/ipam" | ||
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types" | ||
) | ||
|
||
|
@@ -68,6 +70,25 @@ func ResourcePrivateNIC() *schema.Resource { | |
Description: "IPAM ip list, should be for internal use only", | ||
ForceNew: true, | ||
}, | ||
"private_ip": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Description: "List of private IP 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", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would precise it is an IPv4 |
||
}, | ||
"address": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "The private IP address", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would precise it is an IPv4 |
||
}, | ||
}, | ||
}, | ||
}, | ||
"ipam_ip_ids": { | ||
Type: schema.TypeList, | ||
Elem: &schema.Schema{ | ||
|
@@ -160,6 +181,23 @@ func ResourceInstancePrivateNICRead(ctx context.Context, d *schema.ResourceData, | |
_ = d.Set("tags", privateNIC.Tags) | ||
} | ||
|
||
region, err := zone.Region() | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
resourceType := ipamAPI.ResourceTypeInstancePrivateNic | ||
opts := &ipam.GetResourcePrivateIPsOptions{ | ||
ResourceID: &privateNIC.ID, | ||
ResourceType: &resourceType, | ||
PrivateNetworkID: &privateNIC.PrivateNetworkID, | ||
} | ||
privateIP, err := ipam.GetResourcePrivateIPs(ctx, m, region, opts) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
_ = d.Set("private_ip", privateIP) | ||
|
||
return nil | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ import ( | |
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" | ||
block "github.com/scaleway/scaleway-sdk-go/api/block/v1alpha1" | ||
instanceSDK "github.com/scaleway/scaleway-sdk-go/api/instance/v1" | ||
ipamAPI "github.com/scaleway/scaleway-sdk-go/api/ipam/v1" | ||
"github.com/scaleway/scaleway-sdk-go/api/marketplace/v2" | ||
"github.com/scaleway/scaleway-sdk-go/scw" | ||
scwvalidation "github.com/scaleway/scaleway-sdk-go/validation" | ||
|
@@ -30,6 +31,7 @@ import ( | |
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/zonal" | ||
"github.com/scaleway/terraform-provider-scaleway/v2/internal/meta" | ||
"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/services/vpc" | ||
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types" | ||
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify" | ||
|
@@ -336,6 +338,25 @@ func ResourceServer() *schema.Resource { | |
}, | ||
}, | ||
}, | ||
"private_ips": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Description: "List of private IP 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", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would precise it is an IPv4 |
||
}, | ||
"address": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "The private IP address", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would precise it is an IPv4 |
||
}, | ||
}, | ||
}, | ||
}, | ||
"routed_ip_enabled": { | ||
Type: schema.TypeBool, | ||
Description: "If server supports routed IPs, default to true if public_ips is used", | ||
|
@@ -758,6 +779,32 @@ func ResourceInstanceServerRead(ctx context.Context, d *schema.ResourceData, m i | |
return diag.FromErr(err) | ||
} | ||
|
||
var privateNICIDs []string | ||
for _, nic := range ph.privateNICsMap { | ||
privateNICIDs = append(privateNICIDs, nic.ID) | ||
} | ||
|
||
var allPrivateIPs []map[string]interface{} | ||
resourceType := ipamAPI.ResourceTypeInstancePrivateNic | ||
region, err := zone.Region() | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
for _, nicID := range privateNICIDs { | ||
opts := &ipam.GetResourcePrivateIPsOptions{ | ||
ResourceType: &resourceType, | ||
ResourceID: &nicID, | ||
} | ||
privateIPs, err := ipam.GetResourcePrivateIPs(ctx, m, region, opts) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
if privateIPs != nil { | ||
allPrivateIPs = append(allPrivateIPs, privateIPs...) | ||
} | ||
} | ||
_ = d.Set("private_ips", allPrivateIPs) | ||
|
||
return nil | ||
} | ||
return nil | ||
|
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would precise it is an IPv4