-
Notifications
You must be signed in to change notification settings - Fork 3
/
Program.cs
123 lines (105 loc) · 5.32 KB
/
Program.cs
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using Microsoft.Azure.Management.Compute.Fluent;
using Microsoft.Azure.Management.Compute.Fluent.Models;
using Microsoft.Azure.Management.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent.Authentication;
using Microsoft.Azure.Management.ResourceManager.Fluent.Core;
using Microsoft.Azure.Management.ResourceManager.Fluent.Core.ResourceActions;
using Microsoft.Azure.Management.Samples.Common;
using System;
using System.Collections.Generic;
namespace ManageVirtualMachinesInParallel
{
public class Program
{
private const int vmCount = 2;
private static readonly string userName = Utilities.CreateUsername();
private static readonly string password = Utilities.CreatePassword();
/**
* Azure Compute sample for managing virtual machines -
* - Create N virtual machines in parallel
*/
public static void RunSample(IAzure azure)
{
string rgName = SdkContext.RandomResourceName("rgCOPP", 24);
string networkName = SdkContext.RandomResourceName("vnetCOMV", 24);
string storageAccountName = SdkContext.RandomResourceName("stgCOMV", 20);
try
{
// Create a resource group [Where all resources gets created]
IResourceGroup resourceGroup = azure.ResourceGroups
.Define(rgName)
.WithRegion(Region.USEast)
.Create();
// Prepare Creatable Network definition [Where all the virtual machines get added to]
var creatableNetwork = azure.Networks
.Define(networkName)
.WithRegion(Region.USEast)
.WithExistingResourceGroup(resourceGroup)
.WithAddressSpace("172.16.0.0/16");
// Prepare Creatable Storage account definition [For storing VMs disk]
var creatableStorageAccount = azure.StorageAccounts
.Define(storageAccountName)
.WithRegion(Region.USEast)
.WithExistingResourceGroup(resourceGroup);
// Prepare a batch of Creatable Virtual Machines definitions
List<ICreatable<IVirtualMachine>> creatableVirtualMachines = new List<ICreatable<IVirtualMachine>>();
for (int i = 0; i < vmCount; i++)
{
var creatableVirtualMachine = azure.VirtualMachines
.Define("VM-" + i)
.WithRegion(Region.USEast)
.WithExistingResourceGroup(resourceGroup)
.WithNewPrimaryNetwork(creatableNetwork)
.WithPrimaryPrivateIPAddressDynamic()
.WithoutPrimaryPublicIPAddress()
.WithPopularLinuxImage(KnownLinuxVirtualMachineImage.UbuntuServer16_04_Lts)
.WithRootUsername(Utilities.CreateUsername())
.WithRootPassword(Utilities.CreatePassword())
.WithSize(VirtualMachineSizeTypes.Parse("Standard_D2a_v4"))
.WithNewStorageAccount(creatableStorageAccount);
creatableVirtualMachines.Add(creatableVirtualMachine);
}
var startTime = DateTimeOffset.Now.UtcDateTime;
Utilities.Log("Creating the virtual machines");
Utilities.Log("Created virtual machines");
var virtualMachines = azure.VirtualMachines.Create(creatableVirtualMachines.ToArray());
foreach (var virtualMachine in virtualMachines)
{
Utilities.Log(virtualMachine.Id);
}
var endTime = DateTimeOffset.Now.UtcDateTime;
Utilities.Log($"Created VM: took {(endTime - startTime).TotalSeconds} seconds");
}
finally
{
Utilities.Log($"Deleting resource group : {rgName}");
azure.ResourceGroups.DeleteByName(rgName);
Utilities.Log($"Deleted resource group : {rgName}");
}
}
public static void Main(string[] args)
{
try
{
//=============================================================
// Authenticate
AzureCredentials credentials = SdkContext.AzureCredentialsFactory.FromFile(Environment.GetEnvironmentVariable("AZURE_AUTH_LOCATION"));
var azure = Azure
.Configure()
.WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
.Authenticate(credentials)
.WithDefaultSubscription();
// Print selected subscription
Utilities.Log("Selected subscription: " + azure.SubscriptionId);
RunSample(azure);
}
catch (Exception ex)
{
Utilities.Log(ex);
}
}
}
}