Skip to content

RDBug 6218915:[PSH][SRP] implement the adapter interface for the new storage version and reuse the methods from commands.common.storage #2158

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// 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.Management.Automation;
using Microsoft.Azure.Management.Storage;
using Microsoft.WindowsAzure.Commands.Common.Storage;

namespace Microsoft.Azure.Commands.Management.Storage.Models
{
public class ARMStorageProvider : IStorageServiceProvider
{
IStorageManagementClient _client;

public ARMStorageProvider(IStorageManagementClient client)
{
_client = client;
}
public IStorageService GetStorageService(string name, string resourceGroupName)
{
var account = _client.StorageAccounts.GetProperties(resourceGroupName, name);
var keys = _client.StorageAccounts.ListKeys(resourceGroupName, name);
return new ARMStorageService(account, keys.Keys[0].Value,
keys.Keys[1].Value);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// 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.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.WindowsAzure.Commands.Common.Storage;

namespace Microsoft.Azure.Commands.Management.Storage.Models
{
public class ARMStorageService : IStorageService
{
Azure.Management.Storage.Models.StorageAccount _account;
List<string> _authenticationKeys = new List<string>();
public ARMStorageService(Azure.Management.Storage.Models.StorageAccount account,
params string[] authenticationKeys)
{
_account = account;
foreach (var key in authenticationKeys)
{
_authenticationKeys.Add(key);
}
}

public Uri BlobEndpoint
{
get { return GetUri(_account.PrimaryEndpoints.Blob); }
}

public Uri FileEndpoint
{
get { return GetUri(_account.PrimaryEndpoints.File); }
}

public Uri QueueEndpoint
{
get { return GetUri(_account.PrimaryEndpoints.Queue); }
}

public Uri TableEndpoint
{
get { return GetUri(_account.PrimaryEndpoints.Table); }
}

public string Name
{
get { return _account.Name; }
}

public List<string> AuthenticationKeys
{
get { return _authenticationKeys; }
}

/// <summary>
/// Get the resource group name from a storage account resource Id
/// </summary>
/// <param name="resourceId">The resource Id for the storage account</param>
/// <returns>The resource group containing the storage account</returns>
public static string ParseResourceGroupFromId(string resourceId)
{
if (!string.IsNullOrEmpty(resourceId))
{
string[] tokens = resourceId.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
if (tokens == null || tokens.Length < 4)
{
throw new ArgumentOutOfRangeException("resourceId");
}
return tokens[3];
}
return null;
}

public static Uri GetUri(string uriString)
{
return uriString == null ? null : new Uri(uriString);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@
<Reference Include="System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\..\..\Common\Commands.Common.Storage\Adapters\ARM.Storage.5\ARMStorageProvider.cs">
<Link>Common\ARMStorageProvider.cs</Link>
</Compile>
<Compile Include="..\..\..\Common\Commands.Common.Storage\Adapters\ARM.Storage.5\ARMStorageService.cs">
<Link>Common\ARMStorageService.cs</Link>
</Compile>
<Compile Include="Models\PSStorageAccount.cs" />
<Compile Include="Models\PSUsage.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,16 @@ public PSStorageAccount(StorageModels.StorageAccount storageAccount)
public static PSStorageAccount Create(StorageModels.StorageAccount storageAccount, IStorageManagementClient client)
{
var result = new PSStorageAccount(storageAccount);
var credentials = StorageAccountBaseCmdlet.GenerateStorageCredentials(client, result.ResourceGroupName, result.StorageAccountName);
var credentials = StorageUtilities.GenerateStorageCredentials(new ARMStorageProvider(client), result.ResourceGroupName, result.StorageAccountName);
CloudStorageAccount account = new CloudStorageAccount(credentials,
StorageAccountBaseCmdlet.GetUri(storageAccount.PrimaryEndpoints.Blob),
StorageAccountBaseCmdlet.GetUri(storageAccount.PrimaryEndpoints.Queue),
StorageAccountBaseCmdlet.GetUri(storageAccount.PrimaryEndpoints.Table),
StorageAccountBaseCmdlet.GetUri(storageAccount.PrimaryEndpoints.File));
ARMStorageService.GetUri(storageAccount.PrimaryEndpoints.Blob),
ARMStorageService.GetUri(storageAccount.PrimaryEndpoints.Queue),
ARMStorageService.GetUri(storageAccount.PrimaryEndpoints.Table),
ARMStorageService.GetUri(storageAccount.PrimaryEndpoints.File));
result.Context = new AzureStorageContext(account);
return result;
}



private static string ParseResourceGroupFromId(string idFromServer)
{
if (!string.IsNullOrEmpty(idFromServer))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using Microsoft.WindowsAzure.Commands.Common.Storage;
using Microsoft.WindowsAzure.Commands.Utilities.Common;
using Microsoft.WindowsAzure.Storage;
using Microsoft.Azure.Commands.Management.Storage.Models;

namespace Microsoft.Azure.Commands.Management.Storage
{
Expand Down Expand Up @@ -52,7 +53,7 @@ public override void ExecuteCmdlet()
}
else
{
account = GenerateCloudStorageAccount(StorageClient, ResourceGroupName, Name);
account = StorageUtilities.GenerateCloudStorageAccount(new ARMStorageProvider(StorageClient), ResourceGroupName, Name);
}

// Clear the current storage account for both SM and RM
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,34 +150,6 @@ protected static Encryption ParseEncryption(EncryptionSupportServiceEnum? Enable
return accountEncryption;
}

public static StorageCredentials GenerateStorageCredentials(IStorageManagementClient storageClient, string resourceGroupName, string accountName)
{
var storageKeysResponse = storageClient.StorageAccounts.ListKeys(resourceGroupName, accountName);
return new StorageCredentials(accountName, storageKeysResponse.Keys[0].Value);
}

protected static CloudStorageAccount GenerateCloudStorageAccount(IStorageManagementClient storageClient, string resourceGroupName, string accountName)
{

var storageServiceResponse = storageClient.StorageAccounts.GetPropertiesWithHttpMessagesAsync(resourceGroupName, accountName).Result;
Uri blobEndpoint = GetUri(storageServiceResponse.Body.PrimaryEndpoints.Blob);
Uri queueEndpoint = GetUri(storageServiceResponse.Body.PrimaryEndpoints.Queue);
Uri tableEndpoint = GetUri(storageServiceResponse.Body.PrimaryEndpoints.Table);
Uri fileEndpoint = GetUri(storageServiceResponse.Body.PrimaryEndpoints.File);

return new CloudStorageAccount(
GenerateStorageCredentials(storageClient, resourceGroupName, accountName),
blobEndpoint,
queueEndpoint,
tableEndpoint,
fileEndpoint);
}

public static Uri GetUri(string uriString)
{
return uriString == null ? null : new Uri(uriString);
}

protected void WriteStorageAccount(StorageModels.StorageAccount storageAccount)
{
WriteObject(PSStorageAccount.Create(storageAccount, this.StorageClient));
Expand Down