Correct, you can use a regex in this use case. Since $, { and } have **** meaning to regex they must be escaped. This should do the job:
<?xml version="1.0" encoding="UTF-8"?>
<wsp:Policy xmlns:L7p="http://www.layer7tech.com/ws/policy" xmlns:wsp="http://schemas.xmlsoap.org/ws/2002/12/policy">
<wsp:All wsp:Usage="Required">
<L7p:Regex>
<L7p:AutoTarget booleanValue="false"/>
<L7p:OtherTargetMessageVariable stringValue="url"/>
<L7p:Regex stringValue="\$\{appname\}"/>
<L7p:Replace booleanValue="true"/>
<L7p:Replacement stringValue="${appname}"/>
<L7p:Target target="OTHER"/>
</L7p:Regex>
</wsp:All>
</wsp:Policy>
Here is a whole service policy to illustrate the case. I think this covers what you are asking:
<?xml version="1.0" encoding="UTF-8"?>
<wsp:Policy xmlns:L7p="http://www.layer7tech.com/ws/policy" xmlns:wsp="http://schemas.xmlsoap.org/ws/2002/12/policy">
<wsp:All wsp:Usage="Required">
<L7p:CommentAssertion>
<L7p:Comment stringValue="**************************************************************************************************************"/>
</L7p:CommentAssertion>
<L7p:CommentAssertion>
<L7p:Comment stringValue="* Investigate regex to substitute a context variable syntax with a value from another context variable"/>
</L7p:CommentAssertion>
<L7p:CommentAssertion>
<L7p:Comment stringValue="* "/>
</L7p:CommentAssertion>
<L7p:CommentAssertion>
<L7p:Comment stringValue="**************************************************************************************************************"/>
</L7p:CommentAssertion>
<L7p:AuditAssertion/>
<L7p:SetVariable>
<L7p:Base64Expression stringValue="YXBwMQ=="/>
<L7p:VariableToSet stringValue="appname"/>
</L7p:SetVariable>
<L7p:SetVariable>
<L7p:AssertionComment assertionComment="included">
<L7p:Properties mapValue="included">
<L7p:entry>
<L7p:key stringValue="RIGHT.COMMENT"/>
<L7p:value stringValue="// Pull ${url} from cluster-wider property"/>
</L7p:entry>
</L7p:Properties>
</L7p:AssertionComment>
<L7p:Base64Expression stringValue="JHtnYXRld2F5LnVybH0="/>
<L7p:Enabled booleanValue="false"/>
<L7p:VariableToSet stringValue="url"/>
</L7p:SetVariable>
<L7p:SetVariable>
<L7p:AssertionComment assertionComment="included">
<L7p:Properties mapValue="included">
<L7p:entry>
<L7p:key stringValue="RIGHT.COMMENT"/>
<L7p:value stringValue="// Need this to build the string in ${url}"/>
</L7p:entry>
</L7p:Properties>
</L7p:AssertionComment>
<L7p:Base64Expression stringValue="JA=="/>
<L7p:VariableToSet stringValue="dollar"/>
</L7p:SetVariable>
<L7p:SetVariable>
<L7p:AssertionComment assertionComment="included">
<L7p:Properties mapValue="included">
<L7p:entry>
<L7p:key stringValue="RIGHT.COMMENT"/>
<L7p:value stringValue="// Build ${url} in policy"/>
</L7p:entry>
</L7p:Properties>
</L7p:AssertionComment>
<L7p:Base64Expression stringValue="aHR0cDovLyR7ZG9sbGFyfXthcHBuYW1lfS5teXNpdGUuY29t"/>
<L7p:VariableToSet stringValue="url"/>
</L7p:SetVariable>
<L7p:Regex>
<L7p:AutoTarget booleanValue="false"/>
<L7p:OtherTargetMessageVariable stringValue="url"/>
<L7p:Regex stringValue="\$\{appname\}"/>
<L7p:Replace booleanValue="true"/>
<L7p:Replacement stringValue="${appname}"/>
<L7p:Target target="OTHER"/>
</L7p:Regex>
<L7p:HardcodedResponse>
<L7p:Base64ResponseBody stringValue="YXBwbmFtZSA9ICR7YXBwbmFtZX0KCnVybCA9ICR7dXJsfQ=="/>
<L7p:ResponseContentType stringValue="text/plain; charset=UTF-8"/>
</L7p:HardcodedResponse>
</wsp:All>
</wsp:Policy>
Cheers!
JayMac
------------------------------
Jay MacDonald - Adoption Architect - Broadcom API Management (Layer 7)
------------------------------
Original Message:
Sent: May 12, 2022 03:42 AM
From: Tony Svedlund
Subject: Context variable String keyword expansion
Hi Aniket and thanks for you reply!
No, the url variable (in my case a Cluster Wide Property) is literally the string "http://${appname}.mysite.com", so the value app1 is set separately in the appname variable.
So, some regex substitution or similar need to be applied on the url variable to expand the value of appname into the resulting string.
Original Message:
Sent: May 11, 2022 11:56 AM
From: Aniket Negi
Subject: Context variable String keyword expansion
Tony,
You should already see as the value "http://app1.mysite.com" for url context variable. Now you want to reassign the value of url variable to appUrl, you can do something like this.
name: "appUrl", type: String, value: ${url}
Looks like you got it.
Original Message:
Sent: May 11, 2022 04:10 AM
From: Tony Svedlund
Subject: Context variable String keyword expansion
Hi Community!
What is the best way to do the following in a service policy in policy manager (using L7 API GW 10)?
I have a context variable string that contains a keyword that I want to replace with it's value from another variable, e.g:
- Context variable 1: name: "appname", type: String, value: "app1"
- Context variable 2: name: "url", type: String, value: "http://${appname}.mysite.com"
- Wanted result: Context variable 3. name: "appUrl", type: String, value: "http://app1.mysite.com"
Script assertion handling it in javascript code, or..?
BR,
Tony