Is it possible to use an API call in a File template

Hello All,

I’m reaching out again as I posted before asking about what I would find out to be file templates.

Post is here.

This has worked wonders for what my team is trying to do I’m wondering though if there is a way to do an API call within these templates to grab some exact info on two options lists that we have. Instead of me hardcoding them and having the host pull it.

Seems kind of out in the wild but I thought I would reach out here first.

I’m not quite following the ask. If you are able to elaborate a bit more or do a run through of your process.

Morpheus variables (inputs/option lists) automatically get replaced within File Templates with the standard <%= %> syntax so there should be no need for an API call to occur.

Chris and OP,

I believe that beyond using the <%= %> syntax for simple variable substitution, using <% %> tags instead would allow more elaborate Groovy code to be run. Which then allows you to do all sorts of complex things, including API calls to other servers etc.

Just have to keep in mind that that code is run on the Morpheus appliance side, not the Endpoint side and that the output of the Groovy code is combined with the rest of the file template before it’s sent to the Endpoint.

Two important notes on the use of file templates:

  1. Any empty first and last lines in the file template are stripped, as well as the last CR+LR. I had a ticket open for this as a defect but the development team came back with:

String autotrim is system wide. A FILE template should not care about whitespace in general and trim is considered acceptable

  1. I have an open ticket for “File Templates alters existing/new file and folder permissions on Linux system to be less restrictive than they were before deploying the File Template”. The current stance from support is that this is “as designed in order to allow the Agent to write the file”. I’m about to update my response that this can’t be correct as it’s a) a security issue and b) can cause all sorts of system issues (up and not limited to unbootable systems) if people aren’t aware of this “design feature”. The workaround is to always dump file templates into a holding place and use a task to move them into the right place. Often times it then becomes easier to HEREDOC the file template directly in the Task instead of using file templates.

-Yaron.

Thanks for the response,

what I’m trying to accomplish is when I call an option lists during lets say post prov I need it to return the name and not the Value, It sounds weird but we’re trying to the actual meaning and not the Value associated with it. The only way I can see atm would be to hardcore the name but i am trying to find a way for it to print name and not the value just in the case.

If the options mapping doesn’t already contain the name of the input versus the value, you should be able to find the names via the keySet() method.

For example I used the below line to figure out what variables are set in a particular execution context (server vs instance). I used this to understand why serverId or containerId ended up being null and throwing exceptions in my Tasks I was trying to run on the appliance and endpoints.

foo = binding.variables.keySet()

In the above ‘foo’ will end up containing an array of all variable names available within the execution context of the script.

You might be able to use the Groovy containsKey() method as well to directly see if a specific variable is set/available in a mapping. I’ve not tested this method’s availability within Morpheus though.

-Yaron.

1 Like