microsoft / azure-pipelines-terraform

Azure Pipelines tasks for installing Terraform and running Terraform commands in a build or release pipeline.
MIT License
101 stars 62 forks source link

Terraform Import with index #132

Closed Marquis79 closed 1 year ago

Marquis79 commented 1 year ago

Hi,

Have you tried using the terraform plugin with the import command with an index on the resource like this:

Looks like the plugin doesn't allow putting an index on the option line?

Is it a bug?

thanks

Marquis79 commented 1 year ago

More details about my tests :

commandOptions: '--var-file NPR.AZ.COMMUN.01.tfvars azurerm_route.tr-route["pr-svcpartage-commun-001"] ${{ parameters.resourceId }}' output : /usr/bin/terraform import --var-file NPR.AZ.COMMUN.01.tfvars azurerm_route.tr-route[pr-svcpartage-commun-001] ... Index brackets must contain either a literal number or a literal string.

If I add \" like this : commandOptions: '--var-file NPR.AZ.COMMUN.01.tfvars azurerm_route.tr-route[\"pr-svcpartage-commun-001\"] ${{ parameters.resourceId }}' output: /usr/bin/terraform import --var-file NPR.AZ.COMMUN.01.tfvars azurerm_route.tr-route[\pr-svcpartage-commun-001"] ... The import command expects two arguments. the first " of the index has disappeared and I dont know why

AminTi commented 1 year ago

Thanks for reporting, we will look into this and get back to you sooner.

mericstam commented 1 year ago

Hi. Have you tried to escape the quotes? like commandOptions: '--var-file myvarfile.tfvars azurerm_route_table.tr-tblroute[\"index\"]

Marquis79 commented 1 year ago

Yes I have tried like this : commandOptions: '--var-file NPR.AZ.COMMUN.01.tfvars azurerm_route.tr-route[\"pr-svcpartage-commun-001\"] ${{ parameters.resourceId }}' and the output is : usr/bin/terraform import --var-file NPR.AZ.COMMUN.01.tfvars azurerm_route.tr-route[\pr-svcpartage-commun-001"] ... the first " of the index has disappeared

mericstam commented 1 year ago

ok. that is strange. let me look around and see what I can come up with. I think the issue here is that the extension use string input using another tool lib , azure-pipelines-task-lib/task, to parse the quotes ETC.

mericstam commented 1 year ago

Ok found another reported issue on azure-pipelines-task-lib. that stated if you want literal "b" you should escape it like this "\"b\"" that would mean commandOptions: '--var-file myvarfile.tfvars azurerm_route_table.tr-tblroute["\"index\""]

Marquis79 commented 1 year ago

Nice! It works the way you wrote the index: ["\"index\""] Do you plan to correct this problem or I must thinking to write the " the way you indicated to me? Thanks!

mericstam commented 1 year ago

As this is not specific to the extension but rather behavior of the azure-pipelines-task-lib we will not fix this. I will however update the documentation on escaping quotes.