Skip to content

Remove legacy object format from resource cmdlets #1075

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 7, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@
<Compile Include="Entities\Providers\ResourceTypeDefinition.cs" />
<Compile Include="Entities\ResourceGroup\ResourceBatchMoveParameters.cs" />
<Compile Include="Entities\Resources\Resource.cs" />
<Compile Include="Entities\Resources\ResourceObjectFormat.cs" />
<Compile Include="Entities\Resources\ResourcePlan.cs" />
<Compile Include="Entities\Resources\ResourceSku.cs" />
<Compile Include="Entities\Resources\TerminalProvisioningStates.cs" />
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,8 @@ internal static class JTokenExtensions
/// Converts a <see cref="JObject"/> to a <see cref="PSObject"/>
/// </summary>
/// <param name="jtoken">The <see cref="JObject"/></param>
/// <param name="objectFormat">The <see cref="ResourceObjectFormat"/></param>
/// <param name="objectType">The type of the object.</param>
internal static PSObject ToPsObject(this JToken jtoken, ResourceObjectFormat objectFormat, string objectType = null)
internal static PSObject ToPsObject(this JToken jtoken, string objectType = null)
{
if (jtoken == null)
{
Expand All @@ -60,7 +59,7 @@ internal static PSObject ToPsObject(this JToken jtoken, ResourceObjectFormat obj

if (jtoken.Type != JTokenType.Object)
{
return new PSObject(JTokenExtensions.ConvertPropertyValueForPsObject(propertyValue: jtoken, objectFormat: objectFormat));
return new PSObject(JTokenExtensions.ConvertPropertyValueForPsObject(propertyValue: jtoken));
}

var jobject = (JObject)jtoken;
Expand All @@ -75,7 +74,7 @@ internal static PSObject ToPsObject(this JToken jtoken, ResourceObjectFormat obj
{
psObject.Properties.Add(new PSNoteProperty(
name: JTokenExtensions.ConvertToPascalCase(propertyName: property.Name),
value: JTokenExtensions.ConvertPropertyValueForPsObject(propertyValue: property.Value, objectFormat: objectFormat)));
value: JTokenExtensions.ConvertPropertyValueForPsObject(propertyValue: property.Value)));
}

return psObject;
Expand All @@ -86,12 +85,11 @@ internal static PSObject ToPsObject(this JToken jtoken, ResourceObjectFormat obj
/// used as the value of a <see cref="PSNoteProperty"/>.
/// </summary>
/// <param name="propertyValue">The <see cref="JToken"/> value.</param>
/// <param name="objectFormat">The <see cref="ResourceObjectFormat"/></param>
internal static object ConvertPropertyValueForPsObject(JToken propertyValue, ResourceObjectFormat objectFormat)
internal static object ConvertPropertyValueForPsObject(JToken propertyValue)
{
if (propertyValue.Type == JTokenType.Object)
{
return propertyValue.ToPsObject(objectFormat);
return propertyValue.ToPsObject();
}

if (propertyValue.Type == JTokenType.Array)
Expand All @@ -102,7 +100,7 @@ internal static object ConvertPropertyValueForPsObject(JToken propertyValue, Res

for (int i = 0; i < array.Length; ++i)
{
array[i] = JTokenExtensions.ConvertPropertyValueForPsObject(jArray[i], objectFormat);
array[i] = JTokenExtensions.ConvertPropertyValueForPsObject(jArray[i]);
}

return array;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ internal static class ResourceExtensions
/// Converts a <see cref="Resource{JToken}"/> object into a <see cref="PSObject"/> object.
/// </summary>
/// <param name="resource">The <see cref="Resource{JToken}"/> object.</param>
/// <param name="objectFormat">The <see cref="ResourceObjectFormat"/></param>
internal static PSObject ToPsObject(this Resource<JToken> resource, ResourceObjectFormat objectFormat)
internal static PSObject ToPsObject(this Resource<JToken> resource)
{
var resourceType = string.IsNullOrEmpty(resource.Id)
? null
Expand All @@ -56,12 +55,12 @@ internal static PSObject ToPsObject(this Resource<JToken> resource, ResourceObje
{ "Location", resource.Location },
{ "SubscriptionId", string.IsNullOrEmpty(resource.Id) ? null : ResourceIdUtility.GetSubscriptionId(resource.Id) },
{ "Tags", TagsHelper.GetTagsHashtables(resource.Tags) },
{ "Plan", resource.Plan.ToJToken().ToPsObject(objectFormat) },
{ "Properties", ResourceExtensions.GetProperties(resource, objectFormat) },
{ "Plan", resource.Plan.ToJToken().ToPsObject() },
{ "Properties", ResourceExtensions.GetProperties(resource) },
{ "CreatedTime", resource.CreatedTime },
{ "ChangedTime", resource.ChangedTime },
{ "ETag", resource.ETag },
{ "Sku", resource.Sku.ToJToken().ToPsObject(objectFormat) },
{ "Sku", resource.Sku.ToJToken().ToPsObject() },
};

var resourceTypeName = resourceType == null && extensionResourceType == null
Expand All @@ -81,17 +80,14 @@ internal static PSObject ToPsObject(this Resource<JToken> resource, ResourceObje
/// Gets the properties object
/// </summary>
/// <param name="resource">The <see cref="Resource{JToken}"/> object.</param>
/// <param name="objectFormat">The <see cref="ResourceObjectFormat"/></param>
private static object GetProperties(Resource<JToken> resource, ResourceObjectFormat objectFormat)
private static object GetProperties(Resource<JToken> resource)
{
if (resource.Properties == null)
{
return null;
}

return objectFormat == ResourceObjectFormat.Legacy
? JsonUtilities.DeserializeJson(resource.Properties.ToString())
: (object)resource.Properties.ToPsObject(objectFormat);
return (object)resource.Properties.ToPsObject();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private void RunCmdlet()
getFirstPage: () => this.GetResourceGroups(),
getNextPage: nextLink => this.GetNextLink<JObject>(nextLink),
cancellationToken: this.CancellationToken,
action: resourceGroups => this.WriteObject(sendToPipeline: resourceGroups.CoalesceEnumerable().SelectArray(resourceGroup => resourceGroup.ToPsObject(ResourceObjectFormat.New)), enumerateCollection: true));
action: resourceGroups => this.WriteObject(sendToPipeline: resourceGroups.CoalesceEnumerable().SelectArray(resourceGroup => resourceGroup.ToPsObject()), enumerateCollection: true));
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private void RunCmdlet()
getFirstPage: () => this.GetResources(),
getNextPage: nextLink => this.GetNextLink<JObject>(nextLink),
cancellationToken: this.CancellationToken,
action: resources => this.WriteObject(sendToPipeline: resources.CoalesceEnumerable().SelectArray(resource => resource.ToPsObject(ResourceObjectFormat.New)), enumerateCollection: true));
action: resources => this.WriteObject(sendToPipeline: resources.CoalesceEnumerable().SelectArray(resource => resource.ToPsObject()), enumerateCollection: true));
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ protected override void OnProcessRecord()
var resultString = this.GetLongRunningOperationTracker(activityName: activity, isResourceCreateOrUpdate: false)
.WaitOnOperation(operationResult: operationResult);

this.TryConvertAndWriteObject(resultString, ResourceObjectFormat.New);
this.TryConvertAndWriteObject(resultString);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ protected PSObject[] GetOutputObjects(params JToken[] resources)
.Where(resource => resource != null)
.SelectArray(resource =>
{
var psobject = resource.ToResource().ToPsObject(ResourceObjectFormat.New);
var psobject = resource.ToResource().ToPsObject();
psobject.Properties.Add(new PSNoteProperty("LockId", psobject.Properties["ResourceId"].Value));
return psobject;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ protected PSObject[] GetOutputObjects(params JToken[] resources)
.Where(resource => resource != null)
.SelectArray(resource =>
{
var psobject = resource.ToResource().ToPsObject(ResourceObjectFormat.New);
var psobject = resource.ToResource().ToPsObject();
psobject.Properties.Add(new PSNoteProperty("PolicyAssignmentId", psobject.Properties["ResourceId"].Value));
return psobject;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ protected PSObject[] GetOutputObjects(params JToken[] resources)
.Where(resource => resource != null)
.SelectArray(resource =>
{
var psobject = resource.ToResource().ToPsObject(ResourceObjectFormat.New);
var psobject = resource.ToResource().ToPsObject();
psobject.Properties.Add(new PSNoteProperty("PolicyDefinitionId", psobject.Properties["ResourceId"].Value));
return psobject;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,25 +122,11 @@ public sealed class FindAzureResourceCmdlet : ResourceManagerCmdletBase
[Parameter(ParameterSetName = FindAzureResourceCmdlet.ListTenantResourcesParameterSet, Mandatory = true, HelpMessage = "Indicates that this is a tenant level operation.")]
public SwitchParameter TenantLevel { get; set; }

/// <summary>
/// Gets or sets the resource property object format.
/// </summary>
[Parameter(Mandatory = false, HelpMessage = "The output format of the resource properties.")]
public ResourceObjectFormat OutputObjectFormat { get; set; }

/// <summary>
/// Gets or sets the subscription id.
/// </summary>
public Guid? SubscriptionId { get; set; }

/// <summary>
/// Initializes a new instance of the <see cref="FindAzureResourceCmdlet" /> class.
/// </summary>
public FindAzureResourceCmdlet()
{
this.OutputObjectFormat = ResourceObjectFormat.Legacy;
}

/// <summary>
/// Collects subscription ids from the pipeline.
/// </summary>
Expand All @@ -164,11 +150,6 @@ protected override void OnEndProcessing()
/// </summary>
private void RunCmdlet()
{
if(this.OutputObjectFormat == ResourceObjectFormat.Legacy)
{
this.WriteWarning("This cmdlet is using the legacy properties object format. This format is being deprecated. Please use '-OutputObjectFormat New' and update your scripts.");
}

if (!this.TenantLevel)
{
this.SubscriptionId = DefaultContext.Subscription.Id;
Expand All @@ -193,14 +174,14 @@ private void RunCmdlet()
items = this.GetPopulatedResource(batch).Result;
}

var powerShellObjects = items.SelectArray(genericResource => genericResource.ToPsObject(this.OutputObjectFormat));
var powerShellObjects = items.SelectArray(genericResource => genericResource.ToPsObject());

this.WriteObject(sendToPipeline: powerShellObjects, enumerateCollection: true);
}
}
else
{
this.WriteObject(sendToPipeline: resources.CoalesceEnumerable().SelectArray(res => res.ToPsObject(this.OutputObjectFormat)), enumerateCollection: true);
this.WriteObject(sendToPipeline: resources.CoalesceEnumerable().SelectArray(res => res.ToPsObject()), enumerateCollection: true);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,25 +173,11 @@ public sealed class GetAzureResourceCmdlet : ResourceManagerCmdletBase
[Parameter(ParameterSetName = GetAzureResourceCmdlet.ListTenantResourcesParameterSet, Mandatory = true, HelpMessage = "Indicates that this is a tenant level operation.")]
public SwitchParameter TenantLevel { get; set; }

/// <summary>
/// Gets or sets the resource property object format.
/// </summary>
[Parameter(Mandatory = false, HelpMessage = "The output format of the resource properties.")]
public ResourceObjectFormat OutputObjectFormat { get; set; }

/// <summary>
/// Gets or sets the subscription id.
/// </summary>
public Guid? SubscriptionId { get; set; }

/// <summary>
/// Initializes a new instance of the <see cref="GetAzureResourceCmdlet" /> class.
/// </summary>
public GetAzureResourceCmdlet()
{
this.OutputObjectFormat = ResourceObjectFormat.Legacy;
}

/// <summary>
/// Collects subscription ids from the pipeline.
/// </summary>
Expand All @@ -215,11 +201,6 @@ protected override void OnEndProcessing()
/// </summary>
private void RunCmdlet()
{
if(this.OutputObjectFormat == ResourceObjectFormat.Legacy)
{
this.WriteWarning("This cmdlet is using the legacy properties object format. This format is being deprecated. Please use '-OutputObjectFormat New' and update your scripts.");
}

if (string.IsNullOrEmpty(this.ResourceId) && !this.TenantLevel)
{
this.SubscriptionId = DefaultContext.Subscription.Id;
Expand All @@ -244,14 +225,14 @@ private void RunCmdlet()
items = this.GetPopulatedResource(batch).Result;
}

var powerShellObjects = items.SelectArray(genericResource => genericResource.ToPsObject(this.OutputObjectFormat));
var powerShellObjects = items.SelectArray(genericResource => genericResource.ToPsObject());

this.WriteObject(sendToPipeline: powerShellObjects, enumerateCollection: true);
}
}
else
{
this.WriteObject(sendToPipeline: resources.CoalesceEnumerable().SelectArray(res => res.ToPsObject(this.OutputObjectFormat)), enumerateCollection: true);
this.WriteObject(sendToPipeline: resources.CoalesceEnumerable().SelectArray(res => res.ToPsObject()), enumerateCollection: true);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ private void RunCmdlet()
isResourceCreateOrUpdate: false)
.WaitOnOperation(operationResult: operationResult);

this.TryConvertAndWriteObject(result, ResourceObjectFormat.New);
this.TryConvertAndWriteObject(result);
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,36 +81,16 @@ public sealed class NewAzureResourceCmdlet : ResourceManipulationCmdletBase
[Parameter(Mandatory = false, HelpMessage = "When set indicates that the full object is passed in to the -PropertyObject parameter.")]
public SwitchParameter IsFullObject { get; set; }

/// <summary>
/// Gets or sets the resource property object format.
/// </summary>
[Parameter(Mandatory = false, HelpMessage = "The output format of the resource properties.")]
[ValidateNotNull]
public ResourceObjectFormat? OutputObjectFormat { get; set; }

/// <summary>
/// Initializes a new instance of the <see cref="NewAzureResourceCmdlet" /> class.
/// </summary>
public NewAzureResourceCmdlet()
{
this.OutputObjectFormat = ResourceObjectFormat.Legacy;
}

/// <summary>
/// Executes the cmdlet.
/// </summary>
protected override void OnProcessRecord()
{
base.OnProcessRecord();
this.DetermineOutputObjectFormat();
if(this.IsFullObject.IsPresent)
{
this.WriteWarning("The IsFullObject parameter is obsolete and will be removed in future releases.");
}
if (this.OutputObjectFormat == ResourceObjectFormat.Legacy)
{
this.WriteWarning("This cmdlet is using the legacy properties object format. This format is being deprecated. Please use '-OutputObjectFormat New' and update your scripts.");
}

var resourceId = this.GetResourceId();
this.ConfirmAction(
Expand Down Expand Up @@ -142,26 +122,10 @@ protected override void OnProcessRecord()
var result = this.GetLongRunningOperationTracker(activityName: activity, isResourceCreateOrUpdate: true)
.WaitOnOperation(operationResult: operationResult);

this.TryConvertToResourceAndWriteObject(result, this.OutputObjectFormat.Value);
this.TryConvertToResourceAndWriteObject(result);
});
}

/// <summary>
/// Determines the output object format.
/// </summary>
private void DetermineOutputObjectFormat()
{
if (this.Properties != null && this.OutputObjectFormat == null && this.Properties.TypeNames.Any(typeName => typeName.EqualsInsensitively(Constants.MicrosoftAzureResource)))
{
this.OutputObjectFormat = ResourceObjectFormat.New;
}

if (this.OutputObjectFormat == null)
{
this.OutputObjectFormat = ResourceObjectFormat.Legacy;
}
}

/// <summary>
/// Gets the resource body from the parameters.
/// </summary>
Expand Down
Loading