Skip to content

Commit

Permalink
Merge pull request #7 from tamram/master
Browse files Browse the repository at this point in the history
update mgmt sample
  • Loading branch information
tamram committed Dec 10, 2019
2 parents 2afe432 + 32a8001 commit 0d684ac
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 73 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ AppPackages/

# Others
sql/
sqlite3/
*.Cache
ClientBin/
[Ss]tyle[Cc]op.*
Expand Down
14 changes: 11 additions & 3 deletions App.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
<?xml version="1.0" encoding="utf-8" ?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
</startup>
</configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-12.0.0.0" newVersion="12.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
68 changes: 49 additions & 19 deletions AzureStorageNew.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>AzureStorageNew</RootNamespace>
<AssemblyName>AzureStorageNew</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand All @@ -32,40 +33,69 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.Azure.Management.Storage, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>packages\Microsoft.Azure.Management.Storage.5.0.0-preview\lib\net45\Microsoft.Azure.Management.Storage.dll</HintPath>
<Private>True</Private>
<Reference Include="Microsoft.Azure.Management.Storage, Version=14.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>packages\Microsoft.Azure.Management.Storage.14.2.0\lib\net461\Microsoft.Azure.Management.Storage.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Azure.ResourceManager, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>packages\Microsoft.Azure.ResourceManager.1.0.0-preview\lib\net45\Microsoft.Azure.ResourceManager.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.IdentityModel.Clients.ActiveDirectory, Version=2.23.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>packages\Microsoft.IdentityModel.Clients.ActiveDirectory.2.23.302261847\lib\net45\Microsoft.IdentityModel.Clients.ActiveDirectory.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.IdentityModel.Clients.ActiveDirectory.WindowsForms, Version=2.23.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>packages\Microsoft.IdentityModel.Clients.ActiveDirectory.2.23.302261847\lib\net45\Microsoft.IdentityModel.Clients.ActiveDirectory.WindowsForms.dll</HintPath>
<Private>True</Private>
<Reference Include="Microsoft.IdentityModel.Clients.ActiveDirectory, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>packages\Microsoft.IdentityModel.Clients.ActiveDirectory.5.2.4\lib\net45\Microsoft.IdentityModel.Clients.ActiveDirectory.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Rest.ClientRuntime, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>packages\Microsoft.Rest.ClientRuntime.2.1.0\lib\net45\Microsoft.Rest.ClientRuntime.dll</HintPath>
<Private>True</Private>
<HintPath>packages\Microsoft.Rest.ClientRuntime.2.3.21\lib\net461\Microsoft.Rest.ClientRuntime.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Rest.ClientRuntime.Azure, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>packages\Microsoft.Rest.ClientRuntime.Azure.3.1.0\lib\net45\Microsoft.Rest.ClientRuntime.Azure.dll</HintPath>
<Private>True</Private>
<HintPath>packages\Microsoft.Rest.ClientRuntime.Azure.3.3.19\lib\net461\Microsoft.Rest.ClientRuntime.Azure.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>packages\Newtonsoft.Json.6.0.8\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>packages\Newtonsoft.Json.12.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.Composition" />
<Reference Include="System.Core" />
<Reference Include="System.Drawing" />
<Reference Include="System.IdentityModel" />
<Reference Include="System.IO, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>packages\System.IO.4.3.0\lib\net462\System.IO.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.Net" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Net.Http, Version=4.1.1.3, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>packages\System.Net.Http.4.3.4\lib\net46\System.Net.Http.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.Net.Http.WebRequest" />
<Reference Include="System.Runtime, Version=4.1.1.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>packages\System.Runtime.4.3.1\lib\net462\System.Runtime.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Security.Cryptography.Algorithms, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>packages\System.Security.Cryptography.Algorithms.4.3.1\lib\net463\System.Security.Cryptography.Algorithms.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.Security.Cryptography.Encoding, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>packages\System.Security.Cryptography.Encoding.4.3.0\lib\net46\System.Security.Cryptography.Encoding.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.Security.Cryptography.Primitives, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>packages\System.Security.Cryptography.Primitives.4.3.0\lib\net46\System.Security.Cryptography.Primitives.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.Security.Cryptography.X509Certificates, Version=4.1.1.2, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>packages\System.Security.Cryptography.X509Certificates.4.3.2\lib\net461\System.Security.Cryptography.X509Certificates.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
Expand Down
22 changes: 9 additions & 13 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,14 @@
//

