Introduction

  • Any operations that can be performed on orders, workflows, jobs and related objects such as cancelling, suspending and resuming orders are performed by the JS7 - REST Web Service API.
  • In addition, a PowerShell module is available for simplified access to the REST Web Service API. This is described in the JS7 - PowerShell Module article.
  • The REST Web Service API can also be accessed from Python . 

Usage

  • The REST Web Service API is called by using a HTTP client that sends JSON based requests and receives JSON based responses.
  • The following REST Web Service API requests are available:
  • Requirements
    • REST Web Service API requests should use HTTP POST operations as indicated.
    • Should the idle timeout between two web service requests exceed the JOC Cockpit session timeout then a login has to be performed, see the JS7 - Settings article for more information.

Example for Python

  • Example: Create a workflow with a single job in the JS7 inventory and deploy the workflow.
    • The attached example js7_api_sample.py is for use with Python3. We do not consider this a perfect example for handling web service requests. However, it shows the building blocks:
      • Example for use with curl to get list of orders scheduled until a given date
        #!/usr/bin/env python3
        
        from base64 import b64encode
        import json
        import requests
        
        baseUrl = "http://localhost:4446"
        certFile = ""
        # baseUrl = "https://joc-2-0-primary.sos:4443"
        # certFile = "/home/sos/certs/root-ca.crt"
        username = "root"
        password = "root"
        
        
        # ---------- JS7 functions ----------
        
        def js7_connect( base_url, username, password, cert_file="" ):
            body = {}
            headers = {'Content-Type': 'application/json' }
            if cert_file:
                response = requests.post(base_url + "/joc/api/authentication/login", json=body, headers=headers, auth=(username, password), verify=cert_file)
            else:
                response = requests.post(base_url + "/joc/api/authentication/login", json=body, headers=headers, auth=(username, password))
            return response
        
        def js7_disconnect( base_url, access_token, cert_file="" ):
            body = {}
            headers = {'Content-Type': 'application/json', 'X-Access-Token': access_token}
            if cert_file:
                response = requests.post(base_url + "/joc/api/authentication/logout", json=body, headers=headers, verify=cert_file)
            else:
                response = requests.post(base_url + "/joc/api/authentication/logout", json=body, headers=headers )
            return response
        
        def js7_store_inventory_item( base_url, access_token, object_path, object_type, object_json, cert_file="" ):
            body = { "path": object_path, "objectType": object_type, "valid": False, "configuration": object_json }
            headers = {'Content-Type': 'application/json', 'X-Access-Token': access_token}
            if cert_file:
                response = requests.post(base_url + "/joc/api/inventory/store", json=body, headers=headers, verify=cert_file)
            else:
                response = requests.post(base_url + "/joc/api/inventory/store", json=body, headers=headers )
            return response
        
        def js7_deploy( base_url, access_token, controller_id, object_path, object_type, cert_file="" ):
            body = { "controllerIds": [ controller_id ], "store": { "draftConfigurations": [ { "configuration": { "path": object_path, "objectType": object_type } } ] } }
            headers = {'Content-Type': 'application/json', 'X-Access-Token': access_token}
            if cert_file:
                response = requests.post(base_url + "/joc/api/inventory/deployment/deploy", json=body, headers=headers, verify=cert_file)
            else:
                response = requests.post(base_url + "/joc/api/inventory/deployment/deploy", json=body, headers=headers )
            return response
        
        # ---------- JS7 functions ----------
        
        
        # ---------- main ----------
        response = js7_connect( baseUrl, username, password, certFile )
        accessToken = response.json()["accessToken"]
        
        controllerId = "controller"
        workflowPath = "/Samples/Python/sample_001"
        workflow = r"""
        {
          "version": "1.6.0",
          "timeZone": "Etc/UTC",
          "instructions": [
            {
              "TYPE": "Execute.Named",
              "jobName": "job1",
              "label": "job1"
            }
          ],
          "jobs": {
            "job1": {
              "agentName": "primaryAgent",
              "withSubagentClusterIdExpr": false,
              "executable": {
                "TYPE": "ShellScriptExecutable",
                "script": "#!/bin/bash\n\necho \"hello\"",
                "v1Compatible": false
              },
              "skipIfNoAdmissionForOrderDay": false,
              "parallelism": 1,
              "graceTimeout": 1,
              "failOnErrWritten": false,
              "warnOnErrWritten": false
            }
          }
        }
        """
        workflowJson = json.loads(workflow, strict=False)
        
        response = js7_store_inventory_item( baseUrl, accessToken, workflowPath, 'WORKFLOW', workflowJson, certFile )
        
        response = js7_deploy( baseUrl, accessToken, controllerId, workflowPath, 'WORKFLOW', certFile )
        
        response = js7_disconnect( baseUrl, accessToken, certFile )
        
        # ---------- main ---------- 
      • Explanations

        • Line 7-10: Depending on your JOC Cockpit installation the protocol will be http or https. The default port is 4446 but might have been modified during setup.
        • Line 11-12: Default credentials after installation include the account "root" and password "root". The credentials might have been changed after setup of the JOC Cockpit. There are alternative ways for secure management of credentials when using the JS7 REST Web Service API.
        • Line 57-58: The request to the /authentication/login web service returns an access token which is used with further requests.
        • Line 60: The Controller ID is specified during installation of the Controller and identifies a Controller standalone instance or a Controller cluster. The workflow will be deployed to the indicated Controller.
        • Line 61: Specifies the path to the workflow in the JS7 inventory. Any folders included with the path are automatically created.
        • Line 62-90: The workflow is created from a here-string. There are better programmatic ways how to do this.
        • Line 93: The workflow is stored to the JS7 inventory. With this operation being completed the workflow is visible from the JOC Cockpit user interface.
        • Line 95: The workflow is deployed to the indicated Controller.
        • Line 97: Always perform a logout and consider the session idle timeout.



  • No labels