Is it possible to set the mapIdentifier value from a variable for a KeyValueMapOperations policy?

Hello,

Is it possible to set the mapIdentifier value from a variable for a KeyValueMapOperations policy? This would be helpful so that folks could just have one KVM operations policy in a shared flow and not have to have one in each proxy. Thanks. Have a good weekend!

Solved Solved
0 10 1,995
1 ACCEPTED SOLUTION

NEW ANSWER, 2021 September: YES

Since the original question and answer here, Apigee has added the ability to set the map identifier in a variable. The way to do it is omit the mapIdentifier attribute, and add a MapName element. The syntax looks like this:

<KeyValueMapOperations name='KVM-Get'>
  <Scope>environment</Scope>
  <ExpiryTimeInSecs>300</ExpiryTimeInSecs>
  <!-- specify the map name via a variable here -->
  <MapName ref='mapname'/>
  <Get assignTo="private.myvar">
    <Key>
      <Parameter ref='request.queryparam.key'/>
    </Key>
  </Get>
</KeyValueMapOperations>

View solution in original post

10 REPLIES 10

AFAIk it is not possible to use dynamic/variable values in KVM mapIdentifier.

Why not use one KVM where you have all the configurations?

Use prefix to differentiate.

Thanks for the response. Yes, I was aware of that possibility. I was hoping to be able to have code reuse for the KeyValueMapOperations policy in the Shared Flow. It sounds like that's not currently possible though.

You might be able to accomplish what you want; Just use the variable as part of the KEY, which is a dynamically computed composite value. The mapIdentifier remains fixed, but the key can vary.

<KeyValueMapOperations name='KVM-get1' mapIdentifier='nameOfMap'>
  <Scope>environment</Scope>
  <ExpiryTimeInSecs>300</ExpiryTimeInSecs>
  <Get assignTo='variable.to.set' >
    <Key>
      <Parameter ref='variable.containing.discriminator'/>
      <Parameter ref='variable.containing.original.key'/>
    </Key>
  </Get>
</KeyValueMapOperations>

Thanks! Great idea. I will try that. Cheers.

Hi Dino.

We are using this KeyValueMapOperations policy within a shared flow.

How can I solve then problem mentioned in this article?

If I call Shared Flow from Proxy P1, then I want to use mapIdentifier, say P1Identifier and when I call from proxy P2 I have to use P2Identifier

I suppose you could use apiproxy.name as the discriminator.

<KeyValueMapOperations name='KVM-get1' mapIdentifier='nameOfMap'>
  <Scope>environment</Scope>
  <ExpiryTimeInSecs>300</ExpiryTimeInSecs>
  <Get assignTo='variable.to.set' >
    <Key>
      <Parameter ref='apiproxy.name'/>
      <Parameter ref='variable.containing.original.key'/>
    </Key>
  </Get>
</KeyValueMapOperations>

Is it as simple as that?

It would be good if mapIdenfier can be set dynamically using variable. For example we don't want to load one kvm containing 100 entries (25 groups x 4 entries per group, with different prefix) again and again but only need 4 of them each time. Not efficient maintenance and performance wise. Ideally we should have 25 kvms, one for each group, then load the one we need by passing in group id/name in mapIdenfier field via a variable. Is this a valid use case?

It would be good if mapIdenfier can be set dynamically using variable.

Thanks for that feedback.  I agree with you!

Since the original question and answer here, Apigee has added the ability to set the map identifier in a variable. The way to do it is omit the mapIdentifier attribute, and add a MapName element. The syntax looks like this;

 

<KeyValueMapOperations name='KVM-Get'>
  <Scope>environment</Scope>
  <ExpiryTimeInSecs>300</ExpiryTimeInSecs>
  <!-- Specify the mapname with a variable here -->
  <MapName ref='mapname'/>
  <Get assignTo="private.myvar">
    <Key>
      <Parameter ref='request.queryparam.key'/>
    </Key>
  </Get>
</KeyValueMapOperations>

 

 

Ideally we should have 25 kvms, one for each group, then load the one we need by passing in group id/name in mapIdenfier field via a variable. Is this a valid use case?

Maybe rethink that and use the group ID as a part of the KEY in the KVM. You don't need distinct KVMs for that purpose. The key in the KVM is designed to support your use case nicely. a "map" is just a named domain for the Key-Value-Map capability in Apigee. You don't need distinct "maps" just to segregate values. If you have 25 groups, and within those groups 4 different settings, you can store all 100 of those settings with distinct keys in a single map.  That seems like the easiest and most bueno thing to do.

Specify a composite key with multiple key parameters. Like this:

 

<KeyValueMapOperations name='KVM-Get'>
  <Scope>environment</Scope>
  <ExpiryTimeInSecs>300</ExpiryTimeInSecs>
  <MapName ref='mapname'/>
  <Get assignTo="private.myvar">
    <Key>
      <Parameter ref='groupid'/>
      <Parameter ref='settingid'/>
      <!-- add more parameters as you need them -->
    </Key>
  </Get>
</KeyValueMapOperations>

 

That is exactly what we need. Great thanks @dchiesa1 !

NEW ANSWER, 2021 September: YES

Since the original question and answer here, Apigee has added the ability to set the map identifier in a variable. The way to do it is omit the mapIdentifier attribute, and add a MapName element. The syntax looks like this:

<KeyValueMapOperations name='KVM-Get'>
  <Scope>environment</Scope>
  <ExpiryTimeInSecs>300</ExpiryTimeInSecs>
  <!-- specify the map name via a variable here -->
  <MapName ref='mapname'/>
  <Get assignTo="private.myvar">
    <Key>
      <Parameter ref='request.queryparam.key'/>
    </Key>
  </Get>
</KeyValueMapOperations>