@@ -4,10 +4,13 @@ import (
44 "context"
55 "fmt"
66 "os"
7+ "strings"
8+ "time"
79
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"
911 "github.com/Azure/azure-sdk-for-go/services/network/mgmt/2019-11-01/network"
1012 "github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2020-06-01/resources"
13+
1114 "github.com/Azure/go-autorest/autorest/azure/auth"
1215 "github.com/Azure/go-autorest/autorest/to"
1316
@@ -25,18 +28,20 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
2528 instanceType := getInstanceType (d )
2629 keyPublic := d .Get ("key_public" ).(string )
2730 vmName := d .Get ("instance_name" ).(string )
31+ hddSize := int32 (d .Get ("instance_hdd_size" ).(int ))
2832
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 , ":" )
3338
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 ]
3843
39- gpName := vmName + "-sg"
44+ gpName := vmName
4045 nsgName := vmName + "-nsg"
4146 vnetName := vmName + "-vnet"
4247 ipName := vmName + "-ip"
@@ -45,7 +50,8 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
4550 ipConfigName := vmName + "-ipc"
4651
4752 username := "ubuntu"
48- password := "ubuntu"
53+
54+ d .Set ("instance_id" , vmName )
4955
5056 groupsClient , err := getGroupsClient (subscriptionID )
5157 _ , err = groupsClient .CreateOrUpdate (
@@ -80,7 +86,7 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
8086 SourceAddressPrefix : to .StringPtr ("0.0.0.0/0" ),
8187 SourcePortRange : to .StringPtr ("1-65535" ),
8288 DestinationAddressPrefix : to .StringPtr ("0.0.0.0/0" ),
83- DestinationPortRange : to .StringPtr ("22 " ),
89+ DestinationPortRange : to .StringPtr ("1-65535 " ),
8490 Access : network .SecurityRuleAccessAllow ,
8591 Direction : network .SecurityRuleDirectionInbound ,
8692 Priority : to .Int32Ptr (100 ),
@@ -93,6 +99,8 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
9399 futureNsg .WaitForCompletionRef (ctx , nsgClient .Client )
94100 nsg , err := nsgClient .Get (ctx , gpName , nsgName , "" )
95101 if err != nil {
102+ ResourceMachineDelete (ctx , d , m )
103+
96104 diags = append (diags , diag.Diagnostic {
97105 Severity : diag .Error ,
98106 Summary : fmt .Sprintf ("Security group is not ready: %v" , err ),
@@ -119,6 +127,7 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
119127 futureIP .WaitForCompletionRef (ctx , ipClient .Client )
120128 ip , err := ipClient .Get (ctx , gpName , ipName , "" )
121129 if err != nil {
130+ ResourceMachineDelete (ctx , d , m )
122131 diags = append (diags , diag.Diagnostic {
123132 Severity : diag .Error ,
124133 Summary : fmt .Sprintf ("cannot create pi: %v" , err ),
@@ -143,6 +152,7 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
143152 futureVnet .WaitForCompletionRef (ctx , vnetClient .Client )
144153 _ , err = vnetClient .Get (ctx , gpName , vnetName , "" )
145154 if err != nil {
155+ ResourceMachineDelete (ctx , d , m )
146156 diags = append (diags , diag.Diagnostic {
147157 Severity : diag .Error ,
148158 Summary : fmt .Sprintf ("cannot create vnet: %v" , err ),
@@ -166,6 +176,7 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
166176 })
167177 futureSubnet .WaitForCompletionRef (ctx , subnetsClient .Client )
168178 if err != nil {
179+ ResourceMachineDelete (ctx , d , m )
169180 diags = append (diags , diag.Diagnostic {
170181 Severity : diag .Error ,
171182 Summary : fmt .Sprintf ("cannot create subnet: %v" , err ),
@@ -175,6 +186,7 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
175186 }
176187 subnet , err := subnetsClient .Get (ctx , gpName , vnetName , subnetName , "" )
177188 if err != nil {
189+ ResourceMachineDelete (ctx , d , m )
178190 diags = append (diags , diag.Diagnostic {
179191 Severity : diag .Error ,
180192 Summary : fmt .Sprintf ("cannot create subnet: %v" , err ),
@@ -205,6 +217,7 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
205217 futureNic .WaitForCompletionRef (ctx , nicClient .Client )
206218 nic , err := nicClient .Get (ctx , gpName , nicName , "" )
207219 if err != nil {
220+ ResourceMachineDelete (ctx , d , m )
208221 diags = append (diags , diag.Diagnostic {
209222 Severity : diag .Error ,
210223 Summary : fmt .Sprintf ("cannot create nic: %v" , err ),
@@ -220,6 +233,13 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
220233 vmName ,
221234 compute.VirtualMachine {
222235 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+ */
223243 VirtualMachineProperties : & compute.VirtualMachineProperties {
224244 HardwareProfile : & compute.HardwareProfile {
225245 VMSize : compute .VirtualMachineSizeTypes (instanceType ),
@@ -231,12 +251,21 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
231251 Sku : to .StringPtr (sku ),
232252 Version : to .StringPtr (version ),
233253 },
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+ },
234263 },
235264 OsProfile : & compute.OSProfile {
236- ComputerName : to .StringPtr (vmName ),
265+ ComputerName : to .StringPtr ("iterative" ),
237266 AdminUsername : to .StringPtr (username ),
238- AdminPassword : to .StringPtr (password ),
239267 LinuxConfiguration : & compute.LinuxConfiguration {
268+ DisablePasswordAuthentication : to .BoolPtr (true ),
240269 SSH : & compute.SSHConfiguration {
241270 PublicKeys : & []compute.SSHPublicKey {
242271 {
@@ -260,9 +289,28 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
260289 },
261290 },
262291 )
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+ }
264311 _ , err = vmClient .Get (ctx , gpName , vmName , "" )
265312 if err != nil {
313+ ResourceMachineDelete (ctx , d , m )
266314 diags = append (diags , diag.Diagnostic {
267315 Severity : diag .Error ,
268316 Summary : fmt .Sprintf ("cannot create vm: %v" , err ),
@@ -272,9 +320,8 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
272320 }
273321
274322 d .SetId (gpName )
275- d .Set ("instance_id" , vmName )
276323 d .Set ("instance_ip" , ip .IPAddress )
277- // d.Set("instance_launch_time", vm. )
324+ d .Set ("instance_launch_time" , time . Now (). String () )
278325
279326 return diags
280327}
@@ -285,8 +332,7 @@ func ResourceMachineDelete(ctx context.Context, d *schema.ResourceData, m interf
285332
286333 subscriptionID := os .Getenv ("AZURE_SUBSCRIPTION_ID" )
287334 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 ))
290336 if err != nil {
291337 diags = append (diags , diag.Diagnostic {
292338 Severity : diag .Error ,
@@ -370,7 +416,7 @@ func getVMClient(subscriptionID string) (compute.VirtualMachinesClient, error) {
370416func getRegion (d * schema.ResourceData ) string {
371417 instanceRegions := make (map [string ]string )
372418 instanceRegions ["us-east" ] = "eastus"
373- instanceRegions ["us-west" ] = "westus "
419+ instanceRegions ["us-west" ] = "westus2 "
374420 instanceRegions ["eu-north" ] = "northeurope"
375421 instanceRegions ["eu-west" ] = "westeurope"
376422
@@ -390,12 +436,13 @@ func getInstanceType(d *schema.ResourceData) string {
390436 instanceTypes ["mk80" ] = "Standard_NC6"
391437 instanceTypes ["lk80" ] = "Standard_NC12"
392438 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 "
396442
397443 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 {
399446 instanceType = val
400447 }
401448
0 commit comments