Skip to content

Commit 2f68a6b

Browse files
authored
Merge pull request #5219 from wastoresh/defaultserviceversion
[Azure.Storage] Support defaultserviceversion
2 parents 6812dc0 + ee0abd3 commit 2f68a6b

15 files changed

+823
-43
lines changed

src/Storage/Azure.Storage.psd1

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@ CmdletsToExport = 'Get-AzureStorageTable', 'New-AzureStorageTableSASToken',
115115
'Set-AzureStorageContainerStoredAccessPolicy',
116116
'Start-AzureStorageBlobCopy',
117117
'Start-AzureStorageBlobIncrementalCopy',
118-
'Stop-AzureStorageBlobCopy'
118+
'Stop-AzureStorageBlobCopy',
119+
'Update-AzureStorageServiceProperty',
120+
'Get-AzureStorageServiceProperty'
119121

120122
# Variables to export from this module
121123
# VariablesToExport = @()

src/Storage/ChangeLog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
- Additional information about change #1
1919
-->
2020
## Current Release
21+
* Add cmdlets to get and set Storage service properties
22+
- Get-AzureStorageServiceProperty
23+
- Update-AzureStorageServiceProperty
2124

2225
## Version 4.0.2
2326
* Upgrade to Azure Storage Client Library 8.6.0 and Azure Storage DataMovement Library 0.6.5

