Jira (BOLT-833) JSON Output for Results is confusing for new users

7 views
Skip to first unread message

Nick Maludy (JIRA)

unread,
Sep 13, 2018, 1:10:05 PM9/13/18
to puppe...@googlegroups.com
Nick Maludy created an issue
 
Puppet Task Runner / Improvement BOLT-833
JSON Output for Results is confusing for new users
Issue Type: Improvement Improvement
Assignee: Unassigned
Created: 2018/09/13 10:09 AM
Priority: Normal Normal
Reporter: Nick Maludy

Teaching some of the people on my team how to use Bolt and several of them have been confused by the way that ResultSet is displayed when printed out with notice() from a plan.

 

By default the ResultSet object's to_s() function returns a JSON representation that looks like:

[{"node":"winrm://hostname.domain.tld","status":"success","result":{"os":{"name":"windows","release":{"full":"2012 R2","major":"2012 R2"},"family":"windows"}}}]

Their initial reaction is to try and read parts of this data by accessing it like an array + dictionary that is shown:

plan encore_st2::get_facts (
  TargetSpec $nodes
) {
  $res = run_task('facts', $nodes)
  notice($res)
  $first = $res[0]
  $result = $first['result']
  $os = $result['os']
  notice($os)
}

This returns an error because there is no [] function on a ResultSet:

{
  "kind": "bolt/pal-error",
  "msg": "Evaluation Error: Operator '[]' is not applicable to an Object. (file: /home/user/bolt/modules/encore_st2/plans/get_facts.pp, line: 6, column: 12)",
  "details": {
  }
}

I then showed them the `first` function which leads to the following code:

plan encore_st2::get_facts (
  TargetSpec $nodes
) {
  $res = run_task('facts', $nodes)
  notice($res)
  $first = $res.first
  $result = $first['result']
  $os = $result['os']
  notice($os)
}

This then returns the following error:

{
  "kind": "bolt/pal-error",
  "msg": "Evaluation Error: Operator '[]' is not applicable to an Undef Value. (file: /home/nuserbolt/modules/encore_st2/plans/get_facts.pp, line: 8, column: 9)",
  "details": {
  }
}

Finally we modify the lines:

$result = $first['result']
$os = $result['os']

 

And it becomes working code:

plan encore_st2::get_facts (
  TargetSpec $nodes
) {
  $res = run_task('facts', $nodes)
  notice($res)
  $first = $res.first
  $os = $first['os']
  notice($os)
}

I think the root of the problem is that the JSON representation is NOT consistent with the way you access the data programatically within a plan. I'm sure there are lots of possible solutions, but i think that no matter what direction is taken, the printable output should be consistent with the way that the user needs to access the data.

 

I'm sure the same goes for other defined types like Target.

Add Comment Add Comment
 
This message was sent by Atlassian JIRA (v7.7.1#77002-sha1:e75ca93)
Atlassian logo

Alex Dreyer (JIRA)

unread,
Sep 13, 2018, 1:33:04 PM9/13/18
to puppe...@googlegroups.com
Alex Dreyer commented on Improvement BOLT-833
 
Re: JSON Output for Results is confusing for new users

We should definitely make the ResultSet object indexable.

The design of the Result object vs it's json representation evolved early in bolt's lifetime based on two problems we ran into.
1. We want to make the json object returned from a task accessible directly in plans because without that everything has the boilerplate $result['result']['foo'].
2. We need to include the "target name" in the json output or there is no way to know what the result relates to.

Changing either the behavior of the Result object will be a pretty big breaking change and I don't see a good way to change the json format(which would also be a breaking change)

Nick Maludy (JIRA)

unread,
Sep 13, 2018, 1:34:03 PM9/13/18
to puppe...@googlegroups.com
Nick Maludy commented on Improvement BOLT-833

Some thoughts on improvements:

 

  1. In ResultSet, could add a function to access Results by index

'[]' => Callable[Integer, Optional[Result]],

  1. In Result, could rename 'result' (in the JSON) to 'value'
  2. In Result, could rename 'node' (in the JSON) to 'target'
  3. In Result, could add a field named 'status' that returns the status string (instead of just ok() and error()) to be consistent with the JSON

 

 

 

Alex Dreyer (JIRA)

unread,
Sep 13, 2018, 1:45:04 PM9/13/18
to puppe...@googlegroups.com
Alex Dreyer commented on Improvement BOLT-833

That seems like the correct solution for ResultSet.

I think without breaking existing plans or the ability to stream Resultsets the best we can do is add a warning around calling $result['result'] .

Michael Smith (JIRA)

unread,
Dec 12, 2018, 2:46:04 PM12/12/18
to puppe...@googlegroups.com
Michael Smith commented on Improvement BOLT-833

This seems like it'll need to be split into near-term improvements and Bolt 2.0 changes.

Henrik Lindberg (JIRA)

unread,
Apr 3, 2019, 6:53:02 AM4/3/19
to puppe...@googlegroups.com

It is not as easy as adding a function `[]` like that since it is an operator in the Puppet Language and there is no automatic mapping of operators to methods.

Alex Dreyer (JIRA)

unread,
Apr 4, 2019, 2:53:02 PM4/4/19
to puppe...@googlegroups.com
Alex Dreyer commented on Improvement BOLT-833

In general I think we should prefer breaking changes to the json format rather then the plan language. One possible path forward that will not break plans is to inject _target and _status keys into the result's value. In Bolt 2.0 we can then change the json output to be just the value rather then have the value nested in result

Yasmin Rajabi (JIRA)

unread,
Aug 15, 2019, 7:15:03 PM8/15/19
to puppe...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages