How to get Target Server Data with JS Policy

Hi,

I'm using AssignMessage policy to create a variable, I need to set the TargetServer URL into this variable.

I'm trying to do this using JS policy, but I don't know how to get that TargetServer to set into the AssignMessage's variable.

Is it possible to do this?

igallardo_0-1695055496577.png

Or maybe if there's another way to do this, I'd appreciate it.

The main idea is not to use "hard corde" url in that policies.

Thanks.

 

Solved Solved
1 5 420
1 ACCEPTED SOLUTION

According to the trace you are showing, the JS policy you have is running very early in the request flow.  Apigee does not select the targetserver until it is invoking the targetserver.  Your proxy logic cannot "see" what the target server will be, before Apigee selects it.  You can "see" the selected targetserver, after Apigee has selected it.  The earliest point where this is possible is in the Target Response flow. 

I will repeat my recommendation from my prior post: If you want to perform your own selection of server + path (each server might have a different path), then I suggest that you avoid using the Apigee TargetServer mechanism, and use your own. You could implement that in JavaScript and rely on settings stored in KVM or a .properties file.  If you want to select JUST the server, then the built-in Apigee TargetServer mechanism will be suitable. 

 

View solution in original post

5 REPLIES 5

I need to set the TargetServer URL into this variable.

I don't know how to get that TargetServer to set into the AssignMessage's variable.

Which TargetServer URL? If you are talking about the environment-scoped target server that you define administratively, then the way to find the addresses is to query the target server entity, administratively. You probably do not want to do that at runtime.

If you want to know the URL after the target has been invoked, I believe examining the target.url variable will give you that. If I am misunderstanding your requirements, maybe you could explain further, what you want to do, and why.

Hi dchiesa,

We have defined an environment, within it we create several Target Servers.
The idea is to create a variable using the AssignMessage policy, using one of those Target Servers by adding a pathsuffix.

Example URL:

igallardo_0-1695057583532.png

My idea was to create the variable plus the suffix within JS and then pass it to the AssignMessage policy, which we then use within the flow.

igallardo_1-1695056511588.png

Is it a little clearer now?

 

 

no,  I'm sorry,it's not clearer to me.  I understand you are defining targetservers in an environment. And then... at some point you want to...set a variable?  Containing... ?  Maybe you want to create a variable, containing the hostname of the target server, and you want to do that, without relying on the runtime actually selecting the targetserver?  Is that right? 

WHY are you trying to set a variable with the contents of a target server? What is your goal here?

The way target servers work in Apigee: The Apigee runtime selects a server and then invokes it. After the selection, you (your proxy) can observe the selected URL.  But... I think you are hoping to see the selection before it happens.  In other words before the target is invoked. Is that right?  Why? 

if you can wait until AFTER the target server has been invoked, then you can see the results.

If you have multiple. targetservers and each one needs to use a different path... well that is not within the design intention of targetservers in Apigee.  The Apigee targetserver expects each server to be a clone of the others. If you want to vary the path for each server, then maybe you need to implement your own targetserver-like mechanism , and avoid using the built-in mechanism in Apigee.

 

The idea is like this:

  • We have an environment with some target servers defined.
  • Using the AssingMessage policy, we need to define a variable.
  • The variable is created like this, using a destination server + some suffix
  • Example: https://pepe.com/me, pepe.com --> Target Server, /me -->Suffix
  • igallardo_0-1695061751227.png

Then we need to know how to select the destination server (that we created) in AssingMessage to customize that variable and use it to our liking later, or maybe do the same BUT using JSPolicy.

Our Target Endpoint is like that:
<HTTPTargetConnection>
<LoadBalancer>
<Server name="targetapi"/>
</LoadBalancer>
<Path>/forecast/{private.targetURL}/</Path>
</HTTPTargetConnection>

Maybe, the AssignMessage policy isn't take the deafult configuration, so we need to setup that variable.

We try this on JS:

igallardo_1-1695062205264.png

But the debbuger shows empy,

igallardo_2-1695062247446.png

 

This is really useful for us because when we move to the PROD instance (production environment), we won't need to manually change those URLs.

 

According to the trace you are showing, the JS policy you have is running very early in the request flow.  Apigee does not select the targetserver until it is invoking the targetserver.  Your proxy logic cannot "see" what the target server will be, before Apigee selects it.  You can "see" the selected targetserver, after Apigee has selected it.  The earliest point where this is possible is in the Target Response flow. 

I will repeat my recommendation from my prior post: If you want to perform your own selection of server + path (each server might have a different path), then I suggest that you avoid using the Apigee TargetServer mechanism, and use your own. You could implement that in JavaScript and rely on settings stored in KVM or a .properties file.  If you want to select JUST the server, then the built-in Apigee TargetServer mechanism will be suitable.