In this NQE Video, I am going cover and how to use let and group-by qualifiers.
Join me for a look at the two NQE qualifiers, let and group-by.
Code examples used in video
foreach device in network.devices
let deviceName = device.name
let affected_interfaces = (foreach interface in device.interfaces
group interface.name as interfaces_name by interface.operStatus as oper_status
select {
int: interfaces_name,
oper: oper_status,
})
foreach entry in affected_interfaces
select {
deviceName: deviceName,
interfaceNames: entry.int,
operStatus: entry.oper
}
Let
Using let to assign expressions that make the code more easily readable, in this case assigning the device OS version and operating system to variables and filtering on them
foreach device in network.devices
let version = device.platform.osVersion
let os = device.platform.os
where os == OS.ARISTA_EOS && matches(version, "4.15*")
select {
deviceName: device.name,
version: version,
os: os
}
Group-By
Using group-by to group interfaces by their status
foreach device in network.devices
let deviceName = device.name
foreach interface in device.interfaces
group interface.name as interfaces_name by interface.operStatus as oper_status
select {
deviceName: deviceName,
interfaceNames: interfaces_name,
operStatus: oper_status
}
Dont forget if you need to use a variable that was declared before the group by, since using group-by changes the scope of what variables you can access, we can use let to assign this expression to a variable
If you would like to see another example, in the Forward Library under vendor-specific/Cisco/Cisco Interface Count by Media Type, this uses both let and group-by by either navigating through the files tree or searching for this query-id: FQ_79ad895f597cc6a875db1eb966032c95dfbb8c81
What use cases have you discovered that you can use either let or group by for? Share your queries and stories with us in the comments below!