Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Getting different values on a KVM

Hello there!

I have this new requirement where:

1. I have to pass an Id in the request header, so in Postman, I add the key - ID.

2. Use a KVM to store -> username, password, host and the url specific for that ID.

So, for my code, please check below:

Extracting of ID from the header:

 

 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ExtractVariables continueOnError="false" enabled="true" name="EV-ExtractUserId">
    <DisplayName>EV-ExtractUserId</DisplayName>
    <Properties/>
    <Header name="ID">
        <Pattern>{ID}</Pattern>
    </Header>
</ExtractVariables>

 

 

 Created a template to map the values to be used in the next step, which is to use a KVM

 

 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage continueOnError="false" enabled="true" name="AM-GetXInformation">
    <DisplayName>AM-GetXInformation</DisplayName>
    <Properties/>
    <AssignVariable>
        <Name>x_host</Name>
        <Template>{ID}_host</Template>
    </AssignVariable>
    <AssignVariable>
        <Name>x_username</Name>
        <Template>{ID}_username</Template>
    </AssignVariable>
    <AssignVariable>
        <Name>x_password</Name>
        <Template>{ID}_password</Template>
    </AssignVariable>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
</AssignMessage>

 

 

 KVM

 

 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<KeyValueMapOperations continueOnError="false" enabled="true" name="KVM-GetXCredentials" mapIdentifier="KVM-X">
    <DisplayName>KVM-GetXCredentials</DisplayName>
    <ExclusiveCache>false</ExclusiveCache>
    <ExpiryTimeInSecs>3600</ExpiryTimeInSecs>
    <Get assignTo="x_host" index="1">
        <Key>
            <Parameter ref="x_host"/>
        </Key>
    </Get>
    <Get assignTo="x_url" index="1">
        <Key>
            <Parameter>x_url</Parameter>
        </Key>
    </Get>
    <Get assignTo="x_username" index="1">
        <Key>
            <Parameter ref="x_username"/>
        </Key>
    </Get>
    <Get assignTo="private.x_password" index="1">
        <Key>
            <Parameter ref="x_password"/>
        </Key>
    </Get>
    <Scope>environment</Scope>
</KeyValueMapOperations>

 

 

 however, the problem here is I have 10 ID's using the same url, password, and host, but differs in username. So I need recommendation on how can I do that as I've been having a hard time how to achieve it. Thank you for any help.

@dchiesa1 @sidd-harth @adas @kurtz 

Solved Solved
1 1 208
1 ACCEPTED SOLUTION

Hi @Char-K ,

Given your requirement to associate multiple values with a single ID, you can take advantage of a multi-valued entry in the KVM. See the Value element here: https://cloud.google.com/apigee/docs/api-platform/reference/policies/key-value-map-operations-policy....

This is easy to see in a KVM with InitialEntries:

<KeyValueMapOperations name="KV-multi-value" mapIdentifier="multi-value">
    <ExclusiveCache>false</ExclusiveCache>
    <ExpiryTimeInSecs>300</ExpiryTimeInSecs>
    <InitialEntries>
        <Entry>
            <Key>
                <Parameter>ID-12345</Parameter>
            </Key>
            <Value>username1</Value>
            <Value>password1</Value>
            <Value>host1.net</Value>
            <Value>https://url1.com/user1</Value>
        </Entry>
        <Entry>
            <Key>
                <Parameter>ID-67890</Parameter>
            </Key>
            <Value>username2</Value>
            <Value>password2</Value>
            <Value>host2.net</Value>
            <Value>https://url2.com/user2</Value>
        </Entry>
    </InitialEntries>
    <Get assignTo="private.username" index="1">
        <Key>
            <Parameter ref="request.header.x-id"/>
        </Key>
    </Get>
    <Get assignTo="private.password" index="2">
        <Key>
            <Parameter ref="request.header.x-id"/>
        </Key>
    </Get>
    <Get assignTo="private.host" index="3">
        <Key>
            <Parameter ref="request.header.x-id"/>
        </Key>
    </Get>
    <Get assignTo="private.url" index="4">
        <Key>
            <Parameter ref="request.header.x-id"/>
        </Key>
    </Get>
    <Scope>environment</Scope>
</KeyValueMapOperations>

