aws / aws-tools-for-powershell

The AWS Tools for PowerShell lets developers and administrators manage their AWS services from the PowerShell scripting environment.
Apache License 2.0
235 stars 78 forks source link

Get-PLSProduct returns JSON, not objects #181

Closed ghost closed 2 years ago

ghost commented 3 years ago

Description

Get-PLSProduct returns JSON string objects, not proper Powershell objects. This requires the client to ConvertFrom-Json the results.

Reproduction Steps

$products = Get-PLSProduct -ServiceCode 'AmazonEC2' -Region 'us-east-1' -Filter `
    @{Type = "TERM_MATCH"; Field = "instanceType"; Value = 't3a.large' }, `
    @{Type = "TERM_MATCH"; Field = "location"; Value = 'US East (Ohio)' }, `
    @{Type = "TERM_MATCH"; Field = "productFamily"; Value = 'Compute Instance' }, `
    @{Type = "TERM_MATCH"; Field = "operatingSystem"; Value = 'Windows' }, `
    @{Type = "TERM_MATCH"; Field = "termType"; Value = 'OnDemand' }, `
    @{Type = "TERM_MATCH"; Field = "preInstalledSw"; Value = 'NA' },
    @{Type = "TERM_MATCH"; Field = "licenseModel"; Value = 'No License required' },
    @{Type = "TERM_MATCH"; Field = "capacitystatus"; Value = 'Used' },
    @{Type = "TERM_MATCH"; Field = "tenancy"; Value = 'Shared' }
PS C:\> $products

{"product":{"productFamily":"Compute Instance","att (...truncated...)

PS C:\> $products.GetType()

IsPublic IsSerial Name                                     BaseType                                                                                                                                                                                                                                                       
-------- -------- ----                                     --------                                                                                                                                                                                                                                                       
True     True     String                                   System.Object

PS C:\> $products | ConvertFrom-Json

product         : @{productFamily=Compute Instance; attributes=; sku=2E23XRETRVNZTSTE}
serviceCode     : AmazonEC2
terms           : @{OnDemand=; Reserved=}
version         : 20201104195536
publicationDate : 2020-11-04T19:55:36Z

Logs

PS C:\> $AWSHistory.LastServiceResponse | ConvertTo-Json
{
  "FormatVersion": "aws_v1",
  "NextToken": null,
  "PriceList": [
    "{\"product\":{\"productFamily\":\"Compute Instance\",\"att (...truncated, lots of quote-escaped json...)"
  ],
  "ResponseMetadata": {
    "RequestId": "01f75ae5-d390-4c79-8640-558b11e4377a",
    "Metadata": {}
  },
  "ContentLength": 10411,
  "HttpStatusCode": 200,
  "LoggedAt": "/Date(1604586869955)/"
}

Environment

(Same behavior also in 7.0.3 core with AWS.Tools.* v4.1.3.0)

Resolution

At a minimum, adding | ConvertFrom-JSON to the objects as they're returning would work.


This is a :bug: bug-report

ashishdhingra commented 3 years ago

Hi @jgardsentry,

Good morning.

Thanks for reporting the issue. I checked the Get-PLSProduct Cmdlet documentation, the examples posted there also indicate the response as JSON. So this doesn't appear to be a bug. Also, not sure if this is a feature-request since any change to the response type could break other customers. Hope this provides some guidance.

Thanks, Ashish

ghost commented 3 years ago

Thank you @ashishdhingra . Good point. I would suggest that a Powershell developer is going to expect responses to to be in usable objects, not in strings. We can call it a bug or feature request, but I do think the current behavior is entirely inconsistent with how everything else in Powershell works.

I understand the concern about backward compatibility. A simple workaround could be to just add something like a -ReturnObjects switch that does the JSON conversion for the user. Ultimately, I do think there should be a timeline to deprecate the current behavior and switch to returning objects by default.

ashishdhingra commented 3 years ago

@jgardsentry Thanks for your reply. I agree it should be consistent and don't have context on why it's the behavior since quite long. I will mark this as feature-request so that this default behavior could be reviewed.

yohay-h commented 3 years ago

Thanks for posting this @jgardsentry, FWIW - I had to use Get-PLSProduct cmdlet in a PowerShell script context and definitely found that JSON string returning from it is odd (and unusable as-is, which indeed forced me to pipe the output to ConvertFrom-Json to do my further processing).

PowerShell is Object-Oriented by nature (and design), and this should be taken into account when designing and developing cmdlets for it.

github-actions[bot] commented 2 years ago

We have noticed this issue has not received attention in 1 year. We will close this issue for now. If you think this is in error, please feel free to comment and reopen the issue.