How do you declare strings inside brackets in Bicep? - azure

The Bicep template for creating a virtual network has the parameter 'dnsservers'. The expected value type is a string, but it's enclosed inside square brackets. How would you set two IP addresses for it?
dhcpOptions: {
dnsServers: [
'string'
]
}
I've tried
param dnsservers string = '10.100.1.1, 10.100.1.2'
...but of course that fails, because
'10.100.1.1, 10.100.1.2' is not a valid IP address.
I should be able to set two IP addresses, but I don't know how, since "dnsservers" is a string and not an array.

See documentation, the dnsServers is an array of string:
param dnsservers array = [
'10.100.1.1'
'10.100.1.2'
]
Also when you see brackets [], it means it is an array so ['string'] means array of strings

Related

How to create a custom string using map value - Terraform

I struggle to create a custom string in Terraform when I loop through a list of map which contains a map.
The problem isn't the looping itself but creation of a custom string.
E.g
locals {
for var1 ... : [
for var2 ... : {
myString = "${var2.map["key1"]_${var2.map["key2"]}" <-- This does NOT work as im using '"'
}]
}
Aware that the example does not work as I need to use double quote " to access the map value, but also because I need to use " for the creation of the string.
You are missing } and it should be var, not var2:
myString = "${var2.map["key1"]}_${var2.map["key2"]}"

retrieve values from a list based on criterea using terraform syntax HCL

I am looking for a terraform expression to retrieve values from a list, i have a list of values
namespaces = [blue,red,green,ns-blue,ns-green,ns-grey]
I would like to retrieve in list format just the values contains "ns", as a result i must get:
namepsace-filtred = [ns-blue,ns-green,ns-grey]
thanks in advance.
Assuming you have a list of strings for a variable namespace:
variable "namespaces" {
default = ["blue", "red", "green", "ns-blue", "ns-green", "ns-grey"]
}
You can use a for with the regex function to check if a string contains a substring. Also, you have to use the can function to transform the result of the regex to a boolean:
locals {
namepsace_filtred = [for ns in var.namespaces : ns if can(regex("ns", ns))]
}
The result of this should be something like this:
namepsace_filtred = [
"ns-blue",
"ns-green",
"ns-grey",
]

How to use format for list of strings in terraform 0.12.20?

I'm creating data resource to create a policy document for allowing users to access rds, but i'm stuck on how to use format to pass account_id and rds's resource_id,
Code:
data "aws_iam_policy_document" "iam_authentication_doc" {
depends_on = [aws_db_instance.name]
statement {
effect = "Allow"
actions = [
"rds-db:connect"
]
resources = flatten([format("arn:aws:rds-db:us-east-1:${var.account_id}:dbuser:${aws_db_instance.name.resource_id}/%s", var.usernames)])
}
}
Error:
resources = flatten([format("arn:aws:rds-db:us-east-1:${var.account_id}:dbuser:${aws_db_instance.pgauth.resource_id}/%s", var.usernames)])
|----------------
| aws_db_instance.pgauth.resource_id is "db-xxxxxxxxxxxxxxxx"
| var.account_id is 8.12345678901+11
| var.usernames is list of string with 12 elements
Call to function "format" failed: unsupported value for "%s" at 75: string
required.
I tried passing
[formatlist("arn:aws:rds-db:us-east-1:%s:dbuser:%s/%s", var.account_id, aws_db_instance.pgauth.resource_id, var.amp_usernames)]
got an error
22: resources = [formatlist("arn:aws:rds-db:us-east-1:%s:dbuser:%s/%s", var.account_id, aws_db_instance.name.resource_id, var.usernames)]
|----------------
| aws_db_instance.name.resource_id is "db-xxxxxxxxxxxxxxx"
| var.account_id is "123456789012"
| var.usernames is list of string with 12 elements
Inappropriate value for attribute "resources": element 0: string required.
I want resources like
arn:aws:rds-db:us-east1:1234567890:dbuser:db-xxxxxxxxxxxxxx/foo,
arn:aws:rds-db:us-east1:1234567890:dbuser:db-xxxxxxxxxxxxxx/bar,
arn:aws:rds-db:us-east1:1234567890:dbuser:db-xxxxxxxxxxxxxx/tim
The first example with format did not work because format expects all of its arguments to be single values and it produces a single value.
As you've seen, the formatlist function is one way to solve your problem: it produces a list as its result, and if any of its arguments are lists then it repeats the formatting process once for each set of elements with the same index across the lists.
Your second example didn't work because you wrapped the call to formatlist in [ ... ], which constructs a list. Becuse formatlist returns a list itself, the result was therefore a list of lists of strings rather than just a list of strings.
We can get it working by removing the redundant brackets:
resources = formatlist("arn:aws:rds-db:us-east-1:%s:dbuser:%s/%s", var.account_id, aws_db_instance.name.resource_id, var.usernames)
Another way to write this is using a for expression, which will perhap make the repetition more explicit in your configuration:
resources = [for u in var.usernames : "arn:aws:rds-db:us-east-1:${var.account_id}:dbuser:${aws_db_instance.name.resource_id}/${u}"]
Which one is easier to understand is of course subjective: the formatlist approach shows the format string up front but it leaves it implied that we're repeating based on elements of var.usernames. The for expression approach pushes the template to the end of the line, but it makes the repetition based on var.usernames more explicit.
resources = flatten(formatlist("arn:aws:rds-db:us-east-1:%s:dbuser:%s/%s", var.account_id, aws_db_instance.pgauth.resource_id, var.usernames))
i did not specify the type for account_id.

pythonstring value vs string in django orm

this is what I want
I made the parameter by str value.
because I have to get parameters by list variable.
but when I use str parameter in filter, wrong result is comming.
whole source is here.
In the first picture, you are providing a list of strings and in the second picture, you are providing string.
You can solve it by:
import json
fieldQueryString = json.loads(fieldQueryString)
this will convert this string into a list. So the output will change from
'["001", "002", "004", "005", "006"]' # this is a string
to
["001", "002", "004", "005", "006"] # this is a list
(notice the quotes before and after [ and ]).

Groovy string comparison in Jenkins pipeline

I'm trying to compare two strings in Jenkins pipeline. The code more or less look like this:
script {
def str1 = 'test1.domainname-test.com'
def str2 = 'test1.domainname-test.com'
if ( str1 == str2 ) {
currentBuild.result = 'ABORT'
error("TENANT_NAME $TENANT_NAME.domainname-test.com is already defined in domainname-test.com record set. Please specify unique name. Exiting...")
}
}
str1 is fed by a preceeding command I skipped here due to simplicity. I am getting this error:
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: No such field found: field java.lang.String domainname
What am I doing wrong? I tried equals method too, same result. As if it stucked on those dots, thinking it's some kind of property. Thanks in advance
You're missing curly brackets surrounding the TENANT_NAME variable name. In your example:
error("TENANT_NAME $TENANT_NAME.domainname-test.com is already defined in domainname-test.com record set. Please specify unique name. Exiting...")
the $ sign gets applied to TENANT_NAME.domainname. And because TENANT_NAME is a string, Groovy interprets the following part as you were trying to access domainname property from a String class, and you get No such field found: field java.lang.String domainname exception.
To avoid such problems, wrap your variable name with {} and you will be fine.
error("TENANT_NAME ${TENANT_NAME}.domainname-test.com is already defined in domainname-test.com record set. Please specify unique name. Exiting...")

Resources