This repository was archived by the owner on Nov 16, 2023. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
118 lines (103 loc) · 5.03 KB
/
Copy pathProgram.cs
File metadata and controls
118 lines (103 loc) · 5.03 KB
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
using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Linq;
using System.IO;
// Azure Management dependencies
using Microsoft.Rest.Azure.Authentication;
using Microsoft.Azure.Management.ResourceManager;
using Microsoft.Azure.Management.ResourceManager.Models;
namespace ConsoleApplication
{
public class Program
{
public static void Main(string[] args)
{
var tenantId = Environment.GetEnvironmentVariable("AZURE_TENANT_ID");
var clientId = Environment.GetEnvironmentVariable("AZURE_CLIENT_ID");
var secret = Environment.GetEnvironmentVariable("AZURE_SECRET");
var subscriptionId = Environment.GetEnvironmentVariable("AZURE_SUBSCRIPTION_ID");
if(new List<string>{ tenantId, clientId, secret, subscriptionId }.Any(i => String.IsNullOrEmpty(i))) {
Console.WriteLine("Please provide ENV vars for AZURE_TENANT_ID, AZURE_CLIENT_ID, AZURE_SECRET and AZURE_SUBSCRIPTION_ID.");
}
else
{
RunSample(tenantId, clientId, secret, subscriptionId).Wait();
}
}
public static async Task RunSample(string tenantId, string clientId, string secret, string subscriptionId)
{
// Build the service credentials and Azure Resource Manager clients
var serviceCreds = await ApplicationTokenProvider.LoginSilentAsync(tenantId, clientId, secret);
var resourceClient = new ResourceManagementClient(serviceCreds);
resourceClient.SubscriptionId = subscriptionId;
Random r = new Random();
int postfix = r.Next(0, 1000000);
var resourceGroupName = "sample-dotnet-group-mgmt";
var westus = "westus";
Write("Listing resource groups:");
resourceClient.ResourceGroups.List().ToList().ForEach(rg => {
Write("\tName: {0}, Id: {1}", rg.Name, rg.Id);
});
Write(Environment.NewLine);
Write("Creating resource group named {0} in {1}", resourceGroupName, westus);
var groupParams = new ResourceGroup { Location = westus};
resourceClient.ResourceGroups.CreateOrUpdate(resourceGroupName, groupParams);
Write(Environment.NewLine);
Write("Adding tags to the resource group");
groupParams.Tags = new Dictionary<string, string>{{"Hello", "World"}};
resourceClient.ResourceGroups.CreateOrUpdate(resourceGroupName, groupParams);
Write(Environment.NewLine);
Write("Listing resource groups:");
resourceClient.ResourceGroups.List().ToList().ForEach(rg => {
Write("\tName: {0}, Id: {1}", rg.Name, rg.Id);
});
Write(Environment.NewLine);
Write("Create a Key Vault resource with a generic PUT");
var keyVaultParams = new GenericResource{
Location = westus,
Properties = new Dictionary<string, object>{
{"tenantId", tenantId},
{"sku", new Dictionary<string, object>{{"family", "A"}, {"name", "standard"}}},
{"accessPolicies", Array.Empty<string>()},
{"enabledForDeployment", true},
{"enabledForTemplateDeployment", true},
{"enabledForDiskEncryption", true}
}
};
var keyVault = resourceClient.Resources.CreateOrUpdate(
resourceGroupName,
"Microsoft.KeyVault",
"",
"vaults",
"azureSampleVault",
"2015-06-01",
keyVaultParams);
Write("\tKey Vault Name: {0} and Id: {1}", keyVault.Name, keyVault.Id);
Write(Environment.NewLine);
Write("Listing resources within group {0}", resourceGroupName);
resourceClient.ResourceGroups.ListResources(resourceGroupName).ToList().ForEach(resource => {
Write("\tName: {0}, Id: {1}", resource.Name, resource.Id);
});
Write(Environment.NewLine);
Write("Exporting the resource group template for {0}", resourceGroupName);
Write(Environment.NewLine);
var exportResult = resourceClient.ResourceGroups.ExportTemplate(
resourceGroupName,
new ExportTemplateRequest{
Resources = new List<string>{"*"}
});
Write("{0}", exportResult.Template);
Write(Environment.NewLine);
Write("Press any key to continue and delete the sample resources");
Console.ReadLine();
Write(Environment.NewLine);
Write("deleting resource group {0} and all resources within it", resourceGroupName);
resourceClient.ResourceGroups.Delete(resourceGroupName);
}
private static void Write(string format, params object[] items)
{
Console.WriteLine(String.Format(format, items));
}
}
}