jason-johnson / azure-pipelines-tasks-terraform

Azure Pipelines extension for Terraform
MIT License
121 stars 52 forks source link

terraform output does not support object type #352

Open flcdrg opened 1 year ago

flcdrg commented 1 year ago

Current implementation only supports string, number and bool.

https://github.com/jason-johnson/azure-pipelines-tasks-terraform/blob/6c5d1b5653d5977f2eca16d54e7ea1d7bd929825/tasks/terraform-cli/src/commands/tf-output.ts#L26

Ideally object values should also be able to be extracted (probably preserving the JSON structure for the value).

brany09 commented 11 months ago

Indeed that would be very helpful. @jason-johnson are there any plans to support this?

jason-johnson commented 10 months ago

What is the use case for this @flcdrg and @brany09 ? You can't pass an object as a variable in Azure Devops, but only strings. For what you want to do, couldn't you call e.g. jsonencode in terraform to turn the object into a string which you then pass via the output?

flcdrg commented 10 months ago

I suppose you could, but it does seem a bit odd that you need to force Terraform to convert the output type for you, rather than let it stay as an object.

If I use terraform output directly (not via the extension) then the data does come through and is a JSON value that I can deal with however I like (with PowerShell or jq etc). eg.

$json = terraform output -no-color --json | ConvertFrom-Json
$database = $json.sql_server_database.value.dev

That's what I had to resort to as a workaround, but it would be nice for consistency to use the extension for this.

jason-johnson commented 10 months ago

Ok, I see in your command that you call terraform output, which produces a string that ConvertFrom-Json turns into an object. So are you wanting the terraformCli task to return this string so you can do the same steps but without having to manually call terraform output? That doesn’t work right now?

flcdrg commented 10 months ago

If it didn't filter out object type then that was what I was hoping. I'd then get an individual TFOUT env var corresponding to the Terraform output variable that I could do with what I wanted (eg. call ConvertFrom-Json or pass on to something else that was expecting JSON data).

jason-johnson commented 10 months ago

Ok, I'll have to have a look at this. The code worked this way from the earliest history I have so I'm not sure how it got this way. In Azure Devops, variables can only be strings so it could be that just trying to export it failed.