Skip to content

Commit 9962c1c

Browse files
committed
Refactoring commands.common.storage to remove storage management dependency
1 parent 8abf31d commit 9962c1c

File tree

13 files changed

+294
-41
lines changed

13 files changed

+294
-41
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.StorageAccount, keys.StorageAccountKeys.Key1,
34+
keys.StorageAccountKeys.Key2);
35+
}
36+
}
37+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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 _account.PrimaryEndpoints.Blob; }
41+
}
42+
43+
public Uri FileEndpoint
44+
{
45+
get { return _account.PrimaryEndpoints.File; }
46+
}
47+
48+
public Uri QueueEndpoint
49+
{
50+
get { return _account.PrimaryEndpoints.Queue; }
51+
}
52+
53+
public Uri TableEndpoint
54+
{
55+
get { return _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+
public static string ParseResourceGroupFromId(string idFromServer)
69+
{
70+
if (!string.IsNullOrEmpty(idFromServer))
71+
{
72+
string[] tokens = idFromServer.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
73+
74+
return tokens[3];
75+
}
76+
77+
return null;
78+
}
79+
80+
}
81+
}

src/Common/Commands.Common.Storage/AzureContextExtensions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
using Microsoft.Azure.Commands.Common.Authentication.Models;
1616
using Microsoft.WindowsAzure.Commands.Common.Storage;
17-
using ArmStorage = Microsoft.Azure.Management.Storage;
1817
using SmStorage = Microsoft.WindowsAzure.Management.Storage;
1918
using Microsoft.WindowsAzure.Storage;
2019

