Change Multiple Netsuite Credentials in Multiple Zuar Runner Jobs

Zuar Runner Netsite jobs do not currently have the ability to use a named credential. Because of this, if you have a large number of Netsuite jobs, you will have to update each of them whenever you update the credentials in Netsuite. Here is a Python SDK script that will do that for you.

changeNetsuiteCreds.py

from mitto_client import Mitto
import getopt
import requests
import sys

argv = sys.argv[1:]

try:
    opts, args = getopt.getopt(argv, "i:")
except:
    print("Error")

infile = "/var/mitto/data/netsuite_data.txt"

for opt, arg in opts:
    if opt in ['-i', '--infile']:
        infile = arg

API_KEY="<API_KEY>"
MITTO_INSTANCE="<MITTO_INSTANCE_URL>"

mitto = Mitto(
        base_url=MITTO_INSTANCE,
        api_key=API_KEY
)

if infile:
    f = open(infile)
    for line in f:
        line = line.rstrip()             # remove the trailing newline
        if line.startswith('ACCOUNT: '):
            account = line[9:]
        elif line.startswith('CONSUMER_KEY: '):
            consumer_key = line[14:]
        elif line.startswith('CONSUMER_SECRET'):
            consumer_secret = line[17:]
        elif line.startswith('TOKEN_ID: '):
            token_id = line[10:]
        elif line.startswith('TOKEN_SECRET: '):
            token_secret = line[14:]
        elif line.startswith('FIRST_JOB: '):
            first_job = int(line[10:])
        elif line.startswith('LAST_JOB: '):
            last_job = int(line[9:])
        else:
            raise ValueError('Unknown attribute: %r' % line)
    f.close()

for jobid in range(first_job,last_job+1):

    try:
        #Get the job config:
        job = mitto.get_job(job_id = jobid)
            #Find tablename in job
        for k,v in job["conf"]["input"]["credentials"].items():
            # Get the dbo lines
            if k == "account" and v == account:
                job["conf"]["input"]["credentials"]["consumer_key"] = consumer_key
                job["conf"]["input"]["credentials"]["consumer_secret"] = consumer_secret
                job["conf"]["input"]["credentials"]["token_id"] = token_id
                job["conf"]["input"]["credentials"]["token_secret"] = token_secret
                print("new creds: ")
                print(job["conf"]["input"]["credentials"])
                resp = mitto.update_job(jobid, job)
    except requests.exceptions.HTTPError as e:
        print(e)

This script requires an input file with a default location of /var/mitto/data/netsuite_data.txt. This location allows you to create a file with the appropriate input and upload it in your File Manager as netsuite_data.txt. You may also choose a different location for the input file by using the -i <filepath/filename> option. The file will also contain variables which define the jobs to search by providing a first job id and a last job id.

netsuite_data.txt

ACCOUNT: <ACCOUNT_ID>
CONSUMER_KEY: <CONSUMER_KEY>
CONSUMER_SECRET: <CONSUMER_SECRET>
TOKEN_ID: <TOKEN_ID>
TOKEN_SECRET: <TOKEN_SECRET>
FIRST_JOB: <first job>
LAST_JOB: <last_job>

This script will connect to the Zuar Runner instance, and alter the following lines of any jobs where the input has an account name that matches the one in the input file and a job id within the provided range:

CONSUMER_KEY
CONSUMER_SECRET
TOKEN_ID
TOKEN_SECRET

To run this as a Zuar Runner CMD job, you will need to create a Python virtual environment in /var/mitto/data/ like this:
Create CMD job with the following configuration:

{
    "shell": true
    "cmd:: "/app/env/bin/python3 -m venv /var/mitto/data/<virtualenv_directory>"
    "exec": false
    "timeout": null
    "cmd_env": {
    }
}
* Note: If you are using Mitto 2, the cmd line would be this:
"cmd: /opt/mitto/pyenv/bin/python3 -m venv /var/mitto/data/<virtualenv_directory>"

You will need to install the following Python packages within your new Python Virtual environment:

mitto_client-1.0-py2.py3-none-any.whl requests hjson

  • Note: you will need to upload the mitto_client-1.0-py2.py3-none-any.whl file via the File Manager.

Install Python packages via PIP via a CMD job with this configuration:

{
  "cmd":
    '''
    "/var/mitto/data/<virtualenv_directory>/bin/pip3 install /var/mitto/data/mitto_client-1.0-py2.py3-none-any.whl"
    "/var/mitto/data/<virtualenv_directory>/bin/pip3 install requests
    "/var/mitto/data/<virtualenv_directory>/bin/pip3 install hjson
    '''
  "cmd_env": {
  }
  "exec": false
  "shell": true
}

Create the changeNetsuiteCreds.py script locally, remembering to edit the Zuar Runner URL and API key. Upload in the File Manager. Create the netsuite_data.txt file locally with the appropriate account information, and upload it in the File Manager. Finally, create the following CMD job to run them:

{
     "cmd": "/var/mitto/data/<virtualenv_directory>/bin/python3 /var/mitto/data/changeNetsuiteCreds.py",
     "cmd_env": {},
     "exec": false,
     "shell": true
 }