scottwinkler / terraform-provider-shell

Terraform provider for executing shell commands and saving output to state file
Mozilla Public License 2.0
278 stars 61 forks source link

JSON lists are not parsed as valid output #104

Open blacksd opened 2 years ago

blacksd commented 2 years ago

I saw a mention in #31 that the provider output doesn't parse JSON lists, only maps. This is unfortunately accurate; if I produce a JSON-valid list (it's from a jq output), I get a "no valid JSON" debug message:

2022-01-21T15:28:58.159+0100 [DEBUG] provider.terraform-provider-shell_v1.7.10: 2022/01/21 15:28:58 -------------------------
2022-01-21T15:28:58.159+0100 [DEBUG] provider.terraform-provider-shell_v1.7.10: 2022/01/21 15:28:58 [DEBUG] Starting execution...
2022-01-21T15:28:58.159+0100 [DEBUG] provider.terraform-provider-shell_v1.7.10: 2022/01/21 15:28:58 -------------------------
2022-01-21T15:28:59.248+0100 [DEBUG] provider.terraform-provider-shell_v1.7.10: 2022/01/21 15:28:59   [
2022-01-21T15:28:59.248+0100 [DEBUG] provider.terraform-provider-shell_v1.7.10: 2022/01/21 15:28:59     "value1",
2022-01-21T15:28:59.248+0100 [DEBUG] provider.terraform-provider-shell_v1.7.10: 2022/01/21 15:28:59     "value2",
2022-01-21T15:28:59.248+0100 [DEBUG] provider.terraform-provider-shell_v1.7.10: 2022/01/21 15:28:59     "value3",
2022-01-21T15:28:59.248+0100 [DEBUG] provider.terraform-provider-shell_v1.7.10: 2022/01/21 15:28:59     "value4",
2022-01-21T15:28:59.248+0100 [DEBUG] provider.terraform-provider-shell_v1.7.10: 2022/01/21 15:28:59     "value5",
2022-01-21T15:28:59.248+0100 [DEBUG] provider.terraform-provider-shell_v1.7.10: 2022/01/21 15:28:59     "value6",
2022-01-21T15:28:59.249+0100 [DEBUG] provider.terraform-provider-shell_v1.7.10: 2022/01/21 15:28:59     "value7",
2022-01-21T15:28:59.249+0100 [DEBUG] provider.terraform-provider-shell_v1.7.10: 2022/01/21 15:28:59     "value8",
2022-01-21T15:28:59.249+0100 [DEBUG] provider.terraform-provider-shell_v1.7.10: 2022/01/21 15:28:59     "value9",
2022-01-21T15:28:59.249+0100 [DEBUG] provider.terraform-provider-shell_v1.7.10: 2022/01/21 15:28:59     "value10",
2022-01-21T15:28:59.249+0100 [DEBUG] provider.terraform-provider-shell_v1.7.10: 2022/01/21 15:28:59     "value11",
2022-01-21T15:28:59.249+0100 [DEBUG] provider.terraform-provider-shell_v1.7.10: 2022/01/21 15:28:59     "value12"
2022-01-21T15:28:59.249+0100 [DEBUG] provider.terraform-provider-shell_v1.7.10: 2022/01/21 15:28:59   ]
2022-01-21T15:28:59.249+0100 [DEBUG] provider.terraform-provider-shell_v1.7.10: 2022/01/21 15:28:59 -------------------------
2022-01-21T15:28:59.249+0100 [DEBUG] provider.terraform-provider-shell_v1.7.10: 2022/01/21 15:28:59 [DEBUG] Command execution completed:
2022-01-21T15:28:59.249+0100 [DEBUG] provider.terraform-provider-shell_v1.7.10: 2022/01/21 15:28:59 -------------------------
2022-01-21T15:28:59.249+0100 [DEBUG] provider.terraform-provider-shell_v1.7.10: 2022/01/21 15:28:59 [DEBUG] no valid JSON strings found at end of output:

if I get the same list enclosed within a map, it works:

