Best way to look up multiple values from the KVM?

I have need of looking up multiple values from the KVM, and I'm running into some challenges:

  1. The number of keys to look up will vary with each call, so I cannot use the KVM policy directly.
  2. There is no looping mechanism in flows
  3. I cannot access the KVM from either a JavaScript policy or a Node.js proxy.
  4. Proxy chaining might help, but there isn't a clean way to call it from a JavaScript policy at this time.

In desperation, I've thought about using Vault as a KVM (bad idea for perf reasons) because I could access it from Node.js, but I'm likely to fall back to option #4 and hope that the port wouldn't change.

Is there something I'm missing here? I sure wish I could pass in an array of keys and get back an array of values…

1 2 2,412
2 REPLIES 2

Not applicable

I wonder why using Vault is bad for performance reasons. Is access to Vault slower than KVM?

One benefit in Node.js is that you can retrieve all values in parallel by leveraging Async or even Promisifying the lookups.

robinm
Participant IV

I can think of a few ways to solve your problem - you can decide which way is 'Best' 🙂

1. Retrieve multiple KVM values.

If you are able to dictate/cater for the maximum number of lookup values, you could employ a single KVM lookup, retrieving values for passed lookup values.
After the lookup, react to the populated variables via Javascript, creating your array as needed ...

<Get assignTo="value1" index="1">
    <Key>
        <Parameter ref="request.queryparam.var1"/>    /* wherever the key is */
    </Key>
</Get>
<Get assignTo="value2" index="1">
    <Key>
        <Parameter ref="request.queryparam.var2"/>
    </Key>
</Get>
...

Depending on your use case, you may want to take advantage of the built-in mechanism for caching KVM keys/values and increase the <ExpiryTimeInSecs> from the default 300 seconds.


2. Access the KVM from NodeJs.

You say you 'cannot access the KVM from either a JavaScript policy or a Node.js proxy'.
As of apigee-access 1.4.0 [circa February 2017], KVM access functionality now exists for NodeJs. ( see doc )

var apigee = require('apigee-access');
var lookup1 = apigee.getVariable(request,'request.queryparam.var1');  /*wherever key is*/
var kvm = apigee.getKeyValueMap('YourKVMnameHere', 'environment');
kvm.get(lookup1, function(err, key_value) {
    if (key_value) {
        var kval = key_value.split(',');
        request.value1 = kval[0];   /*store as variable in the request object*/
    }
}
...


3. Access the KVM via management APIs.

Although this is possible, the approach is not recommended as per the best practices doc.
If you decided to use it, only use the Management API for low volume calls.
Depending on your use case and KVM size, it may even be viable to load all the KVM keys into cache after a one-time "initialisation" service call, say on cache miss to force (re)load.
You could then run through these decoded keys from the cache lookup variable.

<ServiceCallout async="false" continueOnError="false" enabled="true" name="callKVMlookup">
    <DisplayName>callKVMlookup</DisplayName>
    <Request clearPayload="true">
        <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
        <Set>
            <Headers>
                <Header name="Authorization">Basic base64-org-admin-creds</Header>
                <Header name="Accept">application/json</Header>
            </Headers>
        </Set>
    </Request>
    <Response>kvmResponse</Response>
    <HTTPTargetConnection>
        <URL>https://api.enterprise.apigee.com/v1/o/{organization.name}/environments/{environment.name}/keyvaluemaps/kvmCodes</URL>
    </HTTPTargetConnection>
</ServiceCallout>

Your KVM content is then available in Javascript

var kvmJSON = JSON.parse(context.getVariable("kvmResponse.content"));