自定义脚本 - V1 API

你需要会使用下面两个编程语言:

  • JavaScript Object Notation (JSON)
  • Python

自定义脚本文件的结构大致如下:

  1. 导入所需安装包
  2. 获取输入参数
  3. 实施逻辑
  4. 返还JSON

导入所需安装包

以下列出了经常使用的安装包以及它们的作用:

安装包    作用

Sys    获取输入参数

json    处理JSON数据

requests    调用API

datetime    将时间从毫秒转换为所需的日期格式

  

获取输入参数

使用sys.argv[索引]可以获取脚本文件中的参数,其中的索引从1到通过的参数。

当该通过的参数为 $COMPLETE_JSON_FILE 时(去往包含JSON文件的路径), JSON文件通过使用如下代码段可读:

file_Path = sys.argv[1]
 
with open(file_Path) as data_file:
data = json.load(data_file)

实施逻辑

以下代码段用于调用API:

with requests.Session() as s:
url = 'api_url'
r = s.post(url,verify=True, data=post_data,headers=headers)

按要求构建 api_url, post_data 和 headers 。

以下代码段用于将时间从毫秒转换为所需的日期格式:

date = datetime.datetime.fromtimestamp(int(millisec)/1e3).strftime('%d %b %Y, %H:%M:%S')

构建返还JSON

例如{"key":"value"}的JSON样本可以构建如下:

json = {}

json["key"] = "value"

print(json)

 

例如[{"key":"value"}]的JSON组可以构建如下:

 

json = {}

json["key"] = "value"

result = []

result.append(json)

print(result)

 

 

使用脚本中的返还JSON可以执行一些操作。了解使用返还JSON执行的操作请点击 这里.

下面是一个JSON架构示例,可以更新请求的附加字段  JIRA_ISSUE_ID & JIRA_ISSUE_URL 并为其添加注释。

 

jiraissueid = responseobj['id']

jiraissueurl = responseobj['self']

 

updateReqArray={}

updateReqArray['JIRA_ISSUE_ID']=jiraissueid

updateReqArray['JIRA_ISSUE_URL']=jiraissueurl

 

updateFieldsJson={"INPUT_DATA":[]}

updateFieldsJson['INPUT_DATA'].append(updateReqArray)

updateFieldsJson["OPERATIONNAME"]="EDIT_REQUEST"

 

note={}

note["notestext"] = "Jira Request Created with ID: "+jiraissueid+"</br> Issue Link:  "+jiraissueurl

 

noteObject={}

noteObject["notes"]=note

 

addNoteJson={}

addNoteJson['INPUT_DATA']=[]

addNoteJson['INPUT_DATA'].append(noteObject)

addNoteJson["OPERATIONNAME"]="ADD_NOTE"

 

resultjson={}

resultjson["result"] = "success"

resultjson["message"] = "A Jira Request has been Created. Note with the Issue ID and URL has been added."

resultjson["operation"] = []

resultjson["operation"].append(updateFieldsJson)

resultjson["operation"].append(addNoteJson)

 

#Returning the JSON

print(resultjson)

 

 

输入参数

请求参数和所有API参数可被直接用作脚本文件中的参数。注意该参数不能被用在类文件中。

以下列出支持的参数。

Parameter    Returns Request Property

$WORKORDERID    Request ID

$REQUESTER    Name of the requester

$CREATEDBY    Name of the technician who created the ticket on requester's behalf

$CREATEDTIME    Time at which the request was created (Millisec)

$DUEBYTIME    Due by time (Millisec)

$RESPONSEDUEBYTIME    Response due by time (Millisec)

$RESPONDEDTIME    Time at which the request was responded to (Millisec)

$RESOLVEDTIME    Time at which the request was resolved (Millisec)

$COMPLETEDTIME    Time at which the request was closed (Millisec)

$SHORTDESCRIPTION    Shortened description

$TIMESPENTONREQ    Time spent on request (Hours and minutes)

$SUBJECT    Subject

$REQUESTTEMPLATE    Request template using which the corresponding request was created (String)

$MODE    Mode (String)

$SLA    Service Level Agreement (String)

$ASSET    Asset (String)

$DEPARTMENT    Department (String)

$EDITORID    The user ID of the Editor of the Service Request (Long)

$EDITING_STATUS    The editing status of the Service Request (Long)

$IS_CATALOG_TEMPLATE    Denotes whether the request is created using service catalog or incident catalog (Boolean)

$SITE    Site (String)

$ISVIPUSER    Indicates whether the user is a VIP or not (Yes or no)

$SERVICE    Service category (String)

$CATEGORY    Category (String)

$SUBCATEGORY    Subcategory (String)

$ITEM    Item (String)

$TECHNICIAN    Name of the Technician assigned

$TECHNICIAN_LOGINNAME    Login name of the logged in technician

$STATUS    Status (String)

$PRIORITY    Priority (String)

$LEVEL    Level (String)

$IMPACT    Impact (String)

$URGENCY    Urgency (String)

$IMPACTDETAILS    Impact Details

