-
Notifications
You must be signed in to change notification settings - Fork 8
/
Utilities.cs
119 lines (106 loc) · 4.35 KB
/
Utilities.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using Azure.Core;
using Azure.ResourceManager.Network.Models;
using Azure.ResourceManager.Network;
using Azure.ResourceManager.Resources;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Azure.ResourceManager.Compute;
using Azure.ResourceManager.Compute.Models;
using System.Xml.Linq;
namespace Azure.ResourceManager.Samples.Common
{
public static class Utilities
{
public static Action<string> LoggerMethod { get; set; }
public static Func<string> PauseMethod { get; set; }
public static string ProjectPath { get; set; }
private static Random _random => new Random();
static Utilities()
{
LoggerMethod = Console.WriteLine;
PauseMethod = Console.ReadLine;
ProjectPath = ".";
}
public static void Log(string message)
{
LoggerMethod.Invoke(message);
}
public static void Log(object obj)
{
if (obj != null)
{
LoggerMethod.Invoke(obj.ToString());
}
else
{
LoggerMethod.Invoke("(null)");
}
}
public static void Log()
{
Utilities.Log("");
}
public static string ReadLine() => PauseMethod.Invoke();
public static string CreateRandomName(string namePrefix) => $"{namePrefix}{_random.Next(9999)}";
public static string CreatePassword() => "azure12345QWE!";
public static string CreateUsername() => "tirekicker";
public static async Task<PublicIPAddressResource> CreatePublicIP(ResourceGroupResource resourceGroup, String pipName)
{
PublicIPAddressData publicIPInput = new PublicIPAddressData()
{
Location = resourceGroup.Data.Location,
PublicIPAllocationMethod = NetworkIPAllocationMethod.Dynamic,
DnsSettings = new PublicIPAddressDnsSettings()
{
DomainNameLabel = pipName
}
};
var publicIPLro = await resourceGroup.GetPublicIPAddresses().CreateOrUpdateAsync(WaitUntil.Completed, pipName, publicIPInput);
return publicIPLro.Value;
}
public static VirtualMachineData GetDefaultVMInputData(ResourceGroupResource resourceGroup, string vmName) =>
new VirtualMachineData(resourceGroup.Data.Location)
{
HardwareProfile = new VirtualMachineHardwareProfile() { VmSize = VirtualMachineSizeType.StandardB4Ms },
StorageProfile = new VirtualMachineStorageProfile()
{
ImageReference = new ImageReference()
{
Publisher = "MicrosoftWindowsDesktop",
Offer = "Windows-10",
Sku = "win10-21h2-ent",
Version = "latest",
},
OSDisk = new VirtualMachineOSDisk(DiskCreateOptionType.FromImage)
{
OSType = SupportedOperatingSystemType.Windows,
Name = CreateRandomName("myVMOSdisk"),
Caching = CachingType.ReadOnly,
ManagedDisk = new VirtualMachineManagedDisk()
{
StorageAccountType = StorageAccountType.StandardLrs,
},
},
},
OSProfile = new VirtualMachineOSProfile()
{
AdminUsername = CreateUsername(),
AdminPassword = CreatePassword(),
ComputerName = vmName,
},
NetworkProfile = new VirtualMachineNetworkProfile() { }
};
public static void PrintNetworkSecurityGroup(NetworkSecurityGroupResource nsg)
{
Log($"List all security rules under {nsg.Data.Name}:");
foreach (var rule in nsg.Data.SecurityRules)
{
Log(rule.Name);
}
}
}
}