Skip to content

Commit 11d9da0

Browse files
committed
Merge pull request Azure#2158 from wastoresh/dev
RDBug 6218915:[PSH][SRP] implement the adapter interface for the new storage version and reuse the methods from commands.common.storage
2 parents 7edd186 + f564847 commit 11d9da0

File tree

6 files changed

+142
-36
lines changed

6 files changed

+142
-36
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System.Management.Automation;
16+
using Microsoft.Azure.Management.Storage;
17+
using Microsoft.WindowsAzure.Commands.Common.Storage;
18+
19+
namespace Microsoft.Azure.Commands.Management.Storage.Models
20+
{
21+
public class ARMStorageProvider : IStorageServiceProvider
22+
{
23+
IStorageManagementClient _client;
24+
25+
public ARMStorageProvider(IStorageManagementClient client)
26+
{
27+
_client = client;
28+
}
29+
public IStorageService GetStorageService(string name, string resourceGroupName)
30+
{
31+
var account = _client.StorageAccounts.GetProperties(resourceGroupName, name);
32+
var keys = _client.StorageAccounts.ListKeys(resourceGroupName, name);
33+
return new ARMStorageService(account, keys.Keys[0].Value,
34+
keys.Keys[1].Value);
35+
}
36+
}
37+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System;
16+
using System.Collections.Generic;
17+
using System.Linq;
18+
using System.Text;
19+
using System.Threading.Tasks;
20+
using Microsoft.WindowsAzure.Commands.Common.Storage;
21+
22+
namespace Microsoft.Azure.Commands.Management.Storage.Models
23+
{
24+
public class ARMStorageService : IStorageService
25+
{
26+
Azure.Management.Storage.Models.StorageAccount _account;
27+
List<string> _authenticationKeys = new List<string>();
28+
public ARMStorageService(Azure.Management.Storage.Models.StorageAccount account,
29+
params string[] authenticationKeys)
30+
{
31+
_account = account;
32+
foreach (var key in authenticationKeys)
33+
{
34+
_authenticationKeys.Add(key);
35+
}
36+
}
37+
38+
public Uri BlobEndpoint
39+
{
40+
get { return GetUri(_account.PrimaryEndpoints.Blob); }
41+
}
42+
43+
public Uri FileEndpoint
44+
{
45+
get { return GetUri(_account.PrimaryEndpoints.File); }
46+
}
47+
48+
public Uri QueueEndpoint
49+
{
50+
get { return GetUri(_account.PrimaryEndpoints.Queue); }
51+
}
52+
53+
public Uri TableEndpoint
54+
{
55+
get { return GetUri(_account.PrimaryEndpoints.Table); }
56+
}
57+
58+
public string Name
59+
{
60+
get { return _account.Name; }
61+
}
62+
63+
public List<string> AuthenticationKeys
64+
{
65+
get { return _authenticationKeys; }
66+
}
67+
68+
/// <summary>
69+
/// Get the resource group name from a storage account resource Id
70+
/// </summary>
71+
/// <param name="resourceId">The resource Id for the storage account</param>
72+
/// <returns>The resource group containing the storage account</returns>
73+
public static string ParseResourceGroupFromId(string resourceId)
74+
{
75+
if (!string.IsNullOrEmpty(resourceId))
76+
{
77+
string[] tokens = resourceId.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
78+
if (tokens == null || tokens.Length < 4)
79+
{
80+
throw new ArgumentOutOfRangeException("resourceId");
81+
}
82+
return tokens[3];
83+
}
84+
return null;
85+
}
86+
87+
public static Uri GetUri(string uriString)
88+
{
89+
return uriString == null ? null : new Uri(uriString);
90+
}
91+
}
92+
}

src/ResourceManager/Storage/Commands.Management.Storage/Commands.Management.Storage.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,12 @@
136136
<Reference Include="System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
137137
</ItemGroup>
138138
<ItemGroup>
139+
<Compile Include="..\..\..\Common\Commands.Common.Storage\Adapters\ARM.Storage.5\ARMStorageProvider.cs">
140+
<Link>Common\ARMStorageProvider.cs</Link>
141+
</Compile>
142+
<Compile Include="..\..\..\Common\Commands.Common.Storage\Adapters\ARM.Storage.5\ARMStorageService.cs">
143+
<Link>Common\ARMStorageService.cs</Link>
144+
</Compile>
139145
<Compile Include="Models\PSStorageAccount.cs" />
140146
<Compile Include="Models\PSUsage.cs" />
141147
<Compile Include="Properties\AssemblyInfo.cs" />

src/ResourceManager/Storage/Commands.Management.Storage/Models/PSStorageAccount.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,18 +85,16 @@ public PSStorageAccount(StorageModels.StorageAccount storageAccount)
8585
public static PSStorageAccount Create(StorageModels.StorageAccount storageAccount, IStorageManagementClient client)
8686
{
8787
var result = new PSStorageAccount(storageAccount);
88-
var credentials = StorageAccountBaseCmdlet.GenerateStorageCredentials(client, result.ResourceGroupName, result.StorageAccountName);
88+
var credentials = StorageUtilities.GenerateStorageCredentials(new ARMStorageProvider(client), result.ResourceGroupName, result.StorageAccountName);
8989
CloudStorageAccount account = new CloudStorageAccount(credentials,
90-
StorageAccountBaseCmdlet.GetUri(storageAccount.PrimaryEndpoints.Blob),
91-
StorageAccountBaseCmdlet.GetUri(storageAccount.PrimaryEndpoints.Queue),
92-
StorageAccountBaseCmdlet.GetUri(storageAccount.PrimaryEndpoints.Table),
93-
StorageAccountBaseCmdlet.GetUri(storageAccount.PrimaryEndpoints.File));
90+
ARMStorageService.GetUri(storageAccount.PrimaryEndpoints.Blob),
91+
ARMStorageService.GetUri(storageAccount.PrimaryEndpoints.Queue),
92+
ARMStorageService.GetUri(storageAccount.PrimaryEndpoints.Table),
93+
ARMStorageService.GetUri(storageAccount.PrimaryEndpoints.File));
9494
result.Context = new AzureStorageContext(account);
9595
return result;
9696
}
9797

