Skip to content

Commit 7de2457

Browse files
committed
Throw warning only if using legacy object format
1 parent 48d9590 commit 7de2457

File tree

5 files changed

+309
-265
lines changed

5 files changed

+309
-265
lines changed

src/ResourceManager/ResourceManager/Commands.ResourceManager/Cmdlets/Extensions/PsObjectExtensions.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions
1616
{
1717
using System;
1818
using System.Collections;
19+
using System.Collections.Generic;
1920
using System.Linq;
2021
using System.Management.Automation;
2122
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components;
@@ -69,9 +70,18 @@ private static JToken ToJToken(object value)
6970
JObject obj = new JObject();
7071
if (valueAsPsObject.BaseObject != null && valueAsPsObject.BaseObject is IDictionary)
7172
{
72-
foreach (var dictionaryEntry in ((IDictionary)valueAsPsObject.BaseObject).OfType<DictionaryEntry>())
73+
var valueAsIDictionary = valueAsPsObject.BaseObject as IDictionary;
74+
var dictionaryEntries = valueAsIDictionary is IDictionary<string, object>
75+
? valueAsIDictionary.OfType<KeyValuePair<string, object>>().Select(kvp => Tuple.Create(kvp.Key, kvp.Value))
76+
: valueAsIDictionary.OfType<DictionaryEntry>().Select(dictionaryEntry => Tuple.Create(dictionaryEntry.Key.ToString(), dictionaryEntry.Value));
77+
78+
dictionaryEntries = dictionaryEntries.Any(dictionaryEntry => dictionaryEntry.Item1.EqualsInsensitively(Constants.MicrosoftAzureResource))
79+
? dictionaryEntries.Where(dictionaryEntry => !PsObjectExtensions.PropertiesToRemove.ContainsKey(dictionaryEntry.Item1))
80+
: dictionaryEntries;
81+
82+
foreach (var dictionaryEntry in dictionaryEntries)
7383
{
74-
obj.Add(dictionaryEntry.Key.ToString(), PsObjectExtensions.ToJToken(dictionaryEntry.Value));
84+
obj.Add(dictionaryEntry.Item1, PsObjectExtensions.ToJToken(dictionaryEntry.Item2));
7585
}
7686
}
7787
else

src/ResourceManager/ResourceManager/Commands.ResourceManager/Cmdlets/Implementation/NewAzureResourceCmdlet.cs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
1616
{
1717
using System.Collections;
1818
using System.Collections.Generic;
19+
using System.Linq;
1920
using System.Management.Automation;
2021
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components;
2122
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Resources;
@@ -75,7 +76,8 @@ public sealed class NewAzureResourceCmdlet : ResourceManipulationCmdletBase
7576
/// Gets or sets the resource property object format.
7677
/// </summary>
7778
[Parameter(Mandatory = false, HelpMessage = "The output format of the resource properties.")]
78-
public ResourceObjectFormat OutputObjectFormat { get; set; }
79+
[ValidateNotNull]
80+
public ResourceObjectFormat? OutputObjectFormat { get; set; }
7981

8082
/// <summary>
8183
/// Initializes a new instance of the <see cref="NewAzureResourceCmdlet" /> class.
@@ -91,9 +93,13 @@ public NewAzureResourceCmdlet()
9193
protected override void OnProcessRecord()
9294
{
9395
base.OnProcessRecord();
96+
this.DetermineOutputObjectFormat();
97+
if (this.OutputObjectFormat == ResourceObjectFormat.Legacy)
98+
{
99+
this.WriteWarning("This cmdlet is using the legacy properties object format. This format is being deprecated. Please use '-OutputObjectFormat New' and update your scripts.");
100+
}
94101

95102
var resourceId = this.GetResourceId();
96-
97103
this.ConfirmAction(
98104
this.Force,
99105
"Are you sure you want to create the following resource: "+ resourceId,
@@ -123,10 +129,26 @@ protected override void OnProcessRecord()
123129
var result = this.GetLongRunningOperationTracker(activityName: activity, isResourceCreateOrUpdate: true)
124130
.WaitOnOperation(operationResult: operationResult);
125131

126-
this.WriteObject(result, this.OutputObjectFormat);
132+
this.WriteObject(result, this.OutputObjectFormat.Value);
127133
});
128134
}
129135

136+
/// <summary>
137+
/// Determines the output object format.
138+
/// </summary>
139+
private void DetermineOutputObjectFormat()
140+
{
141+
if (this.Properties != null && this.OutputObjectFormat == null && this.Properties.TypeNames.Any(typeName => typeName.EqualsInsensitively(Constants.MicrosoftAzureResource)))
142+
{
143+
this.OutputObjectFormat = ResourceObjectFormat.New;
144+
}
145+
146+
if (this.OutputObjectFormat == null)
147+
{
148+
this.OutputObjectFormat = ResourceObjectFormat.Legacy;
149+
}
150+
}
151+
130152
/// <summary>
131153
/// Gets the resource body from the parameters.
132154
/// </summary>

src/ResourceManager/ResourceManager/Commands.ResourceManager/Cmdlets/Implementation/SetAzureResourceCmdlet.cs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
1616
{
1717
using System.Collections;
18+
using System.Linq;
1819
using System.Management.Automation;
1920
using System.Threading.Tasks;
2021
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components;
@@ -69,26 +70,21 @@ public sealed class SetAzureResourceCmdlet : ResourceManipulationCmdletBase
6970
/// Gets or sets the resource property object format.
7071
/// </summary>
7172
[Parameter(Mandatory = false, HelpMessage = "The output format of the resource properties.")]
72-
public ResourceObjectFormat OutputObjectFormat { get; set; }
73-
74-
/// <summary>
75-
/// Initializes a new instance of the <see cref="SetAzureResourceCmdlet" /> class.
76-
/// </summary>
77-
public SetAzureResourceCmdlet()
78-
{
79-
this.OutputObjectFormat = ResourceObjectFormat.Legacy;
80-
}
73+
[ValidateNotNull]
74+
public ResourceObjectFormat? OutputObjectFormat { get; set; }
8175

8276
/// <summary>
8377
/// Executes the cmdlet.
8478
/// </summary>
8579
protected override void OnProcessRecord()
8680
{
8781
base.OnProcessRecord();
82+
this.DetermineOutputObjectFormat();
8883
if (this.OutputObjectFormat == ResourceObjectFormat.Legacy)
8984
{
9085
this.WriteWarning("This cmdlet is using the legacy properties object format. This format is being deprecated. Please use '-OutputObjectFormat New' and update your scripts.");
9186
}
87+
9288
var resourceId = this.GetResourceId();
9389
this.ConfirmAction(
9490
this.Force,
@@ -128,10 +124,26 @@ protected override void OnProcessRecord()
128124
var result = this.GetLongRunningOperationTracker(activityName: activity, isResourceCreateOrUpdate: true)
129125
.WaitOnOperation(operationResult: operationResult);
130126

131-
this.WriteObject(result, this.OutputObjectFormat);
127+
this.WriteObject(result, this.OutputObjectFormat.Value);
132128
});
133129
}
134130

131+
/// <summary>
132+
/// Determines the output object format.
133+
/// </summary>
134+
private void DetermineOutputObjectFormat()
135+
{
136+
if (this.Properties != null && this.OutputObjectFormat == null && this.Properties.TypeNames.Any(typeName => typeName.EqualsInsensitively(Constants.MicrosoftAzureResource)))
137+
{
138+
this.OutputObjectFormat = ResourceObjectFormat.New;
139+
}
140+
141+
if (this.OutputObjectFormat == null)
142+
{
143+
this.OutputObjectFormat = ResourceObjectFormat.Legacy;
144+
}
145+
}
146+
135147
/// <summary>
136148
/// Gets the resource body.
137149
/// </summary>

src/ResourceManager/Resources/Commands.Resources.Test/SessionRecords/Microsoft.Azure.Commands.Resources.Test.ScenarioTests.ResourceTests/TestMoveAResourceTest.json

Lines changed: 169 additions & 169 deletions
Large diffs are not rendered by default.

src/ResourceManager/Resources/Commands.Resources.Test/SessionRecords/Microsoft.Azure.Commands.Resources.Test.ScenarioTests.ResourceTests/TestSetAResourceTest.json

Lines changed: 81 additions & 81 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)