ExtractVariables and KeyValueMapOperations polices not working as expected

Could someone help me to solve this odd behavior ? Created ExtractVariables policy to fetch proxyname and based on that proxyname fetch host from kvm, and inject host to request.unfortunately Extracted.basepath is not setting and host_value is not pulling from kvm. Did i miss something ? 

 

Extract base basepath: 
------------------------------
Input:  https://<domain>//proxy1
Expected output: proxy1
Actual output: null

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ExtractVariables continueOnError="false" enabled="true" name="EV-ExtractBasePath">
  <DisplayName>EV-ExtractBasePath</DisplayName>
  <Properties/>
  <URIPath name="basepath">
    <Segment fragment="2"/>
  </URIPath>
  <VariablePrefix>Extracted</VariablePrefix>
  <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
</ExtractVariables>

KeyValueMapOperations
----------------------
extracting the kvm 
{
  "name": "KVM-Host",
  "encrypted": false,
  "entry": [
    {
      "name": "proxy1",
      "value": "proxy1.anthos.domain.com"
    }, {
      "name": "proxy2",
      "value": "proxy2.anthos.domain.com"
    }
  ]
}

expected output: proxy1.anthos.domain.com
Actual output: null
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<KeyValueMapOperations async="false" continueOnError="false" enabled="true" name="KVM-GetHostValue">
  <DisplayName>KVM-GetHostValue</DisplayName>
  <ExpiryTimeInSecs>300</ExpiryTimeInSecs>
  <Get assignTo="host_value">
    <Key>
      <Parameter>{Extracted.basepath}</Parameter>
    </Key>
    <Scope>environment</Scope>
  </Get>
  <KeyValueMapRef>KVM-Host</KeyValueMapRef>
</KeyValueMapOperations>


This should work if i hardcode

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="AM-AddHostHeader">
  <DisplayName>AM-AddHostHeader</DisplayName>
  <Set>
    <Headers>
      <Header name="Host">{host_value}</Header>
    </Headers>
  </Set>
  <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
  <AssignTo createNew="false" transport="http" type="request"/>
</AssignMessage>

 

eldhosepaul_0-1713277788850.pngeldhosepaul_1-1713277817440.png

 

Solved Solved
2 4 139
2 ACCEPTED SOLUTIONS

I think your problem is in the extract variables. The policy docs only mention the <Pattern> as child of <URIPath>.

<URIPath>
   
<Pattern ignoreCase="true">/something/{basepath}</Pattern>
  </URIPath>
However, given the use case you mentioned perhaps the best approach would be using a flow variable to get the name of the current API Proxy (https://cloud.google.com/apigee/docs/api-platform/reference/variables-reference#proxy)

View solution in original post

<Parameter ref="Key"/>, sorted 🙂

View solution in original post

4 REPLIES 4

Input: https://<domain>/proxy1

I think your problem is in the extract variables. The policy docs only mention the <Pattern> as child of <URIPath>.

<URIPath>
   
<Pattern ignoreCase="true">/something/{basepath}</Pattern>
  </URIPath>
However, given the use case you mentioned perhaps the best approach would be using a flow variable to get the name of the current API Proxy (https://cloud.google.com/apigee/docs/api-platform/reference/variables-reference#proxy)

bselistre-dvt

Thanks for your response. I'm still wondering why KeyValueMapOperations policy still not working.

Revised the implementation and extracted base path using a js policy and assigned my basepath to variable like this

 

var url = context.getVariable('request.uri');
var pathSuffix = context.getVariable('proxy.pathsuffix');
var basePath = url.replace(pathSuffix, '');
basePath = basePath.replace(/^\//, '');
context.setVariable('custom.basepath', basePath);

 

Now i'm getting the base path as expected:

eldhosepaul_0-1713299637245.png

Here is my kvm get

 

<KeyValueMapOperations name="KVM-GetHostValue" enabled="true" continueOnError="false" async="false" mapIdentifier="KVM-Host">
  <DisplayName>KVM-GetHostValue</DisplayName>
  <ExpiryTimeInSecs>300</ExpiryTimeInSecs>
  <Scope>environment</Scope>
  <Get assignTo="host_value">
    <Key>
      <Parameter>{custom.basepath}</Parameter>
    </Key>
  </Get>
</KeyValueMapOperations>

 

Here i'm trying to assign my kvm to host_value variable, unfortunately not getting any value. if i hard code the value in custom.basepath here

 

 

<Parameter>basepathvalue</Parameter>

 

, getting the response and my variable is set.

eldhosepaul_0-1713300434508.png
Did i miss something over here ? how should i able to remove hard coding ?

<Parameter ref="Key"/>, sorted 🙂