Terraform Import strips quotations from index - terraform

I'm trying to import state from Azure via Terraform and save the state to a resource with indexes, such as:
terraform import azurerm_subnet.test_subnets[\"test-subnet-1\"] /subscriptions/xxxxxx-xxx-xxx-xxxxx/resourceGroups/test-rg/providers/Microsoft.Network/virtualNetworks/test-vnet/subnets/test-subnet-1
I read other threads that say I have to escape the quotation marks, so I did. Even though the quotes are escaped, it seems as though the Terraform compiler is still pulling the quotations out and leaving the \ characters in place.
Here's the error output that I'm seeing:
Error: Invalid character
on <import-address> line 1:
1: azurerm_subnet.test_subnets[\test-subnet-1\]
This character is not used within the language.
Error: Index value required
on <import-address> line 1:
1: azurerm_subnet.test_subnets[\test-subnet-1\]
Index brackets must contain either a literal number or a literal string.
Error: Invalid character
on <import-address> line 1:
1: azurerm_subnet.test_subnets[\test-subnet-1\]
This character is not used within the language.
For information on valid syntax, see:
https://www.terraform.io/docs/internals/resource-addressing.html
Any thoughts here? Am I doing something wrong or is this a known limitation of the application?

In PowerShell, the import command should be like this:
terraform import 'azurerm_subnet.test_subnets[\"test-subnet-1\"]' subnet_resource_id
The screenshot here shows the output:
But if you use the cmd in windows, the command you use is also right. See more details about command import.

Related

Reading coordinate data

I am trying to read coordinate into Databricks as follows:
00°00'0.00"N
However, when read the data, I get the following output:
00?00'0.00"N
It returns back a question mark.
I have tried to replace the value, but this did not work as I received the following error:
Dangling meta character '?' near index 0
? is a special character in regex and you probably tried to replace the ? using regexp_replace, which caused an error. You can instead try replace which interprets the string to be replaced as it is, not as a regex.
df2 = df.withColumn('col1', F.expr("replace(col1, '?', '°')"))

Can I escape curly bracket symbol in terraform?

I'm using the terraform template_file data resource to create a file that should be written to the dynamically-created EC2 instance when I apply the stack. In other words, I want this file to be created in the home folder of the newly-created EC2 instance. However, this file contains curly bracket syntax ${}, which terraform is trying to interpolate. How can I escape these curly brackets?
As background, I'm using the cloud-config syntax to write these files.
Ex:
${username} should be written to the file, not interpolated in terraform.
Even when I use the double dollar sign $$, terraform still fails because it can't find the variable:
... failed to render : <template_file>:105,18-26: Unknown variable; There is no variable named "username".
Terraform uses a fairly unique escape for curly braces:
Sequence
Replacement
$${
Literal ${, without beginning an interpolation sequence.
%%{
Literal %{, without beginning a template directive sequence.
Documentation for reference: https://www.terraform.io/docs/language/expressions/strings.html
FYI I ended up working around this by writing the template in another file, then reading it into the terraform stack using the file method:
data "template_file" "config" {
template = "${file("./user_data.tpl")}"
}
For example if you would like to add into Tags {{_payload.ref}} as it was in my case, I have done it this way:
tags = ["CRITICAL", "\u007B\u007B_payload.ref\u007D\u007D"]
so
\uNNNN Unicode character from the basic multilingual plane (NNNN is four hex digits)
ref.page: https://www.terraform.io/language/expressions/strings

How to interpolate expressions in Terraform?

I'm trying to use the keys expression in Terraform to grab a list of keys (from a map variable) and assign it to a local variable. Here is the code snippet:
locals {
project_name_list = keys(${var.project_map})
}
However, I'm getting the following error:
Unknown token: 29:22 IDENT keys
Am I missing something here. Nowhere can I find an example of this expression. As bad as it is, even the official documentation does not help -https://www.terraform.io/docs/configuration/functions/keys.html
HashiCorp has really done a bad job of elaborating the nuances of Terraform for beginners on their website.
Terraform functions need to be wrapped in expression syntax to show that it's not a literal value: "${}"
So try this: project_name_list = "${keys(var.project_map)}"
The example in the documentation is written as though being run from the terraform command line, which already assumes the command is a HCL expression and doesn't require that syntax.
UPDATE
I said above that the expression syntax is to show that it's not a literal value. It's probably more accurate to speak of it as expression syntax vs. configuration syntax. Configuration syntax is the first level of interpolation, which forms the basic structure of your terraform file with resource blocks, data blocks, etc. The second interpolation level is expression syntax which is used to generate values used by your configuration.
Thinking of it in these terms makes better sense of the error message, Unknown token, because terraform is attempting to read it as a configuration key word.
I had compared it to a literal value because it's in the same position as where a literal value would be.

How to test if a string variable in Robot Framework is empty?

How to test if a string variable in Robot Framework is empty?
My first naïve attempt looked like this:
Run Keyword If ${myVar}!=${EMPTY}
but it failed:
Evaluating expression '!=' failed: SyntaxError: unexpected EOF while parsing (, line 1)
I then found this issue at Github but it didn't suggest a solution, just that the error message was unclear. An alternative solution was presented here:
${length}= Get Length ${Portfolio_ste}
Run Keyword If ${length} Go To Edit Portfolio
but is this really the best practice?
(The context is that I use a variable argument list and if a certain variable contains a value something should be done, otherwise just ignore it)
The expression needs to be a valid python expression after variable substitution. Assuming for the moment that myVar might be something like the number 42, your expression would end up looking like this after substitution:
Run Keyword if 42!=
When comparing against the empty string you need to add quotes to guarantee that the expression is a proper python expression after substitution. For example:
Run Keyword If "${myVar}"!="${EMPTY}"
Try Get Variable Value. It solved my problem.

How to match line & error in Sublime Text 3?

How can I match this error in the build with regex to locate line and file with result_line_regex & result_file_regex?
project4.dpr(9) Hint: H2164 Variable 'I' is declared but never used in 'Project3'
I have tried this but it won't work.
"result_file_regex": "^.*\\(.*)/.?(.*)$",
"result_line_regex": "^([^\\]*)\.(\w+)$",
As already mentioned in the comments, file_regex is the setting that gets passed to result_line_regex (have a look at the run() method signature of class ExecCommand in Packages/Default/exec.py).
A good regex in your case would be ^([\w-]+\.\w+)\((\d+)\). The first group captures something like my-file.ext and the second one the digit(s) in parentheses.
In order to set that expression in a string in the json file you need to escape each backslash with another backslash (\ is the escape character in strings), so it becomes:
"file_regex": "^([\\w-]+\\.\\w+)\\((\\d+)\\)"
Notice that the matched file has to be in the path of the file that is active when triggering the build system. If you want it to be relative to a certain path no matter where you trigger the build, you can also pass a working directory like:
"working_dir": "/path/to/my/source"
This will be set as result_base_dir in the output view.

Resources