-
Notifications
You must be signed in to change notification settings - Fork 5
/
Program.cs
101 lines (91 loc) · 4.7 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
// 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;
namespace DeployUsingARMTemplateWithProgress
{
public class Program
{
private static ResourceIdentifier? _resourceGroupId = null;
/**
* Azure Resource sample for deploying resources using an ARM template and
* showing progress.
*/
public static async Task RunSample(ArmClient client)
{
try
{
// Get default subscription
SubscriptionResource subscription = await client.GetDefaultSubscriptionAsync();
// Create a resource group in the EastUS region
string rgName = Utilities.CreateRandomName("ARMTemplateRG");
Utilities.Log($"created resource group with name:{rgName}");
ArmOperation<ResourceGroupResource> rgLro = await subscription.GetResourceGroups().CreateOrUpdateAsync(WaitUntil.Completed, rgName, new ResourceGroupData(AzureLocation.EastUS));
ResourceGroupResource resourceGroup = rgLro.Value;
Utilities.Log("Created a resource group with name: " + resourceGroup.Data.Name);
//=============================================================
// Create a deployment for an Azure App Service via an ARM template.
string deploymentName = Utilities.CreateRandomName("myDeployment");
Utilities.Log("Starting a deployment for an Azure App Service: " + deploymentName);
// Get the deployment collection from the resource group
ArmDeploymentCollection armDeploymentCollection = resourceGroup.GetArmDeployments();
Utilities.Log("Load a template JSON, which can originate from local or network sources...");
ArmDeploymentContent input = new ArmDeploymentContent(new ArmDeploymentProperties(ArmDeploymentMode.Incremental)
{
TemplateLink = new ArmDeploymentTemplateLink()
{
Uri = new Uri("https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/quickstarts/microsoft.web/app-service-docs-linux/azuredeploy.json")
},
});
ArmOperation<ArmDeploymentResource> lro = await armDeploymentCollection.CreateOrUpdateAsync(WaitUntil.Completed, deploymentName, input);
ArmDeploymentResource deployment = lro.Value;
Utilities.Log("Started a deployment for an Azure App Service: " + deploymentName);
Utilities.Log("Current deployment status : " + deployment.Data.Properties.ProvisioningState);
}
finally
{
try
{
if (_resourceGroupId is not null)
{
Utilities.Log($"Deleting Resource Group: {_resourceGroupId}");
await client.GetResourceGroupResource(_resourceGroupId).DeleteAsync(WaitUntil.Completed);
Utilities.Log($"Deleted Resource Group: {_resourceGroupId}");
}
}
catch (NullReferenceException)
{
Utilities.Log("Did not create any resources in Azure. No clean up is necessary");
}
catch (Exception ex)
{
Utilities.Log(ex);
}
}
}
public static async Task Main(string[] args)
{
try
{
//=================================================================
// Authenticate
var clientId = Environment.GetEnvironmentVariable("CLIENT_ID");
var clientSecret = Environment.GetEnvironmentVariable("CLIENT_SECRET");
var tenantId = Environment.GetEnvironmentVariable("TENANT_ID");
var subscription = Environment.GetEnvironmentVariable("SUBSCRIPTION_ID");
ClientSecretCredential credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
ArmClient client = new ArmClient(credential, subscription);
await RunSample(client);
}
catch (Exception ex)
{
Utilities.Log(ex);
}
}
}
}