Skip to content

Commit 7b5de51

Browse files
committed
Improved dynamic properties by adding them to a parameter set and providing more useful help messages.
1 parent 5dfaed9 commit 7b5de51

File tree

2 files changed

+46
-20
lines changed

2 files changed

+46
-20
lines changed

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,5 +133,32 @@ private static JToken ToJToken(object value)
133133

134134
return new JValue(value.ToString());
135135
}
136+
137+
/// <summary>
138+
/// Gets nested property values from a <see cref="PSObject"/> easily using property path.
139+
/// </summary>
140+
/// <param name="propertyObject">The <see cref="PSObject"/> to get property value from.</param>
141+
/// <param name="propertyPath">The nested property path, e.g. "metadata.description".</param>
142+
/// <returns>The value of the specified property.</returns>
143+
internal static object GetPSObjectProperty(this PSObject propertyObject, string propertyPath)
144+
{
145+
var propertyNames = propertyPath.Split('.');
146+
var tmpObject = propertyObject;
147+
foreach (var propertyName in propertyNames)
148+
{
149+
var propertyInfo = tmpObject.Properties[propertyName];
150+
if (propertyInfo != null)
151+
{
152+
if (propertyInfo.Value is PSObject)
153+
{
154+
tmpObject = propertyInfo.Value as PSObject;
155+
continue;
156+
}
157+
return propertyInfo.Value;
158+
}
159+
return null;
160+
}
161+
return tmpObject;
162+
}
136163
}
137164
}

src/ResourceManager/Resources/Commands.ResourceManager/Cmdlets/Implementation/Policy/NewAzurePolicyAssignment.cs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -156,29 +156,28 @@ object IDynamicParameters.GetDynamicParameters()
156156
{
157157
if (this.PolicyDefinition != null)
158158
{
159-
var properties = this.PolicyDefinition.Properties["Properties"];
160-
if (properties != null)
159+
var parameters = this.PolicyDefinition.GetPSObjectProperty("Properties.parameters") as PSObject;
160+
if (parameters != null)
161161
{
162-
var parameters = (properties.Value as PSObject).Properties["parameters"];
163-
if (parameters != null)
162+
foreach (var param in parameters.Properties)
164163
{
165-
foreach (var param in (parameters.Value as PSObject).Properties)
164+
var type = (param.Value as PSObject).Properties["type"];
165+
var typeString = type != null ? type.Value.ToString() : string.Empty;
166+
var description = (param.Value as PSObject).GetPSObjectProperty("metadata.description");
167+
var helpString = description != null ? description.ToString() : string.Format("The {0} policy parameter.", param.Name);
168+
var dp = new RuntimeDefinedParameter
166169
{
167-
var type = (param.Value as PSObject).Properties["type"];
168-
var typeString = type != null ? type.Value.ToString() : string.Empty;
169-
var dp = new RuntimeDefinedParameter
170-
{
171-
Name = param.Name,
172-
ParameterType = typeString.Equals("array", StringComparison.OrdinalIgnoreCase) ? typeof(object[]) : typeof(string)
173-
};
174-
dp.Attributes.Add(new ParameterAttribute
175-
{
176-
Mandatory = false,
177-
ValueFromPipelineByPropertyName = true,
178-
HelpMessage = param.Name
179-
});
180-
this.dynamicParameters.Add(param.Name, dp);
181-
}
170+
Name = param.Name,
171+
ParameterType = typeString.Equals("array", StringComparison.OrdinalIgnoreCase) ? typeof(object[]) : typeof(string)
172+
};
173+
dp.Attributes.Add(new ParameterAttribute
174+
{
175+
ParameterSetName = ParameterlessPolicyParameterSetName,
176+
Mandatory = true,
177+
ValueFromPipelineByPropertyName = true,
178+
HelpMessage = helpString
179+
});
180+
this.dynamicParameters.Add(param.Name, dp);
182181
}
183182
}
184183
}

0 commit comments

Comments
 (0)