$REQUESTTYPE    Type of the Request

$APPROVAL_STATUS    The Approval status of the request (String)

$CLOSURECODE    Reason for closing a request (String)

$CLOSURECOMMENTS    Explanation for closing the request

$FCR    First Call Resolution (Boolean)

$YETTOREPLYCOUNT    A count of the number of replies to which the technician has not yet replied

$GROUP    Group (String)

$DESCRIPTION    Full description

$LOGIN_NAME    Login name through which the request was raised

$LOGGEDIN_USER_NAME    Display name of the user who raised the request

 

 

其他支持的参数

$DIFF_JSON ---> When a request is updated, the difference in the previous data and updated data will be passed as JSON string.

$COMPLETE_JSON_FILE ---> Complete request object and the difference object will be saved to a JSON file and the file path will be passed as string. E.g. (SDP_Home\integration\custom_scripts\request\12_1426143538036.JSON).

$COMPLETE_JSON 架构

{

  "request": {

    "WORKORDERID": "17",

    "REQUESTER": "Annie",

    "CREATEDBY": "Heather Graham",

    "CREATEDTIME": "1477984359352",

    "DUEBYTIME": "1477991559352",

    "RESPONSEDUEBYTIME": "1477991559052",

    "RESPONDEDTIME": "0",

    "RESOLVEDTIME": "0",

    "COMPLETEDTIME": "0",

    "SHORTDESCRIPTION": "Please create a user account for the new joinee and provide the requested resources.",

    "TIMESPENTONREQ": "0hrs 0min",

    "SUBJECT": "New hire request form",

    "REQUESTTEMPLATE": "New Hire",

    "MODE": "Web Form",

    "SLA": "Medium SLA",

    "ASSET": "iPhone 6 - PO# Apple_iPhone_43[52], MacBook Pro",

    "IS_CATALOG_TEMPLATE": "false",

    "SITE": "Zoho Corp - Chennai",

    "ISVIPUSER": "No",

    "SERVICE": "User Management",

    "CATEGORY": "User Administration",

    "TECHNICIAN": "Shawn Adams",

    "TECHNICIAN_LOGINNAME": "shawn.adams",

    "STATUS": "Open",

    "PRIORITY": "Medium",

    "LEVEL": "Tier 1",

    "IMPACT": "Medium",

    "URGENCY": "Normal",

    "REQUESTTYPE": "New Joinee Request",

    "FCR": "false",

    "INTERESTEDPARTY": "hradmin1@org.com,hradmin2@org.com",

    "GROUP": "HR group",

    "DESCRIPTION": "Please create a user account for the new joinee and provide the requested resources.",

    "Languages Known": [

      "Java",

      "Python",

      "SQL"

    ],

    "Job Type": "Software Developer Engineer",

    "Qualification": "B. Tech Computer Science",

    "Address": "My Door No : xx,\r\nMy street name,\r\nCity I live in.\r\nPostal code",

    "Expected DOJ": "15 Nov 2016, 08:00:00",

    "New Joinee Name": "John William",

    "Years of Experience": "2.0",

    "LOGIN_NAME": "heather.graham",

    "LOGGEDIN_USER_NAME": "Heather Graham"

  }

}

$DIFF_JSON 架构

{

  "diff": {

    "DUEBYTIME": {

      "OLD": "1476961659524",

      "NEW": "1476940059524"

    },

    "MODE": {

      "OLD": "E-Mail",

      "NEW": "Phone Call"

    },

    "SLA": {

      "OLD": "Low SLA",

      "NEW": "Medium SLA"

    },

    "SERVICE": {

      "OLD": "",

      "NEW": "Data Management"

    },

    "CATEGORY": {

      "OLD": "Desktop Hardware",

      "NEW": "General"

    },

    "TECHNICIAN": {

      "OLD": "Howard Stern",

      "NEW": "Jeniffer Doe"

    },

    "PRIORITY": {

      "OLD": "Low",

      "NEW": "Medium"

    },

    "LEVEL": {

      "OLD": "",

      "NEW": "Tier 2"

    },

    "IMPACT": {

      "OLD": "Low",

      "NEW": "Medium"

    },

    "URGENCY": {

      "OLD": "Low",

      "NEW": "Normal"

    },

    "REQUESTTYPE": {

      "OLD": "Incident",

      "NEW": "Request For Information"

    },

    "GROUP": {

      "OLD": "Network",

      "NEW": "Printer Problems"

    }

  }

}

一旦编辑一个请求后, 就产生 DIFF_JSON,可根据字段变化设计工作流。

$COMPLETE_JSON_FILE

$COMPLETE_JSON_FILE表示包含$COMPLETE_JSON 和 $DIFF_JSON的文件的文件路径放在一个JSON中。不向类/脚本传递过多的参数,而是通过文件路径,脚本可以打开文件并访问所需的值。

该文件是临时创建的,并在脚本执行后删除。

该临时JSON文件被创建于 SDP_Home\integration\custom_scripts\request\ 目录下, 文件名为<requestid_timestamp>.json.

Back to Top