Iterate through multiple lists using locals in Terraform code - foreach

I have the following list of Resource Group names and Virtual Machine Names, I will like to create a map using locals so I can iterate through it using each.key and each.value in my code.
VARIABLES:
variable "vm_names" {
default = [
"vm1",
"vm2",
"vm3",
"vm4",
"vm5"
]
variable "rg_names" {
default = [
"rg1",
"rg2",
"rg3",
"rg4",
"rg5"
]
LOCALS:
locals {
vm = var.vm_names
rg = var.rg_names
vmrg=[for k,v in zipmap(local.vm, local.rg):{
vm = k
rg = v
}]
}
RESULT:
+ vmrg = [
+ {
+ rg = "rg1"
+ vm = "vm1"
},
+ {
+ rg = "rg2"
+ vm = "vm2"
},
+ {
+ rg = "rg3"
+ vm = "vm3"
},
+ {
+ rg = "rg4"
+ vm = "vm4"
},
+ {
+ rg = "rg5"
+ vm = "vm5"
},
]
DESIRED RESULT:
vmrg = {
vm1 = "rg1"
vm2 = "rg2"
vm3 = "rg3"
vm4 = "rg4"
vm5 = "rg5"
}

Actually, it is much simpler and you were already close to figuring it out. The only thing you need is the zipmap built-in function:
vmrg = zipmap(local.vm, local.rg)
will yield:
> local.vmrg
{
"vm1" = "rg1"
"vm2" = "rg2"
"vm3" = "rg3"
"vm4" = "rg4"
"vm5" = "rg5"
}

Related

Eager print in Nix in the repl

The documentation flake-utils has the following example as a doc
eachSystem [ system.x86_64-linux ] (system: { hello = 42; })
# => { hello = { x86_64-linux = 42; }; }
eachSystem allSystems (system: { hello = 42; })
# => {
hello.aarch64-darwin = 42,
hello.aarch64-genode = 42,
hello.aarch64-linux = 42,
...
hello.x86_64-redox = 42,
hello.x86_64-solaris = 42,
hello.x86_64-windows = 42
}
as far as I can tel, one has to
> nix repl
nix-repl> e = builtins.getFlake("github:numtide/flake-utils")
nix-repl> with e.outputs.lib;
eachSystem [ system.x86_64-linux ] (system: { hello = 42; })
to get a result value (one can also do :a e.outputs.lib to "Add attributes from resulting set to scope" and not use the with ..; line )
{ hello = { ... }; }
Is there a way to "eagerly print" the value ?
What you are looking for is :p:
> { a = 3; b = [ 1 2 3 ]; }
{ a = 3; b = [ ... ]; }
> :p { a = 3; b = [ 1 2 3 ]; }
{ a = 3; b = [ 1 2 3 ]; }
>

nix function to merge attributes / records recursively and concatenate arrays

Does someone know such function that merges list of records
if all values to merge are records - merge them recursively
if all values to merge are arrays - concatenate arrays
If values can't be merged - the latter value is preferred
Example 1:
recursiveMergeAttrs [
{ a = "x"; c = "m"; list = [1]; }
{ a = "y"; b = "z"; list = [2]; }
]
returns
{ a = "y"; b = "z"; c="m"; list = [1 2] }
Example 2
recursiveMergeAttrs [
{
boot.loader.grub.enable = true;
boot.loader.grub.device = "/dev/hda";
}
{
boot.loader.grub.device = "";
}
]
returns
{
boot.loader.grub.enable = true;
boot.loader.grub.device = "";
}
P.S.
recursiveUpdate is not working
recursiveMergeAttrs = listOfAttrsets: lib.fold (attrset: acc: lib.recursiveUpdate attrset acc) {} listOfAttrsets
recursiveMergeAttrs [ { a = "x"; c = "m"; list = [1]; } { a = "y"; b = "z"; list = [2]; } ]
returns
{ a = "y"; b = "z"; c = "m"; list = [ 2 ]; }
Did it
{ lib, ... }:
with lib;
/*
Merges list of records, concatenates arrays, if two values can't be merged - the latter is preferred
Example 1:
recursiveMerge [
{ a = "x"; c = "m"; list = [1]; }
{ a = "y"; b = "z"; list = [2]; }
]
returns
{ a = "y"; b = "z"; c="m"; list = [1 2] }
Example 2:
recursiveMerge [
{
a.a = [1];
a.b = 1;
a.c = [1 1];
boot.loader.grub.enable = true;
boot.loader.grub.device = "/dev/hda";
}
{
a.a = [2];
a.b = 2;
a.c = [1 2];
boot.loader.grub.device = "";
}
]
returns
{
a = {
a = [ 1 2 ];
b = 2;
c = [ 1 2 ];
};
boot = {
loader = {
grub = {
device = "";
enable = true;
};
};
};
}
*/
let
recursiveMerge = attrList:
let f = attrPath:
zipAttrsWith (n: values:
if tail values == []
then head values
else if all isList values
then unique (concatLists values)
else if all isAttrs values
then f (attrPath ++ [n]) values
else last values
);
in f [] attrList;
in
recursiveMerge

QueryBuilder update with concat

I have the following raw SQL query:
UPDATE mySock s1
LEFT JOIN mySock s2
ON s1.parentId = s2.id
SET
s1.status = 1
s1.mylevel = (s2.mylevel + 1),
s1.parentString = CONCAT(s2.parentString, ':' CONCT(s1.id as char))
WHERE
s1.zz = 0;
and in create in Symfony 3.2
public function updateParentNew($idParent)
{
return $this->createQueryBuilder('s1')
->update('MyBundle:Stock', 's1')
->leftJoin(''MyBundle:Stock', 's2', 'WITH', 's2.id = s1.parentId')
->set('s1.zz', 1)
->set('s1.leveltask', 's2.leveltask + 1')
->set('s1.parentString', '?par2_string')
->where('s1.zz = 0')
->andWhere('s1.parentId = ?par1')
->setParameter('par1', $idParent)
->setParameter('par2_string', s2.parentString + ':' + (string)s1.id)
->getQuery()
->getSingleScalarResult();
}
It doesn't work. What is the way to Concat values (string and number)?
I would try something like this:
public function updateParentNew($idParent)
{
$qb = $this->createQueryBuilder('s1')
->update('MyBundle:Stock', 's1')
->leftJoin('MyBundle:Stock', 's2', 'WITH', 's2.id = s1.parentId')
;
$qb->set('s1.zz', 1)
->set('s1.leveltask', $qb->expr()->sum('s2.leveltask', 1))
->set('s1.parentString',
$qb->expr()->concat(
$qb->expr()->literal('s2.parentString'),
$qb->expr()->concat(':', $qb->expr()->literal('s1.id'))
))
->where('s1.zz = 0')
->andWhere('s1.parentId = :par1')
->setParameter('par1', $idParent)
->getQuery()
->getSingleScalarResult()
;
return $qb;
}

Computercraft: Can a coroutine do infinite loops?

I'm trying to do a software which can handle other programs, to start a program I use os.run(...) inside a coroutine,
Here is my code:
--VARS
local os_ver = "0.1"
local w,h = term.getSize() --Terminal size
local bar_b_color, bar_f_color --Background and foreground colors (bar)
local menu_b_color, menu_f_color --Background and foreground colors (menu)
local bg_image, bg_color --Background image, color
local root_dir = "/MaOS/"
local sys_dir = root_dir .. "sys/"
local usr_dir = root_dir .. "usr/"
local start_txt = "START" --The text printed in start button
local menus_index --Gui's index of menus
local prog_name --Name of the sel. program
local prog_path --Path of the sel. program
local win, process
--LOAD APIS
os.loadAPI(sys_dir .. "apis/flib")
os.loadAPI(sys_dir .. "apis/stdlib")
os.loadAPI(sys_dir .. "apis/gui")
--BASE FUNCS
function init() --Loads settings
menu_b_color = tonumber(flib.getSetting(sys_dir .. "conf", "Background Menu Color"))
menu_f_color = tonumber(flib.getSetting(sys_dir .. "conf", "Foreground Menu Color"))
bar_b_color = tonumber(flib.getSetting(sys_dir .. "conf", "Background Bar Color"))
bar_f_color = tonumber(flib.getSetting(sys_dir .. "conf", "Foreground Bar Color"))
bg_image = paintutils.loadImage(flib.getSetting(sys_dir .. "conf", "Background Image Path"))
bg_color = tonumber(flib.getSetting(sys_dir .. "conf", "Background Color"))
end
--PRE-MAIN
init()
--TABLES
--Menu: [1] -> Menu informations
local menus = {
[1] = { --Start menu
[1] = {is_shown = false; parent=0; x = 1; xx = 11; y = 1; yy = 6; b_color = menu_b_color; f_color = menu_f_color};
[2] = {txt = " "; cmd = function() end};
[3] = {txt = " PROGRAMS >"; cmd = function()
loadPrograms()
gui.showMenu(menus_index, 3)
end};
[4] = {txt = " CUSTOMIZE "; cmd = function() launchShell("CUSTOMIZE", "/rom/programs/edit", sys_dir .. "conf") end};
[5] = {txt = " SHELL "; cmd = function() launchShell("SHELL", "/rom/programs/shell") end};
[6] = {txt = " EXIT >"; cmd = function() gui.showMenu(menus_index, 2) end};
[7] = {txt = " "; cmd = function()
win = window.create(term.current(),15,5,20,10, true)
process = coroutine.create(function() os.run({term = win}, sys_dir .. "programs/Ice-Browser") end)
coroutine.resume(process)
end}
};
[2] = { --Exit menu
[1] = {is_shown = false; parent=1; x = 12; xx = 23; y = 1; yy = 6; b_color = menu_b_color; f_color = menu_f_color};
[2] = {txt = " "; cmd = function() end};
[3] = {txt = " SHUTDOWN "; cmd = function() os.shutdown() end};
[4] = {txt = " REBOOT "; cmd = function() os.reboot() end};
[5] = {txt = " "; cmd = function() end}
};
[3] = { --Programs menus
[1] = {is_shown = false; parent=1; x = 12; xx = 25; y = 1; yy = 0; b_color = menu_b_color; f_color = menu_f_color};
[2] = {txt = " "; cmd = function() end};
};
[4] = { --Args menu
[1] = {is_shown = false; parent=3; x = 26; xx = 38; y = 1; yy = 5; b_color = menu_b_color; f_color = menu_f_color};
[2] = {txt = " "; cmd = function() end};
[3] = {txt = " RUN "; cmd = function() launchShell(prog_name, prog_path, nil) end};
[4] = {txt = " RUN WITH ARGS "; cmd = function() end};
[5] = {txt = " "; cmd = function() end};
}
}
--FUNCS
function loadPrograms() --Load programs
stdlib.clearTable(gui.menus[menus_index][3], 2)
local programs = flib.getFiles(sys_dir .. "programs/")
for _, program in pairs(programs) do
name = " " .. program
path = sys_dir .. "programs/" .. program
if string.len(name) > (menus[3][1].xx-menus[3][1].x) then
name = string.sub(name, 1, string.len(name)-4) .."... "
elseif string.len(name)-1 < (menus[3][1].xx-menus[3][1].x) then
for i=string.len(name),(menus[3][1].xx-menus[3][1].x),1 do
name = name .. " "
end
end
name = string.upper(name)
table.insert(gui.menus[menus_index][3], {txt = name; cmd = function() end})
end
table.insert(gui.menus[menus_index][3], {txt = " "; cmd = function() end})
gui.menus[menus_index][3][1].yy = table.getn(programs)+1
end
function drawScr() --Draw screen
--Draw bg
term.setBackgroundColor(bg_color)
term.clear()
paintutils.drawImage(bg_image, 1, 2)
--Draw bar
term.setBackgroundColor(bar_b_color)
term.setTextColor(bar_f_color)
term.setCursorPos(1, 1)
term.clearLine()
term.setCursorPos(w-#os_ver, 1)
print(os_ver)
term.setCursorPos(2, 1)
print(start_txt)
--Draw menus
gui.drawAllShownMenus()
if win ~= nil then win.redraw() end
end
function launchShell(title, path, arg)
local shell_tab = multishell.launch({shell = shell}, path, arg)
multishell.setTitle(shell_tab, title)
multishell.setFocus(shell_tab)
return shell_tab
end
--MAIN
multishell.setTitle(multishell.getCurrent(), "MaOS") --Set title of this shell
init()
menus_index = gui.loadMenuGroup(menus) --Load menus into gui
while true do
drawScr()
gui.handleMenuClick(2, 2+string.len(start_txt), 1, 1)
end
When I try to run "Ice-Browser", I immediately get the status, of the process, "suspended", that happens after drawning program.
The problem happens not when I don't use coroutines, that brings me to think, the problem is the coroutine.
My question is "Can a coroutine do infinite loops (while)"?
Thanks in advance
Ok, I resolved my problem, I didn't saw that the function os.pullEvent(...) yields the coroutine, thanks for the support

More efficient way to execute this jquery script

Is there a more efficient way to execute the following jquery script? I need to access the 4 individual variables once the script has run, which I will then send to my database using ajax
var column_1 = $('#column-1').sortable("toArray");
for ( var i = 0, n = column_1.length; i < n; i++ ) {
var v = $('#' + column_1[i] ).find('.inner').is(':visible');
column_1[i] = column_1[i] + ":" + v;
}
var column_2 = $('#column-2').sortable("toArray");
for ( var i = 0, n = column_2.length; i < n; i++ ) {
var v = $('#' + column_2[i] ).find('.inner').is(':visible');
column_2[i] = column_2[i] + ":" + v;
}
var column_3 = $('#column-3').sortable("toArray");
for ( var i = 0, n = column_3.length; i < n; i++ ) {
var v = $('#' + column_3[i] ).find('.inner').is(':visible');
column_3[i] = column_3[i] + ":" + v;
}
var column_4 = $('#column-4').sortable("toArray");
for ( var i = 0, n = column_4.length; i < n; i++ ) {
var v = $('#' + column_4[i] ).find('.inner').is(':visible');
column_4[i] = column_4[i] + ":" + v;
}
This code has not been tested. But should work fine, does what you need it to do. ^^
function x ()
{
var columns = new Array();
columns.push({
column_1 : $('#column-1').sortable("toArray"),
column_2 : $('#column-2').sortable("toArray"),
column_3 : $('#column-3').sortable("toArray"),
column_4 : $('#column-4').sortable("toArray")
});
$.each(columns, function (key, item)
{
SaveToDatabase(item);
});
}
function SaveToDatabase (yourArray)
{
$.each(yourArray, function (key, item) {
var v = $('#' + item).find('.inner').is(':visible');
item = item + ":" + v;
});
}

Resources