How to route requests to desired endpoint using Environment Variables

Hi,

I'm new to apigee learning by doing.

I've a situation where I need to route requests to desired endpoint based on Environment the request hits. for example QA to QA, Prod to Prod

I've configured a proxy and defined a default target host during initial config.

Then I'm using a javascript to decide target host based on the env the request comes in.

I've used this JS file in target endpoint(default) preflow as a step.

I see that all requests are sent to the default host that I configured during initial process.

Am I missing something here please help.

Also I've seen about using Target Server Env config. I've configured the hosts but how do I reference/use it in my proxy.

var env = context.getVariable('environment.name');  

if(env=="prod") {
     var host = 'https://prod.com';
}
if(env=="test") {
     var host = 'https://qa.com';
}


0 5 1,158
5 REPLIES 5

Some feedback.

First, a JavaScript policy that runs within Apigee Edge runs... within its own sandbox, or maybe think of it as a VM. When you set a variable within the scope of the script, as with

var host = 'https://foo.bar'; 

...that only affects the operation of the script. The variable can be read or modified by later logic running in the script. Setting a variable within the JavaScript code doesn't affect the execution context of the message being handled.** There is a thing called "context variables" within Apigee Edge, and those are available to the various policies, and to the flow logic.

**Unfortunately and confusingly, there are exceptions, for some specially named variables. Some variables that you set within the JS code will also cause context variables to be set. If you set response.content, for example, within the JS code, it will also affect the similarly named context variable available to other policies.

The way you actually set a context variable from within JS is:

context.setVariable('variable_name', variable_value); 

Second, if you want to do conditional routing, there are two ways to do it.

  1. use RouteRules with multiple Targets
  2. set the target.url context variable

You're attempting to do something like the latter.

The code you need is

if (a) {
  context.setVariable('target.url', 'https://a.example.com');
}
else {
  context.setVariable('target.url', 'https://b.example.com');
} 

@Dino-at-Google Thanks for your answer. however I see one thing

When I use variable target.host I see that proxy is picking the host value but not using it please see attached pic .hosterr.jpg.

when I use target.url it works fine but the outbound request has proxy basepath in it which is not desired. how do I resolve this

var env = context.getVariable('environment.name');
 
if(env=="prod") {
   var host = 'https://prod.com';
}
if(env=="test") {
   var host = 'https://staging.com';
}
context.setVariable("target.host", host);

yes, appending the proxy pathsuffix is a standard behavior of Apigee Edge, which is sometimes not desired. To disable that behavior, you can flip a flag from within your JS code.

context.setVariable('target.copy.pathsuffix', false);

@Dino-at-Google I've tried using but the behavior is that it removes path completely and only appends query params to outbound request.

context.setVariable('target.copy.pathsuffix', false);

sidd-harth
Participant V

Hi @Raju, as mentioned by Dino you need to use target.url not target.host

context.setVariable('target.copy.pathsuffix', false); will remove all pathsuffix. If you want to use part of the resource path or add a new URI path to your target URL, check below JS and attached policy,

context.setVariable('target.copy.pathsuffix', false);


var env = context.getVariable('environment.name');
var id = context.getVariable('id');


if((id !== null) && (env=="prod")) {
    var url = 'https://jsonplaceholder.typicode.com'
    context.setVariable('target.url', url+"/users/"+id);
}
if((id !== null) && (env=="test")) {
   context.setVariable('target.url', 'https://reqres.in/api/users/1');
}

In my proxy,

HTTPTargetConnection - https://www.google.com

In PrxoyEndpoint there is a resource flow with extract variable policy to get the ID value,

<Flow name="getEmployeesFlow">
            <Description/>
            <Request>
                <Step>
                    <Name>EV-Get-ID</Name>
                </Step>
            </Request>
            <Response/>
            <Condition>(proxy.pathsuffix MatchesPath "/employees/{id}") and (request.verb = "GET")</Condition>
        </Flow>

In Targetendpoint preflow I used a JS policy to remove the pathsuffix and get the ID value to set target.url.

I am using two testing Rest services one per environment,

https://jsonplaceholder.typicode.com/users/1
https://reqres.in/api/users/1

If you see my JS in prod env if case, I am using custom and dynamic uri path with the help of + and "

raju-community-rev1-2018-12-09-rev1-2018-12-09.zip