-
Notifications
You must be signed in to change notification settings - Fork 4
/
Program.cs
171 lines (144 loc) · 7.61 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
160
161
162
163
164
165
166
167
168
169
170
171
// 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;
using Azure.ResourceManager.AppService;
using Azure.ResourceManager.AppService.Models;
using Azure.ResourceManager.Resources;
using Azure.ResourceManager.Samples.Common;
using Azure.ResourceManager.Sql;
using Azure.ResourceManager.Sql.Models;
using Azure.ResourceManager.Storage;
using Azure.ResourceManager.Storage.Models;
using Azure.Storage.Blobs;
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace ManageWebAppStorageAccountConnection
{
public class Program
{
private const string SUFFIX = ".azurewebsites.net";
/**
* Azure App Service basic sample for managing web apps.
* - Create a storage account and upload a couple blobs
* - Create a web app that contains the connection string to the storage account
* - Deploy a Tomcat application that reads from the storage account
* - Clean up
*/
public static async Task RunSample(ArmClient client)
{
AzureLocation region = AzureLocation.EastUS;
string app1Name = Utilities.CreateRandomName("webapp1-");
string app1Url = app1Name + SUFFIX;
string storageName = Utilities.CreateRandomName("jsdkstore");
string containerName = Utilities.CreateRandomName("jcontainer");
string resourceGroupName = Utilities.CreateRandomName("rg1NEMV_");
var lro = await client.GetDefaultSubscription().GetResourceGroups().CreateOrUpdateAsync(Azure.WaitUntil.Completed, resourceGroupName, new ResourceGroupData(AzureLocation.EastUS));
var resourceGroup = lro.Value;
try
{
//============================================================
// Create a storage account for the web app to use
Utilities.Log("Creating storage account " + storageName + "...");
var accountCollection = resourceGroup.GetStorageAccounts();
var accountData = new StorageAccountCreateOrUpdateContent(new StorageSku("sku"), StorageKind.Storage, region);
var account_lro = await accountCollection.CreateOrUpdateAsync(WaitUntil.Completed, storageName, accountData);
var account = account_lro.Value;
var accountKey = account.GetKeys().FirstOrDefault().Value;
var connectionString = $"DefaultEndpointsProtocol=https;AccountName={account.Data.Name};AccountKey={accountKey}";
var blobClient = new BlobContainerClient(connectionString, containerName);
Utilities.Log("Created storage account " + account.Data.Name);
//============================================================
// Upload a few files to the storage account blobs
Utilities.Log("Uploading 2 blobs to container " + containerName + "...");
await Utilities.UploadFromFileAsync(
blobClient,
new[]
{
Path.Combine(Utilities.ProjectPath, "Asset", "helloworld.war"),
Path.Combine(Utilities.ProjectPath, "Asset", "install_apache.Sh")
});
Utilities.Log("Uploaded 2 blobs to container " + containerName);
//============================================================
// Create a web app with a new app service plan
Utilities.Log("Creating web app " + app1Name + "...");
var webSiteCollection = resourceGroup.GetWebSites();
var webSiteData = new WebSiteData(region)
{
SiteConfig = new SiteConfigProperties()
{
WindowsFxVersion = "PricingTier.StandardS1",
NetFrameworkVersion = "NetFrameworkVersion.V4_6",
PhpVersion = "PhpVersion.V5_6",
},
};
var webSite_lro = await webSiteCollection.CreateOrUpdateAsync(Azure.WaitUntil.Completed, app1Name, webSiteData);
var webSite = webSite_lro.Value;
Utilities.Log("Created web app " + webSite.Data.Name);
Utilities.Print(webSite);
//============================================================
// Deploy a web app that connects to the storage account
// Source code: https://github.com/jianghaolu/azure-samples-blob-explorer
Utilities.Log("Deploying azure-samples-blob-traverser.war to " + app1Name + " through FTP...");
var csm = new CsmPublishingProfile()
{
Format = PublishingProfileFormat.Ftp
};
Utilities.UploadFileToWebApp(
await webSite.GetPublishingProfileXmlWithSecretsAsync(csm),
Path.Combine(Utilities.ProjectPath, "Asset", "azure-samples-blob-traverser.war"));
Utilities.Log("Deployment azure-samples-blob-traverser.war to web app " + webSite.Data.Name + " completed");
Utilities.Print(webSite);
// warm up
Utilities.Log("Warming up " + app1Url + "/azure-samples-blob-traverser...");
Utilities.CheckAddress("http://" + app1Url + "/azure-samples-blob-traverser");
Thread.Sleep(5000);
Utilities.Log("CURLing " + app1Url + "/azure-samples-blob-traverser...");
Utilities.Log(Utilities.CheckAddress("http://" + app1Url + "/azure-samples-blob-traverser"));
}
finally
{
try
{
Utilities.Log("Deleting Resource Group: " + resourceGroupName);
await resourceGroup.DeleteAsync(WaitUntil.Completed);
Utilities.Log("Deleted Resource Group: " + resourceGroupName);
}
catch (NullReferenceException)
{
Utilities.Log("Did not create any resources in Azure. No clean up is necessary");
}
catch (Exception g)
{
Utilities.Log(g);
}
}
}
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);
// Print selected subscription
Utilities.Log("Selected subscription: " + client.GetSubscriptions().Id);
await RunSample(client);
}
catch (Exception e)
{
Utilities.Log(e);
}
}
}
}