@@ -9,9 +9,12 @@ import (
9
9
applesilicon "github.com/scaleway/scaleway-sdk-go/api/applesilicon/v1alpha1"
10
10
"github.com/scaleway/scaleway-sdk-go/scw"
11
11
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
12
+ "github.com/scaleway/terraform-provider-scaleway/v2/internal/locality"
12
13
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/zonal"
14
+ "github.com/scaleway/terraform-provider-scaleway/v2/internal/meta"
13
15
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/account"
14
16
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
17
+ "github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
15
18
)
16
19
17
20
func ResourceServer () * schema.Resource {
@@ -41,6 +44,61 @@ func ResourceServer() *schema.Resource {
41
44
Required : true ,
42
45
ForceNew : true ,
43
46
},
47
+ "enable_vpc" : {
48
+ Type : schema .TypeBool ,
49
+ Optional : true ,
50
+ Default : false ,
51
+ Description : "Whether or not to enable VPC access" ,
52
+ },
53
+ "private_network" : {
54
+ Type : schema .TypeSet ,
55
+ Optional : true ,
56
+ Description : "The private networks to attach to the server" ,
57
+ Elem : & schema.Resource {
58
+ Schema : map [string ]* schema.Schema {
59
+ "id" : {
60
+ Type : schema .TypeString ,
61
+ Description : "The private network ID" ,
62
+ Required : true ,
63
+ ValidateDiagFunc : verify .IsUUIDorUUIDWithLocality (),
64
+ StateFunc : func (i interface {}) string {
65
+ return locality .ExpandID (i .(string ))
66
+ },
67
+ },
68
+ "ipam_ip_ids" : {
69
+ Type : schema .TypeList ,
70
+ Optional : true ,
71
+ Computed : true ,
72
+ Elem : & schema.Schema {
73
+ Type : schema .TypeString ,
74
+ ValidateDiagFunc : verify .IsUUIDorUUIDWithLocality (),
75
+ },
76
+ Description : "List of IPAM IP IDs to attach to the server" ,
77
+ },
78
+ // computed
79
+ "vlan" : {
80
+ Type : schema .TypeInt ,
81
+ Computed : true ,
82
+ Description : "The VLAN ID associated to the private network" ,
83
+ },
84
+ "status" : {
85
+ Type : schema .TypeString ,
86
+ Computed : true ,
87
+ Description : "The private network status" ,
88
+ },
89
+ "created_at" : {
90
+ Type : schema .TypeString ,
91
+ Computed : true ,
92
+ Description : "The date and time of the creation of the private network" ,
93
+ },
94
+ "updated_at" : {
95
+ Type : schema .TypeString ,
96
+ Computed : true ,
97
+ Description : "The date and time of the last update of the private network" ,
98
+ },
99
+ },
100
+ },
101
+ },
44
102
// Computed
45
103
"ip" : {
46
104
Type : schema .TypeString ,
@@ -72,6 +130,11 @@ func ResourceServer() *schema.Resource {
72
130
Computed : true ,
73
131
Description : "The minimal date and time on which you can delete this server due to Apple licence" ,
74
132
},
133
+ "vpc_status" : {
134
+ Type : schema .TypeString ,
135
+ Computed : true ,
136
+ Description : "The VPC status of the server" ,
137
+ },
75
138
76
139
// Common
77
140
"zone" : zonal .Schema (),
@@ -91,6 +154,7 @@ func ResourceAppleSiliconServerCreate(ctx context.Context, d *schema.ResourceDat
91
154
Name : types .ExpandOrGenerateString (d .Get ("name" ), "m1" ),
92
155
Type : d .Get ("type" ).(string ),
93
156
ProjectID : d .Get ("project_id" ).(string ),
157
+ EnableVpc : d .Get ("enable_vpc" ).(bool ),
94
158
}
95
159
96
160
res , err := asAPI .CreateServer (createReq , scw .WithContext (ctx ))
@@ -105,6 +169,25 @@ func ResourceAppleSiliconServerCreate(ctx context.Context, d *schema.ResourceDat
105
169
return diag .FromErr (err )
106
170
}
107
171
172
+ if pn , ok := d .GetOk ("private_network" ); ok {
173
+ privateNetworkAPI := applesilicon .NewPrivateNetworkAPI (meta .ExtractScwClient (m ))
174
+ req := & applesilicon.PrivateNetworkAPISetServerPrivateNetworksRequest {
175
+ Zone : zone ,
176
+ ServerID : res .ID ,
177
+ PerPrivateNetworkIpamIPIDs : expandPrivateNetworks (pn ),
178
+ }
179
+
180
+ _ , err := privateNetworkAPI .SetServerPrivateNetworks (req , scw .WithContext (ctx ))
181
+ if err != nil {
182
+ return diag .FromErr (err )
183
+ }
184
+
185
+ _ , err = waitForAppleSiliconPrivateNetworkServer (ctx , privateNetworkAPI , zone , res .ID , d .Timeout (schema .TimeoutCreate ))
186
+ if err != nil {
187
+ return diag .FromErr (err )
188
+ }
189
+ }
190
+
108
191
return ResourceAppleSiliconServerRead (ctx , d , m )
109
192
}
110
193
@@ -114,6 +197,8 @@ func ResourceAppleSiliconServerRead(ctx context.Context, d *schema.ResourceData,
114
197
return diag .FromErr (err )
115
198
}
116
199
200
+ privateNetworkAPI := applesilicon .NewPrivateNetworkAPI (meta .ExtractScwClient (m ))
201
+
117
202
res , err := asAPI .GetServer (& applesilicon.GetServerRequest {
118
203
Zone : zone ,
119
204
ServerID : ID ,
@@ -136,11 +221,26 @@ func ResourceAppleSiliconServerRead(ctx context.Context, d *schema.ResourceData,
136
221
_ = d .Set ("deletable_at" , res .DeletableAt .Format (time .RFC3339 ))
137
222
_ = d .Set ("ip" , res .IP .String ())
138
223
_ = d .Set ("vnc_url" , res .VncURL )
139
-
224
+ _ = d . Set ( "vpc_status" , res . VpcStatus )
140
225
_ = d .Set ("zone" , res .Zone .String ())
141
226
_ = d .Set ("organization_id" , res .OrganizationID )
142
227
_ = d .Set ("project_id" , res .ProjectID )
143
228
229
+ listPrivateNetworks , err := privateNetworkAPI .ListServerPrivateNetworks (& applesilicon.PrivateNetworkAPIListServerPrivateNetworksRequest {
230
+ Zone : res .Zone ,
231
+ ServerID : & res .ID ,
232
+ })
233
+ if err != nil {
234
+ return diag .FromErr (err )
235
+ }
236
+
237
+ pnRegion , err := res .Zone .Region ()
238
+ if err != nil {
239
+ return diag .FromErr (err )
240
+ }
241
+
242
+ _ = d .Set ("private_network" , flattenPrivateNetworks (pnRegion , listPrivateNetworks .ServerPrivateNetworks ))
243
+
144
244
return nil
145
245
}
146
246
@@ -150,6 +250,11 @@ func ResourceAppleSiliconServerUpdate(ctx context.Context, d *schema.ResourceDat
150
250
return diag .FromErr (err )
151
251
}
152
252
253
+ appleSilisonPrivateNetworkAPI , zonePN , err := newPrivateNetworkAPIWithZone (d , m )
254
+ if err != nil {
255
+ return diag .FromErr (err )
256
+ }
257
+
153
258
req := & applesilicon.UpdateServerRequest {
154
259
Zone : zone ,
155
260
ServerID : ID ,
@@ -159,11 +264,40 @@ func ResourceAppleSiliconServerUpdate(ctx context.Context, d *schema.ResourceDat
159
264
req .Name = types .ExpandStringPtr (d .Get ("name" ))
160
265
}
161
266
267
+ if d .HasChange ("enable_vpc" ) {
268
+ enableVpc := d .Get ("enable_vpc" ).(bool )
269
+ req .EnableVpc = & enableVpc
270
+ }
271
+
162
272
_ , err = asAPI .UpdateServer (req , scw .WithContext (ctx ))
163
273
if err != nil {
164
274
return diag .FromErr (err )
165
275
}
166
276
277
+ err = waitForTerminalVPCState (ctx , asAPI , zone , ID , d .Timeout (schema .TimeoutCreate ))
278
+ if err != nil {
279
+ return diag .FromErr (err )
280
+ }
281
+
282
+ if d .HasChange ("private_network" ) && d .Get ("enable_vpc" ).(bool ) {
283
+ privateNetwork := d .Get ("private_network" )
284
+ req := & applesilicon.PrivateNetworkAPISetServerPrivateNetworksRequest {
285
+ Zone : zonePN ,
286
+ ServerID : ID ,
287
+ PerPrivateNetworkIpamIPIDs : expandPrivateNetworks (privateNetwork ),
288
+ }
289
+
290
+ _ , err := appleSilisonPrivateNetworkAPI .SetServerPrivateNetworks (req , scw .WithContext (ctx ))
291
+ if err != nil {
292
+ return diag .FromErr (err )
293
+ }
294
+ }
295
+
296
+ _ , err = waitForAppleSiliconPrivateNetworkServer (ctx , appleSilisonPrivateNetworkAPI , zone , ID , d .Timeout (schema .TimeoutCreate ))
297
+ if err != nil {
298
+ return diag .FromErr (err )
299
+ }
300
+
167
301
return ResourceAppleSiliconServerRead (ctx , d , m )
168
302
}
169
303
@@ -173,6 +307,11 @@ func ResourceAppleSiliconServerDelete(ctx context.Context, d *schema.ResourceDat
173
307
return diag .FromErr (err )
174
308
}
175
309
310
+ err = detachAllPrivateNetworkFromServer (ctx , d , m , ID )
311
+ if err != nil {
312
+ return diag .FromErr (err )
313
+ }
314
+
176
315
err = asAPI .DeleteServer (& applesilicon.DeleteServerRequest {
177
316
Zone : zone ,
178
317
ServerID : ID ,
0 commit comments