Here's an AssignMessage to see the values in a test proxy

<AssignMessage name="AM-multi-response">
    <Set>
        <Payload contentType="application/json">
{
    "id":"{request.header.x-id}",
    "username":"{private.username}",
    "password":"{private.password}",
    "host":"{private.host}",
    "url":"{private.url}"
}
</Payload>
    </Set>
</AssignMessage>

You can create these types of KVM entries using apigeecli, for example:

apigeecli kvms entries create --org=$ORG --env=$ENV \
    --map=multi-value --key=ID-11111 \
    --value=username11111,password11111,host11111.net,https://url11111.com/user11111

Or using the Apigee API, for example:

curlx -X POST -H "Content-Type: application/json" \
https://apigee.googleapis.com/v1/organizations/$ORG/environments/$ENV/keyvaluemaps/multi-value/entries -d '
{
  "name": "ID-22222",
  "value": "username22222,password22222,host22222.net,https://url22222.com/user22222"
}'

And finally test:

curl https://$HOSTNAME/kvms-initial-entries/multi-value -H X-ID:ID-11111
{
    "id":"ID-11111",
    "username":"username11111",
    "password":"password11111",
    "host":"host11111.net",
    "url":"https://url11111.com/user11111"
}

 

View solution in original post

1 REPLY 1

Hi @Char-K ,

Given your requirement to associate multiple values with a single ID, you can take advantage of a multi-valued entry in the KVM. See the Value element here: https://cloud.google.com/apigee/docs/api-platform/reference/policies/key-value-map-operations-policy....

This is easy to see in a KVM with InitialEntries:

<KeyValueMapOperations name="KV-multi-value" mapIdentifier="multi-value">
    <ExclusiveCache>false</ExclusiveCache>
    <ExpiryTimeInSecs>300</ExpiryTimeInSecs>
    <InitialEntries>
        <Entry>
            <Key>
                <Parameter>ID-12345</Parameter>
            </Key>
            <Value>username1</Value>
            <Value>password1</Value>
            <Value>host1.net</Value>
            <Value>https://url1.com/user1</Value>
        </Entry>
        <Entry>
            <Key>
                <Parameter>ID-67890</Parameter>
            </Key>
            <Value>username2</Value>
            <Value>password2</Value>
            <Value>host2.net</Value>
            <Value>https://url2.com/user2</Value>
        </Entry>
    </InitialEntries>
    <Get assignTo="private.username" index="1">
        <Key>
            <Parameter ref="request.header.x-id"/>
        </Key>
    </Get>
    <Get assignTo="private.password" index="2">
        <Key>
            <Parameter ref="request.header.x-id"/>
        </Key>
    </Get>
    <Get assignTo="private.host" index="3">
        <Key>
            <Parameter ref="request.header.x-id"/>
        </Key>
    </Get>
    <Get assignTo="private.url" index="4">
        <Key>
            <Parameter ref="request.header.x-id"/>
        </Key>
    </Get>
    <Scope>environment</Scope>
</KeyValueMapOperations>

Here's an AssignMessage to see the values in a test proxy

<AssignMessage name="AM-multi-response">
    <Set>
        <Payload contentType="application/json">
{
    "id":"{request.header.x-id}",
    "username":"{private.username}",
    "password":"{private.password}",
    "host":"{private.host}",
    "url":"{private.url}"
}
</Payload>
    </Set>
</AssignMessage>

You can create these types of KVM entries using apigeecli, for example:

apigeecli kvms entries create --org=$ORG --env=$ENV \
    --map=multi-value --key=ID-11111 \
    --value=username11111,password11111,host11111.net,https://url11111.com/user11111

Or using the Apigee API, for example:

curlx -X POST -H "Content-Type: application/json" \
https://apigee.googleapis.com/v1/organizations/$ORG/environments/$ENV/keyvaluemaps/multi-value/entries -d '
{
  "name": "ID-22222",
  "value": "username22222,password22222,host22222.net,https://url22222.com/user22222"
}'

And finally test:

curl https://$HOSTNAME/kvms-initial-entries/multi-value -H X-ID:ID-11111
{
    "id":"ID-11111",
    "username":"username11111",
    "password":"password11111",
    "host":"host11111.net",
    "url":"https://url11111.com/user11111"
}