Configure Api Proxy Using Environment Script

Hi All, we want to configure the api proxy as per environment like dev/prod. As proxy configuration is having hardcoded things in xml file . So we want to replace those using property file(Property set) in resource folder.  Is here any way to achieve this by keeping the property files in 1 place like we do in Java/Maven project. If i deploy my proxy for dev environment it should pick the dev property configuration and should work same for prod.  

0 5 193
5 REPLIES 5

https://cloud.google.com/apigee/docs/api-platform/cache/property-sets

Property sets can exist as an API proxy resource or scoped to the environment level instead. So if you have properties that make sense to be stored at the environment level, you can use the Management API to store these, then access them from your API Proxy.

If instead you have property sets that are both API proxy AND environment specific, you could consider multiple property sets in your API proxy that are environment specific (eg 1 per environment), and then interact conditionally based on the environment your API proxy request is getting processed in ie this can be determined from the environment.name flow variable

Yes, as Dane said, use an AssignMessage /AssignVariable in the proxy to de-reference the values.

Let me show you an example. Suppose you have a properties file named settings.properties in your API proxy, and the contents are like this:

 

target1-dev=https://my-dev-target.internal.company.com
target1-stage=https://my-stage-target.internal.company.com
target1-prod=https://my-target.company.com

 

According to the docs for propertysets, in the apiproxy you can refer to those settings with a name convention like propertyset.SETTINGS_FILE_NAME.SETTING, where the things in UPPERCASE get replaced. In this example, my properties file name is "settings.properties", therefore the SETTINGS_FILE_NAME should be replaced with settings . And therefore, to refer to the target for dev environment, the full variable name will be propertyset.settings.target1-dev. But, from the description of your desire, you probably do not want to refer, in your policies, directly to propertyset.settings.target1-dev or propertyset.settings.target1-prod, etc., directly. You want to refer to them indirectly, using a variable that gets the value of propertyset.settings.target1-dev when the proxy is deployed to dev, and using propertyset.settings.target1-prod when the proxy is deployed to prod. How can you do this?

AssignMessage/AssignVariable has a mechanism by which you can assign a variable based on a template. Here's how it would work:

 

<AssignMessage name='AM-Eval-Settings'>
  <AssignVariable>
    <Name>target-variable</Name>
    <Template>propertyset.settings.target1-{environment.name}</Template>
    <!-- 
      This uses the "message template" feature of Apigee: an open/close curly
      brace pair, surrounding a variable name, gets replaced with the value 
      contained in the named variable. In this case, {environment.name} gets
      replaced with dev, or stage, or prod. 
      The result of the above will be propertyset.settings.target1-dev in 
      the dev environment, propertyset.settings.target1-prod in prod, etc.
    -->
  </AssignVariable>
  <AssignVariable>
    <Name>target-template</Name>
    <Template>{{target-variable}}</Template>
    <!-- 
      The result of the above will be {propertyset.settings.target1-dev} in
      the dev environment.  In other words, it's a template, referring to 
      a variable.
    -->
  </AssignVariable>
  <AssignVariable>
    <Name>assigned-target</Name>
    <!-- 
      The ref syntax below says: 
      treat whatever is in the named variable, as a message template.
    --> 
    <Template ref='target-template'/>
    <!-- 
      The result will be https://my-dev-target.internal.company.com in dev.
    -->
  </AssignVariable>
</AssignMessage>

 

The value of assigned-target will be https://my-dev-target.internal.company.com when the environment is "dev" and it will be https://my-target.company.com when the environment is "prod".

You can then use this variable to set the target.url .

Hi , thanks for giving the solution. Can't we use the assign variable single time instead mapping it 2/3 times like you cleared in above example. I just want to access it via single name. I mean i just want to access it target-variable instead assigned-target. Because this multi mapping will create a big file for me for dev-prod property sets. 

I'm sorry it's not succinct enough for your purposes, and ... you need at least two steps. This works for me, and I don't think it is possible to make it shorter: 

<AssignMessage name='AM-Eval-Settings-2'>
  <AssignVariable>
    <Name>target-template</Name>
    <Template>{propertyset.settings.target1-{environment.name}}</Template>
  </AssignVariable>
  <AssignVariable>
    <Name>assigned-target</Name>
    <Template ref='target-template'/>
  </AssignVariable>
</AssignMessage>

 

Hi Dchiesa,

My issue is still not resolved. I have done the above steps that you have mentioned. I have added a property set file in resource dir. I have created the Assign Message file in policies and gave the variable reference. Below is the detail that i have done :

I have created env_script.property and added the url in that:

url-dev=service.apigee-useast1.trn03-insight-us.com

This one i have added in Assigned Message Policy

<AssignVariable>
<Name>devurl</Name>
<Template>propertyset.env_script.url-{environment.name}</Template>
</AssignVariable>
<AssignVariable>
<Name>assigned_devurl</Name>
<Template ref="devurl"/>
</AssignVariable>

This one i have added in PreFlow

<HTTPTargetConnection>
<URL>http://{assigned_devurl}/xyz-service</URL>
</HTTPTargetConnection>

I have deployed the proxy successfully. But while calling the api via postman i am getting below error:

 "fault": {
        "faultstring""Unresolved variable : assigned_devurl",
        "detail": {
            "errorcode""entities.UnresolvedVariable"
        }
    }

Please help to fix it. Also please let me know if i am doing something wrong .