scottwinkler / terraform-provider-shell

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

JSON Node Traversal - get attribute #34

Closed scottwinkler closed 3 years ago

scottwinkler commented 4 years ago

Currently, complex JSON is flattened into an outputs map. It would be nice to also a way to easily get nested JSON values at the top level. I propose adding a new output value called get to do this (potentially deprecating the output attribute). For example, in the following JSON

{
  "last_state_loss_time": 0,
  "spark_version": "5.3.x-scala2.11",
  "azure_attributes": [8, 17, 20 ],
  "state": "PENDING",
  "enable_elastic_disk": true,
  "init_scripts_safe_mode": false,
  "num_workers": 1,
  "driver_node_type_id": "Standard_D3_v2",
  "default_tags": {
    "Creator": "lagripp@microsoft.com",
    "ClusterName": "my-cluster",
    "ClusterId": "0327-174802-howdy690",
    "Vendor": "Databricks"
  }
} 

The ClusterName attribute could be read with the following expression: shell_script.test.get["default_tags.ClusterName"].asString

the get object has the following attributes. It will try to cast a value into as many fields as possible. If it cannot set a field, it will use nil, or perhaps the default no-value if nil does not work.

get = {
  asString = string
  asNumber = number
  asBool = bool
  asList = list(string)
  asMap = map(string)
}

To get a value from a List you could do: tonumber(shell_script.test.get["azure_attributes"].asList[1])

Alternatively you can /refer to individual elements with the [] accessor. For example:

shell_script.test.get["azure_attributes.[1]"]asNumber returns the number 17

syyang-in-cloud commented 4 years ago

Hello, I am writing many of my resources/data sources using this shell provider. Thanks! I got into this issue today, and I'm glad to see this issue is created here.

scottwinkler commented 3 years ago

replaced with #88