using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using Microsoft.Azure;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Microsoft.Azure.Management.Resources;
using Microsoft.Azure.Management.Resources.Models;
using Microsoft.Azure.Management.Storage;
using Microsoft.Azure.Management.Storage.Models;
using Microsoft.Rest;
using Microsoft.Rest.Azure;

/// <summary>
/// Azure Storage Resource Provider Sample - Demonstrate how to create and manage storage accounts using Storage Resource Provider.
Expand All @@ -40,7 +37,7 @@ 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>";
const string subscriptionId = "<subscription-id>";

//Specify a resource group name of your choice. Specifying a new value will create a new resource group.
const string rgName = "TestResourceGroup";
Expand All @@ -62,19 +59,18 @@ public class StorageAccountTests
// 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.
const string DefaultLocation = "westus";
public static Sku DefaultSku = new Sku(SkuName.StandardGRS);
public static Kind DefaultKind = Kind.Storage;
public static Dictionary<string, string> DefaultTags = new Dictionary<string, string>
{
{"key1","value1"},
{"key2","value2"}
};

//The following method will enable you to use the token to create credentials
private static string GetAuthorizationHeader()
private static async Task<string> GetAuthorizationHeader()
{
ClientCredential cc = new ClientCredential(applicationId, password);
var context = new AuthenticationContext("https://login.windows.net/" + tenantId);
var result = context.AcquireToken("https://management.azure.com/", cc);
var result = await context.AcquireTokenAsync("https://management.azure.com/", cc);

if (result == null)
{
Expand All @@ -88,7 +84,7 @@ private static string GetAuthorizationHeader()

static void Main(string[] args)
{
string token = GetAuthorizationHeader();
string token = GetAuthorizationHeader().Result;
TokenCredentials credential = new TokenCredentials(token);
ResourceManagementClient resourcesClient = new ResourceManagementClient(credential) { SubscriptionId = subscriptionId };
StorageManagementClient storageMgmtClient = new StorageManagementClient(credential) { SubscriptionId = subscriptionId };
Expand Down Expand Up @@ -194,15 +190,15 @@ private static void DeleteStorageAccount(string rgname, string acctName, Storage
Console.WriteLine("Deleting a storage account...");
storageMgmtClient.StorageAccounts.Delete(rgname, acctName);
Console.WriteLine("Storage account " + acctName + " deleted");
}
}

/// <summary>
/// Updates the storage account
/// </summary>
/// <param name="rgname">Resource Group Name</param>
/// <param name="acctName">Account Name</param>
/// <param name="storageMgmtClient"></param>
private static void UpdateStorageAccountSku(string rgname, string acctName, SkuName skuName, StorageManagementClient storageMgmtClient)
private static void UpdateStorageAccountSku(string rgname, string acctName, string skuName, StorageManagementClient storageMgmtClient)
{
Console.WriteLine("Updating storage account...");
// Update storage account sku
Expand All @@ -211,7 +207,7 @@ private static void UpdateStorageAccountSku(string rgname, string acctName, SkuN
Sku = new Sku(skuName)
};
var storageAccount = storageMgmtClient.StorageAccounts.Update(rgname, acctName, parameters);
Console.WriteLine("Sku on storage account updated to " + storageAccount.Sku.Name);
Console.WriteLine("Sku on storage account updated to " + storageAccount.Sku.Name);
}

/// <summary>
Expand All @@ -223,7 +219,7 @@ private static StorageAccountCreateParameters GetDefaultStorageAccountParameters
StorageAccountCreateParameters account = new StorageAccountCreateParameters
{
Location = DefaultLocation,
Kind = DefaultKind,
Kind = Kind.StorageV2,
Tags = DefaultTags,
Sku = DefaultSku
};
Expand Down
73 changes: 40 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,31 @@ This sample shows how to manage your storage account using the Azure Storage Res
To run the sample, follow these steps:

1. If you don't already have a Microsoft Azure subscription, you can register for a [free trial account](http://go.microsoft.com/fwlink/?LinkId=330212).
2. Install [Visual Studio](https://www.visualstudio.com/downloads/download-visual-studio-vs.aspx) if you don't have it already.
3. Install the [Azure SDK for .NET](https://azure.microsoft.com/downloads/) if you have not already done so. We recommend using the most recent version.
4. Clone the sample repository.
1. Install [Visual Studio](https://www.visualstudio.com/downloads/download-visual-studio-vs.aspx) if you don't have it already.
1. Install the [Azure SDK for .NET](https://azure.microsoft.com/downloads/) if you have not already done so. We recommend using the most recent version.
1. Clone the sample repository.

https://github.com/Azure-Samples/storage-dotnet-resource-provider-getting-started.git
`https://github.com/Azure-Samples/storage-dotnet-resource-provider-getting-started.git`

5. Create an Azure service principal using
[Azure CLI](https://azure.microsoft.com/documentation/articles/resource-group-authenticate-service-principal-cli/),
[PowerShell](https://azure.microsoft.com/documentation/articles/resource-group-authenticate-service-principal/)
or [Azure Portal](https://azure.microsoft.com/documentation/articles/resource-group-create-service-principal-portal/). Note that you will need to specify the values shown in step 8 in order to run the sample, so it's recommended that you copy and save them during this step.
1. Create an Azure service principal using [Azure CLI](https://azure.microsoft.com/documentation/articles/resource-group-authenticate-service-principal-cli/), [PowerShell](https://azure.microsoft.com/documentation/articles/resource-group-authenticate-service-principal/), or the [Azure portal](https://azure.microsoft.com/documentation/articles/resource-group-create-service-principal-portal/). Note that you will need to specify the values shown in step 8 in order to run the sample, so it's recommended that you copy and save them during this step.

6. Open the sample solution in Visual Studio, and restore any packages if prompted.
7. In the sample source code, locate the constants for your subscription ID and resource group name, and specify values for them.

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";
1. Open the sample solution in Visual Studio, and restore any packages if prompted.
1. In the sample source code, locate the constants for your subscription ID and resource group name, and specify values for them.

8. In the sample source code, locate the following variables, and provide the values that you generated when you created the Azure service principal in step 5 above:
```csharp
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";
```

const string applicationId = "<applicationId>";
const string password = "<password>";
const string tenantId = "<tenantId>";
1. In the sample source code, locate the following variables, and provide the values that you generated when you created the Azure service principal in step 5 above:

```csharp
const string applicationId = "<applicationId>";
const string password = "<password>";
const string tenantId = "<tenantId>";
```

## What is program.cs doing?

Expand All @@ -56,31 +57,37 @@ The sample gets an authorization token and constructs the necessary credentials

Next, the sample sets up a ResourceManagementClient object and a StorageManagementClient object using your subscription and the credentials.

string token = GetAuthorizationHeader();
TokenCredentials credential = new TokenCredentials(token);
ResourceManagementClient resourcesClient = new ResourceManagementClient(credential) { SubscriptionId = subscriptionId };
StorageManagementClient storageMgmtClient = new StorageManagementClient(credential) { SubscriptionId = subscriptionId };
```csharp
string token = GetAuthorizationHeader();
TokenCredentials credential = new TokenCredentials(token);
ResourceManagementClient resourcesClient = new ResourceManagementClient(credential) { SubscriptionId = subscriptionId };
StorageManagementClient storageMgmtClient = new StorageManagementClient(credential) { SubscriptionId = subscriptionId };
```

### Register the Storage Resource Provider

The sample registers the Storage Resource Provider for the subscription:
The sample registers the Storage Resource Provider for the subscription:

resourcesClient.Providers.Register("Microsoft.Storage");
```csharp
resourcesClient.Providers.Register("Microsoft.Storage");
```

### Specify a resource group

The sample creates a new resource group or specifies an existing resource group for the new storage account.
The sample creates a new resource group or specifies an existing resource group for the new storage account.

var resourceGroup = resourcesClient.ResourceGroups.CreateOrUpdate(
rgname,
new ResourceGroup
{
Location = DefaultLocation
});
```csharp
var resourceGroup = resourcesClient.ResourceGroups.CreateOrUpdate(
rgname,
new ResourceGroup
{
Location = DefaultLocation
});
```

### Create a new storage account

Next, the sample creates a new storage account that is associated with the resource group created in the previous step.
Next, the sample creates a new storage account that is associated with the resource group created in the previous step.

In this case, the storage account name is randomly generated to assure uniqueness. However, the call to create a new storage account will succeed if an account with the same name already exists in the subscription.

Expand Down
Loading

0 comments on commit 0d684ac

Please sign in to comment.