Jinja Variable Array Dynamics - google-app-engine

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.

Related

How to use if/else statement in terraform for loop

Is it possible to do this in terraform?
for i in range(10):
if var != "" and i > 2:
# Something to do
elif var != "" and i < 2:
# Something to do
else:
# Something else to do
What i want to achieve is to create list but i need if/else statement in for loop. What i have achieved so far is:
for i in range(10):
if var != "":
# Something to do
It's hard to answer this question because it seems like you already decided on a solution to a problem but you haven't actually stated what the underlying problem is. However, I will try to answer directly what you asked, nonetheless.
When thinking about approaching problems in Terraform it's best to think from the perspective of constructing values from expressions using other values, rather than writing imperative statements to describe how to construct those. In this case, I would try to pick your problem into two parts:
Doing something different when var is set to the empty string vs. other cases.
Doing something different when constructing the first two elements of a list, but only in the non-empty string case.
The first of those sounds like a conditional expression, because you want to choose between two possible outcomes based on a condition:
locals {
example = (
var != "" ?
(expression for normal case) :
(expression for empty case)
)
}
You haven't included details about what ought to happen in the "empty case", but since you suggested you would use a loop in an imperative language I assume that your problem would best map to a for expression in Terraform.
locals {
example = (
var != "" ?
(expression for normal case) :
[ for i in range(10) : (something based on i) ]
)
}
The "normal case" has the extra detail of doing something different based on whether the index is less than two:
locals {
example = (
var != "" ?
[
for i in range(10) : (
i < 2 ?
(something based on i) :
(something else based on i)
)
]
[ for i in range(10) : (something based on i) ]
)
}
(Your original question used i < 2 and i > 2, and my answer above admittedly uses i < 2 and i >= 2 instead; I made an assumption that this interpretation was more likely, but this answer won't work if you really do need to do something special when i == 2 that's different to when it's less than or greater than.)
I can't take this answer any further without more detail about the underlying problem you're trying to solve, but since you seem to be coming from familiarity with Python I do have two general mappings for you that might help you translate from Python-ish approaches to Terraform-ish approaches:
Terraform's conditional operator c ? t : f is equivalent to Python's conditional expressions.
Terraform's for expressions are equivalent to Python's list comprehensions.
In both cases these constructs have similar capabilities but different syntax. If you feel more comfortable exploring in Python first then I'd suggest using these particular Python constructs to build your solution and then you should be able to translate the result to an equivalent Terraform expression.
Terraform is (sadly) not a programming language, so it's not easy (or possible) to convert any pseudo-code to HCL.
Anyway, for your specific case you need to combine two things:
the equivalent of i of your pseudo-code in Terraform would be: count.index (in case you use count)
the if-else-else is probably something like:
(local.a != "" && count.index > 2) ? "option 1" : ((local.a != "" && count.index < 2) ? "option 2" : "option 3" )
(not tested)
Also it might come across as nit-picking, but a tiny remark is "Something to do" in Terraform you rather not declare things to be done (as it's not a procedural programming language), but rather desired state of things.

Want to pass in a string array into a WS.sendRequest using groovy

I am new to API testing and am using Katalon to develop the tests. I've Googled any question I could think of and couldn't find anything to answer my question.
We have an API with the following Body
[
"${idValue1}",
"${idValue2}",
"${idValue3}",
"${idValue4}",
"${idValue5}",
]
I believe the purpose of this one is to delete multiple records at once by id. The script that we have in the step definition is
response = WS.sendRequest(findTestObject('EquipmentAPI/data-objects/DELETE Equipment By Ids', [('host') : GlobalVariable.host, ('idValues') : GlobalVariable.equipId]))
GlobalVariable.equipId = WS.getElementPropertyValue(response, 'data[0].id')
There are other step definitions that run before this one to set the Global Variables for use. I was able to generate the string array without issue.
Is this something that's possible? Please help!
Please let me know if further information is needed. Thanks.

Rails update remove number from an array attribute?

Is there a way to remove a number from an attibute array in an update? For example, if I want to update all of an alchy's booze stashes if he runs out of a particular type of booze:
Alchy has_many :stashes
Stash.available_booze_types = [] (filled with booze.ids)
Booze is also a class
#booze.id = 7
if #booze.is_all_gone
#alchy.stashes.update(available_booze_types: "remove #booze.id")
end
update: #booze.id may or may not be present in the available_booze_types array
... so if #booze.id was in any of the Alchy.stash instances (in the available_booze_types attribute array), it would be removed.
I think you can do what you want in the following way:
if #booze.is_all_gone
#alchy.stashes.each do |stash|
stash.available_booze_types.delete(#booze.id)
end
end
However, it looks to me like there are better ways to do what you are trying to do. Rails gives you something like that array by using relations. Also, the data in the array will be lost if you reset the app (if as I understand available_booze_types is an attribute which is not stored in a database). If your application is correctly set up (an stash has many boozes), an scope like the following in Stash class seems to me like the correct approach:
scope :available_boozes, -> { joins(:boozes).where("number > ?", 0) }
You can use it in the following way:
#alchy.stashes.available_boozes
which would only return the ones that are available.

Package-qualified names. Differences (if any) between Package::<&var> vs &Package::var?

Reading through https://docs.perl6.org/language/packages#Package-qualified_names it outlines qualifying package variables with this syntax:
Foo::Bar::<$quux>; #..as an alternative to Foo::Bar::quux;
For reference the package structure used as the example in the document is:
class Foo {
sub zape () { say "zipi" }
class Bar {
method baz () { return 'Þor is mighty' }
our &zape = { "zipi" }; #this is the variable I want to resolve
our $quux = 42;
}
}
The same page states this style of qualification doesn't work to access &zape in the Foo::Bar package listed above:
(This does not work with the &zape variable)
Yet, if I try:
Foo::Bar::<&zape>; # instead of &Foo::Bar::zape;
it is resolves just fine.
Have I misinterpreted the document or completely missed the point being made? What would be the logic behind it 'not working' with code reference variables vs a scalar for example?
I'm not aware of differences, but Foo::Bar::<&zape> can also be modified to use {} instead of <>, which then can be used with something other than literals, like this:
my $name = '&zape';
Foo::Bar::{$name}()
or
my $name = 'zape';
&Foo::Bar::{$name}()
JJ and Moritz have provided useful answers.
This nanswer is a whole nother ball of wax. I've written and discarded several nanswers to your question over the last few days. None have been very useful. I'm not sure this is either but I've decided I've finally got a first version of something worth publishing, regardless of its current usefulness.
In this first installment my nanswer is just a series of observations and questions. I also hope to add an explanation of my observations based on what I glean from spelunking the compiler's code to understand what we see. (For now I've just written up the start of that process as the second half of this nanswer.)
Differences (if any) between Package::<&var> vs &Package::var?
They're fundamentally different syntax. They're not fully interchangeable in where you can write them. They result in different evaluations. Their result can be different things.
Let's step thru lots of variations drawing out the differences.
say Package::<&var>; # compile-time error: Undeclared name: Package
So, forget the ::<...> bit for a moment. P6 is looking at that Package bit and demanding that it be an already declared name. That seems simple enough.
say &Package::var; # (Any)
Quite a difference! For some reason, for this second syntax, P6 has no problem with those two arbitrary names (Package and var) not having been declared. Who knows what it's doing with the &. And why is it (Any) and not (Callable) or Nil?
Let's try declaring these things. First:
my Package::<&var> = { 42 } # compile-time error: Type 'Package' is not declared
OK. But if we declare Package things don't really improve:
package Package {}
my Package::<&var> = { 42 } # compile-time error: Malformed my
OK, start with a clean slate again, without the package declaration. What about the other syntax?:
my &Package::var = { 42 }
Yay. P6 accepts this code. Now, for the next few lines we'll assume the declaration above. What about:
say &Package::var(); # 42
\o/ So can we use the other syntax?:
say Package::<&var>(); # compile-time error: Undeclared name: Package
Nope. It seems like the my didn't declare a Package with a &var in it. Maybe it declared a &Package::var, where the :: just happens to be part of the name but isn't about packages? P6 supports a bunch of "pseudo" packages. One of them is LEXICAL:
say LEXICAL::; # PseudoStash.new(... &Package::var => (Callable) ...
Bingo. Or is it?
say LEXICAL::<&Package::var>(); # Cannot invoke this object
# (REPR: Uninstantiable; Callable)
What happened to our { 42 }?
Hmm. Let's start from a clean slate and create &Package::var in a completely different way:
package Package { our sub var { 99 } }
say &Package::var(); # 99
say Package::<&var>(); # 99
Wow. Now, assuming those lines above and trying to add more:
my Package::<&var> = { 42 } # Compile-time error: Malformed my
That was to be expected given our previous attempt above. What about:
my &Package::var = { 42 } # Cannot modify an immutable Sub (&var)
Is it all making sense now? ;)
Spelunking the compiler code, checking the grammar
1 I spent a long time trying to work out what the deal really is before looking at the source code of the Rakudo compiler. This is a footnote covering my initial compiler spelunking. I hope to continue it tomorrow and turn this nanswer into an answer this weekend.
The good news is it's just P6 code -- most of Rakudo is written in P6.
The bad news is knowing where to look. You might see the doc directory and then the compiler overview. But then you'll notice the overview doc has barely been touched since 2010! Don't bother. Perhaps Andrew Shitov's "internals" posts will help orient you? Moving on...
In this case what I am interested in is understanding the precise nature of the Package::<&var> and &Package::var forms of syntax. When I type "syntax" into GH's repo search field the second file listed is the Perl 6 Grammar. Bingo.
Now comes the ugly news. The Perl 6 Grammar file is 6K LOC and looks super intimidating. But I find it all makes sense when I keep my cool.
Next, I'm wondering what to search for on the page. :: nets 600+ matches. Hmm. ::< is just 1, but it is in an error message. But in what? In token morename. Looking at that I can see it's likely not relevant. But the '::' near the start of the token is just the ticket. Searching the page for '::' yields 10 matches. The first 4 (from the start of the file) are more error messages. The next two are in the above morename token. 4 matches left.
The next one appears a quarter way thru token term:sym<name>. A "name". .oO ( Undeclared name: Package So maybe this is relevant? )
Next, token typename. A "typename". .oO ( Type 'Package' is not declared So maybe this is relevant too? )
token methodop. Definitely not relevant.
Finally token infix:sym<?? !!>. Nope.
There are no differences between Package::<&var> and &Package::var.
package Foo { our $var = "Bar" };
say $Foo::var === Foo::<$var>; # OUTPUT: «True␤»
Ditto for subs (of course):
package Foo { our &zape = { "Bar" } };
say &Foo::zape === Foo::<&zape>;# OUTPUT: «True␤»
What the documentation (somewhat confusingly) is trying to say is that package-scope variables can only be accessed if declared using our. There are two zapes, one of them has got lexical scope (subs get lexical scope by default), so you can't access that one. I have raised this issue in the doc repo and will try to fix it as soon as possible.

how do i extract a value from a lua array?

I'm debugging someone else's application and I've run across a data structure that, when dumped to a file, looks like this:
["value"] = {}
["value"]["0.ouname"] = {}
["value"]["0.ouname"]["label"] = "Test value"
["value"]["0.ouname"]["seq"] = 90
["value"]["0.ouname"]["type"] = "text"
["value"]["0.ouname"]["value"] = ""
["value"]["1.ouname"] = {}
["value"]["localityName"]["value"] = "California"
I need to be able to extract the seq number "90" from it but I'm at a loss as to how.
I am able to get at the "California" value by doing the following:
print(myvar.value.localityName.value)
However, I can't seem to get the sequence number
So far, I've tried the following:
print(myvar.value.0.ouname.seq)
print(myvar.value.["0.ouname"].seq)
print(myvar.value."0.ouname".seq)
But I haven't been successful!
If you have any suggestions, I'd appreciate it.
In Lua, a.b is syntactic sugar for a["b"]. Of course, you can use it only when it's possible, and the resulting expression won't mean something else (as in your example).
Thus, you have to use
print(myvar.value["0.ouname"].seq)
You could also drop all dots and use only [], as such:
print(myvar["value"]["0.ouname"]["seq"])
Which is exactly the same format that has been written to file.
print(myvar.value['0.ouname'].seq)

Resources