2022-01-21T16:06:36.261+0100 [DEBUG] provider.terraform-provider-shell_v1.7.10: 2022/01/21 16:06:36 -------------------------
2022-01-21T16:06:36.261+0100 [DEBUG] provider.terraform-provider-shell_v1.7.10: 2022/01/21 16:06:36 [DEBUG] Starting execution...
2022-01-21T16:06:36.261+0100 [DEBUG] provider.terraform-provider-shell_v1.7.10: 2022/01/21 16:06:36 -------------------------
2022-01-21T16:06:37.300+0100 [DEBUG] provider.terraform-provider-shell_v1.7.10: 2022/01/21 16:06:37   {
2022-01-21T16:06:37.300+0100 [DEBUG] provider.terraform-provider-shell_v1.7.10: 2022/01/21 16:06:37     "roles": [
2022-01-21T16:06:37.300+0100 [DEBUG] provider.terraform-provider-shell_v1.7.10: 2022/01/21 16:06:37       "value1",
2022-01-21T16:06:37.300+0100 [DEBUG] provider.terraform-provider-shell_v1.7.10: 2022/01/21 16:06:37       "value2",
2022-01-21T16:06:37.301+0100 [DEBUG] provider.terraform-provider-shell_v1.7.10: 2022/01/21 16:06:37       "value3",
2022-01-21T16:06:37.301+0100 [DEBUG] provider.terraform-provider-shell_v1.7.10: 2022/01/21 16:06:37       "value4",
2022-01-21T16:06:37.301+0100 [DEBUG] provider.terraform-provider-shell_v1.7.10: 2022/01/21 16:06:37       "value5",
2022-01-21T16:06:37.301+0100 [DEBUG] provider.terraform-provider-shell_v1.7.10: 2022/01/21 16:06:37       "value6",
2022-01-21T16:06:37.301+0100 [DEBUG] provider.terraform-provider-shell_v1.7.10: 2022/01/21 16:06:37       "value7",
2022-01-21T16:06:37.301+0100 [DEBUG] provider.terraform-provider-shell_v1.7.10: 2022/01/21 16:06:37       "value8",
2022-01-21T16:06:37.301+0100 [DEBUG] provider.terraform-provider-shell_v1.7.10: 2022/01/21 16:06:37       "value9",
2022-01-21T16:06:37.301+0100 [DEBUG] provider.terraform-provider-shell_v1.7.10: 2022/01/21 16:06:37       "value10",
2022-01-21T16:06:37.301+0100 [DEBUG] provider.terraform-provider-shell_v1.7.10: 2022/01/21 16:06:37       "value11",
2022-01-21T16:06:37.301+0100 [DEBUG] provider.terraform-provider-shell_v1.7.10: 2022/01/21 16:06:37       "value12"
2022-01-21T16:06:37.301+0100 [DEBUG] provider.terraform-provider-shell_v1.7.10: 2022/01/21 16:06:37     ]
2022-01-21T16:06:37.301+0100 [DEBUG] provider.terraform-provider-shell_v1.7.10: 2022/01/21 16:06:37   }
2022-01-21T16:06:37.301+0100 [DEBUG] provider.terraform-provider-shell_v1.7.10: 2022/01/21 16:06:37 -------------------------
2022-01-21T16:06:37.301+0100 [DEBUG] provider.terraform-provider-shell_v1.7.10: 2022/01/21 16:06:37 [DEBUG] Command execution completed:
2022-01-21T16:06:37.301+0100 [DEBUG] provider.terraform-provider-shell_v1.7.10: 2022/01/21 16:06:37 -------------------------
2022-01-21T16:06:37.301+0100 [DEBUG] provider.terraform-provider-shell_v1.7.10: 2022/01/21 16:06:37 [DEBUG] Valid map[string]string:

I'll use the simple workaround for the time being, as soon as I'll get the chance I'll look into fixing this for good.

freimer commented 2 years ago

I don't believe there is anything to fix. The output from the read script (or create and update if read not implemented) is used as the output for the resource. What key would be used if a list were accepted? It has to be a single JSON object, not a list, as the properties of the object are used as the keys in the map for the resource output. I'd suggest closing this, as there is nothing to fix. I believe your second example, with a "roles" property of the single output object in JSON is correct, and not a work-around.