I've started using Google Cloud Deployment recently, having come from AWS CloudFormation (and Terraform alongside) and I'm really struggling to use Jinja in dynamic ways that I do so simplistically in the others.
My current issue is that my deployment is entirely dynamic based on some user input, and so in AWS CF and Terraform (which points at both AWS and GCP) I use maps to get settings determined by a previous choice. The following is an example from Terraform:
variable "Cluster_Instance_Map" {
type = map
default = {
"C1" = "Single-Node : 0 : A : B"
"C2" = "Multi-Node : 2 : Q : R"
"C3" = "Multi-Node : 4 : X : Y"
"C4" = "Multi-Node : 8 : S : T"
...
}
}
And then I would, for example, grab the first value for the respective row by using the Cluster_Config_Choice variable chosen from 'C1, C2, C3, C4' by a user previously as follows:
split ( " : ", var.Cluster_Instance_Map[ var.Cluster_Config_Choice ] ) [0]
Thus far, I've really struggled to re-create this type of variable in Jinja for GCP. I'm new to GCP in general, but also Jinja, and what I find online is, for the most part, confusing me more than anything, and so any help with this is muchly appreciated!
--- Edit ---
As per request, I will give some detail into what I've done with Jinja thus far, although it sadly isn't too much. My initial idea is via the use of LIST and SPLIT. I figure I could do something like the following:
{% set list_example = ({ "A" : "1 ; 2 ; 3", "B" : "4 ; 5 ; 6" }) %}
{{ list_example [ user_input_variable ].split(';')[1] }}
And the second line would then return, "5" if the user selected B, for example. I did make that code up, though (second line) so it doesn't work for syntax errors (100% expected) but I don't know if it's even close.
Is a LIST and SPLIT the way to go? Or are there MAP-like functions available that I am missing out on..
I also don't know how to put my SET function across multiple lines without erroring, so sorry for the mess above. Though I assume Google can tell me that when I'm not busy! >.>
Hope this helps clarify things.
After going around in circles, and learning very minimal in the process, I've actually realised that my answer was what I had already tried... The code I posted above saying doesn't work, works - Though it turns out there were a multitude of other reasons it didn't, but not related directly to itself.
And so, my answer to re-create what I have as a 'map' in Terraform/AWS CF is as follows:
{% set Cluster_Instance_Map = ({
"C1" : "Single-Node : 0 : A : B",
"C2" : "Multi-Node : 2 : Q : R",
"C3" : "Multi-Node : 4 : X : Y",
"C4" : "Multi-Node : 8 : S : T",
...
}) %}
{% set user_input_variable = "C3" %}
{{ Cluster_Instance_Map [ user_input_variable ].split(':')[1] }}
And the final piece would return the number 4, in that case. My code is also across multiple lines, so it's much easier to read now as well - This also was an issue unrelated (as mentioned, I'm entirely new to Google and Jinja, so lessons learned).
I must say, though, the documentation for Google Cloud really is terrible in comparison to others. Very easy to loose yourself. So even though the above doesn't really need an answer, figure best to put this here just in case others have a similar question.