-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProgram.cs
159 lines (143 loc) · 7.04 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
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using Azure;
using Azure.Core;
using Azure.Identity;
using Azure.ResourceManager.Resources.Models;
using Azure.ResourceManager.Samples.Common;
using Azure.ResourceManager.Resources;
using Azure.ResourceManager;
using System.Net;
using Azure.ResourceManager.Network.Models;
using Azure.ResourceManager.Network;
using Azure.ResourceManager.Compute.Models;
using Azure.ResourceManager.Compute;
using System.Net.NetworkInformation;
using System.Xml.Linq;
using Azure.Core.Pipeline;
namespace ManageVirtualMachineFromMSIEnabledVirtualMachine
{
public class Program
{
/**
* Azure Compute sample for managing virtual machine from Managed Service Identity (MSI) enabled virtual machine -
* - Create a virtual machine using MSI credentials from System assigned or User Assigned MSI enabled VM.
*/
public static void Main(string[] args)
{
// This sample required to be run from a MSI (User Assigned or System Assigned) enabled virtual machine with role
// based contributor access to the resource group specified as the second command line argument.
//
// see /~https://github.com/Azure-Samples/compute-dotnet-manage-user-assigned-msi-enabled-virtual-machine.git
//
string usage = "Usage: dotnet run <subscription-id> <rg-name> [<client-id>]";
if (args.Length < 2)
{
throw new ArgumentException(usage);
}
string subscriptionId = args[0];
string resourceGroupName = args[1];
string clientId = args.Length > 2 ? args[2] : null;
string linuxVMName = Utilities.CreateRandomName("vm");
string userName = Utilities.CreateUsername();
string password = Utilities.CreatePassword();
//=============================================================
// MSI Authenticate
var credential = new DefaultAzureCredential(new DefaultAzureCredentialOptions
{
ManagedIdentityClientId = clientId
});
ArmClient client = new ArmClient(credential, subscriptionId);
SubscriptionResource subscription = client.GetDefaultSubscription();
var resourceGroupLro = subscription.GetResourceGroups().Get(resourceGroupName);
ResourceGroupResource resourceGroup = resourceGroupLro.Value;
Console.WriteLine("Selected subscription: " + subscription.Data.Id);
//=============================================================
// Create a Linux VM using MSI credentials
Console.WriteLine("Creating a Linux VM using MSI credentials");
Utilities.Log("Pre-creating some resources that the VM depends on");
// Creating a virtual network
Utilities.Log("Creating virtual network...");
string vnetName = Utilities.CreateRandomName("vnet");
VirtualNetworkData vnetInput = new VirtualNetworkData()
{
Location = resourceGroup.Data.Location,
AddressPrefixes = { "10.10.0.0/16" },
Subnets =
{
new SubnetData() { Name = "subnet1", AddressPrefix = "10.10.1.0/24"},
new SubnetData() { Name = "subnet2", AddressPrefix = "10.10.2.0/24"},
},
};
var vnetLro = resourceGroup.GetVirtualNetworks().CreateOrUpdate(WaitUntil.Completed, vnetName, vnetInput);
Utilities.Log($"Created a virtual network: {vnetLro.Value.Data.Name}");
// Creating network interface
Utilities.Log($"Creating network interface...");
string nicName = Utilities.CreateRandomName("nic");
var nicInput = new NetworkInterfaceData()
{
Location = resourceGroup.Data.Location,
IPConfigurations =
{
new NetworkInterfaceIPConfigurationData()
{
Name = "default-config",
PrivateIPAllocationMethod = NetworkIPAllocationMethod.Dynamic,
Subnet = new SubnetData()
{
Id = vnetLro.Value.Data.Subnets[0].Id
},
}
}
};
var networkInterfaceLro = resourceGroup.GetNetworkInterfaces().CreateOrUpdate(WaitUntil.Completed, nicName, nicInput);
Utilities.Log($"Created network interface: {networkInterfaceLro.Value.Data.Name}");
Utilities.Log("Creating a Linux VM with MSI associated and install DotNet and Git");
VirtualMachineData linuxVMInput = new VirtualMachineData(resourceGroup.Data.Location)
{
HardwareProfile = new VirtualMachineHardwareProfile()
{
VmSize = VirtualMachineSizeType.StandardF2
},
StorageProfile = new VirtualMachineStorageProfile()
{
ImageReference = new ImageReference()
{
Publisher = "Canonical",
Offer = "UbuntuServer",
Sku = "16.04-LTS",
Version = "latest",
},
OSDisk = new VirtualMachineOSDisk(DiskCreateOptionType.FromImage)
{
OSType = SupportedOperatingSystemType.Linux,
Caching = CachingType.ReadWrite,
ManagedDisk = new VirtualMachineManagedDisk()
{
StorageAccountType = StorageAccountType.StandardLrs
}
},
},
OSProfile = new VirtualMachineOSProfile()
{
AdminUsername = userName,
AdminPassword = password,
ComputerName = linuxVMName,
},
NetworkProfile = new VirtualMachineNetworkProfile()
{
NetworkInterfaces =
{
new VirtualMachineNetworkInterfaceReference()
{
Id = networkInterfaceLro.Value.Data.Id,
Primary = true,
}
}
},
};
var linuxVmLro = resourceGroup.GetVirtualMachines().CreateOrUpdate(WaitUntil.Completed, linuxVMName, linuxVMInput);
Console.WriteLine($"Created virtual machine {linuxVmLro.Value.Data.Name} using MSI credentials: ");
}
}
}