Skip to content

Commit dfba5c6

Browse files
authored
Merge pull request #286 from blueww/pageblobtier
Upgrade Azure.Storage to XSCL 8.4.0 and Support PremiumPageBlobTier
2 parents 776e214 + a2772c1 commit dfba5c6

26 files changed

+948
-90
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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 Microsoft.Azure.Commands.Common.Authentication.Abstractions;
16+
using Microsoft.WindowsAzure.Storage;
17+
using Microsoft.WindowsAzure.Storage.Auth;
18+
using Microsoft.WindowsAzure.Commands.Utilities.Common;
19+
using Microsoft.WindowsAzure.Commands.Common.Storage;
20+
using System.Linq;
21+
using System;
22+
using System.Text;
23+
24+
namespace Microsoft.WindowsAzure.Commands.Storage.Adapters
25+
{
26+
public static class AzureContextAdapterExtensions
27+
{
28+
/// <summary>
29+
/// Get the current storage account.
30+
/// </summary>
31+
/// <param name="context">The current Azure context.</param>
32+
/// <returns>The current storage account, or null, if no current storage account is set.</returns>
33+
public static CloudStorageAccount GetCurrentStorageAccount(this IAzureContext context, IStorageServiceProvider provider)
34+
{
35+
if (context != null)
36+
{
37+
var storageConnectionString = context.GetCurrentStorageAccountConnectionString();
38+
try
39+
{
40+
return
41+
CloudStorageAccount.Parse(storageConnectionString);
42+
}
43+
catch
44+
{
45+
var service = provider.GetStorageService(storageConnectionString, null);
46+
return service.GetCloudStorageAccount();
47+
}
48+
}
49+
50+
return null;
51+
}
52+
53+
/// <summary>
54+
/// Get a CloudStorageAccount client for the storage account represented by this context
55+
/// </summary>
56+
/// <param name="context">The storage context for the storage account</param>
57+
/// <returns>A CloudStorageAccount client for storage data plane tasks</returns>
58+
public static CloudStorageAccount GetCloudStorageAccount(this IStorageContext context)
59+
{
60+
CloudStorageAccount result = null;
61+
CloudStorageAccount.TryParse(context.ConnectionString, out result);
62+
return result;
63+
}
64+
65+
/// <summary>
66+
/// Get a CloudStorageAccount client for the storage account represented by this service
67+
/// </summary>
68+
/// <param name="service">The storage service</param>
69+
/// <returns>A CloudStorageAccount client for storage data plane tasks</returns>
70+
public static CloudStorageAccount GetCloudStorageAccount( this IStorageService service)
71+
{
72+
return new CloudStorageAccount(new StorageCredentials(service.Name, service.AuthenticationKeys.First()),
73+
new StorageUri(service.BlobEndpoint), new StorageUri(service.QueueEndpoint),
74+
new StorageUri(service.TableEndpoint), new StorageUri(service.FileEndpoint));
75+
}
76+
77+
/// <summary>
78+
/// Get a storage context client for the storage account represented by this service
79+
/// </summary>
80+
/// <param name="service">The storage service</param>
81+
/// <returns>A CloudStorageAccount client for storage data plane tasks</returns>
82+
public static IStorageContext GetStorageContext(this IStorageService service)
83+
{
84+
return new AzureStorageContext(new CloudStorageAccount(new StorageCredentials(service.Name, service.AuthenticationKeys.First()),
85+
new StorageUri(service.BlobEndpoint), new StorageUri(service.QueueEndpoint),
86+
new StorageUri(service.TableEndpoint), new StorageUri(service.FileEndpoint)));
87+
}
88+
89+
90+
/// <summary>
91+
/// Get a CloudStorageAccount client for the given storage service using the given storage service provider
92+
/// </summary>
93+
/// <param name="provider">The storage service provider to retrieve storage service details</param>
94+
/// <param name="accountName">The storage accoutn name</param>
95+
/// <returns>A CloudStorageAccount client for storage data plane tasks</returns>
96+
public static CloudStorageAccount GetCloudStorageAccount(this IStorageServiceProvider provider, string accountName, string resourceGroupName = null)
97+
{
98+
#if DEBUG
99+
if (TestMockSupport.RunningMocked)
100+
{
101+
return new CloudStorageAccount(new StorageCredentials(accountName,
102+
Convert.ToBase64String(Encoding.UTF8.GetBytes(Guid.NewGuid().ToString()))), true);
103+
}
104+
#endif
105+
return provider.GetStorageService(accountName, resourceGroupName).GetCloudStorageAccount();
106+
}
107+
108+
109+
110+
/// <summary>
111+
/// Get a CloudStorageAccount client for the current storage service using the given storage service provider
112+
/// </summary>
113+
/// <param name="context">The current Azure context.</param>
114+
/// <param name="provider">The storage service provider to retrieve storage service details</param>
115+
/// <returns>A CloudStorageAccount client for storage data plane tasks</returns>
116+
public static CloudStorageAccount GetCloudStorageAccount(this IAzureContext context, IStorageServiceProvider provider)
117+
{
118+
CloudStorageAccount account;
119+
var storageConnectionString = context.GetCurrentStorageAccountConnectionString();
120+
if(!CloudStorageAccount.TryParse(storageConnectionString, out account))
121+
{
122+
if (null == provider)
123+
{
124+
throw new ArgumentNullException("provider");
125+
}
126+
127+
account = provider.GetCloudStorageAccount(storageConnectionString);
128+
}
129+
130+
return account;
131+
}
132+
133+
}
134+
}
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
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+
namespace Microsoft.WindowsAzure.Commands.Common.Storage
16+
{
17+
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
18+
using Microsoft.WindowsAzure.Commands.Common.Storage.Properties;
19+
using Microsoft.WindowsAzure.Storage;
20+
using System;
21+
using System.Collections.Generic;
22+
23+
/// <summary>
24+
/// Storage context
25+
/// </summary>
26+
public class AzureStorageContext : IStorageContext
27+
{
28+
private static AzureStorageContext emptyContextInstance = new AzureStorageContext();
29+
30+
/// <summary>
31+
/// Storage account name used in this context
32+
/// </summary>
33+
public string StorageAccountName { get; protected set; }
34+
35+
/// <summary>
36+
/// Blob end point of the storage context
37+
/// </summary>
38+
public virtual string BlobEndPoint { get; protected set; }
39+
40+
/// <summary>
41+
/// Table end point of the storage context
42+
/// </summary>
43+
public virtual string TableEndPoint { get; protected set; }
44+
45+
/// <summary>
46+
/// Queue end point of the storage context
47+
/// </summary>
48+
public virtual string QueueEndPoint { get; protected set; }
49+
50+
/// <summary>
51+
/// File end point of the storage context
52+
/// </summary>
53+
public virtual string FileEndPoint { get; protected set; }
54+
55+
/// <summary>
56+
/// Self reference, it could enable New-AzureStorageContext can be used in pipeline
57+
/// </summary>
58+
public IStorageContext Context { get; protected set; }
59+
60+
/// <summary>
61+
/// Name place holder, and force pipeline to ignore this property
62+
/// </summary>
63+
public virtual string Name { get; protected set; }
64+
65+
/// <summary>
66+
/// Storage account in context
67+
/// </summary>
68+
public virtual CloudStorageAccount StorageAccount { get; protected set; }
69+
70+
/// <summary>
71+
/// Endpoint suffix (everything after "table.", "blob." or "queue.")
72+
/// </summary>
73+
/// <returns>
74+
/// This will return an empty string if the endpoints are not correctly set.
75+
/// </returns>
76+
public string EndPointSuffix
77+
{
78+
get
79+
{
80+
if (!string.IsNullOrEmpty(BlobEndPoint))
81+
{
82+
string blobSpliter = "blob.";
83+
if (BlobEndPoint.LastIndexOf(blobSpliter) >= 0)
84+
return BlobEndPoint.Substring(BlobEndPoint.LastIndexOf(blobSpliter) + blobSpliter.Length);
85+
}
86+
if (!string.IsNullOrEmpty(TableEndPoint))
87+
{
88+
string tableSpliter = "table.";
89+
if (TableEndPoint.LastIndexOf(tableSpliter) >= 0)
90+
return TableEndPoint.Substring(TableEndPoint.LastIndexOf(tableSpliter) + tableSpliter.Length);
91+
}
92+
if (!string.IsNullOrEmpty(QueueEndPoint))
93+
{
94+
string queueSpliter = "queue.";
95+
if (QueueEndPoint.LastIndexOf(queueSpliter) >= 0)
96+
return QueueEndPoint.Substring(QueueEndPoint.LastIndexOf(queueSpliter) + queueSpliter.Length);
97+
}
98+
if (!string.IsNullOrEmpty(FileEndPoint))
99+
{
100+
string fileSpliter = "file.";
101+
if (FileEndPoint.LastIndexOf(fileSpliter) >= 0)
102+
return FileEndPoint.Substring(FileEndPoint.LastIndexOf(fileSpliter) + fileSpliter.Length);
103+
}
104+
return string.Empty;
105+
}
106+
}
107+
108+
/// <summary>
109+
/// Get the connection string for this storage account
110+
/// </summary>
111+
public string ConnectionString {
112+
get
113+
{
114+
string returnValue = null;
115+
if (this.StorageAccount != null)
116+
{
117+
returnValue = this.StorageAccount.ToString(true);
118+
}
119+
120+
return returnValue;
121+
}
122+
}
123+
124+
/// <summary>
125+
/// Custom propeties for the storage context
126+
/// </summary>
127+
public IDictionary<string, string> ExtendedProperties { get; } = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
128+
129+
/// <summary>
130+
/// Create a storage context usign cloud storage account
131+
/// </summary>
132+
/// <param name="account">cloud storage account</param>
133+
public AzureStorageContext(CloudStorageAccount account)
134+
{
135+
StorageAccount = account;
136+
137+
if (account.BlobEndpoint != null)
138+
{
139+
BlobEndPoint = account.BlobEndpoint.ToString();
140+
}
141+
142+
if (account.TableEndpoint != null)
143+
{
144+
TableEndPoint = account.TableEndpoint.ToString();
145+
}
146+
147+
if (account.QueueEndpoint != null)
148+
{
149+
QueueEndPoint = account.QueueEndpoint.ToString();
150+
}
151+
152+
if (account.FileEndpoint != null)
153+
{
154+
FileEndPoint = account.FileEndpoint.ToString();
155+
}
156+
157+
StorageAccountName = account.Credentials.AccountName;
158+
Context = this;
159+
Name = String.Empty;
160+
161+
if (string.IsNullOrEmpty(StorageAccountName))
162+
{
163+
if (account.Credentials.IsSAS)
164+
{
165+
StorageAccountName = "[SasToken]";
166+
}
167+
else
168+
{
169+
StorageAccountName = "[Anonymous]";
170+
}
171+
}
172+
}
173+
174+
/// <summary>
175+
/// Proivides a private constructor for building empty instance which
176+
/// contains no account information.
177+
/// </summary>
178+
protected AzureStorageContext()
179+
{
180+
}
181+
182+
public static AzureStorageContext EmptyContextInstance
183+
{
184+
get
185+
{
186+
return emptyContextInstance;
187+
}
188+
}
189+
}
190+
}

0 commit comments

Comments
 (0)