-
Notifications
You must be signed in to change notification settings - Fork 12
/
Program.cs
165 lines (137 loc) · 8.94 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
//
// Copyright (c) Microsoft. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Azure;
using Azure.Core;
using Azure.Identity;
using Azure.ResourceManager;
using Azure.ResourceManager.Resources;
using Azure.ResourceManager.Storage;
using Azure.ResourceManager.Storage.Models;
/// <summary>
/// Azure Storage Resource Provider Sample - Demonstrate how to create and manage storage accounts using Storage Resource Provider.
/// Azure Storage Resource Provider enables customers to create and manage storage accounts
///
/// Documentation References:
/// - How to create, manage, or delete a storage account in the Azure Portal - https://azure.microsoft.com/en-us/documentation/articles/storage-create-storage-account/
/// - Storage Resource Provider REST API documentation - https://msdn.microsoft.com/en-us/library/azure/mt163683.aspx
/// </summary>
namespace AzureStorageNew
{
public class StorageAccountTests
{
// You can locate your subscription ID on the Subscriptions blade of the Azure Portal (https://portal.azure.com).
const string subscriptionId = "<subscriptionId>";
// Specify a resource group name of your choice. Specifying a new value will create a new resource group.
const string rgName = "TestResourceGroup";
// Storage account name. Using random value to avoid conflicts. Replace this with a storage account of your choice.
static readonly string storAccountName = $"storagesample{Guid.NewGuid().ToString().Substring(0, 8)}";
// To run the sample, you must first create an Azure service principal. To create the service principal, follow one of these guides:
// Azure Portal: https://azure.microsoft.com/documentation/articles/resource-group-create-service-principal-portal/)
// PowerShell: https://azure.microsoft.com/documentation/articles/resource-group-authenticate-service-principal/
// Azure CLI: https://azure.microsoft.com/documentation/articles/resource-group-authenticate-service-principal-cli/
// Creating the service principal will generate the values you need to specify for the constansts below.
// These values are used by the sample as defaults to create a new storage account. You can specify any location and any storage account type.
static readonly AzureLocation location = AzureLocation.WestUS;
static readonly StorageSku sku = new StorageSku(StorageSkuName.StandardGrs);
static readonly StorageKind kind = StorageKind.StorageV2;
static async Task Main()
{
// Authenticate to Azure and create the top-level ArmClient
ArmClient armClient = new ArmClient(new DefaultAzureCredential());
try
{
// Create a resource identifier, then get the subscription resource
ResourceIdentifier resourceIdentifier = new ResourceIdentifier($"/subscriptions/{subscriptionId}");
SubscriptionResource subscription = armClient.GetSubscriptionResource(resourceIdentifier);
// Register the Storage resource provider in the subscription
ResourceProviderResource resourceProvider = await subscription.GetResourceProviderAsync("Microsoft.Storage");
resourceProvider.Register();
// Create a new resource group (if one already exists then it gets updated)
ArmOperation<ResourceGroupResource> rgOperation = await subscription.GetResourceGroups().CreateOrUpdateAsync(WaitUntil.Completed, rgName, new ResourceGroupData(location));
ResourceGroupResource resourceGroup = rgOperation.Value;
Console.WriteLine($"Resource group: {resourceGroup.Id.Name}");
// Create a new storage account in a specific resource group with the specified account name (request still succeeds if one already exists)
// First we need to define the StorageAccountCreateOrUpdateContent parameters
StorageAccountCreateOrUpdateContent parameters = GetStorageAccountParameters();
// Now we can create a storage account with defined account name and parameters
Console.WriteLine("Creating a storage account...");
StorageAccountCollection accountCollection = resourceGroup.GetStorageAccounts();
ArmOperation<StorageAccountResource> acctOperation = await accountCollection.CreateOrUpdateAsync(WaitUntil.Completed, storAccountName, parameters);
StorageAccountResource storageAccount = acctOperation.Value;
Console.WriteLine($"Storage account created with name {storageAccount.Id.Name}");
// Get all the storage accounts for a given subscription
await GetStorageAccountsForSubscription(subscription);
// Get a list of storage accounts within a specific resource group
await GetStorageAccountsInResourceGroup(resourceGroup);
// Get the storage account keys for a given account and resource group
Pageable<StorageAccountKey> acctKeys = storageAccount.GetKeys();
// Regenerate an account key for a given account
StorageAccountRegenerateKeyContent regenKeyContent = new StorageAccountRegenerateKeyContent("key1");
Pageable<StorageAccountKey> regenAcctKeys = storageAccount.RegenerateKey(regenKeyContent);
//Update the storage account for a given account name and resource group
await UpdateStorageAccountSkuAsync(storageAccount, accountCollection);
// Check if the account name is available
bool? nameAvailable = subscription.CheckStorageAccountNameAvailability(new StorageAccountNameAvailabilityContent(storAccountName)).Value.IsNameAvailable;
// Delete a storage account with the given account name and a resource group
storageAccount = await accountCollection.GetAsync(storAccountName);
Console.WriteLine($"Deleting storage account {storageAccount.Id.Name}");
await storageAccount.DeleteAsync(WaitUntil.Completed);
Console.ReadLine();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.ReadLine();
}
}
/// <summary>
private static async Task GetStorageAccountsInResourceGroup(ResourceGroupResource resourceGroup)
{
Console.WriteLine($"List of storage accounts in {resourceGroup.Id.Name}:");
await foreach (StorageAccountResource storAcct in resourceGroup.GetStorageAccounts())
{
Console.WriteLine($"\t{storAcct.Id.Name}");
}
}
private static async Task GetStorageAccountsForSubscription(SubscriptionResource subscription)
{
AsyncPageable<StorageAccountResource> storAcctsSub = subscription.GetStorageAccountsAsync();
Console.WriteLine($"List of storage accounts in subscription {subscription.Get().Value.Data.DisplayName}:");
await foreach (StorageAccountResource storAcctSub in storAcctsSub)
{
Console.WriteLine($"\t{storAcctSub.Id.Name}");
}
}
private static async Task UpdateStorageAccountSkuAsync(StorageAccountResource storageAccount, StorageAccountCollection accountCollection)
{
Console.WriteLine("Updating storage account...");
// Update storage account sku
var currentSku = storageAccount.Get().Value.Data.Sku.Name; // capture the current Sku value before updating
StorageSku updateSku = new StorageSku(StorageSkuName.StandardLrs);
StorageAccountCreateOrUpdateContent updateParams = new StorageAccountCreateOrUpdateContent(updateSku, kind, location);
await accountCollection.CreateOrUpdateAsync(WaitUntil.Completed, storAccountName, updateParams);
Console.WriteLine($"Sku on storage account updated from {currentSku} to {storageAccount.Get().Value.Data.Sku.Name}");
}
/// <returns>The parameters to provide for the account</returns>
private static StorageAccountCreateOrUpdateContent GetStorageAccountParameters()
{
StorageAccountCreateOrUpdateContent parameters = new StorageAccountCreateOrUpdateContent(sku, kind, location);
return parameters;
}
}
}