Question

I would like to use KeyValueMaps to store some simple values, but they keys I need to use would be computed at runtime. For example in my 'InitialEntries' I want to do something like this:

<KeyValueMapOperations async="false" continueOnError="false" enabled="true" name="Sandbox-Read-Count">
    <DisplayName>Sandbox - Read Count</DisplayName>
    <FaultRules/>
    <Properties/>
    <ExclusiveCache>false</ExclusiveCache>
    <ExpiryTimeInSecs>-1</ExpiryTimeInSecs>
    <InitialEntries>
        <Entry>
            <Key>
                <Parameter>{variable}.sandbox.calls</Parameter>
            </Key>
            <Value>0</Value>
        </Entry>
    </InitialEntries>
    <Scope>apiproxy</Scope>
</KeyValueMapOperations>

However, when doing this I get an error when I try to save the policy:

Error while Uploading file for API Test.
messaging.config.beans.InvalidBundle. Errors:[Entity : policy-Sandbox-Read-Count, Invalid Key Names For Entries: [{apikey}.sandbox.calls];]

Is it possible to use computed values in the KeyValueMap policy? Is there a different syntax that I should be using?

Was it helpful?

Solution

I've investigated this. What happens is when you save the proxy with InitialEntries in the apiproxy-scoped KVM, the KVM is immediately created with the initial entries. Therefore, there is no way to use runtime variables, because the priming of the KVM has happened before the proxy ever runs.

You didn't use the mapIdentifier field in your KeyValueMapOperations element (look at the KeyValueMap PUT Sample in the Apigee docs), so the KVM you would create would be named kvmap.

You can use the following management API call to get a list of the KVMs and their contents for a given apiproxy:

GET https://api.enterprise.apigee.com/v1/o/{org}/apis/{apiname}/keyvaluemaps?expand=true

Authorization: Basic {base64 username:password}

Since The InitialEntries section is only used when the proxy is first loaded successfully (even if you change the InitialEntries section and redeploy, no changes will be made if the KVM of that name already exists), I think the usefulness of the InitialEntries section is rather limited. I'd recommend manually priming your KVM's using the management API to initialize the KVM:

PUT https://api.enterprise.apigee.com/v1/o/{org}/apis/{apiname}/keyvaluemaps

Authorization: Basic {base64 username:password}
Content-Type: application/json

{
  "entry" : [ {
    "name" : "key",
    "value" : "0"
  } ],
  "name" : "{kvmName}"
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top