Skip to main content

Parameterized NQE query from the API

  • 16 August 2024
  • 3 replies
  • 66 views

Suppose we have the following NQE query, which takes a List of Strings as a parameter called “awsRegion”. This NQE query returns a list of all NAT Gateways in AWS that are in a region in the given list.

@query
natGatewaysByRegion(awsRegion : List<String>) =
foreach cloudAccount in network.cloudAccounts
where cloudAccount.cloudType == CloudType.AWS
foreach vpc in cloudAccount.vpcs
foreach natGateway in vpc.natGateways
where natGateway.region in awsRegion
select { vpcId: vpc.id, natGatewayId: natGateway.id, region: natGateway.region };

In the GUI, you click the Parameters button, enter the region names, click save, and then execute.

You can perform this same NQE query from the API.

Using the curl command and entering a list of regions with s“us-east-1”, “us-east-2”] as the parameter.

curl -X 'POST' \
'https://fwd.app/api/nqe?networkId=123123' \
-H 'accept: application/json' \
-H 'Authorization: Basic <redacted>' \
-H 'Content-Type: application/json' \
-d '{
"queryId": "Q_1234567891011121231415161718192021",
"parameters": {
"awsRegion": "us-east-1", "us-east-2"]
}
}'

Note the network ID is required in the URL, and a queryId is required to specify what NQE query is being run.

Hi Christopher,

 

How would I be able to set the parameters up so that it returns all results starting with “us”, rather than providing the full name of the region.

 

I have tried using “*” at the end of the string, e.g. “us*” but this doesn’t seem to work.

 

Is there another way to do this?

 

Kind regards,

Sebastian


@Sebastian Coros you could change the query to this

 

@query
natGatewaysByRegion(awsRegion : List<String>) =
foreach cloudAccount in network.cloudAccounts
where cloudAccount.cloudType == CloudType.AWS
foreach vpc in cloudAccount.vpcs
foreach natGateway in vpc.natGateways
foreach region in awsRegion
where matches(natGateway.region, region)
select { vpcId: vpc.id, natGatewayId: natGateway.id, region: natGateway.region };

then you can use glob characters like us*


@GaryB ‘s query might produce duplicate rows if a gateway’s region matches multiple region globs. To avoid those duplicates, you could write the query like this:

@query
natGatewaysByRegion(awsRegionGlobs : List<String>) =
foreach cloudAccount in network.cloudAccounts
where cloudAccount.cloudType == CloudType.AWS
foreach vpc in cloudAccount.vpcs
foreach natGateway in vpc.natGateways
where max(foreach glob in awsRegionGlobs
select matches(natGateway.region, glob))
select {
vpcId: vpc.id,
natGatewayId: natGateway.id,
region: natGateway.region
};

In this query, instead of emitting a row for every glob that matches, it emits a single row for a gateway only when it matches at least one of the provided globs.


Reply