src/Storage/Commands.Storage.Test/Commands.Storage.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@
211211
<Compile Include="Common\ResourceNotFoundExceptionTest.cs" />
212212
<Compile Include="Common\AccessPolicyHelperTest.cs" />
213213
<Compile Include="Common\SasTokenHelperTest.cs" />
214+
<Compile Include="Common\PSServicePropertiesTest.cs" />
214215
<Compile Include="Common\StorageCloudCmdletBaseTest.cs" />
215216
<Compile Include="Common\StorageExceptionUtilTest.cs" />
216217
<Compile Include="File\Cmdlet\GetAzureStorageFileContentTest.cs" />
Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright 2012 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 Microsoft.VisualStudio.TestTools.UnitTesting;
18+
using Microsoft.WindowsAzure.Commands.Storage.Common;
19+
using Microsoft.WindowsAzure.Commands.Storage.Model.ResourceModel;
20+
using Microsoft.WindowsAzure.Storage.Blob;
21+
using Microsoft.WindowsAzure.Storage.Table;
22+
using Microsoft.WindowsAzure.Storage.File;
23+
using Microsoft.WindowsAzure.Storage.Queue;
24+
using Microsoft.WindowsAzure.Storage.Shared.Protocol;
25+
26+
namespace Microsoft.WindowsAzure.Commands.Storage.Test.Common
27+
{
28+
/// <summary>
29+
/// Unit test for Access Policy helper
30+
/// </summary>
31+
[TestClass]
32+
public class PSServicePropertiesTest : StorageTestBase
33+
{
34+
/// <summary>
35+
/// unit test for ParseCorsRules() functional
36+
/// </summary>
37+
[TestMethod]
38+
public void ValidateParseCorsRulesTest()
39+
{
40+
CorsProperties coresproperties = PrepareCoresproperties();
41+
PSCorsRule[] pscores = PSCorsRule.ParseCorsRules(coresproperties);
42+
CompareCors(pscores, coresproperties);
43+
44+
coresproperties = new CorsProperties();
45+
pscores = PSCorsRule.ParseCorsRules(coresproperties);
46+
CompareCors(pscores, coresproperties);
47+
48+
coresproperties = null;
49+
pscores = PSCorsRule.ParseCorsRules(coresproperties);
50+
CompareCors(pscores, coresproperties);
51+
}
52+
53+
/// <summary>
54+
/// unit test for new PSSeriviceProperties() functional
55+
/// </summary>
56+
[TestMethod]
57+
public void ValidateParseServicePropertiesTest()
58+
{
59+
ServiceProperties serviceProperties = new ServiceProperties();
60+
serviceProperties.Cors = PrepareCoresproperties();
61+
serviceProperties.HourMetrics = new MetricsProperties("1.0");
62+
serviceProperties.HourMetrics.MetricsLevel = MetricsLevel.ServiceAndApi;
63+
serviceProperties.HourMetrics.RetentionDays = 1;
64+
serviceProperties.MinuteMetrics = new MetricsProperties("1.0");
65+
serviceProperties.MinuteMetrics.MetricsLevel = MetricsLevel.Service;
66+
serviceProperties.MinuteMetrics.RetentionDays = 3;
67+
serviceProperties.Logging = new LoggingProperties("1.0");
68+
serviceProperties.Logging.LoggingOperations = LoggingOperations.All;
69+
serviceProperties.Logging.RetentionDays = 5;
70+
serviceProperties.DefaultServiceVersion = "2017-04-17";
71+
PSSeriviceProperties pSSeriviceProperties = new PSSeriviceProperties(serviceProperties);
72+
CompareServiceProperties(pSSeriviceProperties, serviceProperties);
73+
74+
serviceProperties = new ServiceProperties();
75+
pSSeriviceProperties = new PSSeriviceProperties(serviceProperties);
76+
CompareServiceProperties(pSSeriviceProperties, serviceProperties);
77+
78+
serviceProperties = null;
79+
pSSeriviceProperties = new PSSeriviceProperties(serviceProperties);
80+
CompareServiceProperties(pSSeriviceProperties, serviceProperties);
81+
}
82+
83+
/// <summary>
84+
/// Comapare PSServiceProperties and ServiceProperties, to make sure they are same content
85+
/// </summary>
86+
static private void CompareServiceProperties(PSSeriviceProperties pSSeriviceProperties, ServiceProperties serviceProperties)
87+
{
88+
if ((pSSeriviceProperties != null && pSSeriviceProperties.HourMetrics != null) || (serviceProperties != null && serviceProperties.HourMetrics != null))
89+
{
90+
Assert.AreEqual(serviceProperties.HourMetrics.Version, pSSeriviceProperties.HourMetrics.Version);
91+
Assert.AreEqual(serviceProperties.HourMetrics.MetricsLevel, pSSeriviceProperties.HourMetrics.MetricsLevel);
92+
Assert.AreEqual(serviceProperties.HourMetrics.RetentionDays, pSSeriviceProperties.HourMetrics.RetentionDays);
93+
}
94+
if ((pSSeriviceProperties != null && pSSeriviceProperties.MinuteMetrics != null) || (serviceProperties != null && serviceProperties.MinuteMetrics != null))
95+
{
96+
Assert.AreEqual(serviceProperties.MinuteMetrics.Version, pSSeriviceProperties.MinuteMetrics.Version);
97+
Assert.AreEqual(serviceProperties.MinuteMetrics.MetricsLevel, pSSeriviceProperties.MinuteMetrics.MetricsLevel);
98+
Assert.AreEqual(serviceProperties.MinuteMetrics.RetentionDays, pSSeriviceProperties.MinuteMetrics.RetentionDays);
99+
}
100+
if ((pSSeriviceProperties != null && pSSeriviceProperties.Logging != null) || (serviceProperties != null && serviceProperties.Logging != null))
101+
{
102+
Assert.AreEqual(serviceProperties.Logging.Version, pSSeriviceProperties.Logging.Version);
103+
Assert.AreEqual(serviceProperties.Logging.LoggingOperations, pSSeriviceProperties.Logging.LoggingOperations);
104+
Assert.AreEqual(serviceProperties.Logging.RetentionDays, pSSeriviceProperties.Logging.RetentionDays);
105+
}
106+
if ((pSSeriviceProperties != null && pSSeriviceProperties.Cors != null) || (serviceProperties != null && serviceProperties.Cors != null))
107+
{
108+
CompareCors(pSSeriviceProperties.Cors, serviceProperties.Cors);
109+
}
110+
if ((pSSeriviceProperties != null && pSSeriviceProperties.DefaultServiceVersion != null) || (serviceProperties != null && serviceProperties.DefaultServiceVersion != null))
111+
{
112+
Assert.AreEqual(serviceProperties.DefaultServiceVersion, pSSeriviceProperties.DefaultServiceVersion);
113+
}
114+
}
115+
116+
/// <summary>
117+
/// Comapare PSCorsRule and CorsProperties, to make sure they are same content
118+
/// </summary>
119+
static private void CompareCors(PSCorsRule[] psCorsRules, CorsProperties corsProperties)
120+
{
121+
if ((psCorsRules == null || psCorsRules.Length == 0)
122+
&& (corsProperties == null || corsProperties.CorsRules == null || corsProperties.CorsRules.Count == 0))
123+
{
124+
return;
125+
}
126+
Assert.AreEqual(psCorsRules.Length, corsProperties.CorsRules.Count);
127+
int i = 0;
128+
foreach (CorsRule CorsRule in corsProperties.CorsRules)
129+
{
130+
PSCorsRule psCorsRule = psCorsRules[i];
131+
i++;
132+
CompareStrings(psCorsRule.AllowedHeaders, CorsRule.AllowedHeaders);
133+
CompareStrings(psCorsRule.ExposedHeaders, CorsRule.ExposedHeaders);
134+
CompareStrings(psCorsRule.AllowedOrigins, CorsRule.AllowedOrigins);
135+
Assert.AreEqual(psCorsRule.MaxAgeInSeconds, CorsRule.MaxAgeInSeconds);
136+
137+
CorsHttpMethods psAllowedMethods = CorsHttpMethods.None;
138+
foreach (string method in psCorsRule.AllowedMethods)
139+
{
140+
CorsHttpMethods allowedCorsMethod = CorsHttpMethods.None;
141+
if (Enum.TryParse<CorsHttpMethods>(method, true, out allowedCorsMethod))
142+
{
143+
psAllowedMethods |= allowedCorsMethod;
144+
}
145+
else
146+
{
147+
throw new InvalidOperationException(string.Format("Can't parse {0} to CorsHttpMethods.", method));
148+
}
149+
}
150+
151+
Assert.AreEqual(psAllowedMethods, CorsRule.AllowedMethods);
152+
}
153+
}
154+
155+
/// <summary>
156+
/// Comapare String Array and String List, to make sure they are same content
157+
/// </summary>
158+
static private void CompareStrings(string[] stringArray, IList<String> stringList)
159+
{
160+
if ((stringArray == null || stringArray.Length == 0) && (stringList == null || stringList.Count == 0))
161+
{
162+
return;
163+
}
164+
string[] stringArray2 = new string[stringList.Count];
165+
stringList.CopyTo(stringArray2, 0);
166+
Assert.AreEqual(stringArray.Length, stringArray2.Length);
167+
168+
for(int i=0; i< stringArray.Length; i++)
169+
{
170+
Assert.AreEqual(stringArray[i], stringArray2[i]);
171+
}
172+
}
173+
174+
/// <summary>
175+
/// Create a set of CorsRule that containers different parameters combination
176+
/// </summary>
177+
static private CorsProperties PrepareCoresproperties()
178+
{
179+
CorsProperties coresproperties = new CorsProperties();
180+
coresproperties.CorsRules.Add(
181+
new CorsRule()
182+
{
183+
AllowedHeaders = new List<string>
184+
{
185+
"x-ms-meta-data*",
186+
"x -ms-meta-target*",
187+
"x -ms-meta-abc"
188+
},
189+
AllowedMethods = CorsHttpMethods.Connect | CorsHttpMethods.Delete | CorsHttpMethods.Get | CorsHttpMethods.Head | CorsHttpMethods.Merge,
190+
AllowedOrigins = new List<string>
191+
{
192+
"http://www.contoso.com",
193+
"http://www.fabrikam.com"
194+
},
195+
ExposedHeaders = new List<string>
196+
{
197+
"x-ms-meta-*"
198+
},
199+
MaxAgeInSeconds = 100
200+
});
201+
coresproperties.CorsRules.Add(
202+
new CorsRule()
203+
{
204+
AllowedHeaders = new List<string>
205+
{
206+
"x -ms-meta-12345675754564*"
207+
},
208+
AllowedMethods = CorsHttpMethods.None,
209+
AllowedOrigins = new List<string>
210+
{
211+
"http://www.abc23.com",
212+
"https://www.fabrikam.com/*"
213+
},
214+
ExposedHeaders = new List<string>
215+
{
216+
"x-ms-meta-data*",
217+
"x -ms-meta-target*",
218+
"x -ms-meta-abc"
219+
},
220+
MaxAgeInSeconds = 2000
221+
});
222+
coresproperties.CorsRules.Add(
223+
new CorsRule()
224+
{
225+
AllowedHeaders = new List<string>
226+
{
227+
"*"
228+
},
229+
AllowedMethods = CorsHttpMethods.Options | CorsHttpMethods.Post | CorsHttpMethods.Put | CorsHttpMethods.Trace,
230+
AllowedOrigins = new List<string>
231+
{
232+
"*"
233+
},
234+
ExposedHeaders = new List<string>
235+
{
236+
"*",
237+
},
238+
MaxAgeInSeconds = 0
239+
});
240+
return coresproperties;
241+
}
242+
}
243+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,14 @@
120120
<Compile Include="Common\BlobToFileSystemNameResolver.cs" />
121121
<Compile Include="Blob\Cmdlet\StartAzureStorageBlobCopy.cs" />
122122
<Compile Include="Blob\Cmdlet\StopAzureStorageBlobCopy.cs" />
123+
<Compile Include="Common\Cmdlet\GetAzureStorageServiceProperties.cs" />
123124
<Compile Include="Common\Cmdlet\NewAzureStorageAccountSasToken.cs" />
124125
<Compile Include="Common\Cmdlet\SetAzureStorageCORSRule.cs" />
125126
<Compile Include="Common\Cmdlet\GetAzureStorageCORSRule.cs" />
126127
<Compile Include="Common\Cmdlet\GetAzureStorageServiceLogging.cs" />
127128
<Compile Include="Common\Cmdlet\GetAzureStorageServiceMetrics.cs" />
128129
<Compile Include="Common\Cmdlet\RemoveAzureStorageCORSRule.cs" />
130+
<Compile Include="Common\Cmdlet\SetAzureStorageServiceProperties.cs" />
129131
<Compile Include="Common\Cmdlet\SetAzureStorageServiceLogging.cs" />
130132
<Compile Include="Common\Cmdlet\SetAzureStorageServiceMetrics.cs" />
131133
<Compile Include="Common\ConfirmTaskCompletionSource.cs" />
@@ -184,6 +186,7 @@
184186
<Compile Include="Model\Contract\StorageQueueManagement.cs" />
185187
<Compile Include="Model\Contract\StorageTableManagement.cs" />
186188
<Compile Include="Model\ResourceModel\PSCorsRule.cs" />
189+
<Compile Include="Model\ResourceModel\PSSeriviceProperties.cs" />
187190
<Compile Include="Properties\AssemblyInfo.cs" />
188191
<Compile Include="Model\Contract\IStorageBlobManagement.cs" />
189192
<Compile Include="Model\Contract\StorageBlobManagement.cs" />

src/Storage/Commands.Storage/Common/Cmdlet/GetAzureStorageCORSRule.cs

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -44,47 +44,8 @@ public GetAzureStorageCORSRuleCommand()
4444
public override void ExecuteCmdlet()
4545
{
4646
ServiceProperties currentServiceProperties = Channel.GetStorageServiceProperties(ServiceType, GetRequestOptions(ServiceType), OperationContext);
47-
List<PSCorsRule> ruleList = new List<PSCorsRule>();
48-
49-
foreach (var corsRule in currentServiceProperties.Cors.CorsRules)
50-
{
51-
PSCorsRule psCorsRule = new PSCorsRule();
52-
psCorsRule.AllowedOrigins = this.ListToArray(corsRule.AllowedOrigins);
53-
psCorsRule.AllowedHeaders = this.ListToArray(corsRule.AllowedHeaders);
54-
psCorsRule.ExposedHeaders = this.ListToArray(corsRule.ExposedHeaders);
55-
psCorsRule.AllowedMethods = this.ConvertCorsHttpMethodToString(corsRule.AllowedMethods);
56-
psCorsRule.MaxAgeInSeconds = corsRule.MaxAgeInSeconds;
57-
ruleList.Add(psCorsRule);
58-
}
59-
60-
WriteObject(ruleList.ToArray());
61-
}
62-
63-
private string[] ConvertCorsHttpMethodToString(CorsHttpMethods methods)
64-
{
65-
List<string> methodList = new List<string>();
66-
67-
foreach (CorsHttpMethods methodValue in Enum.GetValues(typeof(CorsHttpMethods)).Cast<CorsHttpMethods>())
68-
{
69-
if (methodValue != CorsHttpMethods.None && (methods & methodValue) != 0)
70-
{
71-
methodList.Add(methodValue.ToString());
72-
}
73-
}
74-
75-
return methodList.ToArray();
76-
}
77-
78-
private string[] ListToArray(IList<string> stringList)
79-
{
80-
if (null == stringList)
81-
{
82-
return null;
83-
}
84-
85-
string[] stringArray = new string[stringList.Count];
86-
stringList.CopyTo(stringArray, 0);
87-
return stringArray;
47+
48+
WriteObject(PSCorsRule.ParseCorsRules(currentServiceProperties.Cors));
8849
}
8950
}
9051
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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.Storage.Common.Cmdlet
16+
{
17+
using Microsoft.WindowsAzure.Storage.Shared.Protocol;
18+
using System.Management.Automation;
19+
using System.Security.Permissions;
20+
using Microsoft.WindowsAzure.Commands.Storage.Model.ResourceModel;
21+
22+
/// <summary>
23+
/// Show Azure Storage service properties
24+
/// </summary>
25+
[Cmdlet(VerbsCommon.Get, StorageNouns.StorageServiceProperty),
26+
OutputType(typeof(PSSeriviceProperties))]
27+
public class GetAzureStorageServicePropertyCommand : StorageCloudBlobCmdletBase
28+
{
29+
[Parameter(Mandatory = true, Position = 0, HelpMessage = GetAzureStorageServiceLoggingCommand.ServiceTypeHelpMessage)]
30+
public StorageServiceType ServiceType { get; set; }
31+
32+
// Overwrite the useless parameter
33+
public override int? ServerTimeoutPerRequest { get; set; }
34+
public override int? ClientTimeoutPerRequest { get; set; }
35+
public override int? ConcurrentTaskCount { get; set; }
36+
37+
public GetAzureStorageServicePropertyCommand()
38+
{
39+
EnableMultiThread = false;
40+
}
41+
42+
/// <summary>
43+
/// Execute command
44+
/// </summary>
45+
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
46+
public override void ExecuteCmdlet()
47+
{
48+
ServiceProperties serviceProperties = Channel.GetStorageServiceProperties(ServiceType, GetRequestOptions(ServiceType), OperationContext);
49+
WriteObject(new PSSeriviceProperties(serviceProperties));
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)