Module: restHelper

This class includes constants and helper methods relating to REST operations and URIs as well as references to the wellKnownPorts and restOperationFactory instances


Examples:

MyWorker.prototype.onGet = function(restOperation) {
  var body = {
    state: { foo: "bar"},
    selfLink: this.restHelper.makePublicUri(restOperation.getUri()).href,
    kind: this.restHelper.makeKind(this.WORKER_URI_PATH, this)
  };
  restOperation.setStatusCode(200).setBody(body);
  this.completeRestOperation(restOperation);
};

restHelper Methods

(static) -> initialize()

Initialization function to set up default values.

Returns:
Type Description
Object A reference to the current restHelper object.

(static) -> getDefaultTimeout()

The timeout value for a RestOperation request.

Returns:
Type Description
number default timeout in miliseconds

(static) -> makePublicUri(uri)

Inspects the input URI and returns either the unmodified input URI or a URI that is publicly accessible through the public HTTPS end-point. If the URI is modified it will always be a full URI of the following form: https://localhost/mgmt/[path] Query parameters are not preserved. Public URIs should not contain any queries. The URI host name is always set to “localhost” forcing the remote client to replace the host name for an appropriate IP address (self-ip, management ip or some front end proxy)

1) If the input URI has a fully qualified DNS host name (not an IP address), the returned URI matches the input URI

2) If the input URI is relative, the result will be a fully qualified HTTPS URI with host set to “localhost” plus potentially some other annotations

Parameters:
Name Required Type Default Value Description
uri Y url   possibly non-public uri that needs to be made ‘public’

Returns:
Type Description
url url in output-standard format

Examples:

var url = require("url");
var url1="http://user:pass@host.com:8080/p/a/t/h?query=string#hash";
var urlObj = this.restHelper.makePublicUri(url1);
const logger = require('f5-logger').getInstance();
logger.info(url1 + " became " + urlObj.href);

//outputs: http://user:pass@host.com:8080/p/a/t/h?query=string#hash became
// https://localhost:443/mgmt/p/a/t/h
myWorker.prototype.getExampleState = function() {
   return {
      kind: this.restHelper.makeKind(this.WORKER_URI_PATH, this),
      selfLink: this.restHelper.makePublicUri(this.getUri()).href
   };
};

(static) -> makeKind(workerUriPath, state, [suffix])

Constructs a string that identifies a PODO for a REST worker

Parameters:
Name Required Type Default Value Description
workerUriPath Y string   Worker URI path, should be the same as the WORKER_URI_PATH field of the worker
state Y Object   A prototype representing worker
suffix N string   suffix used to append to name

Examples:

// make the kind to return in the "kind" field if needed manually
var kindName = "shared:echo:child:superworkerstate";
function SuperWorker(){
  // prepare other needed fields
  this.generation = 100;
  this.time = -9;
}

var worker = new SuperWorker();
var kind = this.restHelper.makeKind("shared/echo/child", worker);

(static) -> isAbsolute(uri)

Checks the given url to determine if it is an absolute, as opposed to relative, path.

Parameters:
Name Required Type Default Value Description
uri Y uri   uri to be examined

Returns:
Type Description
boolean whether the url is absolute

Examples:

var abs = url.parse("http://user:pass@host.com:8080/p/a/t/h?query=string#hash");
console.log(this.restHelper.isAbsolute(abs)); // logs true

var rel = url.parse("//user:pass@host.com:8080/p/a/t/h?query=string#hash");
console.log(this.restHelper.isAbsolute(rel)); // logs false

(static) -> buildUriPath(pathSegments)

Concatenate the pathSegments into a path. It’s okay for each segment to start or end with a PATH_SEPARATOR (/): the final path will start with a separator, each pathSegement will be separated by exactly one separator, and the path will not end with a separator. If you pass a pathSegement that includes embedded separators, you’re on your own.

Parameters:
Name Required Type Default Value Description
pathSegments Y Array    

Returns:
Type Description
string the concatenated path

Examples:

var uriPath=this.restHelper.buildUriPath(["my","path","here"]);
logger.info("buildUriPath:"+uriPath);
// expect: buildUriPath:/my/path/here

(static) -> normalizeUriPath(pathname)

Return a normalized (cannonized) path

Parameters:
Name Required Type Default Value Description
pathname Y string   path to be normalized

Returns:
Type Description
string normalized path

(static) -> makeRestjavadUri(path, [query])

construct request uris for restjavad server

Parameters:
Name Required Type Default Value Description
path Y string   restjavad worker uri path or any public path starts with /mgmt/
query N     query parameters to encode

Returns:
Type Description
url resulting Uri

Examples:

const NODE_URI = '/mgmt/tm/ltm/node';
uri = this.restHelper.makeRestjavadUri(NODE_URI + '/' + "1");
restOp = this.restOperationFactory.createRestOperationInstance()
  .setUri(uri)
  .setIsSetBasicAuthHeader(true)
  .setReferer(this.getUri().href);
logger.info("makeRestjavadUri:"+uri.href);
// expect makeRestjavadUri:http://localhost:8100/mgmt/tm/ltm/node/1

(static) -> makeRestnodedUri(path, query)

Construct a restnoded URI object from a given path and query

Parameters:
Name Required Type Default Value Description
path Y string   Uri path
query Y object   Query parameters

Returns:
Type Description
object url Resulting Uri

Examples:

makeRestnodedUri("/a/b/c", "someQuery").href == "http://localhost:8105/a/b/c?someQuery"

(static) -> makeExternalUri(hostname, path)

Construct a node URI object for an external request given a path and hostname

Parameters:
Name Required Type Default Value Description
hostname Y string   ip address of remote device
path Y string   uri path you would like to access

Returns:
Type Description
object url

Examples:

makeExternalUri("10.10.10.1", "/a/b/c?q").href == "https://10.10.10.1:443/a/b/c/?q"

(static) -> getQueryParam(uri, paramName)

Take a URI and look for a query parameter

Parameters:
Name Required Type Default Value Description
uri Y url   uri to parse (ie. uri = http://localhost:8105/tm/shared/iapp/processors/vs-js?blockId=212e010b-fcac-4ee4-92ba-a3c393887ce8)
paramName Y string   query parameter to get value of (ex. paramName = blockId;)

Returns:
Type Description
  value of query parameter or null if not found

Examples:

// for url something?Part=Engine
aPart = this.restHelper.getQueryParam(restOperation.uri, "Part");
logger.info("getQueryParam:"+ aPart);
// expect getQueryParam:Engine

(static) -> jsonPrinter(o)

Simple helper function that fixes JSON.stringify issues with circular references. Useful for printing RestOperation.

Parameters:
Name Required Type Default Value Description
o Y Object   Any object with or without circular references.

Returns:
Type Description
string
  • the jsonified representation

Examples:

// log the rest operation context
var jp  = this.restHelper.jsonPrinter(restOperation);
logger.info(jp);