Read all the KVM Key values from a KVM and store in local variable

I have a use case where i need to store some details (limits on rates,quota etc.) as a list of key value pairs in a KVM. Then i need to read the entire list of key-value pairs every time the proxy flow is run and get the details stored in local variables to process further in the proxy flow.

The catch is that the KVM details can change over the time (new key value pairs added, some removed etc.) and i would still want that the KVM operations block/or some other logic should be able to do a look on the current values and give me a set of local key-values pair to work with in the proxy flow.

Please let know if anyone have worked with something like this before .Thanks,Chandra

Solved Solved
0 3 1,216
1 ACCEPTED SOLUTION

You could store a single entry in the KVM that is a JSON representation of all values.

You would have to parse the values but you could use Extract Variables or JavaScript policies to do that. Granted it may make populating the KVM a bit more involved.

Here's a simple example, put these policies in a no-target proxy named "kvm-parse" and create a KVM named "kvm-parse" with no entries. Put the policies in this sequence, then run trace.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<KeyValueMapOperations mapIdentifier="kvm-parse" async="false" continueOnError="false" enabled="true" name="KV-key-values">
    <DisplayName>KV-key-values</DisplayName>
    <InitialEntries>
        <Entry>
            <Key>
                <Parameter>key_values</Parameter>
            </Key>
            <Value>
{
    "key1":"value1",
    "key2":"value2",
    "kvm-parse":"quota_limit_10_per_1_minute"
}                
</Value>
        </Entry>
    </InitialEntries>
    <Get assignTo="key_values">
        <Key>
            <Parameter>key_values</Parameter>
        </Key>
    </Get>
    <Scope>environment</Scope>
</KeyValueMapOperations>

Follow that with an Extract Variables to get the "kvm-parse" entry.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ExtractVariables async="false" continueOnError="false" enabled="true" name="EV-key-values">
    <DisplayName>EV-key-values</DisplayName>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <JSONPayload>
        <Variable name="value1">
            <JSONPath>key1</JSONPath>
        </Variable>
        <Variable name="value2">
            <JSONPath>key2</JSONPath>
        </Variable>
        <Variable name="kvm_parse">
            <JSONPath>{apiproxy.name}</JSONPath>
        </Variable>
    </JSONPayload>
    <Source clearPayload="false">key_values</Source>
</ExtractVariables>

Then one more Extract Variables to parse the entry for "kvm_parse"

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ExtractVariables async="false" continueOnError="false" enabled="true" name="EV-variable">
    <DisplayName>EV-variable</DisplayName>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <Variable name="kvm_parse">
        <Pattern>quota_limit_{quota_limit}_per_{quota_per}_{quota_timeunit}</Pattern>
    </Variable>
</ExtractVariables>

Hopefully this will give you some ideas for your specific use case.

View solution in original post

3 REPLIES 3

Not applicable

You can fetch some keys and use in kvm policy. Those will be cached in the environment or proxy.

If you want all kvms only option is management api call which is not suggested to be used in proxy.

In this case I would suggest you to use product/developer/app custom attributes and you can add to cache using policies.

You could store a single entry in the KVM that is a JSON representation of all values.

You would have to parse the values but you could use Extract Variables or JavaScript policies to do that. Granted it may make populating the KVM a bit more involved.

Here's a simple example, put these policies in a no-target proxy named "kvm-parse" and create a KVM named "kvm-parse" with no entries. Put the policies in this sequence, then run trace.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<KeyValueMapOperations mapIdentifier="kvm-parse" async="false" continueOnError="false" enabled="true" name="KV-key-values">
    <DisplayName>KV-key-values</DisplayName>
    <InitialEntries>
        <Entry>
            <Key>
                <Parameter>key_values</Parameter>
            </Key>
            <Value>
{
    "key1":"value1",
    "key2":"value2",
    "kvm-parse":"quota_limit_10_per_1_minute"
}                
</Value>
        </Entry>
    </InitialEntries>
    <Get assignTo="key_values">
        <Key>
            <Parameter>key_values</Parameter>
        </Key>
    </Get>
    <Scope>environment</Scope>
</KeyValueMapOperations>

Follow that with an Extract Variables to get the "kvm-parse" entry.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ExtractVariables async="false" continueOnError="false" enabled="true" name="EV-key-values">
    <DisplayName>EV-key-values</DisplayName>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <JSONPayload>
        <Variable name="value1">
            <JSONPath>key1</JSONPath>
        </Variable>
        <Variable name="value2">
            <JSONPath>key2</JSONPath>
        </Variable>
        <Variable name="kvm_parse">
            <JSONPath>{apiproxy.name}</JSONPath>
        </Variable>
    </JSONPayload>
    <Source clearPayload="false">key_values</Source>
</ExtractVariables>

Then one more Extract Variables to parse the entry for "kvm_parse"

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ExtractVariables async="false" continueOnError="false" enabled="true" name="EV-variable">
    <DisplayName>EV-variable</DisplayName>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <Variable name="kvm_parse">
        <Pattern>quota_limit_{quota_limit}_per_{quota_per}_{quota_timeunit}</Pattern>
    </Variable>
</ExtractVariables>

Hopefully this will give you some ideas for your specific use case.

Great answer Kurt...let me implement this and will let know how it went..!