@@ -4,10 +4,13 @@ import (
4
4
"context"
5
5
"fmt"
6
6
"os"
7
+ "strings"
8
+ "time"
7
9
8
- "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-07-01 /compute"
10
+ "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2020-06-30 /compute"
9
11
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2019-11-01/network"
10
12
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2020-06-01/resources"
13
+
11
14
"github.com/Azure/go-autorest/autorest/azure/auth"
12
15
"github.com/Azure/go-autorest/autorest/to"
13
16
@@ -25,18 +28,20 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
25
28
instanceType := getInstanceType (d )
26
29
keyPublic := d .Get ("key_public" ).(string )
27
30
vmName := d .Get ("instance_name" ).(string )
31
+ hddSize := int32 (d .Get ("instance_hdd_size" ).(int ))
28
32
29
- publisher := "Canonical"
30
- offer := "UbuntuServer"
31
- sku := "18.04-LTS"
32
- version := "latest"
33
+ image := d .Get ("ami" ).(string )
34
+ if image == "" {
35
+ image = "Canonical:UbuntuServer:18.04-LTS:latest"
36
+ }
37
+ imageParts := strings .Split (image , ":" )
33
38
34
- // publisher := "NVIDIA-GPU"
35
- // offer := "GPU"
36
- // sku := "Cloud-Image"
37
- // version := "2020.06.29"
39
+ publisher := imageParts [ 0 ]
40
+ offer := imageParts [ 1 ]
41
+ sku := imageParts [ 2 ]
42
+ version := imageParts [ 3 ]
38
43
39
- gpName := vmName + "-sg"
44
+ gpName := vmName
40
45
nsgName := vmName + "-nsg"
41
46
vnetName := vmName + "-vnet"
42
47
ipName := vmName + "-ip"
@@ -45,7 +50,8 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
45
50
ipConfigName := vmName + "-ipc"
46
51
47
52
username := "ubuntu"
48
- password := "ubuntu"
53
+
54
+ d .Set ("instance_id" , vmName )
49
55
50
56
groupsClient , err := getGroupsClient (subscriptionID )
51
57
_ , err = groupsClient .CreateOrUpdate (
@@ -80,7 +86,7 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
80
86
SourceAddressPrefix : to .StringPtr ("0.0.0.0/0" ),
81
87
SourcePortRange : to .StringPtr ("1-65535" ),
82
88
DestinationAddressPrefix : to .StringPtr ("0.0.0.0/0" ),
83
- DestinationPortRange : to .StringPtr ("22 " ),
89
+ DestinationPortRange : to .StringPtr ("1-65535 " ),
84
90
Access : network .SecurityRuleAccessAllow ,
85
91
Direction : network .SecurityRuleDirectionInbound ,
86
92
Priority : to .Int32Ptr (100 ),
@@ -93,6 +99,8 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
93
99
futureNsg .WaitForCompletionRef (ctx , nsgClient .Client )
94
100
nsg , err := nsgClient .Get (ctx , gpName , nsgName , "" )
95
101
if err != nil {
102
+ ResourceMachineDelete (ctx , d , m )
103
+
96
104
diags = append (diags , diag.Diagnostic {
97
105
Severity : diag .Error ,
98
106
Summary : fmt .Sprintf ("Security group is not ready: %v" , err ),
@@ -119,6 +127,7 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
119
127
futureIP .WaitForCompletionRef (ctx , ipClient .Client )
120
128
ip , err := ipClient .Get (ctx , gpName , ipName , "" )
121
129
if err != nil {
130
+ ResourceMachineDelete (ctx , d , m )
122
131
diags = append (diags , diag.Diagnostic {
123
132
Severity : diag .Error ,
124
133
Summary : fmt .Sprintf ("cannot create pi: %v" , err ),
@@ -143,6 +152,7 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
143
152
futureVnet .WaitForCompletionRef (ctx , vnetClient .Client )
144
153
_ , err = vnetClient .Get (ctx , gpName , vnetName , "" )
145
154
if err != nil {
155
+ ResourceMachineDelete (ctx , d , m )
146
156
diags = append (diags , diag.Diagnostic {
147
157
Severity : diag .Error ,
148
158
Summary : fmt .Sprintf ("cannot create vnet: %v" , err ),
@@ -166,6 +176,7 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
166
176
})
167
177
futureSubnet .WaitForCompletionRef (ctx , subnetsClient .Client )
168
178
if err != nil {
179
+ ResourceMachineDelete (ctx , d , m )
169
180
diags = append (diags , diag.Diagnostic {
170
181
Severity : diag .Error ,
171
182
Summary : fmt .Sprintf ("cannot create subnet: %v" , err ),
@@ -175,6 +186,7 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
175
186
}
176
187
subnet , err := subnetsClient .Get (ctx , gpName , vnetName , subnetName , "" )
177
188
if err != nil {
189
+ ResourceMachineDelete (ctx , d , m )
178
190
diags = append (diags , diag.Diagnostic {
179
191
Severity : diag .Error ,
180
192
Summary : fmt .Sprintf ("cannot create subnet: %v" , err ),
@@ -205,6 +217,7 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
205
217
futureNic .WaitForCompletionRef (ctx , nicClient .Client )
206
218
nic , err := nicClient .Get (ctx , gpName , nicName , "" )
207
219
if err != nil {
220
+ ResourceMachineDelete (ctx , d , m )
208
221
diags = append (diags , diag.Diagnostic {
209
222
Severity : diag .Error ,
210
223
Summary : fmt .Sprintf ("cannot create nic: %v" , err ),
@@ -220,6 +233,13 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
220
233
vmName ,
221
234
compute.VirtualMachine {
222
235
Location : to .StringPtr (region ),
236
+ /*
237
+ Plan: &compute.Plan{
238
+ Name: to.StringPtr(sku),
239
+ Publisher: to.StringPtr(publisher),
240
+ Product: to.StringPtr(offer),
241
+ },
242
+ */
223
243
VirtualMachineProperties : & compute.VirtualMachineProperties {
224
244
HardwareProfile : & compute.HardwareProfile {
225
245
VMSize : compute .VirtualMachineSizeTypes (instanceType ),
@@ -231,12 +251,21 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
231
251
Sku : to .StringPtr (sku ),
232
252
Version : to .StringPtr (version ),
233
253
},
254
+ OsDisk : & compute.OSDisk {
255
+ Name : to .StringPtr (fmt .Sprintf (vmName + "-hdd" )),
256
+ Caching : compute .CachingTypesReadWrite ,
257
+ CreateOption : compute .DiskCreateOptionTypesFromImage ,
258
+ DiskSizeGB : to .Int32Ptr (hddSize ),
259
+ ManagedDisk : & compute.ManagedDiskParameters {
260
+ StorageAccountType : compute .StorageAccountTypesStandardLRS ,
261
+ },
262
+ },
234
263
},
235
264
OsProfile : & compute.OSProfile {
236
- ComputerName : to .StringPtr (vmName ),
265
+ ComputerName : to .StringPtr ("iterative" ),
237
266
AdminUsername : to .StringPtr (username ),
238
- AdminPassword : to .StringPtr (password ),
239
267
LinuxConfiguration : & compute.LinuxConfiguration {
268
+ DisablePasswordAuthentication : to .BoolPtr (true ),
240
269
SSH : & compute.SSHConfiguration {
241
270
PublicKeys : & []compute.SSHPublicKey {
242
271
{
@@ -260,9 +289,28 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
260
289
},
261
290
},
262
291
)
263
- futureVM .WaitForCompletionRef (ctx , vmClient .Client )
292
+ if err != nil {
293
+ ResourceMachineDelete (ctx , d , m )
294
+ diags = append (diags , diag.Diagnostic {
295
+ Severity : diag .Error ,
296
+ Summary : fmt .Sprintf ("cannot create vm: %v" , err ),
297
+ })
298
+
299
+ return diags
300
+ }
301
+ err = futureVM .WaitForCompletionRef (ctx , vmClient .Client )
302
+ if err != nil {
303
+ ResourceMachineDelete (ctx , d , m )
304
+ diags = append (diags , diag.Diagnostic {
305
+ Severity : diag .Error ,
306
+ Summary : fmt .Sprintf ("cannot create vm: %v" , err ),
307
+ })
308
+
309
+ return diags
310
+ }
264
311
_ , err = vmClient .Get (ctx , gpName , vmName , "" )
265
312
if err != nil {
313
+ ResourceMachineDelete (ctx , d , m )
266
314
diags = append (diags , diag.Diagnostic {
267
315
Severity : diag .Error ,
268
316
Summary : fmt .Sprintf ("cannot create vm: %v" , err ),
@@ -272,9 +320,8 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
272
320
}
273
321
274
322
d .SetId (gpName )
275
- d .Set ("instance_id" , vmName )
276
323
d .Set ("instance_ip" , ip .IPAddress )
277
- // d.Set("instance_launch_time", vm. )
324
+ d .Set ("instance_launch_time" , time . Now (). String () )
278
325
279
326
return diags
280
327
}
@@ -285,8 +332,7 @@ func ResourceMachineDelete(ctx context.Context, d *schema.ResourceData, m interf
285
332
286
333
subscriptionID := os .Getenv ("AZURE_SUBSCRIPTION_ID" )
287
334
groupsClient , err := getGroupsClient (subscriptionID )
288
- future , err := groupsClient .Delete (context .Background (), d .Id ())
289
- err = future .WaitForCompletionRef (ctx , groupsClient .Client )
335
+ _ , err = groupsClient .Delete (context .Background (), d .Get ("instance_id" ).(string ))
290
336
if err != nil {
291
337
diags = append (diags , diag.Diagnostic {
292
338
Severity : diag .Error ,
@@ -370,7 +416,7 @@ func getVMClient(subscriptionID string) (compute.VirtualMachinesClient, error) {
370
416
func getRegion (d * schema.ResourceData ) string {
371
417
instanceRegions := make (map [string ]string )
372
418
instanceRegions ["us-east" ] = "eastus"
373
- instanceRegions ["us-west" ] = "westus "
419
+ instanceRegions ["us-west" ] = "westus2 "
374
420
instanceRegions ["eu-north" ] = "northeurope"
375
421
instanceRegions ["eu-west" ] = "westeurope"
376
422
@@ -390,12 +436,13 @@ func getInstanceType(d *schema.ResourceData) string {
390
436
instanceTypes ["mk80" ] = "Standard_NC6"
391
437
instanceTypes ["lk80" ] = "Standard_NC12"
392
438
instanceTypes ["xlk80" ] = "Standard_NC24"
393
- instanceTypes ["mtesla" ] = "Standard_NC6s_v2 "
394
- instanceTypes ["ltesla" ] = "Standard_NC12s_v2 "
395
- instanceTypes ["xltesla" ] = "Standard_NC24s_v2 "
439
+ instanceTypes ["mtesla" ] = "Standard_NC6s_v3 "
440
+ instanceTypes ["ltesla" ] = "Standard_NC12s_v3 "
441
+ instanceTypes ["xltesla" ] = "Standard_NC24s_v3 "
396
442
397
443
instanceType := d .Get ("instance_type" ).(string )
398
- if val , ok := instanceTypes [instanceType + d .Get ("instance_gpu" ).(string )]; ok {
444
+ instanceGPU := d .Get ("instance_gpu" ).(string )
445
+ if val , ok := instanceTypes [instanceType + instanceGPU ]; ok {
399
446
instanceType = val
400
447
}
401
448
0 commit comments