Skip to content
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/apple_silicon_server.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ In addition to all arguments above, the following attributes are exported:
- `state` - The state of the server.
- `ip` - IPv4 address of the server (IPv4 address).
- `vnc_url` - URL of the VNC.
- `private_ips` - The list of private IPv4 and IPv6 addresses associated with the server.
- `id` - The ID of the IP address resource.
- `address` - The private IP address.
- `created_at` - The date and time of the creation of the Apple Silicon server.
- `updated_at` - The date and time of the last update of the Apple Silicon server.
- `deleted_at` - The minimal date and time on which you can delete this server due to Apple licence.
Expand Down
75 changes: 74 additions & 1 deletion internal/services/applesilicon/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@ package applesilicon

import (
"context"
"fmt"
"time"

"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
applesilicon "github.com/scaleway/scaleway-sdk-go/api/applesilicon/v1alpha1"
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/httperrors"
"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/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/types"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
)
Expand Down Expand Up @@ -112,6 +116,26 @@ func ResourceServer() *schema.Resource {
Description: "IPv4 address of the server",
Computed: true,
},
"private_ips": {
Type: schema.TypeList,
Computed: true,
Optional: true,
Description: "List of private IPv4 and IPv6 addresses associated with the server",
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",
},
},
},
},
"vnc_url": {
Type: schema.TypeString,
Description: "VNC url use to connect remotely to the desktop GUI",
Expand Down Expand Up @@ -262,7 +286,56 @@ func ResourceAppleSiliconServerRead(ctx context.Context, d *schema.ResourceData,

_ = 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)
}

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

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

privateIPs, err := ipam.GetResourcePrivateIPs(ctx, m, pnRegion, opts)

switch {
case err == nil:
allPrivateIPs = append(allPrivateIPs, privateIPs...)
case httperrors.Is403(err):
authorized = false

diags = append(diags, diag.Diagnostic{
Severity: diag.Warning,
Summary: "Unauthorized to read server's private IPs, please check your IAM permissions",
Detail: err.Error(),
AttributePath: cty.GetAttrPath("private_ips"),
})
default:
diags = append(diags, diag.Diagnostic{
Severity: diag.Warning,
Summary: fmt.Sprintf("Unable to get private IP for server %q", res.Name),
Detail: err.Error(),
AttributePath: cty.GetAttrPath("private_ips"),
})
}

if !authorized {
break
}
}

if authorized {
_ = d.Set("private_ips", allPrivateIPs)
}

return diags
}

func ResourceAppleSiliconServerUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
Expand Down
12 changes: 12 additions & 0 deletions internal/services/applesilicon/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ func TestAccServer_EnableVPC(t *testing.T) {
resource.TestCheckResourceAttrSet("scaleway_apple_silicon_server.main", "deletable_at"),
resource.TestCheckResourceAttrPair("scaleway_apple_silicon_server.main", "private_network.0.id", "scaleway_vpc_private_network.pn01", "id"),
resource.TestCheckResourceAttr("scaleway_apple_silicon_server.main", "vpc_status", "vpc_enabled"),
resource.TestCheckResourceAttrSet("scaleway_apple_silicon_server.main", "private_ips.0.id"),
resource.TestCheckResourceAttrSet("scaleway_apple_silicon_server.main", "private_ips.0.address"),
resource.TestCheckResourceAttrSet("scaleway_apple_silicon_server.main", "private_ips.1.id"),
resource.TestCheckResourceAttrSet("scaleway_apple_silicon_server.main", "private_ips.1.address"),
),
},
{
Expand Down Expand Up @@ -182,6 +186,14 @@ func TestAccServer_EnableVPC(t *testing.T) {
resource.TestCheckResourceAttrPair("scaleway_apple_silicon_server.main", "private_network.0.id", "scaleway_vpc_private_network.pn01", "id"),
resource.TestCheckResourceAttrPair("scaleway_apple_silicon_server.main", "private_network.1.id", "scaleway_vpc_private_network.pn02", "id"),
resource.TestCheckResourceAttr("scaleway_apple_silicon_server.main", "vpc_status", "vpc_enabled"),
resource.TestCheckResourceAttrSet("scaleway_apple_silicon_server.main", "private_ips.0.id"),
resource.TestCheckResourceAttrSet("scaleway_apple_silicon_server.main", "private_ips.0.address"),
resource.TestCheckResourceAttrSet("scaleway_apple_silicon_server.main", "private_ips.1.id"),
resource.TestCheckResourceAttrSet("scaleway_apple_silicon_server.main", "private_ips.1.address"),
resource.TestCheckResourceAttrSet("scaleway_apple_silicon_server.main", "private_ips.2.id"),
resource.TestCheckResourceAttrSet("scaleway_apple_silicon_server.main", "private_ips.2.address"),
resource.TestCheckResourceAttrSet("scaleway_apple_silicon_server.main", "private_ips.3.id"),
resource.TestCheckResourceAttrSet("scaleway_apple_silicon_server.main", "private_ips.3.address"),
),
},
{
Expand Down
Loading
Loading