Skip to content

Commit 909c025

Browse files
committed
private_ips as optional attribute, only set it when no error occurred
1 parent ee4eaf3 commit 909c025

File tree

9 files changed

+343
-186
lines changed

9 files changed

+343
-186
lines changed

internal/services/baremetal/server.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ If this behaviour is wanted, please set 'reinstall_on_ssh_key_changes' argument
273273
"private_ips": {
274274
Type: schema.TypeList,
275275
Computed: true,
276+
Optional: true,
276277
Description: "List of private IPv4 and IPv6 addresses associated with the resource",
277278
Elem: &schema.Resource{
278279
Schema: map[string]*schema.Schema{
@@ -534,6 +535,7 @@ func ResourceServerRead(ctx context.Context, d *schema.ResourceData, m interface
534535
privateNetworkIDs = append(privateNetworkIDs, pn.PrivateNetworkID)
535536
}
536537

538+
// Read private IPs if possible
537539
allPrivateIPs := make([]map[string]interface{}, 0, listPrivateNetworks.TotalCount)
538540
diags := diag.Diagnostics{}
539541

@@ -542,25 +544,28 @@ func ResourceServerRead(ctx context.Context, d *schema.ResourceData, m interface
542544
opts := &ipam.GetResourcePrivateIPsOptions{
543545
ResourceType: &resourceType,
544546
PrivateNetworkID: &privateNetworkID,
547+
ProjectID: &server.ProjectID,
545548
}
546549

547550
privateIPs, err := ipam.GetResourcePrivateIPs(ctx, m, pnRegion, opts)
548-
if err != nil {
549-
if !httperrors.Is403(err) {
550-
return diag.FromErr(err)
551-
}
552-
551+
switch {
552+
case err == nil:
553+
allPrivateIPs = append(allPrivateIPs, privateIPs...)
554+
case httperrors.Is403(err):
555+
return append(diags, diag.Diagnostic{
556+
Severity: diag.Warning,
557+
Summary: "Unauthorized to read server's private IPs, please check your IAM permissions",
558+
Detail: err.Error(),
559+
AttributePath: cty.GetAttrPath("private_ips"),
560+
})
561+
default:
553562
diags = append(diags, diag.Diagnostic{
554563
Severity: diag.Warning,
555-
Summary: err.Error(),
556-
Detail: "Got 403 while reading private IPs from IPAM API, please check your IAM permissions",
564+
Summary: fmt.Sprintf("Unable to get private IPs for server %s (pn_id: %s)", server.ID, privateNetworkID),
565+
Detail: err.Error(),
557566
AttributePath: cty.GetAttrPath("private_ips"),
558567
})
559568
}
560-
561-
if privateIPs != nil {
562-
allPrivateIPs = append(allPrivateIPs, privateIPs...)
563-
}
564569
}
565570

566571
_ = d.Set("private_ips", allPrivateIPs)

internal/services/instance/helpers_instance.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,3 +536,19 @@ func prepareRootVolume(rootVolumeI map[string]any, serverType *instance.ServerTy
536536
Boot: rootVolumeIsBootVolume,
537537
}
538538
}
539+
540+
func getServerProjectID(ctx context.Context, api *instance.API, zone scw.Zone, serverID string) (string, error) {
541+
server, err := api.GetServer(&instance.GetServerRequest{
542+
Zone: zone,
543+
ServerID: serverID,
544+
}, scw.WithContext(ctx))
545+
if err != nil {
546+
return "", fmt.Errorf("get private NIC's project ID: error getting server %s", serverID)
547+
}
548+
549+
if server.Server.Project == "" {
550+
return "", fmt.Errorf("no project ID found for server %s", serverID)
551+
}
552+
553+
return server.Server.Project, nil
554+
}

internal/services/instance/private_nic.go

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package instance
22

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

67
"github.com/hashicorp/go-cty/cty"
78
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
@@ -74,6 +75,7 @@ func ResourcePrivateNIC() *schema.Resource {
7475
"private_ips": {
7576
Type: schema.TypeList,
7677
Computed: true,
78+
Optional: true,
7779
Description: "List of private IPv4 and IPv6 addresses associated with the resource",
7880
Elem: &schema.Resource{
7981
Schema: map[string]*schema.Schema{
@@ -185,35 +187,54 @@ func ResourceInstancePrivateNICRead(ctx context.Context, d *schema.ResourceData,
185187
_ = d.Set("tags", privateNIC.Tags)
186188
}
187189

190+
// Get private NIC's private IPs if possible
191+
diags := diag.Diagnostics{}
188192
region, err := zone.Region()
189193
if err != nil {
190-
return diag.FromErr(err)
194+
return append(diags, diag.Diagnostic{
195+
Severity: diag.Warning,
196+
Summary: "Unable to get private NIC's private IPs",
197+
Detail: err.Error(),
198+
})
199+
}
200+
201+
projectID, err := getServerProjectID(ctx, instanceAPI, zone, privateNIC.ServerID)
202+
if err != nil {
203+
return append(diags, diag.Diagnostic{
204+
Severity: diag.Warning,
205+
Summary: "Unable to get private NIC's private IPs",
206+
Detail: err.Error(),
207+
})
191208
}
192209

193-
diags := diag.Diagnostics{}
194210
resourceType := ipamAPI.ResourceTypeInstancePrivateNic
195211
opts := &ipam.GetResourcePrivateIPsOptions{
196212
ResourceID: &privateNIC.ID,
197213
ResourceType: &resourceType,
198214
PrivateNetworkID: &privateNIC.PrivateNetworkID,
215+
ProjectID: &projectID,
199216
}
200217

201218
privateIPs, err := ipam.GetResourcePrivateIPs(ctx, m, region, opts)
202-
if err != nil {
203-
if !httperrors.Is403(err) {
204-
return diag.FromErr(err)
205-
}
206-
219+
switch {
220+
case err == nil:
221+
_ = d.Set("private_ips", privateIPs)
222+
case httperrors.Is403(err):
207223
diags = append(diags, diag.Diagnostic{
208224
Severity: diag.Warning,
209-
Summary: err.Error(),
210-
Detail: "Got 403 while reading private IPs from IPAM API, please check your IAM permissions",
225+
Summary: "Unauthorized to read private NIC's private IPs, please check your IAM permissions",
226+
Detail: err.Error(),
227+
AttributePath: cty.GetAttrPath("private_ips"),
228+
})
229+
default:
230+
diags = append(diags, diag.Diagnostic{
231+
Severity: diag.Warning,
232+
Summary: fmt.Sprintf("Unable to get private IPs for pnic %s (server_id: %s)", privateNIC.ID, privateNIC.ServerID),
233+
Detail: err.Error(),
211234
AttributePath: cty.GetAttrPath("private_ips"),
212235
})
213236
}
214237

215-
_ = d.Set("private_ips", privateIPs)
216-
217238
return diags
218239
}
219240

internal/services/instance/server.go

Lines changed: 55 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ func ResourceServer() *schema.Resource {
355355
"private_ips": {
356356
Type: schema.TypeList,
357357
Computed: true,
358+
Optional: true,
358359
Description: "List of private IPv4 addresses associated with the resource",
359360
Elem: &schema.Resource{
360361
Schema: map[string]*schema.Schema{
@@ -780,6 +781,40 @@ func ResourceInstanceServerRead(ctx context.Context, d *schema.ResourceData, m i
780781

781782
_ = d.Set("user_data", userData)
782783

784+
////
785+
// Display warning if server will soon reach End of Service
786+
////
787+
diags := diag.Diagnostics{}
788+
if server.EndOfService {
789+
compatibleTypes, err := api.GetServerCompatibleTypes(&instanceSDK.GetServerCompatibleTypesRequest{
790+
Zone: zone,
791+
ServerID: id,
792+
}, scw.WithContext(ctx))
793+
if err != nil {
794+
return diag.FromErr(err)
795+
}
796+
797+
mostRelevantTypes := compatibleTypes.CompatibleTypes[:5]
798+
799+
diags = append(diags, diag.Diagnostic{
800+
Severity: diag.Warning,
801+
Detail: fmt.Sprintf("Instance type %q will soon reach End of Service", server.CommercialType),
802+
Summary: fmt.Sprintf(`Your Instance will soon reach End of Service. You can check the exact date on the Scaleway console. We recommend that you migrate your Instance before that.
803+
Here are the %d best options for %q, ordered by relevance: [%s]
804+
805+
You can check the full list of compatible server types:
806+
- on the Scaleway console
807+
- using the CLI command 'scw instance server get-compatible-types %s zone=%s'`,
808+
len(mostRelevantTypes),
809+
server.CommercialType,
810+
strings.Join(mostRelevantTypes, ", "),
811+
server.ID,
812+
server.Zone,
813+
),
814+
AttributePath: cty.GetAttrPath("type"),
815+
})
816+
}
817+
783818
////
784819
// Read server private networks
785820
////
@@ -799,75 +834,49 @@ func ResourceInstanceServerRead(ctx context.Context, d *schema.ResourceData, m i
799834
privateNICIDs = append(privateNICIDs, nic.ID)
800835
}
801836

837+
// Read server's private IPs if possible
802838
allPrivateIPs := []map[string]interface{}(nil)
803-
diags := diag.Diagnostics{}
804839
resourceType := ipamAPI.ResourceTypeInstancePrivateNic
805840

806841
region, err := zone.Region()
807842
if err != nil {
808-
return diag.FromErr(err)
843+
return append(diags, diag.Diagnostic{
844+
Severity: diag.Warning,
845+
Summary: "Unable to get server's private IPs",
846+
Detail: err.Error(),
847+
})
809848
}
810849

811850
for _, nicID := range privateNICIDs {
812851
opts := &ipam.GetResourcePrivateIPsOptions{
813852
ResourceType: &resourceType,
814853
ResourceID: &nicID,
854+
ProjectID: &server.Project,
815855
}
816856

817857
privateIPs, err := ipam.GetResourcePrivateIPs(ctx, m, region, opts)
818-
if err != nil {
819-
if !httperrors.Is403(err) {
820-
return diag.FromErr(err)
821-
}
822-
858+
switch {
859+
case err == nil:
860+
allPrivateIPs = append(allPrivateIPs, privateIPs...)
861+
case httperrors.Is403(err):
862+
return append(diags, diag.Diagnostic{
863+
Severity: diag.Warning,
864+
Summary: "Unauthorized to read server's private IPs, please check your IAM permissions",
865+
Detail: err.Error(),
866+
AttributePath: cty.GetAttrPath("private_ips"),
867+
})
868+
default:
823869
diags = append(diags, diag.Diagnostic{
824870
Severity: diag.Warning,
825-
Summary: err.Error(),
826-
Detail: "Got 403 while reading private IPs from IPAM API, please check your IAM permissions",
871+
Summary: fmt.Sprintf("Unable to get private IPs for server %s (pnic_id: %s)", server.ID, nicID),
872+
Detail: err.Error(),
827873
AttributePath: cty.GetAttrPath("private_ips"),
828874
})
829875
}
830-
831-
if privateIPs != nil {
832-
allPrivateIPs = append(allPrivateIPs, privateIPs...)
833-
}
834876
}
835877

836878
_ = d.Set("private_ips", allPrivateIPs)
837879

838-
////
839-
// Display warning if server will soon reach End of Service
840-
////
841-
if server.EndOfService {
842-
compatibleTypes, err := api.GetServerCompatibleTypes(&instanceSDK.GetServerCompatibleTypesRequest{
843-
Zone: zone,
844-
ServerID: id,
845-
}, scw.WithContext(ctx))
846-
if err != nil {
847-
return diag.FromErr(err)
848-
}
849-
850-
mostRelevantTypes := compatibleTypes.CompatibleTypes[:5]
851-
852-
diags = append(diags, diag.Diagnostic{
853-
Severity: diag.Warning,
854-
Detail: fmt.Sprintf("Instance type %q will soon reach End of Service", server.CommercialType),
855-
Summary: fmt.Sprintf(`Your Instance will soon reach End of Service. You can check the exact date on the Scaleway console. We recommend that you migrate your Instance before that.
856-
Here are the %d best options for %q, ordered by relevance: [%s]
857-
858-
You can check the full list of compatible server types:
859-
- on the Scaleway console
860-
- using the CLI command 'scw instance server get-compatible-types %s zone=%s'`,
861-
len(mostRelevantTypes),
862-
server.CommercialType,
863-
strings.Join(mostRelevantTypes, ", "),
864-
server.ID,
865-
server.Zone,
866-
),
867-
AttributePath: cty.GetAttrPath("type"),
868-
})
869-
}
870-
871880
return diags
872881
}
873882

internal/services/k8s/pool.go

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package k8s
22

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

78
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
@@ -223,6 +224,7 @@ func ResourcePool() *schema.Resource {
223224
"private_ips": {
224225
Type: schema.TypeList,
225226
Computed: true,
227+
Optional: true,
226228
Description: "List of private IPv4 and IPv6 addresses associated with the node",
227229
Elem: &schema.Resource{
228230
Schema: map[string]*schema.Schema{
@@ -425,14 +427,14 @@ func ResourceK8SPoolRead(ctx context.Context, d *schema.ResourceData, m interfac
425427
_ = d.Set("placement_group_id", zonal.NewID(pool.Zone, *pool.PlacementGroupID).String())
426428
}
427429

428-
// Get nodes' private IPs
430+
// Get nodes' private IPs (if possible)
429431
diags := diag.Diagnostics{}
430432

431433
projectID, err := getClusterProjectID(ctx, k8sAPI, pool)
432434
if err != nil {
433435
diags = append(diags, diag.Diagnostic{
434436
Severity: diag.Warning,
435-
Summary: "Unable to get nodes private IPs",
437+
Summary: "Unable to get node's private IPs",
436438
Detail: err.Error(),
437439
})
438440
} else {
@@ -453,25 +455,24 @@ func ResourceK8SPoolRead(ctx context.Context, d *schema.ResourceData, m interfac
453455
}
454456

455457
privateIPs, err := ipam.GetResourcePrivateIPs(ctx, m, region, opts)
456-
if err != nil {
457-
if httperrors.Is403(err) {
458-
diags = append(diags, diag.Diagnostic{
459-
Severity: diag.Warning,
460-
Summary: "Unauthorized to read nodes' private IPs, please check your IAM permissions",
461-
Detail: err.Error(),
462-
})
463-
464-
break
465-
} else {
466-
diags = append(diags, diag.Diagnostic{
467-
Severity: diag.Warning,
468-
Summary: "Unable to get nodes private IPs from IPAM API",
469-
Detail: err.Error(),
470-
})
471-
}
458+
switch {
459+
case err == nil:
460+
nodes[i]["private_ips"] = privateIPs
461+
case httperrors.Is403(err):
462+
diags = append(diags, diag.Diagnostic{
463+
Severity: diag.Warning,
464+
Summary: "Unauthorized to read nodes' private IPs, please check your IAM permissions",
465+
Detail: err.Error(),
466+
})
467+
468+
break
469+
default:
470+
diags = append(diags, diag.Diagnostic{
471+
Severity: diag.Warning,
472+
Summary: fmt.Sprintf("Unable to get private IPs for node %q", nodeName),
473+
Detail: err.Error(),
474+
})
472475
}
473-
474-
nodes[i]["private_ips"] = privateIPs
475476
}
476477
}
477478

0 commit comments

Comments
 (0)