src/Common/Commands.Common.Storage/Commands.Common.Storage.csproj

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,6 @@
6363
<HintPath>..\..\packages\Microsoft.Azure.KeyVault.Core.1.0.0\lib\net40\Microsoft.Azure.KeyVault.Core.dll</HintPath>
6464
<Private>True</Private>
6565
</Reference>
66-
<Reference Include="Microsoft.Azure.Management.Storage, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
67-
<SpecificVersion>False</SpecificVersion>
68-
<HintPath>..\..\packages\Microsoft.Azure.Management.Storage.3.0.0\lib\net40\Microsoft.Azure.Management.Storage.dll</HintPath>
69-
</Reference>
7066
<Reference Include="Microsoft.Azure.ResourceManager, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
7167
<SpecificVersion>False</SpecificVersion>
7268
<HintPath>..\..\packages\Microsoft.Azure.Management.Resources.2.19.0-preview\lib\net40\Microsoft.Azure.ResourceManager.dll</HintPath>
@@ -162,6 +158,8 @@
162158
<Compile Include="BlobUploadParameters.cs" />
163159
<Compile Include="IStorageClientWrapper.cs" />
164160
<Compile Include="IStorageContextProvider.cs" />
161+
<Compile Include="IStorageService.cs" />
162+
<Compile Include="IStorageServiceProvider.cs" />
165163
<Compile Include="Properties\Resources.Designer.cs">
166164
<AutoGen>True</AutoGen>
167165
<DesignTime>True</DesignTime>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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.Storage.Auth;
21+
22+
namespace Microsoft.WindowsAzure.Commands.Common.Storage
23+
{
24+
public interface IStorageService
25+
{
26+
/// <summary>
27+
/// The eblob service endpoint
28+
/// </summary>
29+
Uri BlobEndpoint { get; }
30+
31+
/// <summary>
32+
/// The file service endpoint
33+
/// </summary>
34+
Uri FileEndpoint { get; }
35+
36+
/// <summary>
37+
/// The queue service endpoint
38+
/// </summary>
39+
Uri QueueEndpoint { get; }
40+
41+
/// <summary>
42+
/// The table service endpoint
43+
/// </summary>
44+
Uri TableEndpoint { get; }
45+
46+
/// <summary>
47+
/// The storage account name
48+
/// </summary>
49+
string Name { get;}
50+
51+
/// <summary>
52+
/// Authentication keys for the storage account
53+
/// </summary>
54+
List<string> AuthenticationKeys { get; }
55+
}
56+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
21+
namespace Microsoft.WindowsAzure.Commands.Common.Storage
22+
{
23+
public interface IStorageServiceProvider
24+
{
25+
IStorageService GetStorageService(string name, string resourceGroupName);
26+
}
27+
}

src/Common/Commands.Common.Storage/StorageUtilities.cs

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
1-

2-
using System.CodeDom;
3-
using System.Diagnostics.Eventing.Reader;
4-
using System.Text;
5-
using Microsoft.Azure.Management.Storage;
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+
// ----------------------------------------------------------------------------------
614

715
namespace Microsoft.WindowsAzure.Commands.Common.Storage
816
{
917
using System;
18+
using System.Linq;
19+
using System.Text;
1020
using Microsoft.WindowsAzure.Commands.Utilities.Common;
1121
using Microsoft.WindowsAzure.Management.Storage;
1222
using Microsoft.WindowsAzure.Storage;
1323
using Microsoft.WindowsAzure.Storage.Auth;
1424
using Microsoft.WindowsAzure.Storage.Blob;
1525
using Microsoft.WindowsAzure.Storage.Table;
16-
using Arm = Microsoft.Azure.Management.Storage;
1726

1827
public class StorageUtilities
1928
{
@@ -35,27 +44,24 @@ public static Uri CreateHttpsEndpoint(string endpointUri)
3544
/// <summary>
3645
/// Create a cloud storage account using an ARM storage management client
3746
/// </summary>
38-
/// <param name="storageClient">The client to use to get storage account details.</param>
39-
/// <param name="resourceGroupName">The resource group contining the storage account.</param>
47+
/// <param name="provider">The adapter to ARM storage services.</param>
48+
/// <param name="resourceGroupName">The resource group containing the storage account.</param>
4049
/// <param name="accountName">The name of the storage account.</param>
4150
/// <returns>A CloudStorageAccount that can be used by windows azure storage library to manipulate objects in the storage account.</returns>
42-
public static CloudStorageAccount GenerateCloudStorageAccount(Arm.IStorageManagementClient storageClient,
51+
public static CloudStorageAccount GenerateCloudStorageAccount(IStorageServiceProvider provider,
4352
string resourceGroupName, string accountName)
4453
{
4554
if (!TestMockSupport.RunningMocked)
4655
{
47-
var storageServiceResponse = storageClient.StorageAccounts.GetProperties(resourceGroupName, accountName);
48-
Uri blobEndpoint = storageServiceResponse.StorageAccount.PrimaryEndpoints.Blob;
49-
Uri queueEndpoint = storageServiceResponse.StorageAccount.PrimaryEndpoints.Queue;
50-
Uri tableEndpoint = storageServiceResponse.StorageAccount.PrimaryEndpoints.Table;
51-
Uri fileEndpoint = storageServiceResponse.StorageAccount.PrimaryEndpoints.File;
56+
var service = provider.GetStorageService(accountName, resourceGroupName);
57+
5258

5359
return new CloudStorageAccount(
54-
GenerateStorageCredentials(storageClient, resourceGroupName, accountName),
55-
blobEndpoint,
56-
queueEndpoint,
57-
tableEndpoint,
58-
fileEndpoint);
60+
new StorageCredentials(service.Name, service.AuthenticationKeys.First()),
61+
service.BlobEndpoint,
62+
service.QueueEndpoint,
63+
service.TableEndpoint,
64+
service.FileEndpoint);
5965
}
6066
else
6167
{
@@ -64,7 +70,7 @@ public static CloudStorageAccount GenerateCloudStorageAccount(Arm.IStorageManage
6470
Convert.ToBase64String(Encoding.UTF8.GetBytes(Guid.NewGuid().ToString()))),
6571
new Uri(string.Format("https://{0}.blob.core.windows.net", accountName)),
6672
new Uri(string.Format("https://{0}.queue.core.windows.net", accountName)),
67-
new Uri(string.Format("https://{0}.table.core.windows.net", accountName)),
73+
new Uri(string.Format("https://{0}.table.core.windows.net", accountName)),
6874
new Uri(string.Format("https://{0}.file.core.windows.net", accountName)));
6975
}
7076
}
@@ -127,23 +133,22 @@ public static CloudStorageAccount GenerateCloudStorageAccount(IStorageManagement
127133
new Uri(string.Format("https://{0}.table.core.windows.net", accountName)),
128134
new Uri(string.Format("https://{0}.file.core.windows.net", accountName)));
129135
}
130-
}
136+
}
131137

