-
Notifications
You must be signed in to change notification settings - Fork 0
/
linux.go
553 lines (495 loc) · 17.3 KB
/
linux.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
package main
import (
"os"
"context"
"io/ioutil"
"log"
"fmt"
"time"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v4"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v2"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources"
)
var subscriptionId string
const (
resourceGroupName = "demo-group"
vmName = "demo-vm"
vnetName = "demo-vnet"
subnetName = "demo-subnet"
nsgName = "demo-nsg"
nicName = "demo-nic"
diskName = "demo-disk"
IPName = "demo-IP"
location = "westus"
)
var (
resourcesClientFactory *armresources.ClientFactory
computeClientFactory *armcompute.ClientFactory
networkClientFactory *armnetwork.ClientFactory
)
var (
resourceGroupClient *armresources.ResourceGroupsClient
virtualNetworksClient *armnetwork.VirtualNetworksClient
subnetsClient *armnetwork.SubnetsClient
securityGroupsClient *armnetwork.SecurityGroupsClient
publicIPAddressesClient *armnetwork.PublicIPAddressesClient
interfacesClient *armnetwork.InterfacesClient
virtualMachinesClient *armcompute.VirtualMachinesClient
disksClient *armcompute.DisksClient
)
func main() {
subscriptionId = os.Getenv("AZURE_SUBSCRIPTION_ID")
if len(subscriptionId) == 0 {
log.Fatal("SubscriptionId is not set")
}
// Creates VM using helper functions
create()
// Wait 5 minute before deleting
fmt.Println("Start 5 minute timer")
time.Sleep(5*time.Minute)
// Delete VM using helper functions
destroy()
log.Println("Linux Virtual Machine deleted successfully")
}
func create() {
conn, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
log.Fatalf("Can't connect to Azure:%+v", err)
}
// Create background context for API calls
ctx := context.Background()
// Set up ARM client factories for managing resources
resourcesClientFactory, err = armresources.NewClientFactory(subscriptionId, conn, nil)
if err != nil {
log.Fatal(err)
}
networkClientFactory, err = armnetwork.NewClientFactory(subscriptionId, conn, nil)
if err != nil {
log.Fatal(err)
}
computeClientFactory, err = armcompute.NewClientFactory(subscriptionId, conn, nil)
if err != nil {
log.Fatal(err)
}
// Initialize clients using factory
resourceGroupClient = resourcesClientFactory.NewResourceGroupsClient()
virtualNetworksClient = networkClientFactory.NewVirtualNetworksClient()
subnetsClient = networkClientFactory.NewSubnetsClient()
securityGroupsClient = networkClientFactory.NewSecurityGroupsClient()
publicIPAddressesClient = networkClientFactory.NewPublicIPAddressesClient()
interfacesClient = networkClientFactory.NewInterfacesClient()
virtualMachinesClient = computeClientFactory.NewVirtualMachinesClient()
disksClient = computeClientFactory.NewDisksClient()
// Create virtual machine using helper functions
if err := createResourceGroup(ctx); err != nil {
log.Fatalf("Can't create Resource Group:%+v", err)
}
log.Println("Resource Group created successfully")
if err := createVirtualNetwork(ctx); err != nil {
log.Fatalf("Can't create Virtual Network:%+v", err)
}
log.Println("Virtual network created successfully")
subnet, err := createSubnets(ctx)
if err != nil {
log.Fatalf("cannot create subnet:%+v", err)
}
log.Println("Subnet created successfully")
IP, err := createIP(ctx)
if err != nil {
log.Fatalf("cannot create public IP address:%+v", err)
}
log.Println("IP created successfully")
nsg, err := createNetworkSecurityGroup(ctx)
if err != nil {
log.Fatalf("cannot create network security group:%+v", err)
}
log.Println("nsg created successfully")
networkInterface, err := createNetworkInterface(ctx, *subnet.ID, *IP.ID, *nsg.ID)
if err != nil {
log.Fatalf("cannot create network interface:%+v", err)
}
log.Println("Network Interface created successfully")
networkInterfaceID := networkInterface.ID
if err := createVirtualMachine(ctx, *networkInterfaceID); err != nil {
log.Fatalf("Can't create Virtual Machine:%+v", err)
}
log.Println("Linux Virtual machine created successfully")
}
func destroy() {
ctx := context.Background()
//Delete virtual machine resources using helper functions
if err := deleteVirtualMachine(ctx); err != nil {
log.Fatalf("Can't delete Virtual Machine:%+v", err)
}
if err := deleteDisk(ctx); err != nil {
log.Fatalf("Can't delete Disk:%+v", err)
}
log.Println("Disk deleted successfully")
if err := deleteNetworkInterface(ctx); err != nil {
log.Fatalf("Can't delete Network Interface:%+v", err)
}
log.Println("Network Interface deleted successfully")
if err := deleteNetworkSecurityGroup(ctx); err != nil {
log.Fatalf("Can't delete nsg:%+v", err)
}
log.Println("nsg deleted successfully")
if err := deleteIP(ctx); err != nil {
log.Fatalf("Can't delete IP Address:%+v", err)
}
log.Println("IP Address deleted successfully")
if err := deleteSubnets(ctx); err != nil {
log.Fatalf("Can't delete Subnets:%+v", err)
}
log.Println("Subnet deleted successfully")
if err := deleteVirtualNetwork(ctx); err != nil {
log.Fatalf("Can't delete Virtual Network:%+v", err)
}
if err := deleteResourceGroup(ctx); err != nil {
log.Fatalf("Can't delete Resource Group:%+v", err)
}
log.Println("ResourceGroup deleted successfully")
}
func createResourceGroup(ctx context.Context) error{
// Create instance of armresources.ResourceGroup
parameters := armresources.ResourceGroup{
Location: to.Ptr(location),
Tags: map[string]*string{"demo-rs-tag": to.Ptr("demo-tag")},
}
// Create Resource Group
_,err := resourceGroupClient.CreateOrUpdate(ctx, resourceGroupName, parameters, nil)
if err != nil {
return err
}
return nil
}
func deleteResourceGroup(ctx context.Context) error{
// Begin deletion
pollerResponse, err := resourceGroupClient.BeginDelete(ctx, resourceGroupName, nil)
if err != nil {
return err
}
// Poll until deletion is done to check for errors during deletion
_, err = pollerResponse.PollUntilDone(ctx, nil)
if err != nil {
return err
}
return nil
}
func createVirtualNetwork(ctx context.Context) error {
// Create instance of Virtual Network
parameters := armnetwork.VirtualNetwork{
Location: to.Ptr(location), // Set the location for the virtual network.
Properties: &armnetwork.VirtualNetworkPropertiesFormat{
AddressSpace: &armnetwork.AddressSpace{
AddressPrefixes: []*string{
to.Ptr("10.1.0.0/16"), // Set the IPv4 address space for the virtual network.
},
},
},
}
// Begin creating Virtual Network and check for errors
pollerResponse, err := virtualNetworksClient.BeginCreateOrUpdate(ctx, resourceGroupName, vnetName, parameters, nil)
if err != nil {
return err
}
// Check for errors until creation of Virtual Network is done
_, err = pollerResponse.PollUntilDone(ctx, nil)
if err != nil {
return err
}
return nil
}
func deleteVirtualNetwork(ctx context.Context) error {
// Initiate deletion of Virtual network
pollerResponse, err := virtualNetworksClient.BeginDelete(ctx, resourceGroupName, vnetName, nil)
if err != nil {
return err
}
// Poll until deletion is finished
_, err = pollerResponse.PollUntilDone(ctx, nil)
if err != nil {
return err
}
return nil
}
func createSubnets(ctx context.Context) (*armnetwork.Subnet, error){
// Create instance of armnetwork.Subnet
parameters := armnetwork.Subnet {
Properties: &armnetwork.SubnetPropertiesFormat{
AddressPrefix: to.Ptr("10.1.10.0/24"),
},
}
// Initiate creation of subnet
pollerResponse, err := subnetsClient.BeginCreateOrUpdate(ctx, resourceGroupName, vnetName, subnetName, parameters, nil)
if err != nil {
return nil, err
}
// Poll until creation of subnet is done.
res, err := pollerResponse.PollUntilDone(ctx, nil)
if err != nil {
return nil, err
}
return &res.Subnet, nil
}
func deleteSubnets(ctx context.Context) error {
// Initiate deletion of Subnet
pollerResponse, err := subnetsClient.BeginDelete(ctx, resourceGroupName, vnetName, subnetName, nil)
if err != nil {
return err
}
// Poll until deletion is complete
_,err = pollerResponse.PollUntilDone(ctx, nil)
if err != nil {
return err
}
return nil
}
func createIP(ctx context.Context) (*armnetwork.PublicIPAddress, error) {
// Create instance of armnetwork.PublicIPAddress
parameters := armnetwork.PublicIPAddress{
Location: to.Ptr(location),
Properties: &armnetwork.PublicIPAddressPropertiesFormat{
PublicIPAllocationMethod: to.Ptr(armnetwork.IPAllocationMethodStatic),
},
}
// Initiate creation of PublicIPAddress
pollerResponse, err := publicIPAddressesClient.BeginCreateOrUpdate(ctx, resourceGroupName, IPName, parameters, nil)
if err != nil {
return nil, err
}
// Poll until creation of PublicIPAddress is complete
res, err := pollerResponse.PollUntilDone(ctx, nil)
if err != nil {
return nil, err
}
return &res.PublicIPAddress, err
}
func deleteIP(ctx context.Context) error {
// Initiate deletion of PublicIPAddress
pollerResponse, err := publicIPAddressesClient.BeginDelete(ctx, resourceGroupName, IPName, nil)
if err != nil {
return err
}
// Poll until deletion of PublicIPAddress is complete
_,err = pollerResponse.PollUntilDone(ctx, nil)
if err != nil {
return err
}
return nil
}
func createNetworkSecurityGroup(ctx context.Context) (*armnetwork.SecurityGroup, error) {
// Create instance of armnetwork.SecurityGroup
parameters := armnetwork.SecurityGroup{
Location: to.Ptr(location),
Properties: &armnetwork.SecurityGroupPropertiesFormat{
SecurityRules: []*armnetwork.SecurityRule{
{ // Define an inbound security rule for SSH (port 22).
Name: to.Ptr("demo_inbound_22"), // Set the name for the inbound rule.
Properties: &armnetwork.SecurityRulePropertiesFormat{
SourceAddressPrefix: to.Ptr("0.0.0.0/0"), // Allow traffic from any source.
SourcePortRange: to.Ptr("*"), // Allow traffic from any source port.
DestinationAddressPrefix: to.Ptr("0.0.0.0/0"), // Allow traffic to any destination.
DestinationPortRange: to.Ptr("22"), // Allow traffic to port 22 (SSH).
Protocol: to.Ptr(armnetwork.SecurityRuleProtocolTCP), // Set the protocol to TCP.
Access: to.Ptr(armnetwork.SecurityRuleAccessAllow), // Allow the traffic.
Priority: to.Ptr[int32](100), // Set the priority for rule evaluation.
Description: to.Ptr("sample network security group inbound port 22"), // Set a description for the rule.
Direction: to.Ptr(armnetwork.SecurityRuleDirectionInbound), // Set the direction to inbound.
},
},
// Define an outbound security rule for SSH (port 22).
{
Name: to.Ptr("demo_outbound_22"), // Set the name for the outbound rule.
Properties: &armnetwork.SecurityRulePropertiesFormat{
SourceAddressPrefix: to.Ptr("0.0.0.0/0"), // Allow traffic from any source.
SourcePortRange: to.Ptr("*"), // Allow traffic from any source port.
DestinationAddressPrefix: to.Ptr("0.0.0.0/0"), // Allow traffic to any destination.
DestinationPortRange: to.Ptr("22"), // Allow traffic to port 2 (SSH).
Protocol: to.Ptr(armnetwork.SecurityRuleProtocolTCP), // Set the protocol to TCP.
Access: to.Ptr(armnetwork.SecurityRuleAccessAllow), // Allow the traffic.
Priority: to.Ptr[int32](100), // Set the priority for rule evaluation.
Description: to.Ptr("demo network security group outbound port 22"), // Set a description for the rule.
Direction: to.Ptr(armnetwork.SecurityRuleDirectionOutbound), // Set the direction to outbound.
},
},
},
},
}
pollerResponse, err := securityGroupsClient.BeginCreateOrUpdate(ctx, resourceGroupName, nsgName, parameters, nil)
if err != nil {
return nil, err
}
resp, err := pollerResponse.PollUntilDone(ctx, nil)
if err != nil {
return nil, err
}
return &resp.SecurityGroup, nil
}
func deleteNetworkSecurityGroup(ctx context.Context) error{
// Begin deletion of nsg
pollerResponse, err := securityGroupsClient.BeginDelete(ctx, resourceGroupName, nsgName, nil)
if err != nil {
return err
}
// Poll until deletion of nsg is complete
_,err = pollerResponse.PollUntilDone(ctx, nil)
if err != nil {
return err
}
return nil
}
func createNetworkInterface(ctx context.Context, subnetID string, IPID string, nsgID string) (*armnetwork.Interface, error){
// Create an instance of armnetwork.Interface
parameters := armnetwork.Interface{
Location: to.Ptr(location),
Properties: &armnetwork.InterfacePropertiesFormat{
IPConfigurations: []*armnetwork.InterfaceIPConfiguration{
{
Name: to.Ptr("ipConfig"),
Properties: &armnetwork.InterfaceIPConfigurationPropertiesFormat{
PrivateIPAllocationMethod: to.Ptr(armnetwork.IPAllocationMethodDynamic),
Subnet: &armnetwork.Subnet{
ID: to.Ptr(subnetID),
},
PublicIPAddress: &armnetwork.PublicIPAddress{
ID: to.Ptr(IPID),
},
},
},
},
NetworkSecurityGroup: &armnetwork.SecurityGroup{
ID: to.Ptr(nsgID),
},
},
}
// Initiate creation of network interface
pollerResponse, err := interfacesClient.BeginCreateOrUpdate(ctx, resourceGroupName, nicName, parameters, nil)
if err != nil {
return nil, err
}
// Poll until creation of network interface is complete
res, err := pollerResponse.PollUntilDone(ctx, nil)
if err != nil {
return nil, err
}
return &res.Interface, err
}
func deleteNetworkInterface(ctx context.Context) error {
// Initiate deletion of network interface
pollerResponse, err := interfacesClient.BeginDelete(ctx, resourceGroupName, nicName, nil)
if err != nil{
return err
}
// Poll until deletion of network interface is complete
_,err = pollerResponse.PollUntilDone(ctx, nil)
if err != nil {
return err
}
return nil
}
func createVirtualMachine(ctx context.Context, networkInterfaceID string) error {
//require ssh key for authentication on linux
sshPublicKeyPath := "/Users/vknr/.ssh/id_rsa.pub"
var sshBytes []byte
_,err := os.Stat(sshPublicKeyPath)
if err == nil {
sshBytes,err = ioutil.ReadFile(sshPublicKeyPath)
if err != nil {
return err
}
}
// Define parameters for creating the virtual machine.
parameters := armcompute.VirtualMachine{
Location: to.Ptr(location), // Set the location for the virtual machine.
Identity: &armcompute.VirtualMachineIdentity{
Type: to.Ptr(armcompute.ResourceIdentityTypeNone),
},
Properties: &armcompute.VirtualMachineProperties{
StorageProfile: &armcompute.StorageProfile{
ImageReference: &armcompute.ImageReference{
//Specify the image reference for the virtual machine.
//require ssh key for authentication on linux
Offer: to.Ptr("UbuntuServer"),
Publisher: to.Ptr("Canonical"),
SKU: to.Ptr("18.04-LTS"),
Version: to.Ptr("latest"),
},
OSDisk: &armcompute.OSDisk{
Name: to.Ptr(diskName), // Set the name for the OS disk.
CreateOption: to.Ptr(armcompute.DiskCreateOptionTypesFromImage),
Caching: to.Ptr(armcompute.CachingTypesReadWrite),
ManagedDisk: &armcompute.ManagedDiskParameters{
StorageAccountType: to.Ptr(armcompute.StorageAccountTypesStandardLRS),
},
},
},
HardwareProfile: &armcompute.HardwareProfile{
VMSize: to.Ptr(armcompute.VirtualMachineSizeTypes("Standard_F2s")), // Set the VM size.
},
OSProfile: &armcompute.OSProfile{
ComputerName: to.Ptr("demo-compute"), // Set the computer name.
AdminUsername: to.Ptr("demo-user"), // Set the admin username.
AdminPassword: to.Ptr("Password!23"), // Set the admin password.
//require ssh key for authentication on linux
LinuxConfiguration: &armcompute.LinuxConfiguration{
DisablePasswordAuthentication: to.Ptr(true),
SSH: &armcompute.SSHConfiguration{
PublicKeys: []*armcompute.SSHPublicKey{
{
Path: to.Ptr(fmt.Sprintf("/home/%s/.ssh/authorized_keys", "demo-user")),
KeyData: to.Ptr(string(sshBytes)),
},
},
},
},
},
NetworkProfile: &armcompute.NetworkProfile{
// Specify the network interfaces for the virtual machine.
NetworkInterfaces: []*armcompute.NetworkInterfaceReference{
{
ID: to.Ptr(networkInterfaceID),
},
},
},
},
}
// Initiate the creation of the virtual machine.
pollerResponse, err := virtualMachinesClient.BeginCreateOrUpdate(ctx, resourceGroupName, vmName, parameters, nil)
if err != nil {
return err
}
// Poll until the virtual machine creation is done.
_,err = pollerResponse.PollUntilDone(ctx, nil)
if err != nil {
return err
}
return nil
}
func deleteVirtualMachine(ctx context.Context) error {
// Begin deletion of Virtual Machine
pollerResponse, err := virtualMachinesClient.BeginDelete(ctx, resourceGroupName, vmName, nil)
if err != nil {
return err
}
// Poll until deletion of Virtual Machine is done
_,err = pollerResponse.PollUntilDone(ctx, nil)
if err != nil {
return err
}
return nil
}
func deleteDisk(ctx context.Context) error {
// Begin deletion of Disk
pollerResponse, err := disksClient.BeginDelete(ctx, resourceGroupName, diskName, nil)
if err != nil {
return err
}
// Poll until deletion of Disk
_,err = pollerResponse.PollUntilDone(ctx, nil)
if err != nil {
return err
}
return nil
}