98-
99-
10098
private static string ParseResourceGroupFromId(string idFromServer)
10199
{
102100
if (!string.IsNullOrEmpty(idFromServer))

src/ResourceManager/Storage/Commands.Management.Storage/StorageAccount/SetAzureRmCurrentStorageAccount.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using Microsoft.WindowsAzure.Commands.Common.Storage;
1818
using Microsoft.WindowsAzure.Commands.Utilities.Common;
1919
using Microsoft.WindowsAzure.Storage;
20+
using Microsoft.Azure.Commands.Management.Storage.Models;
2021

2122
namespace Microsoft.Azure.Commands.Management.Storage
2223
{
@@ -52,7 +53,7 @@ public override void ExecuteCmdlet()
5253
}
5354
else
5455
{
55-
account = GenerateCloudStorageAccount(StorageClient, ResourceGroupName, Name);
56+
account = StorageUtilities.GenerateCloudStorageAccount(new ARMStorageProvider(StorageClient), ResourceGroupName, Name);
5657
}
5758

5859
// Clear the current storage account for both SM and RM

src/ResourceManager/Storage/Commands.Management.Storage/StorageAccount/StorageAccountBaseCmdlet.cs

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -150,34 +150,6 @@ protected static Encryption ParseEncryption(EncryptionSupportServiceEnum? Enable
150150
return accountEncryption;
151151
}
152152

153-
public static StorageCredentials GenerateStorageCredentials(IStorageManagementClient storageClient, string resourceGroupName, string accountName)
154-
{
155-
var storageKeysResponse = storageClient.StorageAccounts.ListKeys(resourceGroupName, accountName);
156-
return new StorageCredentials(accountName, storageKeysResponse.Keys[0].Value);
157-
}
158-
159-
protected static CloudStorageAccount GenerateCloudStorageAccount(IStorageManagementClient storageClient, string resourceGroupName, string accountName)
160-
{
161-
162-
var storageServiceResponse = storageClient.StorageAccounts.GetPropertiesWithHttpMessagesAsync(resourceGroupName, accountName).Result;
163-
Uri blobEndpoint = GetUri(storageServiceResponse.Body.PrimaryEndpoints.Blob);
164-
Uri queueEndpoint = GetUri(storageServiceResponse.Body.PrimaryEndpoints.Queue);
165-
Uri tableEndpoint = GetUri(storageServiceResponse.Body.PrimaryEndpoints.Table);
166-
Uri fileEndpoint = GetUri(storageServiceResponse.Body.PrimaryEndpoints.File);
167-
168-
return new CloudStorageAccount(
169-
GenerateStorageCredentials(storageClient, resourceGroupName, accountName),
170-
blobEndpoint,
171-
queueEndpoint,
172-
tableEndpoint,
173-
fileEndpoint);
174-
}
175-
176-
public static Uri GetUri(string uriString)
177-
{
178-
return uriString == null ? null : new Uri(uriString);
179-
}
180-
181153
protected void WriteStorageAccount(StorageModels.StorageAccount storageAccount)
182154
{
183155
WriteObject(PSStorageAccount.Create(storageAccount, this.StorageClient));

0 commit comments

Comments
 (0)