132138
/// <summary>
133139
/// Create storage credentials for the given account
134140
/// </summary>
135-
/// <param name="storageClient">The ARM storage management client.</param>
141+
/// <param name="provider">The storage provider for ARM storage services.</param>
136142
/// <param name="resourceGroupName">The resource group containing the storage account.</param>
137143
/// <param name="accountName">The storage account name.</param>
138144
/// <returns>Storage credentials for the given account.</returns>
139-
public static StorageCredentials GenerateStorageCredentials(Arm.IStorageManagementClient storageClient,
145+
public static StorageCredentials GenerateStorageCredentials(IStorageServiceProvider provider,
140146
string resourceGroupName, string accountName)
141147
{
142148
if (!TestMockSupport.RunningMocked)
143149
{
144-
var storageKeysResponse = storageClient.StorageAccounts.ListKeys(resourceGroupName, accountName);
145-
return new StorageCredentials(accountName,
146-
storageKeysResponse.StorageAccountKeys.Key1);
150+
var service = provider.GetStorageService(accountName, resourceGroupName);
151+
return new StorageCredentials(accountName, service.AuthenticationKeys.First());
147152
}
148153
else
149154
{
@@ -158,8 +163,8 @@ public static StorageCredentials GenerateStorageCredentials(Arm.IStorageManageme
158163
/// <param name="storageClient">The RDFE storage management client.</param>
159164
/// <param name="accountName">The storage account name.</param>
160165
/// <returns>Storage credentials for the given account.</returns>
161-
public static StorageCredentials GenerateStorageCredentials(IStorageManagementClient storageClient,
162-
string accountName)
166+
public static StorageCredentials GenerateStorageCredentials(IStorageManagementClient storageClient,
167+
string accountName)
163168
{
164169
if (!TestMockSupport.RunningMocked)
165170
{

src/ResourceManager/Compute/Commands.Compute/Commands.Compute.csproj

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,12 @@
168168
<Reference Include="System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
169169
</ItemGroup>
170170
<ItemGroup>
171+
<Compile Include="..\..\..\Common\Commands.Common.Storage\Adapters\ARM.Storage.3\ARMStorageProvider.cs">
172+
<Link>Common\ARMStorageProvider.cs</Link>
173+
</Compile>
174+
<Compile Include="..\..\..\Common\Commands.Common.Storage\Adapters\ARM.Storage.3\ARMStorageService.cs">
175+
<Link>Common\ARMStorageService.cs</Link>
176+
</Compile>
171177
<Compile Include="..\..\..\ServiceManagement\Compute\Commands.ServiceManagement\IaaS\Extensions\DSC\DscExtensionCmdletConstants.cs">
172178
<Link>Extension\DSC\DscExtensionCmdletConstants.cs</Link>
173179
</Compile>
@@ -196,6 +202,7 @@
196202
<Compile Include="Common\ComputeClientInstancViewMethod.cs" />
197203
<Compile Include="Common\ComputeCloudException.cs" />
198204
<Compile Include="Common\DiagnosticsHelper.cs" />
205+
<Compile Include="Common\StorageManagementClient.cs" />
199206
<Compile Include="ExtensionImages\GetAzureVMExtensionImageTypeCommand.cs" />
200207
<Compile Include="ExtensionImages\GetAzureVMExtensionImageCommand.cs" />
201208
<Compile Include="ExtensionImages\VirtualMachineExtensionImageBaseCmdlet.cs" />
@@ -370,10 +377,6 @@
370377
<Project>{e1f5201d-6067-430e-b303-4e367652991b}</Project>
371378
<Name>Commands.Resources</Name>
372379
</ProjectReference>
373-
<ProjectReference Include="..\..\Storage\Commands.Management.Storage\Commands.Management.Storage.csproj">
374-
<Project>{a50ab133-5c04-4a17-9054-f8343683ec23}</Project>
375-
<Name>Commands.Management.Storage</Name>
376-
</ProjectReference>
377380
<ProjectReference Include="..\..\Tags\Commands.Tags\Commands.Tags.csproj">
378381
<Project>{2493a8f7-1949-4f29-8d53-9d459046c3b8}</Project>
379382
<Name>Commands.Tags</Name>

src/ResourceManager/Compute/Commands.Compute/Common/DiagnosticsHelper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,8 @@ public static string InitializeStorageAccountKey(IStorageManagementClient storag
318318
if (TryGetStorageAccount(storageClient, storageAccountName, out storageAccount))
319319
{
320320
// Help user retrieve the storage account key
321-
var psStorageAccount = new PSStorageAccount(storageAccount);
322-
var credentials = StorageUtilities.GenerateStorageCredentials(storageClient, psStorageAccount.ResourceGroupName, psStorageAccount.StorageAccountName);
321+
var credentials = StorageUtilities.GenerateStorageCredentials(new ARMStorageProvider(storageClient),
322+
ARMStorageService.ParseResourceGroupFromId(storageAccount.Id), storageAccount.Name);
323323
storageAccountKey = credentials.ExportBase64EncodedKey();
324324
}
325325
else

0 commit comments

Comments
 (0)