Skip to content
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

schematics_output data source formats lists incorrectly #1413

Closed
stevestrutt opened this issue May 6, 2020 · 3 comments
Closed

schematics_output data source formats lists incorrectly #1413

stevestrutt opened this issue May 6, 2020 · 3 comments

Comments

@stevestrutt
Copy link
Contributor

ibm_schematics_output.output_values incorrectly returns the outputs of a workspace in the internal golang representation of a Terraform map(list) object. This makes it not possible to access the output values for outputs that were lists in the original workspace by using the built in Terraform variable functions. 'output_values' is returned as a list of map(strings), whereas for list outputs it should be list of map(list).

Flattening the list in this fashion stops output values from one workspace being imported and used in another workspace. Which is the purpose of the schematics_output datasource.

This is with TF0.12 and provider 1.5.1.

data.ibm_schematics_output.output_values returns:

"output_values" = {
    "backend_server_host_ip_addresses.#" = "1"
    "backend_server_host_ip_addresses.0.#" = "1"
    "backend_server_host_ip_addresses.0.0" = "172.17.0.4"
    "bastion_host_ip_addresses.#" = "1"
    "bastion_host_ip_addresses.0" = "52.116.132.36"
    "frontend_server_host_ip_addresses.#" = "1"
    "frontend_server_host_ip_addresses.0.#" = "2"
    "frontend_server_host_ip_addresses.0.0" = "172.16.0.5"
    "frontend_server_host_ip_addresses.0.1" = "172.16.2.5"
  }

Each list has been flattened into multiple key value pairs. There is no function in Terraform to convert this back to a list to be used as variable input for the workspace.

The original workspace output was:

 2020/05/04 14:58:56 Terraform show | Outputs:
 2020/05/04 14:58:56 Terraform show | 
 2020/05/04 14:58:56 Terraform show | backend_server_host_ip_addresses = [
 2020/05/04 14:58:56 Terraform show |     [
 2020/05/04 14:58:56 Terraform show |         "172.17.0.4",
 2020/05/04 14:58:56 Terraform show |     ],
 2020/05/04 14:58:56 Terraform show | ]
 2020/05/04 14:58:56 Terraform show | bastion_host_ip_addresses = [
 2020/05/04 14:58:56 Terraform show |     "52.116.132.36",
 2020/05/04 14:58:56 Terraform show | ]
 2020/05/04 14:58:56 Terraform show | frontend_server_host_ip_addresses = [
 2020/05/04 14:58:56 Terraform show |     [
 2020/05/04 14:58:56 Terraform show |         "172.16.0.5",
 2020/05/04 14:58:56 Terraform show |         "172.16.2.5",
 2020/05/04 14:58:56 Terraform show |     ],
 2020/05/04 14:58:56 Terraform show | ] 

the result of data.ibm_schematics_output.output_values should be to allow it to be accessed as a Terraform map object :

"output_values" = {
    "backend_server_host_ip_addresses = [ [  "172.17.0.4" ] ] 
    "bastion_host_ip_addresses" = ["52.116.132.36"]
    "frontend_server_host_ip_addresses = [ [  "172.16.0.5", "172.16.2.5" ] ] 
  }
@stevestrutt
Copy link
Contributor Author

The usage of ibm_schematics_output.output_values should be equivalent to the implementation of remote_state data source in Terraform where the outputs of a module can be consumed by other TF resources. This is highlighted in section https://www.terraform.io/docs/configuration/outputs.html copied below. The expected usage is that root module outputs can be consumed in the same way as the outputs of folder level modules.

When using remote state, root module outputs can be accessed by other configurations via a terraform_remote_state data source.

@en-ree
Copy link

en-ree commented Jun 15, 2020

I was running into a similar issue and found a workaround for this:

My problem was that a map I wanted to re-use was also flattened and had their keys kind of merged with the keys of the output_values map. The result looked like this:

[...]
Outputs:

test = {
"output_var1" = 1
"output_var2" = 2
[...]
"resource_groups.a" = "true"
"resource_groups.b" = "true"
"resource_groups.c" = "true"
"resource_groups.d" = "true"
"resource_groups.e" = "true"
[...]

My workaround here was to use jsonencode() on the map (which in my case was called resource_groups in the source module and jsondecode() in the module whish uses ibm_schematics_output to access the output:

Output of the source module:

output "resource_groups" {
        value = jsonencode(local.myvar.resource_groups)
}

main.tf of the calling module:

locals {
        resource_groups  = jsondecode(data.ibm_schematics_output.config_outputs.output_values.resource_groups)
}

This works for maps and should also for lists and complex objects IMO. The only thing which might be a problem is when jsondecode() does not parse what you expect.

Hope this helps

@hkantare
Copy link
Collaborator

We don't have any dynamic datatypes in Terraform we introduced a new attribute "output_json" which return's the complete json as string. Now user can use jsondecode and jsondecode functions to work with json output
https://cloud.ibm.com/docs/terraform?topic=terraform-schematics-data-sources#schematics-output-output

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants