Skip to content

Commit 2dd57fa

Browse files
committed
merge server branch + re-record ipam data source cassette
2 parents 308daac + b0f5404 commit 2dd57fa

File tree

6 files changed

+4820
-3439
lines changed

6 files changed

+4820
-3439
lines changed

docs/resources/instance_server.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,10 @@ attached to the server. Updates to this field will trigger a stop/start of the s
250250
- `private_network` - (Optional) The private network associated with the server.
251251
Use the `pn_id` key to attach a [private_network](https://www.scaleway.com/en/developers/api/instance/#path-private-nics-list-all-private-nics) on your instance.
252252

253+
- `private_ip` - The list of private IPv4 addresses associated with the resource.
254+
- `id` - The ID of the IPv4 address resource.
255+
- `address` - The private IPv4 address.
256+
253257
- `boot_type` - The boot Type of the server. Possible values are: `local`, `bootscript` or `rescue`.
254258

255259
- `replace_on_type_change` - (Defaults to false) If true, the server will be replaced if `type` is changed. Otherwise, the server will migrate.

internal/services/instance/server.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
2020
block "github.com/scaleway/scaleway-sdk-go/api/block/v1alpha1"
2121
instanceSDK "github.com/scaleway/scaleway-sdk-go/api/instance/v1"
22+
ipamAPI "github.com/scaleway/scaleway-sdk-go/api/ipam/v1"
2223
"github.com/scaleway/scaleway-sdk-go/api/marketplace/v2"
2324
"github.com/scaleway/scaleway-sdk-go/scw"
2425
scwvalidation "github.com/scaleway/scaleway-sdk-go/validation"
@@ -31,6 +32,7 @@ import (
3132
"github.com/scaleway/terraform-provider-scaleway/v2/internal/meta"
3233
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/account"
3334
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/instance/instancehelpers"
35+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/ipam"
3436
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/vpc"
3537
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
3638
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
@@ -350,6 +352,25 @@ func ResourceServer() *schema.Resource {
350352
},
351353
},
352354
},
355+
"private_ips": {
356+
Type: schema.TypeList,
357+
Computed: true,
358+
Description: "List of private IPv4 addresses associated with the resource",
359+
Elem: &schema.Resource{
360+
Schema: map[string]*schema.Schema{
361+
"id": {
362+
Type: schema.TypeString,
363+
Computed: true,
364+
Description: "The ID of the IPv4 address resource",
365+
},
366+
"address": {
367+
Type: schema.TypeString,
368+
Computed: true,
369+
Description: "The private IPv4 address",
370+
},
371+
},
372+
},
373+
},
353374
"zone": zonal.Schema(),
354375
"organization_id": account.OrganizationIDSchema(),
355376
"project_id": account.ProjectIDSchema(),
@@ -773,6 +794,37 @@ func ResourceInstanceServerRead(ctx context.Context, d *schema.ResourceData, m i
773794
return diag.FromErr(err)
774795
}
775796

797+
privateNICIDs := []string(nil)
798+
for _, nic := range ph.privateNICsMap {
799+
privateNICIDs = append(privateNICIDs, nic.ID)
800+
}
801+
802+
allPrivateIPs := []map[string]interface{}(nil)
803+
resourceType := ipamAPI.ResourceTypeInstancePrivateNic
804+
805+
region, err := zone.Region()
806+
if err != nil {
807+
return diag.FromErr(err)
808+
}
809+
810+
for _, nicID := range privateNICIDs {
811+
opts := &ipam.GetResourcePrivateIPsOptions{
812+
ResourceType: &resourceType,
813+
ResourceID: &nicID,
814+
}
815+
816+
privateIPs, err := ipam.GetResourcePrivateIPs(ctx, m, region, opts)
817+
if err != nil {
818+
return diag.FromErr(err)
819+
}
820+
821+
if privateIPs != nil {
822+
allPrivateIPs = append(allPrivateIPs, privateIPs...)
823+
}
824+
}
825+
826+
_ = d.Set("private_ips", allPrivateIPs)
827+
776828
////
777829
// Display warning if server will soon reach End of Service
778830
////

internal/services/instance/server_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,6 +1108,8 @@ func TestAccServer_PrivateNetwork(t *testing.T) {
11081108
resource.TestCheckResourceAttrSet("scaleway_instance_server.base", "private_network.0.zone"),
11091109
resource.TestCheckResourceAttrPair("scaleway_instance_server.base", "private_network.0.pn_id",
11101110
"scaleway_vpc_private_network.internal", "id"),
1111+
resource.TestCheckResourceAttrSet("scaleway_instance_server.base", "private_ips.0.id"),
1112+
resource.TestCheckResourceAttrSet("scaleway_instance_server.base", "private_ips.0.address"),
11111113
),
11121114
},
11131115
{

internal/services/instance/testdata/server-private-network-missing-pnic.cassette.yaml

Lines changed: 1275 additions & 932 deletions
Large diffs are not rendered by default.

internal/services/instance/testdata/server-private-network.cassette.yaml

Lines changed: 2822 additions & 1989 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)