Skip to content

Commit 7bedecc

Browse files
authored
feat(apple-silicon): read instance private ips (#3113)
1 parent 01c5dcf commit 7bedecc

File tree

4 files changed

+1540
-913
lines changed

4 files changed

+1540
-913
lines changed

docs/resources/apple_silicon_server.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ In addition to all arguments above, the following attributes are exported:
4949
- `state` - The state of the server.
5050
- `ip` - IPv4 address of the server (IPv4 address).
5151
- `vnc_url` - URL of the VNC.
52+
- `private_ips` - The list of private IPv4 and IPv6 addresses associated with the server.
53+
- `id` - The ID of the IP address resource.
54+
- `address` - The private IP address.
5255
- `created_at` - The date and time of the creation of the Apple Silicon server.
5356
- `updated_at` - The date and time of the last update of the Apple Silicon server.
5457
- `deleted_at` - The minimal date and time on which you can delete this server due to Apple licence.

internal/services/applesilicon/server.go

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,21 @@ package applesilicon
22

33
import (
44
"context"
5+
"fmt"
56
"time"
67

8+
"github.com/hashicorp/go-cty/cty"
79
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
810
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
911
applesilicon "github.com/scaleway/scaleway-sdk-go/api/applesilicon/v1alpha1"
12+
ipamAPI "github.com/scaleway/scaleway-sdk-go/api/ipam/v1"
1013
"github.com/scaleway/scaleway-sdk-go/scw"
1114
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
1215
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality"
1316
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/zonal"
1417
"github.com/scaleway/terraform-provider-scaleway/v2/internal/meta"
1518
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/account"
19+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/ipam"
1620
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
1721
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
1822
)
@@ -112,6 +116,26 @@ func ResourceServer() *schema.Resource {
112116
Description: "IPv4 address of the server",
113117
Computed: true,
114118
},
119+
"private_ips": {
120+
Type: schema.TypeList,
121+
Computed: true,
122+
Optional: true,
123+
Description: "List of private IPv4 and IPv6 addresses associated with the server",
124+
Elem: &schema.Resource{
125+
Schema: map[string]*schema.Schema{
126+
"id": {
127+
Type: schema.TypeString,
128+
Computed: true,
129+
Description: "The ID of the IP address resource",
130+
},
131+
"address": {
132+
Type: schema.TypeString,
133+
Computed: true,
134+
Description: "The private IP address",
135+
},
136+
},
137+
},
138+
},
115139
"vnc_url": {
116140
Type: schema.TypeString,
117141
Description: "VNC url use to connect remotely to the desktop GUI",
@@ -262,7 +286,56 @@ func ResourceAppleSiliconServerRead(ctx context.Context, d *schema.ResourceData,
262286

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

265-
return nil
289+
privateNetworkIDs := make([]string, 0, listPrivateNetworks.TotalCount)
290+
for _, pn := range listPrivateNetworks.ServerPrivateNetworks {
291+
privateNetworkIDs = append(privateNetworkIDs, pn.PrivateNetworkID)
292+
}
293+
294+
diags := diag.Diagnostics{}
295+
allPrivateIPs := make([]map[string]interface{}, 0, listPrivateNetworks.TotalCount)
296+
authorized := true
297+
298+
for _, privateNetworkID := range privateNetworkIDs {
299+
resourceType := ipamAPI.ResourceTypeAppleSiliconPrivateNic
300+
opts := &ipam.GetResourcePrivateIPsOptions{
301+
ResourceType: &resourceType,
302+
PrivateNetworkID: &privateNetworkID,
303+
ProjectID: &res.ProjectID,
304+
}
305+
306+
privateIPs, err := ipam.GetResourcePrivateIPs(ctx, m, pnRegion, opts)
307+
308+
switch {
309+
case err == nil:
310+
allPrivateIPs = append(allPrivateIPs, privateIPs...)
311+
case httperrors.Is403(err):
312+
authorized = false
313+
314+
diags = append(diags, diag.Diagnostic{
315+
Severity: diag.Warning,
316+
Summary: "Unauthorized to read server's private IPs, please check your IAM permissions",
317+
Detail: err.Error(),
318+
AttributePath: cty.GetAttrPath("private_ips"),
319+
})
320+
default:
321+
diags = append(diags, diag.Diagnostic{
322+
Severity: diag.Warning,
323+
Summary: fmt.Sprintf("Unable to get private IP for server %q", res.Name),
324+
Detail: err.Error(),
325+
AttributePath: cty.GetAttrPath("private_ips"),
326+
})
327+
}
328+
329+
if !authorized {
330+
break
331+
}
332+
}
333+
334+
if authorized {
335+
_ = d.Set("private_ips", allPrivateIPs)
336+
}
337+
338+
return diags
266339
}
267340

268341
func ResourceAppleSiliconServerUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {

internal/services/applesilicon/server_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ func TestAccServer_EnableVPC(t *testing.T) {
136136
resource.TestCheckResourceAttrSet("scaleway_apple_silicon_server.main", "deletable_at"),
137137
resource.TestCheckResourceAttrPair("scaleway_apple_silicon_server.main", "private_network.0.id", "scaleway_vpc_private_network.pn01", "id"),
138138
resource.TestCheckResourceAttr("scaleway_apple_silicon_server.main", "vpc_status", "vpc_enabled"),
139+
resource.TestCheckResourceAttrSet("scaleway_apple_silicon_server.main", "private_ips.0.id"),
140+
resource.TestCheckResourceAttrSet("scaleway_apple_silicon_server.main", "private_ips.0.address"),
141+
resource.TestCheckResourceAttrSet("scaleway_apple_silicon_server.main", "private_ips.1.id"),
142+
resource.TestCheckResourceAttrSet("scaleway_apple_silicon_server.main", "private_ips.1.address"),
139143
),
140144
},
141145
{
@@ -182,6 +186,14 @@ func TestAccServer_EnableVPC(t *testing.T) {
182186
resource.TestCheckResourceAttrPair("scaleway_apple_silicon_server.main", "private_network.0.id", "scaleway_vpc_private_network.pn01", "id"),
183187
resource.TestCheckResourceAttrPair("scaleway_apple_silicon_server.main", "private_network.1.id", "scaleway_vpc_private_network.pn02", "id"),
184188
resource.TestCheckResourceAttr("scaleway_apple_silicon_server.main", "vpc_status", "vpc_enabled"),
189+
resource.TestCheckResourceAttrSet("scaleway_apple_silicon_server.main", "private_ips.0.id"),
190+
resource.TestCheckResourceAttrSet("scaleway_apple_silicon_server.main", "private_ips.0.address"),
191+
resource.TestCheckResourceAttrSet("scaleway_apple_silicon_server.main", "private_ips.1.id"),
192+
resource.TestCheckResourceAttrSet("scaleway_apple_silicon_server.main", "private_ips.1.address"),
193+
resource.TestCheckResourceAttrSet("scaleway_apple_silicon_server.main", "private_ips.2.id"),
194+
resource.TestCheckResourceAttrSet("scaleway_apple_silicon_server.main", "private_ips.2.address"),
195+
resource.TestCheckResourceAttrSet("scaleway_apple_silicon_server.main", "private_ips.3.id"),
196+
resource.TestCheckResourceAttrSet("scaleway_apple_silicon_server.main", "private_ips.3.address"),
185197
),
186198
},
187199
{

0 commit comments

Comments
 (0)