Getting the execution endpoint of an inbound script in a script.

Not Yet Reviewed

With the new GET /plans and GET /integrations API, we can now programmatically retrieve the endpoint to trigger another inbound integration endpoint. This means, when we want to have one script trigger another one, we don't have to store the url somewhere in code or in a constant. If we do store it in the code or in a constant, then it has potential to break if we export the plan and generally is just not clean. 

So, with these couple of snippets, you can go get the integration ID, and then build the target url. 

For now, you will need to make the target inbound integration have the URL Authentication or create a new endpoint with the basic auth creds. Unfortunately, the existing xMatters endpoint can't properly pick up the credentials. 

 

var plan = getCommPlan( "Some Plan" );
var integration = getIntegration( "Some Integration", plan.id );

/*
Once I have these details, I can then make a call to another script like so:
*/
var req = http.request({
"endpoint": "xMatters",
"path": "/api/integration/1/functions/" + integration.id + "/triggers?apiKey=" + apiKey,
"method": "POST",
"headers": {
"Content-Type": "application/json"
}
});

var newPayload = {
"name": "value"
};

var resp = req.write( newPayload);

/*
* getIntegration - Gets the integration from the parent Comm Plan
* name - The name of the integration to get
* commPlanUUID - The UUID of the comm plan
*
* returns: An "integration object": https://help.xmatters.com/xmapi/index.html#get-integrations
*/
function getIntegration( name, commPlanUUID ) {
var path = "/api/xm/1/plans/" + commPlanUUID + "/integrations?integrationType=INBOUND_WEBHOOK";
return doPages( name, path );
}


/*
* getCommPlan - Gets the comm plan by name
* name - The name of the comm plan to get
*
* returns: A "comm plan object": https://help.xmatters.com/xmapi/index.html#communication-plan-objects
*/
function getCommPlan( name ) {
var path = "/api/xm/1/plans?planType=PLAN";
return doPages( name, path );
}

/*
* doPages - loops through the pages of the API call looking for a match on name
* name - The name of the object to get
* path - The API url path to make the call to
*
* returns: The object found
*/
function doPages( name, path ) {
var list = getMore( path );
var item;
var found = false;
var p = -1;

while( !found ) {
p++;
if( p >= list.count ) {
if( list.links.next ) {
list = getMore( list.links.next );
p = 0;
}

else
return;
}
item = list.data[p];
if( item.name == name )
return item;
}
return;
}

/*
* doMore - helper function to get the next page
* path - The API url path to make the call to
*
* returns: The next page
*/
function getMore( path ) {

var req = http.request({
"endpoint": "xMatters",
"path": path,
"method": "GET"
});
return JSON.parse( req.write().body );
}

 

 

0

Comments

0 comments

Please sign in to leave a comment.

Didn't find what you were looking for?

New post