Writing a Script

This topic includes guidelines for system administrators on how to write a script for the automation service, details about the APIs that are available to execute a script, and how to invoke scripts using the REST API.

Calling an Endpoint for Script Execution

You need to call an endpoint to execute a script.

Request

  • Path: v1/scripts/{script_name}/execute, for example, https://subscription.automation.ingress.eu20.ve.shoot.live.k8s-hana.ondemand.com/app/v1/scripts/renameVisualization/execute

  • HTTP method: POST

  • Request body: It can be empty if the script doesn't expect any parameters, otherwise the request body should be a valid JSON value. For example:
    • A string value: "a_string_value"

    • A numeric value: 123

    • A boolean value: true

    • An object: { "visualizationId": "123" }

    • An array of JSON values: [ "a_string", 123, { "visualizationId": "123" } ]

Response

If the script execution is successful, then the endpoint can return the following response:
  • Status: 200 OK

  • Response Body: a valid JSON value

OR,

  • Status: 204 No Content

  • Response Body: empty

If the script execution has failed, then the endpoint can return the following response:
  • Status: 500 Internal Server Error

  • Response Body: a JSON object describing the error

If the script calls exit(false) then the return value is as follows:
If the script calls exit(false, errorObject) then the return value is as follows:
If the script fails due to JavaScript error, such as SyntaxError or an unhandled exception the return value is as follows:
The events[0].values.error property contains a stringified object that can be parsed to display the error. It has the following structure:

Additionally, the endpoint can return other usual error statuses such as 404 Not Found and so on, if the script doesn't exist.

Script Execution through Script Engine

Scripts can be written in JavaScript and TypeScript.

The .d.ts files for the API provided by Visualization's scripting engine.

The scripting engine is a backend extensibility mechanism for Visualization which allows a high degree of workflow customization as per the customer requirements.

Scripts can use any modern JavaScript features but you must be aware of the differences between the built-in features of JavaScript and Web API.
The scripting engine provides two system modules:
  • sys:runtime/v1: This system is for objects and functions useful for all scripts and modules.

  • sys:data_sources/v1Manage Data Sources app in Visualization.

To have access to the system modules, import them in user scripts as shown below:
User scripts can import other user modules. Use the usr: prefix in the import statement as shown below:

Module sys:runtime/v1

The sys:runtime/v1 module provides functionality required by any script.

  • Type JsonValue: The JSON Value type is defined as follows:
    Examples:
  • Object params: The paramsobject is defined as follows:
    If a valid JSON value is passed to the script in the v1/scripts/{script_name}/execute request body, then it's available in the params object. Otherwise, the params object is undefined.
  • Object console: The console API can be used to log messages, such as for debugging purposes and so on.

    The console object is not a part of the JavaScript's built-in features, but is a Web API.

    The messages written via the console API are stored in the storage database.

    The script execution log can be fetched via endpoint v1/scripts/{script_name}/executions/latest?log=true.

  • Function fetch: The fetch function can be used to make requests to other services.

    The fetchAPI mimics the Fetch Web API.

    • fetch parameters

      • resource: This defines the resource that you wish to fetch. It is not an arbitrary URL, but it should be a string in the form service:path[?query].

        The path component is an endpoint (such as v1/scenes/123/meshes) when the service is a name of one of the Visualization services, such as authoring, automation, business data, conversion, storage, vb-renderer, visualization, and so on.

      • options: An object containing any custom settings that you want to apply to the request.

        The possible options are:
        • method: It's the request method, such as DELETE, GET, PATCH, POST, PUT. The default is GET.

        • headers: It's like any header that you can add to your request, contained within an object literal with string values.

          If the script makes a PATCH, POST or PUT request and passes a JSON value as the request body, it's the script's responsibility to set the Content-Type: application/json header.

        • body: Any body that you want to add to your request, can be a string or an ArrayBuffer object. The string body is encoded as UTF-8. The request using the GET or HEAD method can't have a body, while a request using the PATCH, POST, or PUT method must have a body.

    • fetch result: The fetch function starts the process of fetching a resource from the network, returning a Promise which is fulfilled once the response is available.

      The Promise resolves to the Response object representing the following response to your request:
      • status: This read-only property of the Response interface contains the HTTP status code of the response.

      • headers: This read-only property of the Response interface contains the headers associated with the response.

      The following methods of the Response interface are asynchronous and they consume the Request body (which means you can't call them more than once) are:
      • arrayBuffer(): This method returns a Promise that resolves with an ArrayBuffer. If the request body is empty the ArrayBuffer's length is zero.

      • text: This method returns a promise that resolves with a string. If the request body is empty the string length is zero.

      • json(): This method returns a promise that resolves with a JsonValue. If the request body is empty the promise results in a rejected status with an error.

  • Function exit: The exit function can be used by scripts to return a value from the v1/scripts/{script_name}/execute endpoint.

    If the resultOrError parameter is not undefined, it's returned as the response body.

    If the script does not call the exit function, then it is called by the Script Executors when the script finishes with succeeded equal to true and resultOrError equal to undefined.
    If the exit function is called with parameter succeeded equal to true then the v1/scripts/{script_name}/execute endpoint returns the following HTTP statuses:
    • 200 OK: This status is returned when the resultOrError parameter is not undefined.

    • 204 No Content: This status is returned when the resultOrError parameter is undefined.

    If the exit function is called with parameter succeeded equal to false, then the v1/scripts/{script_name}/execute endpoint returns 500 Internal Server Errorwith a JSON object describing the error.

Module sys:data_sources/v1

The sys:data_sources/v1 module provides function fetchData to request data from the data sources created in the Manage Data Sources app in Visualization.
  • dataSourceName: It's a name of the data source as defined in the Manage Data Sources app.

  • params: It's an object whose properties define the parameters and their values used in the data source mapping.

If the function is successful, it returns a Promise that resolves with a JSON value fetched from the data source.

If the function fails, it returns a Promise that rejected status with an Error object containing an error description.

Script and Module Examples

Rename Visualization

Find Last Public Visualization Version

This module exports a function which finds the last public visualization version.
The module can be imported by other script or module. If you add the name last_visualization_version in a module in the code mentioned above, you can import and use it as follows:

Create Private Visualization Version

Copy Associations from One Scene to Another

Start New Authoring Session

Close Authoring Session

Publish Visualization

Add Prefix to All Node Names

If you are putting all the previously defined functions into a module called my_shared_lib, you can implement a script that renames all nodes, such as by adding a prefix.