feat(servicenow): new ServiceNow ITSM integration
31 commands: ticket lifecycle (create/update/resolve/delete, comments, work notes, links, tags, journal notes, attachments), generic table records CRUD and discovery, CMDB/user/group queries, service catalog ordering, standard change from template, AWA queue routing, generic API call, plus get_incidents ingestion with a bundled OCSF mapper. Basic or OAuth 2.0 (password grant) authentication, stdlib-only scripts. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
import base64, json, os, sys, urllib.parse, urllib.request, urllib.error
|
||||
|
||||
|
||||
def _cfg():
|
||||
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
|
||||
|
||||
|
||||
def _headers(cfg):
|
||||
h = {"Accept": "application/json", "Content-Type": "application/json"}
|
||||
if str(cfg.get("auth_type") or "basic").lower() == "oauth":
|
||||
data = urllib.parse.urlencode({
|
||||
"grant_type": "password",
|
||||
"client_id": cfg.get("client_id", ""),
|
||||
"client_secret": cfg.get("client_secret", ""),
|
||||
"username": cfg.get("username", ""),
|
||||
"password": cfg.get("password", ""),
|
||||
}).encode("utf-8")
|
||||
req = urllib.request.Request(
|
||||
cfg.get("url", "").rstrip("/") + "/oauth_token.do", data=data,
|
||||
headers={"Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"}, method="POST")
|
||||
with urllib.request.urlopen(req, timeout=60) as r:
|
||||
tok = json.loads(r.read())
|
||||
if not tok.get("access_token"):
|
||||
raise Exception("OAuth token request failed: " + json.dumps(tok))
|
||||
h["Authorization"] = "Bearer " + tok["access_token"]
|
||||
else:
|
||||
cred = (cfg.get("username", "") + ":" + cfg.get("password", "")).encode("utf-8")
|
||||
h["Authorization"] = "Basic " + base64.b64encode(cred).decode("ascii")
|
||||
return h
|
||||
|
||||
|
||||
def request(method, path, params=None, body=None, root="/api/now", versioned=True):
|
||||
cfg = _cfg()
|
||||
v = str(cfg.get("api_version") or "").strip().strip("/")
|
||||
url = cfg.get("url", "").rstrip("/") + root + ("/" + v if v and versioned else "") + path
|
||||
q = {k: str(x) for k, x in (params or {}).items() if x not in (None, "")}
|
||||
if q:
|
||||
url += "?" + urllib.parse.urlencode(q)
|
||||
data = json.dumps(body).encode("utf-8") if body is not None else None
|
||||
req = urllib.request.Request(url, data=data, headers=_headers(cfg), method=method)
|
||||
with urllib.request.urlopen(req, timeout=90) as r:
|
||||
raw = r.read()
|
||||
return json.loads(raw) if raw else {}
|
||||
|
||||
|
||||
def main():
|
||||
inputs = json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
|
||||
ticket_id = str(inputs.get("id") or "")
|
||||
if not ticket_id:
|
||||
raise Exception("id is required")
|
||||
table = str(inputs.get("ticket_type") or _cfg().get("ticket_type") or "incident")
|
||||
key = "work_notes" if inputs.get("work_note") else "comments"
|
||||
body = {key: str(inputs.get("comment") or "")}
|
||||
print(json.dumps(request("PATCH", "/table/" + urllib.parse.quote(table) + "/" + urllib.parse.quote(ticket_id), body=body)))
|
||||
|
||||
|
||||
try:
|
||||
main()
|
||||
except urllib.error.HTTPError as e:
|
||||
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
|
||||
sys.exit(1)
|
||||
except Exception as e:
|
||||
print(json.dumps({"error": str(e)}))
|
||||
sys.exit(1)
|
||||
@@ -0,0 +1,67 @@
|
||||
import base64, json, os, sys, urllib.parse, urllib.request, urllib.error
|
||||
|
||||
|
||||
def _cfg():
|
||||
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
|
||||
|
||||
|
||||
def _headers(cfg):
|
||||
h = {"Accept": "application/json", "Content-Type": "application/json"}
|
||||
if str(cfg.get("auth_type") or "basic").lower() == "oauth":
|
||||
data = urllib.parse.urlencode({
|
||||
"grant_type": "password",
|
||||
"client_id": cfg.get("client_id", ""),
|
||||
"client_secret": cfg.get("client_secret", ""),
|
||||
"username": cfg.get("username", ""),
|
||||
"password": cfg.get("password", ""),
|
||||
}).encode("utf-8")
|
||||
req = urllib.request.Request(
|
||||
cfg.get("url", "").rstrip("/") + "/oauth_token.do", data=data,
|
||||
headers={"Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"}, method="POST")
|
||||
with urllib.request.urlopen(req, timeout=60) as r:
|
||||
tok = json.loads(r.read())
|
||||
if not tok.get("access_token"):
|
||||
raise Exception("OAuth token request failed: " + json.dumps(tok))
|
||||
h["Authorization"] = "Bearer " + tok["access_token"]
|
||||
else:
|
||||
cred = (cfg.get("username", "") + ":" + cfg.get("password", "")).encode("utf-8")
|
||||
h["Authorization"] = "Basic " + base64.b64encode(cred).decode("ascii")
|
||||
return h
|
||||
|
||||
|
||||
def request(method, path, params=None, body=None, root="/api/now", versioned=True):
|
||||
cfg = _cfg()
|
||||
v = str(cfg.get("api_version") or "").strip().strip("/")
|
||||
url = cfg.get("url", "").rstrip("/") + root + ("/" + v if v and versioned else "") + path
|
||||
q = {k: str(x) for k, x in (params or {}).items() if x not in (None, "")}
|
||||
if q:
|
||||
url += "?" + urllib.parse.urlencode(q)
|
||||
data = json.dumps(body).encode("utf-8") if body is not None else None
|
||||
req = urllib.request.Request(url, data=data, headers=_headers(cfg), method=method)
|
||||
with urllib.request.urlopen(req, timeout=90) as r:
|
||||
raw = r.read()
|
||||
return json.loads(raw) if raw else {}
|
||||
|
||||
|
||||
def main():
|
||||
inputs = json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
|
||||
ticket_id = str(inputs.get("id") or "")
|
||||
if not ticket_id:
|
||||
raise Exception("id is required")
|
||||
table = str(inputs.get("ticket_type") or _cfg().get("ticket_type") or "incident")
|
||||
link = str(inputs.get("link") or "")
|
||||
text = str(inputs.get("text") or link)
|
||||
html = '[code]<a class="web" target="_blank" href="' + link + '">' + text + '</a>[/code]'
|
||||
key = "work_notes" if inputs.get("work_note") else "comments"
|
||||
body = {key: html}
|
||||
print(json.dumps(request("PATCH", "/table/" + urllib.parse.quote(table) + "/" + urllib.parse.quote(ticket_id), body=body)))
|
||||
|
||||
|
||||
try:
|
||||
main()
|
||||
except urllib.error.HTTPError as e:
|
||||
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
|
||||
sys.exit(1)
|
||||
except Exception as e:
|
||||
print(json.dumps({"error": str(e)}))
|
||||
sys.exit(1)
|
||||
@@ -0,0 +1,68 @@
|
||||
import base64, json, os, sys, urllib.parse, urllib.request, urllib.error
|
||||
|
||||
|
||||
def _cfg():
|
||||
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
|
||||
|
||||
|
||||
def _headers(cfg):
|
||||
h = {"Accept": "application/json", "Content-Type": "application/json"}
|
||||
if str(cfg.get("auth_type") or "basic").lower() == "oauth":
|
||||
data = urllib.parse.urlencode({
|
||||
"grant_type": "password",
|
||||
"client_id": cfg.get("client_id", ""),
|
||||
"client_secret": cfg.get("client_secret", ""),
|
||||
"username": cfg.get("username", ""),
|
||||
"password": cfg.get("password", ""),
|
||||
}).encode("utf-8")
|
||||
req = urllib.request.Request(
|
||||
cfg.get("url", "").rstrip("/") + "/oauth_token.do", data=data,
|
||||
headers={"Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"}, method="POST")
|
||||
with urllib.request.urlopen(req, timeout=60) as r:
|
||||
tok = json.loads(r.read())
|
||||
if not tok.get("access_token"):
|
||||
raise Exception("OAuth token request failed: " + json.dumps(tok))
|
||||
h["Authorization"] = "Bearer " + tok["access_token"]
|
||||
else:
|
||||
cred = (cfg.get("username", "") + ":" + cfg.get("password", "")).encode("utf-8")
|
||||
h["Authorization"] = "Basic " + base64.b64encode(cred).decode("ascii")
|
||||
return h
|
||||
|
||||
|
||||
def request(method, path, params=None, body=None, root="/api/now", versioned=True):
|
||||
cfg = _cfg()
|
||||
v = str(cfg.get("api_version") or "").strip().strip("/")
|
||||
url = cfg.get("url", "").rstrip("/") + root + ("/" + v if v and versioned else "") + path
|
||||
q = {k: str(x) for k, x in (params or {}).items() if x not in (None, "")}
|
||||
if q:
|
||||
url += "?" + urllib.parse.urlencode(q)
|
||||
data = json.dumps(body).encode("utf-8") if body is not None else None
|
||||
req = urllib.request.Request(url, data=data, headers=_headers(cfg), method=method)
|
||||
with urllib.request.urlopen(req, timeout=90) as r:
|
||||
raw = r.read()
|
||||
return json.loads(raw) if raw else {}
|
||||
|
||||
|
||||
def main():
|
||||
inputs = json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
|
||||
ticket_id = str(inputs.get("id") or "")
|
||||
if not ticket_id:
|
||||
raise Exception("id is required")
|
||||
table = str(inputs.get("ticket_type") or _cfg().get("ticket_type") or "incident")
|
||||
body = {
|
||||
"label": str(inputs.get("tag_id") or ""),
|
||||
"table": table,
|
||||
"table_key": ticket_id,
|
||||
"title": str(inputs.get("title") or ""),
|
||||
}
|
||||
print(json.dumps(request("POST", "/table/label_entry", body=body)))
|
||||
|
||||
|
||||
try:
|
||||
main()
|
||||
except urllib.error.HTTPError as e:
|
||||
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
|
||||
sys.exit(1)
|
||||
except Exception as e:
|
||||
print(json.dumps({"error": str(e)}))
|
||||
sys.exit(1)
|
||||
@@ -0,0 +1,60 @@
|
||||
import base64, json, os, sys, urllib.parse, urllib.request, urllib.error
|
||||
|
||||
|
||||
def _cfg():
|
||||
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
|
||||
|
||||
|
||||
def _headers(cfg):
|
||||
h = {"Accept": "application/json", "Content-Type": "application/json"}
|
||||
if str(cfg.get("auth_type") or "basic").lower() == "oauth":
|
||||
data = urllib.parse.urlencode({
|
||||
"grant_type": "password",
|
||||
"client_id": cfg.get("client_id", ""),
|
||||
"client_secret": cfg.get("client_secret", ""),
|
||||
"username": cfg.get("username", ""),
|
||||
"password": cfg.get("password", ""),
|
||||
}).encode("utf-8")
|
||||
req = urllib.request.Request(
|
||||
cfg.get("url", "").rstrip("/") + "/oauth_token.do", data=data,
|
||||
headers={"Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"}, method="POST")
|
||||
with urllib.request.urlopen(req, timeout=60) as r:
|
||||
tok = json.loads(r.read())
|
||||
if not tok.get("access_token"):
|
||||
raise Exception("OAuth token request failed: " + json.dumps(tok))
|
||||
h["Authorization"] = "Bearer " + tok["access_token"]
|
||||
else:
|
||||
cred = (cfg.get("username", "") + ":" + cfg.get("password", "")).encode("utf-8")
|
||||
h["Authorization"] = "Basic " + base64.b64encode(cred).decode("ascii")
|
||||
return h
|
||||
|
||||
|
||||
def request(method, path, params=None, body=None, root="/api/now", versioned=True):
|
||||
cfg = _cfg()
|
||||
v = str(cfg.get("api_version") or "").strip().strip("/")
|
||||
url = cfg.get("url", "").rstrip("/") + root + ("/" + v if v and versioned else "") + path
|
||||
q = {k: str(x) for k, x in (params or {}).items() if x not in (None, "")}
|
||||
if q:
|
||||
url += "?" + urllib.parse.urlencode(q)
|
||||
data = json.dumps(body).encode("utf-8") if body is not None else None
|
||||
req = urllib.request.Request(url, data=data, headers=_headers(cfg), method=method)
|
||||
with urllib.request.urlopen(req, timeout=90) as r:
|
||||
raw = r.read()
|
||||
return json.loads(raw) if raw else {}
|
||||
|
||||
|
||||
def main():
|
||||
inputs = json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
|
||||
template = inputs.get("template") or ""
|
||||
res = request("POST", "/change/standard/" + urllib.parse.quote(template), root="/api/sn_chg_rest", body={})
|
||||
print(json.dumps(res))
|
||||
|
||||
|
||||
try:
|
||||
main()
|
||||
except urllib.error.HTTPError as e:
|
||||
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
|
||||
sys.exit(1)
|
||||
except Exception as e:
|
||||
print(json.dumps({"error": str(e)}))
|
||||
sys.exit(1)
|
||||
@@ -0,0 +1,78 @@
|
||||
import base64, json, os, sys, urllib.parse, urllib.request, urllib.error
|
||||
|
||||
|
||||
def _cfg():
|
||||
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
|
||||
|
||||
|
||||
def _headers(cfg):
|
||||
h = {"Accept": "application/json", "Content-Type": "application/json"}
|
||||
if str(cfg.get("auth_type") or "basic").lower() == "oauth":
|
||||
data = urllib.parse.urlencode({
|
||||
"grant_type": "password",
|
||||
"client_id": cfg.get("client_id", ""),
|
||||
"client_secret": cfg.get("client_secret", ""),
|
||||
"username": cfg.get("username", ""),
|
||||
"password": cfg.get("password", ""),
|
||||
}).encode("utf-8")
|
||||
req = urllib.request.Request(
|
||||
cfg.get("url", "").rstrip("/") + "/oauth_token.do", data=data,
|
||||
headers={"Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"}, method="POST")
|
||||
with urllib.request.urlopen(req, timeout=60) as r:
|
||||
tok = json.loads(r.read())
|
||||
if not tok.get("access_token"):
|
||||
raise Exception("OAuth token request failed: " + json.dumps(tok))
|
||||
h["Authorization"] = "Bearer " + tok["access_token"]
|
||||
else:
|
||||
cred = (cfg.get("username", "") + ":" + cfg.get("password", "")).encode("utf-8")
|
||||
h["Authorization"] = "Basic " + base64.b64encode(cred).decode("ascii")
|
||||
return h
|
||||
|
||||
|
||||
def request(method, path, params=None, body=None, root="/api/now", versioned=True):
|
||||
cfg = _cfg()
|
||||
v = str(cfg.get("api_version") or "").strip().strip("/")
|
||||
url = cfg.get("url", "").rstrip("/") + root + ("/" + v if v and versioned else "") + path
|
||||
q = {k: str(x) for k, x in (params or {}).items() if x not in (None, "")}
|
||||
if q:
|
||||
url += "?" + urllib.parse.urlencode(q)
|
||||
data = json.dumps(body).encode("utf-8") if body is not None else None
|
||||
req = urllib.request.Request(url, data=data, headers=_headers(cfg), method=method)
|
||||
with urllib.request.urlopen(req, timeout=90) as r:
|
||||
raw = r.read()
|
||||
return json.loads(raw) if raw else {}
|
||||
|
||||
|
||||
def split_fields(s, prefix_custom=False):
|
||||
out = {}
|
||||
for part in str(s or "").split(";"):
|
||||
if "=" not in part:
|
||||
continue
|
||||
k, v = part.split("=", 1)
|
||||
k = k.strip()
|
||||
if prefix_custom and k and not k.startswith("u_"):
|
||||
k = "u_" + k
|
||||
if k:
|
||||
out[k] = v
|
||||
return out
|
||||
|
||||
|
||||
def main():
|
||||
inputs = json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
|
||||
iid = inputs.get("id") or ""
|
||||
body = {
|
||||
"sysparm_quantity": str(int(inputs.get("quantity") or 1)),
|
||||
"variables": split_fields(inputs.get("variables")),
|
||||
}
|
||||
res = request("POST", "/servicecatalog/items/" + urllib.parse.quote(iid) + "/order_now", root="/api/sn_sc", body=body)
|
||||
print(json.dumps(res))
|
||||
|
||||
|
||||
try:
|
||||
main()
|
||||
except urllib.error.HTTPError as e:
|
||||
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
|
||||
sys.exit(1)
|
||||
except Exception as e:
|
||||
print(json.dumps({"error": str(e)}))
|
||||
sys.exit(1)
|
||||
@@ -0,0 +1,79 @@
|
||||
import base64, json, os, sys, urllib.parse, urllib.request, urllib.error
|
||||
|
||||
|
||||
def _cfg():
|
||||
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
|
||||
|
||||
|
||||
def _headers(cfg):
|
||||
h = {"Accept": "application/json", "Content-Type": "application/json"}
|
||||
if str(cfg.get("auth_type") or "basic").lower() == "oauth":
|
||||
data = urllib.parse.urlencode({
|
||||
"grant_type": "password",
|
||||
"client_id": cfg.get("client_id", ""),
|
||||
"client_secret": cfg.get("client_secret", ""),
|
||||
"username": cfg.get("username", ""),
|
||||
"password": cfg.get("password", ""),
|
||||
}).encode("utf-8")
|
||||
req = urllib.request.Request(
|
||||
cfg.get("url", "").rstrip("/") + "/oauth_token.do", data=data,
|
||||
headers={"Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"}, method="POST")
|
||||
with urllib.request.urlopen(req, timeout=60) as r:
|
||||
tok = json.loads(r.read())
|
||||
if not tok.get("access_token"):
|
||||
raise Exception("OAuth token request failed: " + json.dumps(tok))
|
||||
h["Authorization"] = "Bearer " + tok["access_token"]
|
||||
else:
|
||||
cred = (cfg.get("username", "") + ":" + cfg.get("password", "")).encode("utf-8")
|
||||
h["Authorization"] = "Basic " + base64.b64encode(cred).decode("ascii")
|
||||
return h
|
||||
|
||||
|
||||
def request(method, path, params=None, body=None, root="/api/now", versioned=True):
|
||||
cfg = _cfg()
|
||||
v = str(cfg.get("api_version") or "").strip().strip("/")
|
||||
url = cfg.get("url", "").rstrip("/") + root + ("/" + v if v and versioned else "") + path
|
||||
q = {k: str(x) for k, x in (params or {}).items() if x not in (None, "")}
|
||||
if q:
|
||||
url += "?" + urllib.parse.urlencode(q)
|
||||
data = json.dumps(body).encode("utf-8") if body is not None else None
|
||||
req = urllib.request.Request(url, data=data, headers=_headers(cfg), method=method)
|
||||
with urllib.request.urlopen(req, timeout=90) as r:
|
||||
raw = r.read()
|
||||
return json.loads(raw) if raw else {}
|
||||
|
||||
|
||||
def split_fields(s, prefix_custom=False):
|
||||
out = {}
|
||||
for part in str(s or "").split(";"):
|
||||
if "=" not in part:
|
||||
continue
|
||||
k, v = part.split("=", 1)
|
||||
k = k.strip()
|
||||
if prefix_custom and k and not k.startswith("u_"):
|
||||
k = "u_" + k
|
||||
if k:
|
||||
out[k] = v
|
||||
return out
|
||||
|
||||
|
||||
def main():
|
||||
inputs = json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
|
||||
table = inputs.get("table_name") or ""
|
||||
body = split_fields(inputs.get("fields"))
|
||||
body.update(split_fields(inputs.get("custom_fields"), prefix_custom=True))
|
||||
if not body:
|
||||
raise Exception("no fields to set")
|
||||
params = {"sysparm_input_display_value": "true"} if inputs.get("input_display_value") else None
|
||||
res = request("POST", "/table/" + urllib.parse.quote(table), params=params, body=body)
|
||||
print(json.dumps(res))
|
||||
|
||||
|
||||
try:
|
||||
main()
|
||||
except urllib.error.HTTPError as e:
|
||||
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
|
||||
sys.exit(1)
|
||||
except Exception as e:
|
||||
print(json.dumps({"error": str(e)}))
|
||||
sys.exit(1)
|
||||
@@ -0,0 +1,88 @@
|
||||
import base64, json, os, sys, urllib.parse, urllib.request, urllib.error
|
||||
|
||||
|
||||
def _cfg():
|
||||
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
|
||||
|
||||
|
||||
def _headers(cfg):
|
||||
h = {"Accept": "application/json", "Content-Type": "application/json"}
|
||||
if str(cfg.get("auth_type") or "basic").lower() == "oauth":
|
||||
data = urllib.parse.urlencode({
|
||||
"grant_type": "password",
|
||||
"client_id": cfg.get("client_id", ""),
|
||||
"client_secret": cfg.get("client_secret", ""),
|
||||
"username": cfg.get("username", ""),
|
||||
"password": cfg.get("password", ""),
|
||||
}).encode("utf-8")
|
||||
req = urllib.request.Request(
|
||||
cfg.get("url", "").rstrip("/") + "/oauth_token.do", data=data,
|
||||
headers={"Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"}, method="POST")
|
||||
with urllib.request.urlopen(req, timeout=60) as r:
|
||||
tok = json.loads(r.read())
|
||||
if not tok.get("access_token"):
|
||||
raise Exception("OAuth token request failed: " + json.dumps(tok))
|
||||
h["Authorization"] = "Bearer " + tok["access_token"]
|
||||
else:
|
||||
cred = (cfg.get("username", "") + ":" + cfg.get("password", "")).encode("utf-8")
|
||||
h["Authorization"] = "Basic " + base64.b64encode(cred).decode("ascii")
|
||||
return h
|
||||
|
||||
|
||||
def request(method, path, params=None, body=None, root="/api/now", versioned=True):
|
||||
cfg = _cfg()
|
||||
v = str(cfg.get("api_version") or "").strip().strip("/")
|
||||
url = cfg.get("url", "").rstrip("/") + root + ("/" + v if v and versioned else "") + path
|
||||
q = {k: str(x) for k, x in (params or {}).items() if x not in (None, "")}
|
||||
if q:
|
||||
url += "?" + urllib.parse.urlencode(q)
|
||||
data = json.dumps(body).encode("utf-8") if body is not None else None
|
||||
req = urllib.request.Request(url, data=data, headers=_headers(cfg), method=method)
|
||||
with urllib.request.urlopen(req, timeout=90) as r:
|
||||
raw = r.read()
|
||||
return json.loads(raw) if raw else {}
|
||||
|
||||
|
||||
def split_fields(s, prefix_custom=False):
|
||||
out = {}
|
||||
for part in str(s or "").split(";"):
|
||||
if "=" not in part:
|
||||
continue
|
||||
k, v = part.split("=", 1)
|
||||
k = k.strip()
|
||||
if prefix_custom and k and not k.startswith("u_"):
|
||||
k = "u_" + k
|
||||
if k:
|
||||
out[k] = v
|
||||
return out
|
||||
|
||||
|
||||
NAMED_FIELDS = [
|
||||
"short_description", "description", "urgency", "impact", "priority", "state",
|
||||
"category", "subcategory", "caller_id", "assigned_to", "assignment_group",
|
||||
"comments", "work_notes",
|
||||
]
|
||||
|
||||
|
||||
def main():
|
||||
inputs = json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
|
||||
table = str(inputs.get("ticket_type") or _cfg().get("ticket_type") or "incident")
|
||||
body = {}
|
||||
for k in NAMED_FIELDS:
|
||||
v = inputs.get(k)
|
||||
if v not in (None, ""):
|
||||
body[k] = v
|
||||
body.update(split_fields(inputs.get("fields")))
|
||||
body.update(split_fields(inputs.get("custom_fields"), prefix_custom=True))
|
||||
params = {"sysparm_input_display_value": "true"} if inputs.get("input_display_value") else None
|
||||
print(json.dumps(request("POST", "/table/" + urllib.parse.quote(table), params=params, body=body)))
|
||||
|
||||
|
||||
try:
|
||||
main()
|
||||
except urllib.error.HTTPError as e:
|
||||
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
|
||||
sys.exit(1)
|
||||
except Exception as e:
|
||||
print(json.dumps({"error": str(e)}))
|
||||
sys.exit(1)
|
||||
@@ -0,0 +1,62 @@
|
||||
import base64, json, os, sys, urllib.parse, urllib.request, urllib.error
|
||||
|
||||
|
||||
def _cfg():
|
||||
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
|
||||
|
||||
|
||||
def _headers(cfg):
|
||||
h = {"Accept": "application/json", "Content-Type": "application/json"}
|
||||
if str(cfg.get("auth_type") or "basic").lower() == "oauth":
|
||||
data = urllib.parse.urlencode({
|
||||
"grant_type": "password",
|
||||
"client_id": cfg.get("client_id", ""),
|
||||
"client_secret": cfg.get("client_secret", ""),
|
||||
"username": cfg.get("username", ""),
|
||||
"password": cfg.get("password", ""),
|
||||
}).encode("utf-8")
|
||||
req = urllib.request.Request(
|
||||
cfg.get("url", "").rstrip("/") + "/oauth_token.do", data=data,
|
||||
headers={"Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"}, method="POST")
|
||||
with urllib.request.urlopen(req, timeout=60) as r:
|
||||
tok = json.loads(r.read())
|
||||
if not tok.get("access_token"):
|
||||
raise Exception("OAuth token request failed: " + json.dumps(tok))
|
||||
h["Authorization"] = "Bearer " + tok["access_token"]
|
||||
else:
|
||||
cred = (cfg.get("username", "") + ":" + cfg.get("password", "")).encode("utf-8")
|
||||
h["Authorization"] = "Basic " + base64.b64encode(cred).decode("ascii")
|
||||
return h
|
||||
|
||||
|
||||
def request(method, path, params=None, body=None, root="/api/now", versioned=True):
|
||||
cfg = _cfg()
|
||||
v = str(cfg.get("api_version") or "").strip().strip("/")
|
||||
url = cfg.get("url", "").rstrip("/") + root + ("/" + v if v and versioned else "") + path
|
||||
q = {k: str(x) for k, x in (params or {}).items() if x not in (None, "")}
|
||||
if q:
|
||||
url += "?" + urllib.parse.urlencode(q)
|
||||
data = json.dumps(body).encode("utf-8") if body is not None else None
|
||||
req = urllib.request.Request(url, data=data, headers=_headers(cfg), method=method)
|
||||
with urllib.request.urlopen(req, timeout=90) as r:
|
||||
raw = r.read()
|
||||
return json.loads(raw) if raw else {}
|
||||
|
||||
|
||||
def main():
|
||||
inputs = json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
|
||||
file_sys_id = str(inputs.get("file_sys_id") or "")
|
||||
if not file_sys_id:
|
||||
raise Exception("file_sys_id is required")
|
||||
res = request("DELETE", "/attachment/" + urllib.parse.quote(file_sys_id), versioned=False)
|
||||
print(json.dumps(res if res else {"ok": True, "file_sys_id": file_sys_id}))
|
||||
|
||||
|
||||
try:
|
||||
main()
|
||||
except urllib.error.HTTPError as e:
|
||||
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
|
||||
sys.exit(1)
|
||||
except Exception as e:
|
||||
print(json.dumps({"error": str(e)}))
|
||||
sys.exit(1)
|
||||
@@ -0,0 +1,61 @@
|
||||
import base64, json, os, sys, urllib.parse, urllib.request, urllib.error
|
||||
|
||||
|
||||
def _cfg():
|
||||
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
|
||||
|
||||
|
||||
def _headers(cfg):
|
||||
h = {"Accept": "application/json", "Content-Type": "application/json"}
|
||||
if str(cfg.get("auth_type") or "basic").lower() == "oauth":
|
||||
data = urllib.parse.urlencode({
|
||||
"grant_type": "password",
|
||||
"client_id": cfg.get("client_id", ""),
|
||||
"client_secret": cfg.get("client_secret", ""),
|
||||
"username": cfg.get("username", ""),
|
||||
"password": cfg.get("password", ""),
|
||||
}).encode("utf-8")
|
||||
req = urllib.request.Request(
|
||||
cfg.get("url", "").rstrip("/") + "/oauth_token.do", data=data,
|
||||
headers={"Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"}, method="POST")
|
||||
with urllib.request.urlopen(req, timeout=60) as r:
|
||||
tok = json.loads(r.read())
|
||||
if not tok.get("access_token"):
|
||||
raise Exception("OAuth token request failed: " + json.dumps(tok))
|
||||
h["Authorization"] = "Bearer " + tok["access_token"]
|
||||
else:
|
||||
cred = (cfg.get("username", "") + ":" + cfg.get("password", "")).encode("utf-8")
|
||||
h["Authorization"] = "Basic " + base64.b64encode(cred).decode("ascii")
|
||||
return h
|
||||
|
||||
|
||||
def request(method, path, params=None, body=None, root="/api/now", versioned=True):
|
||||
cfg = _cfg()
|
||||
v = str(cfg.get("api_version") or "").strip().strip("/")
|
||||
url = cfg.get("url", "").rstrip("/") + root + ("/" + v if v and versioned else "") + path
|
||||
q = {k: str(x) for k, x in (params or {}).items() if x not in (None, "")}
|
||||
if q:
|
||||
url += "?" + urllib.parse.urlencode(q)
|
||||
data = json.dumps(body).encode("utf-8") if body is not None else None
|
||||
req = urllib.request.Request(url, data=data, headers=_headers(cfg), method=method)
|
||||
with urllib.request.urlopen(req, timeout=90) as r:
|
||||
raw = r.read()
|
||||
return json.loads(raw) if raw else {}
|
||||
|
||||
|
||||
def main():
|
||||
inputs = json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
|
||||
table = inputs.get("table_name") or ""
|
||||
rid = inputs.get("id") or ""
|
||||
res = request("DELETE", "/table/" + urllib.parse.quote(table) + "/" + urllib.parse.quote(rid))
|
||||
print(json.dumps(res if res else {"ok": True, "id": rid}))
|
||||
|
||||
|
||||
try:
|
||||
main()
|
||||
except urllib.error.HTTPError as e:
|
||||
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
|
||||
sys.exit(1)
|
||||
except Exception as e:
|
||||
print(json.dumps({"error": str(e)}))
|
||||
sys.exit(1)
|
||||
@@ -0,0 +1,63 @@
|
||||
import base64, json, os, sys, urllib.parse, urllib.request, urllib.error
|
||||
|
||||
|
||||
def _cfg():
|
||||
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
|
||||
|
||||
|
||||
def _headers(cfg):
|
||||
h = {"Accept": "application/json", "Content-Type": "application/json"}
|
||||
if str(cfg.get("auth_type") or "basic").lower() == "oauth":
|
||||
data = urllib.parse.urlencode({
|
||||
"grant_type": "password",
|
||||
"client_id": cfg.get("client_id", ""),
|
||||
"client_secret": cfg.get("client_secret", ""),
|
||||
"username": cfg.get("username", ""),
|
||||
"password": cfg.get("password", ""),
|
||||
}).encode("utf-8")
|
||||
req = urllib.request.Request(
|
||||
cfg.get("url", "").rstrip("/") + "/oauth_token.do", data=data,
|
||||
headers={"Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"}, method="POST")
|
||||
with urllib.request.urlopen(req, timeout=60) as r:
|
||||
tok = json.loads(r.read())
|
||||
if not tok.get("access_token"):
|
||||
raise Exception("OAuth token request failed: " + json.dumps(tok))
|
||||
h["Authorization"] = "Bearer " + tok["access_token"]
|
||||
else:
|
||||
cred = (cfg.get("username", "") + ":" + cfg.get("password", "")).encode("utf-8")
|
||||
h["Authorization"] = "Basic " + base64.b64encode(cred).decode("ascii")
|
||||
return h
|
||||
|
||||
|
||||
def request(method, path, params=None, body=None, root="/api/now", versioned=True):
|
||||
cfg = _cfg()
|
||||
v = str(cfg.get("api_version") or "").strip().strip("/")
|
||||
url = cfg.get("url", "").rstrip("/") + root + ("/" + v if v and versioned else "") + path
|
||||
q = {k: str(x) for k, x in (params or {}).items() if x not in (None, "")}
|
||||
if q:
|
||||
url += "?" + urllib.parse.urlencode(q)
|
||||
data = json.dumps(body).encode("utf-8") if body is not None else None
|
||||
req = urllib.request.Request(url, data=data, headers=_headers(cfg), method=method)
|
||||
with urllib.request.urlopen(req, timeout=90) as r:
|
||||
raw = r.read()
|
||||
return json.loads(raw) if raw else {}
|
||||
|
||||
|
||||
def main():
|
||||
inputs = json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
|
||||
ticket_id = str(inputs.get("id") or "")
|
||||
if not ticket_id:
|
||||
raise Exception("id is required")
|
||||
table = str(inputs.get("ticket_type") or _cfg().get("ticket_type") or "incident")
|
||||
res = request("DELETE", "/table/" + urllib.parse.quote(table) + "/" + urllib.parse.quote(ticket_id))
|
||||
print(json.dumps(res if res else {"ok": True, "id": ticket_id}))
|
||||
|
||||
|
||||
try:
|
||||
main()
|
||||
except urllib.error.HTTPError as e:
|
||||
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
|
||||
sys.exit(1)
|
||||
except Exception as e:
|
||||
print(json.dumps({"error": str(e)}))
|
||||
sys.exit(1)
|
||||
@@ -0,0 +1,64 @@
|
||||
import base64, json, os, sys, urllib.parse, urllib.request, urllib.error
|
||||
|
||||
|
||||
def _cfg():
|
||||
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
|
||||
|
||||
|
||||
def _headers(cfg):
|
||||
h = {"Accept": "application/json", "Content-Type": "application/json"}
|
||||
if str(cfg.get("auth_type") or "basic").lower() == "oauth":
|
||||
data = urllib.parse.urlencode({
|
||||
"grant_type": "password",
|
||||
"client_id": cfg.get("client_id", ""),
|
||||
"client_secret": cfg.get("client_secret", ""),
|
||||
"username": cfg.get("username", ""),
|
||||
"password": cfg.get("password", ""),
|
||||
}).encode("utf-8")
|
||||
req = urllib.request.Request(
|
||||
cfg.get("url", "").rstrip("/") + "/oauth_token.do", data=data,
|
||||
headers={"Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"}, method="POST")
|
||||
with urllib.request.urlopen(req, timeout=60) as r:
|
||||
tok = json.loads(r.read())
|
||||
if not tok.get("access_token"):
|
||||
raise Exception("OAuth token request failed: " + json.dumps(tok))
|
||||
h["Authorization"] = "Bearer " + tok["access_token"]
|
||||
else:
|
||||
cred = (cfg.get("username", "") + ":" + cfg.get("password", "")).encode("utf-8")
|
||||
h["Authorization"] = "Basic " + base64.b64encode(cred).decode("ascii")
|
||||
return h
|
||||
|
||||
|
||||
def request(method, path, params=None, body=None, root="/api/now", versioned=True):
|
||||
cfg = _cfg()
|
||||
v = str(cfg.get("api_version") or "").strip().strip("/")
|
||||
url = cfg.get("url", "").rstrip("/") + root + ("/" + v if v and versioned else "") + path
|
||||
q = {k: str(x) for k, x in (params or {}).items() if x not in (None, "")}
|
||||
if q:
|
||||
url += "?" + urllib.parse.urlencode(q)
|
||||
data = json.dumps(body).encode("utf-8") if body is not None else None
|
||||
req = urllib.request.Request(url, data=data, headers=_headers(cfg), method=method)
|
||||
with urllib.request.urlopen(req, timeout=90) as r:
|
||||
raw = r.read()
|
||||
return json.loads(raw) if raw else {}
|
||||
|
||||
|
||||
def main():
|
||||
inputs = json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
|
||||
queue_id = inputs.get("queue_id") or ""
|
||||
body = {
|
||||
"document_sys_id": inputs.get("document_id") or "",
|
||||
"document_table": inputs.get("document_table") or "incident",
|
||||
}
|
||||
res = request("POST", "/awa/queues/" + urllib.parse.quote(queue_id) + "/work_item", versioned=False, body=body)
|
||||
print(json.dumps(res))
|
||||
|
||||
|
||||
try:
|
||||
main()
|
||||
except urllib.error.HTTPError as e:
|
||||
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
|
||||
sys.exit(1)
|
||||
except Exception as e:
|
||||
print(json.dumps({"error": str(e)}))
|
||||
sys.exit(1)
|
||||
@@ -0,0 +1,71 @@
|
||||
import base64, json, os, sys, urllib.parse, urllib.request, urllib.error
|
||||
|
||||
|
||||
def _cfg():
|
||||
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
|
||||
|
||||
|
||||
def _headers(cfg):
|
||||
h = {"Accept": "application/json", "Content-Type": "application/json"}
|
||||
if str(cfg.get("auth_type") or "basic").lower() == "oauth":
|
||||
data = urllib.parse.urlencode({
|
||||
"grant_type": "password",
|
||||
"client_id": cfg.get("client_id", ""),
|
||||
"client_secret": cfg.get("client_secret", ""),
|
||||
"username": cfg.get("username", ""),
|
||||
"password": cfg.get("password", ""),
|
||||
}).encode("utf-8")
|
||||
req = urllib.request.Request(
|
||||
cfg.get("url", "").rstrip("/") + "/oauth_token.do", data=data,
|
||||
headers={"Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"}, method="POST")
|
||||
with urllib.request.urlopen(req, timeout=60) as r:
|
||||
tok = json.loads(r.read())
|
||||
if not tok.get("access_token"):
|
||||
raise Exception("OAuth token request failed: " + json.dumps(tok))
|
||||
h["Authorization"] = "Bearer " + tok["access_token"]
|
||||
else:
|
||||
cred = (cfg.get("username", "") + ":" + cfg.get("password", "")).encode("utf-8")
|
||||
h["Authorization"] = "Basic " + base64.b64encode(cred).decode("ascii")
|
||||
return h
|
||||
|
||||
|
||||
def main():
|
||||
cfg = _cfg()
|
||||
inputs = json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
|
||||
method = str(inputs.get("method") or "GET").upper()
|
||||
if method not in ("GET", "POST", "PATCH", "PUT", "DELETE"):
|
||||
raise Exception("Unsupported method: " + method)
|
||||
path = str(inputs.get("path") or "")
|
||||
if not path.startswith("/"):
|
||||
raise Exception("path must start with /")
|
||||
|
||||
url = cfg.get("url", "").rstrip("/") + path
|
||||
params = inputs.get("params")
|
||||
if params:
|
||||
if isinstance(params, str):
|
||||
params = json.loads(params)
|
||||
q = {k: str(v) for k, v in params.items() if v not in (None, "")}
|
||||
if q:
|
||||
url += ("&" if "?" in url else "?") + urllib.parse.urlencode(q)
|
||||
|
||||
body = inputs.get("body")
|
||||
data = None
|
||||
if body not in (None, ""):
|
||||
if isinstance(body, str):
|
||||
body = json.loads(body)
|
||||
data = json.dumps(body).encode("utf-8")
|
||||
|
||||
req = urllib.request.Request(url, data=data, headers=_headers(cfg), method=method)
|
||||
with urllib.request.urlopen(req, timeout=90) as r:
|
||||
raw = r.read()
|
||||
print(json.dumps(json.loads(raw) if raw else {}))
|
||||
|
||||
|
||||
try:
|
||||
main()
|
||||
except urllib.error.HTTPError as e:
|
||||
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
|
||||
sys.exit(1)
|
||||
except Exception as e:
|
||||
print(json.dumps({"error": str(e)}))
|
||||
sys.exit(1)
|
||||
@@ -0,0 +1,60 @@
|
||||
import base64, json, os, sys, urllib.parse, urllib.request, urllib.error
|
||||
|
||||
|
||||
def _cfg():
|
||||
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
|
||||
|
||||
|
||||
def _headers(cfg):
|
||||
h = {"Accept": "application/json", "Content-Type": "application/json"}
|
||||
if str(cfg.get("auth_type") or "basic").lower() == "oauth":
|
||||
data = urllib.parse.urlencode({
|
||||
"grant_type": "password",
|
||||
"client_id": cfg.get("client_id", ""),
|
||||
"client_secret": cfg.get("client_secret", ""),
|
||||
"username": cfg.get("username", ""),
|
||||
"password": cfg.get("password", ""),
|
||||
}).encode("utf-8")
|
||||
req = urllib.request.Request(
|
||||
cfg.get("url", "").rstrip("/") + "/oauth_token.do", data=data,
|
||||
headers={"Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"}, method="POST")
|
||||
with urllib.request.urlopen(req, timeout=60) as r:
|
||||
tok = json.loads(r.read())
|
||||
if not tok.get("access_token"):
|
||||
raise Exception("OAuth token request failed: " + json.dumps(tok))
|
||||
h["Authorization"] = "Bearer " + tok["access_token"]
|
||||
else:
|
||||
cred = (cfg.get("username", "") + ":" + cfg.get("password", "")).encode("utf-8")
|
||||
h["Authorization"] = "Basic " + base64.b64encode(cred).decode("ascii")
|
||||
return h
|
||||
|
||||
|
||||
def request(method, path, params=None, body=None, root="/api/now", versioned=True):
|
||||
cfg = _cfg()
|
||||
v = str(cfg.get("api_version") or "").strip().strip("/")
|
||||
url = cfg.get("url", "").rstrip("/") + root + ("/" + v if v and versioned else "") + path
|
||||
q = {k: str(x) for k, x in (params or {}).items() if x not in (None, "")}
|
||||
if q:
|
||||
url += "?" + urllib.parse.urlencode(q)
|
||||
data = json.dumps(body).encode("utf-8") if body is not None else None
|
||||
req = urllib.request.Request(url, data=data, headers=_headers(cfg), method=method)
|
||||
with urllib.request.urlopen(req, timeout=90) as r:
|
||||
raw = r.read()
|
||||
return json.loads(raw) if raw else {}
|
||||
|
||||
|
||||
def main():
|
||||
inputs = json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
|
||||
cid = inputs.get("id") or ""
|
||||
res = request("GET", "/change/" + urllib.parse.quote(cid) + "/task", root="/api/sn_chg_rest")
|
||||
print(json.dumps(res))
|
||||
|
||||
|
||||
try:
|
||||
main()
|
||||
except urllib.error.HTTPError as e:
|
||||
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
|
||||
sys.exit(1)
|
||||
except Exception as e:
|
||||
print(json.dumps({"error": str(e)}))
|
||||
sys.exit(1)
|
||||
@@ -0,0 +1,96 @@
|
||||
import base64, json, os, sys, urllib.parse, urllib.request, urllib.error
|
||||
from datetime import datetime, timezone
|
||||
|
||||
|
||||
def _cfg():
|
||||
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
|
||||
|
||||
|
||||
def _headers(cfg):
|
||||
h = {"Accept": "application/json", "Content-Type": "application/json"}
|
||||
if str(cfg.get("auth_type") or "basic").lower() == "oauth":
|
||||
data = urllib.parse.urlencode({
|
||||
"grant_type": "password",
|
||||
"client_id": cfg.get("client_id", ""),
|
||||
"client_secret": cfg.get("client_secret", ""),
|
||||
"username": cfg.get("username", ""),
|
||||
"password": cfg.get("password", ""),
|
||||
}).encode("utf-8")
|
||||
req = urllib.request.Request(
|
||||
cfg.get("url", "").rstrip("/") + "/oauth_token.do", data=data,
|
||||
headers={"Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"}, method="POST")
|
||||
with urllib.request.urlopen(req, timeout=60) as r:
|
||||
tok = json.loads(r.read())
|
||||
if not tok.get("access_token"):
|
||||
raise Exception("OAuth token request failed: " + json.dumps(tok))
|
||||
h["Authorization"] = "Bearer " + tok["access_token"]
|
||||
else:
|
||||
cred = (cfg.get("username", "") + ":" + cfg.get("password", "")).encode("utf-8")
|
||||
h["Authorization"] = "Basic " + base64.b64encode(cred).decode("ascii")
|
||||
return h
|
||||
|
||||
|
||||
def request(method, path, params=None, body=None, root="/api/now", versioned=True):
|
||||
cfg = _cfg()
|
||||
v = str(cfg.get("api_version") or "").strip().strip("/")
|
||||
url = cfg.get("url", "").rstrip("/") + root + ("/" + v if v and versioned else "") + path
|
||||
q = {k: str(x) for k, x in (params or {}).items() if x not in (None, "")}
|
||||
if q:
|
||||
url += "?" + urllib.parse.urlencode(q)
|
||||
data = json.dumps(body).encode("utf-8") if body is not None else None
|
||||
req = urllib.request.Request(url, data=data, headers=_headers(cfg), method=method)
|
||||
with urllib.request.urlopen(req, timeout=90) as r:
|
||||
raw = r.read()
|
||||
return json.loads(raw) if raw else {}
|
||||
|
||||
|
||||
def to_snow_time(v):
|
||||
"""Normalize ISO8601 / epoch (s or ms) / native format to 'YYYY-MM-DD HH:MM:SS' UTC."""
|
||||
s = str(v or "").strip()
|
||||
if not s:
|
||||
return None
|
||||
if s.isdigit():
|
||||
ts = int(s)
|
||||
if ts > 10**12:
|
||||
ts //= 1000
|
||||
return datetime.fromtimestamp(ts, tz=timezone.utc).strftime("%Y-%m-%d %H:%M:%S")
|
||||
if len(s) == 19 and s[4] == "-" and s[10] == " ":
|
||||
return s
|
||||
try:
|
||||
dt = datetime.fromisoformat(s.replace("Z", "+00:00"))
|
||||
if dt.tzinfo is not None:
|
||||
dt = dt.astimezone(timezone.utc)
|
||||
return dt.strftime("%Y-%m-%d %H:%M:%S")
|
||||
except Exception:
|
||||
return s
|
||||
|
||||
|
||||
def main():
|
||||
cfg = _cfg()
|
||||
inputs = json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
|
||||
table = str(inputs.get("ticket_type") or cfg.get("ticket_type") or "incident")
|
||||
ts_field = str(cfg.get("timestamp_field") or "opened_at")
|
||||
limit = int(inputs.get("limit") or 100)
|
||||
|
||||
parts = []
|
||||
if inputs.get("query"):
|
||||
parts.append(str(inputs["query"]))
|
||||
watermark = to_snow_time(inputs.get("created_after"))
|
||||
if watermark:
|
||||
parts.append(ts_field + ">" + watermark)
|
||||
parts.append("ORDERBY" + ts_field)
|
||||
|
||||
print(json.dumps(request("GET", "/table/" + urllib.parse.quote(table), params={
|
||||
"sysparm_query": "^".join(parts),
|
||||
"sysparm_limit": limit,
|
||||
})))
|
||||
|
||||
|
||||
try:
|
||||
main()
|
||||
except urllib.error.HTTPError as e:
|
||||
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
|
||||
sys.exit(1)
|
||||
except Exception as e:
|
||||
print(json.dumps({"error": str(e)}))
|
||||
sys.exit(1)
|
||||
@@ -0,0 +1,60 @@
|
||||
import base64, json, os, sys, urllib.parse, urllib.request, urllib.error
|
||||
|
||||
|
||||
def _cfg():
|
||||
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
|
||||
|
||||
|
||||
def _headers(cfg):
|
||||
h = {"Accept": "application/json", "Content-Type": "application/json"}
|
||||
if str(cfg.get("auth_type") or "basic").lower() == "oauth":
|
||||
data = urllib.parse.urlencode({
|
||||
"grant_type": "password",
|
||||
"client_id": cfg.get("client_id", ""),
|
||||
"client_secret": cfg.get("client_secret", ""),
|
||||
"username": cfg.get("username", ""),
|
||||
"password": cfg.get("password", ""),
|
||||
}).encode("utf-8")
|
||||
req = urllib.request.Request(
|
||||
cfg.get("url", "").rstrip("/") + "/oauth_token.do", data=data,
|
||||
headers={"Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"}, method="POST")
|
||||
with urllib.request.urlopen(req, timeout=60) as r:
|
||||
tok = json.loads(r.read())
|
||||
if not tok.get("access_token"):
|
||||
raise Exception("OAuth token request failed: " + json.dumps(tok))
|
||||
h["Authorization"] = "Bearer " + tok["access_token"]
|
||||
else:
|
||||
cred = (cfg.get("username", "") + ":" + cfg.get("password", "")).encode("utf-8")
|
||||
h["Authorization"] = "Basic " + base64.b64encode(cred).decode("ascii")
|
||||
return h
|
||||
|
||||
|
||||
def request(method, path, params=None, body=None, root="/api/now", versioned=True):
|
||||
cfg = _cfg()
|
||||
v = str(cfg.get("api_version") or "").strip().strip("/")
|
||||
url = cfg.get("url", "").rstrip("/") + root + ("/" + v if v and versioned else "") + path
|
||||
q = {k: str(x) for k, x in (params or {}).items() if x not in (None, "")}
|
||||
if q:
|
||||
url += "?" + urllib.parse.urlencode(q)
|
||||
data = json.dumps(body).encode("utf-8") if body is not None else None
|
||||
req = urllib.request.Request(url, data=data, headers=_headers(cfg), method=method)
|
||||
with urllib.request.urlopen(req, timeout=90) as r:
|
||||
raw = r.read()
|
||||
return json.loads(raw) if raw else {}
|
||||
|
||||
|
||||
def main():
|
||||
inputs = json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
|
||||
iid = inputs.get("id") or ""
|
||||
res = request("GET", "/servicecatalog/items/" + urllib.parse.quote(iid), root="/api/sn_sc")
|
||||
print(json.dumps(res))
|
||||
|
||||
|
||||
try:
|
||||
main()
|
||||
except urllib.error.HTTPError as e:
|
||||
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
|
||||
sys.exit(1)
|
||||
except Exception as e:
|
||||
print(json.dumps({"error": str(e)}))
|
||||
sys.exit(1)
|
||||
@@ -0,0 +1,67 @@
|
||||
import base64, json, os, sys, urllib.parse, urllib.request, urllib.error
|
||||
|
||||
|
||||
def _cfg():
|
||||
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
|
||||
|
||||
|
||||
def _headers(cfg):
|
||||
h = {"Accept": "application/json", "Content-Type": "application/json"}
|
||||
if str(cfg.get("auth_type") or "basic").lower() == "oauth":
|
||||
data = urllib.parse.urlencode({
|
||||
"grant_type": "password",
|
||||
"client_id": cfg.get("client_id", ""),
|
||||
"client_secret": cfg.get("client_secret", ""),
|
||||
"username": cfg.get("username", ""),
|
||||
"password": cfg.get("password", ""),
|
||||
}).encode("utf-8")
|
||||
req = urllib.request.Request(
|
||||
cfg.get("url", "").rstrip("/") + "/oauth_token.do", data=data,
|
||||
headers={"Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"}, method="POST")
|
||||
with urllib.request.urlopen(req, timeout=60) as r:
|
||||
tok = json.loads(r.read())
|
||||
if not tok.get("access_token"):
|
||||
raise Exception("OAuth token request failed: " + json.dumps(tok))
|
||||
h["Authorization"] = "Bearer " + tok["access_token"]
|
||||
else:
|
||||
cred = (cfg.get("username", "") + ":" + cfg.get("password", "")).encode("utf-8")
|
||||
h["Authorization"] = "Basic " + base64.b64encode(cred).decode("ascii")
|
||||
return h
|
||||
|
||||
|
||||
def request(method, path, params=None, body=None, root="/api/now", versioned=True):
|
||||
cfg = _cfg()
|
||||
v = str(cfg.get("api_version") or "").strip().strip("/")
|
||||
url = cfg.get("url", "").rstrip("/") + root + ("/" + v if v and versioned else "") + path
|
||||
q = {k: str(x) for k, x in (params or {}).items() if x not in (None, "")}
|
||||
if q:
|
||||
url += "?" + urllib.parse.urlencode(q)
|
||||
data = json.dumps(body).encode("utf-8") if body is not None else None
|
||||
req = urllib.request.Request(url, data=data, headers=_headers(cfg), method=method)
|
||||
with urllib.request.urlopen(req, timeout=90) as r:
|
||||
raw = r.read()
|
||||
return json.loads(raw) if raw else {}
|
||||
|
||||
|
||||
def main():
|
||||
inputs = json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
|
||||
table = inputs.get("table_name") or ""
|
||||
rid = inputs.get("id") or ""
|
||||
fields = str(inputs.get("fields") or "").strip()
|
||||
if fields and "sys_id" not in [x.strip() for x in fields.split(",")]:
|
||||
fields += ",sys_id"
|
||||
res = request("GET", "/table/" + urllib.parse.quote(table) + "/" + urllib.parse.quote(rid), params={
|
||||
"sysparm_fields": fields or None,
|
||||
"sysparm_display_value": inputs.get("display_value"),
|
||||
})
|
||||
print(json.dumps(res))
|
||||
|
||||
|
||||
try:
|
||||
main()
|
||||
except urllib.error.HTTPError as e:
|
||||
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
|
||||
sys.exit(1)
|
||||
except Exception as e:
|
||||
print(json.dumps({"error": str(e)}))
|
||||
sys.exit(1)
|
||||
@@ -0,0 +1,64 @@
|
||||
import base64, json, os, sys, urllib.parse, urllib.request, urllib.error
|
||||
|
||||
|
||||
def _cfg():
|
||||
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
|
||||
|
||||
|
||||
def _headers(cfg):
|
||||
h = {"Accept": "application/json", "Content-Type": "application/json"}
|
||||
if str(cfg.get("auth_type") or "basic").lower() == "oauth":
|
||||
data = urllib.parse.urlencode({
|
||||
"grant_type": "password",
|
||||
"client_id": cfg.get("client_id", ""),
|
||||
"client_secret": cfg.get("client_secret", ""),
|
||||
"username": cfg.get("username", ""),
|
||||
"password": cfg.get("password", ""),
|
||||
}).encode("utf-8")
|
||||
req = urllib.request.Request(
|
||||
cfg.get("url", "").rstrip("/") + "/oauth_token.do", data=data,
|
||||
headers={"Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"}, method="POST")
|
||||
with urllib.request.urlopen(req, timeout=60) as r:
|
||||
tok = json.loads(r.read())
|
||||
if not tok.get("access_token"):
|
||||
raise Exception("OAuth token request failed: " + json.dumps(tok))
|
||||
h["Authorization"] = "Bearer " + tok["access_token"]
|
||||
else:
|
||||
cred = (cfg.get("username", "") + ":" + cfg.get("password", "")).encode("utf-8")
|
||||
h["Authorization"] = "Basic " + base64.b64encode(cred).decode("ascii")
|
||||
return h
|
||||
|
||||
|
||||
def request(method, path, params=None, body=None, root="/api/now", versioned=True):
|
||||
cfg = _cfg()
|
||||
v = str(cfg.get("api_version") or "").strip().strip("/")
|
||||
url = cfg.get("url", "").rstrip("/") + root + ("/" + v if v and versioned else "") + path
|
||||
q = {k: str(x) for k, x in (params or {}).items() if x not in (None, "")}
|
||||
if q:
|
||||
url += "?" + urllib.parse.urlencode(q)
|
||||
data = json.dumps(body).encode("utf-8") if body is not None else None
|
||||
req = urllib.request.Request(url, data=data, headers=_headers(cfg), method=method)
|
||||
with urllib.request.urlopen(req, timeout=90) as r:
|
||||
raw = r.read()
|
||||
return json.loads(raw) if raw else {}
|
||||
|
||||
|
||||
def main():
|
||||
inputs = json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
|
||||
label = inputs.get("label") or ""
|
||||
res = request("GET", "/table/sys_db_object", params={
|
||||
"sysparm_query": "label=" + label,
|
||||
"sysparm_limit": inputs.get("limit") or 10,
|
||||
"sysparm_fields": "sys_id,name,label,sys_name",
|
||||
})
|
||||
print(json.dumps(res))
|
||||
|
||||
|
||||
try:
|
||||
main()
|
||||
except urllib.error.HTTPError as e:
|
||||
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
|
||||
sys.exit(1)
|
||||
except Exception as e:
|
||||
print(json.dumps({"error": str(e)}))
|
||||
sys.exit(1)
|
||||
@@ -0,0 +1,74 @@
|
||||
import base64, json, os, sys, urllib.parse, urllib.request, urllib.error
|
||||
|
||||
|
||||
def _cfg():
|
||||
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
|
||||
|
||||
|
||||
def _headers(cfg):
|
||||
h = {"Accept": "application/json", "Content-Type": "application/json"}
|
||||
if str(cfg.get("auth_type") or "basic").lower() == "oauth":
|
||||
data = urllib.parse.urlencode({
|
||||
"grant_type": "password",
|
||||
"client_id": cfg.get("client_id", ""),
|
||||
"client_secret": cfg.get("client_secret", ""),
|
||||
"username": cfg.get("username", ""),
|
||||
"password": cfg.get("password", ""),
|
||||
}).encode("utf-8")
|
||||
req = urllib.request.Request(
|
||||
cfg.get("url", "").rstrip("/") + "/oauth_token.do", data=data,
|
||||
headers={"Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"}, method="POST")
|
||||
with urllib.request.urlopen(req, timeout=60) as r:
|
||||
tok = json.loads(r.read())
|
||||
if not tok.get("access_token"):
|
||||
raise Exception("OAuth token request failed: " + json.dumps(tok))
|
||||
h["Authorization"] = "Bearer " + tok["access_token"]
|
||||
else:
|
||||
cred = (cfg.get("username", "") + ":" + cfg.get("password", "")).encode("utf-8")
|
||||
h["Authorization"] = "Basic " + base64.b64encode(cred).decode("ascii")
|
||||
return h
|
||||
|
||||
|
||||
def request(method, path, params=None, body=None, root="/api/now", versioned=True):
|
||||
cfg = _cfg()
|
||||
v = str(cfg.get("api_version") or "").strip().strip("/")
|
||||
url = cfg.get("url", "").rstrip("/") + root + ("/" + v if v and versioned else "") + path
|
||||
q = {k: str(x) for k, x in (params or {}).items() if x not in (None, "")}
|
||||
if q:
|
||||
url += "?" + urllib.parse.urlencode(q)
|
||||
data = json.dumps(body).encode("utf-8") if body is not None else None
|
||||
req = urllib.request.Request(url, data=data, headers=_headers(cfg), method=method)
|
||||
with urllib.request.urlopen(req, timeout=90) as r:
|
||||
raw = r.read()
|
||||
return json.loads(raw) if raw else {}
|
||||
|
||||
|
||||
def main():
|
||||
inputs = json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
|
||||
table = str(inputs.get("ticket_type") or _cfg().get("ticket_type") or "incident")
|
||||
fields = str(inputs.get("fields") or "")
|
||||
if fields and "sys_id" not in fields:
|
||||
fields += ",sys_id"
|
||||
params = {
|
||||
"sysparm_fields": fields or None,
|
||||
"sysparm_display_value": inputs.get("display_value"),
|
||||
}
|
||||
if inputs.get("id"):
|
||||
res = request("GET", "/table/" + urllib.parse.quote(table) + "/" + urllib.parse.quote(str(inputs["id"])), params=params)
|
||||
elif inputs.get("number"):
|
||||
params["sysparm_query"] = "number=" + str(inputs["number"])
|
||||
params["sysparm_limit"] = 1
|
||||
res = request("GET", "/table/" + urllib.parse.quote(table), params=params)
|
||||
else:
|
||||
raise Exception("id or number is required")
|
||||
print(json.dumps(res))
|
||||
|
||||
|
||||
try:
|
||||
main()
|
||||
except urllib.error.HTTPError as e:
|
||||
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
|
||||
sys.exit(1)
|
||||
except Exception as e:
|
||||
print(json.dumps({"error": str(e)}))
|
||||
sys.exit(1)
|
||||
@@ -0,0 +1,62 @@
|
||||
import base64, json, os, sys, urllib.parse, urllib.request, urllib.error
|
||||
|
||||
|
||||
def _cfg():
|
||||
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
|
||||
|
||||
|
||||
def _headers(cfg):
|
||||
h = {"Accept": "application/json", "Content-Type": "application/json"}
|
||||
if str(cfg.get("auth_type") or "basic").lower() == "oauth":
|
||||
data = urllib.parse.urlencode({
|
||||
"grant_type": "password",
|
||||
"client_id": cfg.get("client_id", ""),
|
||||
"client_secret": cfg.get("client_secret", ""),
|
||||
"username": cfg.get("username", ""),
|
||||
"password": cfg.get("password", ""),
|
||||
}).encode("utf-8")
|
||||
req = urllib.request.Request(
|
||||
cfg.get("url", "").rstrip("/") + "/oauth_token.do", data=data,
|
||||
headers={"Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"}, method="POST")
|
||||
with urllib.request.urlopen(req, timeout=60) as r:
|
||||
tok = json.loads(r.read())
|
||||
if not tok.get("access_token"):
|
||||
raise Exception("OAuth token request failed: " + json.dumps(tok))
|
||||
h["Authorization"] = "Bearer " + tok["access_token"]
|
||||
else:
|
||||
cred = (cfg.get("username", "") + ":" + cfg.get("password", "")).encode("utf-8")
|
||||
h["Authorization"] = "Basic " + base64.b64encode(cred).decode("ascii")
|
||||
return h
|
||||
|
||||
|
||||
def request(method, path, params=None, body=None, root="/api/now", versioned=True):
|
||||
cfg = _cfg()
|
||||
v = str(cfg.get("api_version") or "").strip().strip("/")
|
||||
url = cfg.get("url", "").rstrip("/") + root + ("/" + v if v and versioned else "") + path
|
||||
q = {k: str(x) for k, x in (params or {}).items() if x not in (None, "")}
|
||||
if q:
|
||||
url += "?" + urllib.parse.urlencode(q)
|
||||
data = json.dumps(body).encode("utf-8") if body is not None else None
|
||||
req = urllib.request.Request(url, data=data, headers=_headers(cfg), method=method)
|
||||
with urllib.request.urlopen(req, timeout=90) as r:
|
||||
raw = r.read()
|
||||
return json.loads(raw) if raw else {}
|
||||
|
||||
|
||||
def main():
|
||||
inputs = json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
|
||||
ticket_id = str(inputs.get("id") or "")
|
||||
if not ticket_id:
|
||||
raise Exception("id is required")
|
||||
params = {"sysparm_query": "table_sys_id=" + ticket_id}
|
||||
print(json.dumps(request("GET", "/attachment", params=params, versioned=False)))
|
||||
|
||||
|
||||
try:
|
||||
main()
|
||||
except urllib.error.HTTPError as e:
|
||||
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
|
||||
sys.exit(1)
|
||||
except Exception as e:
|
||||
print(json.dumps({"error": str(e)}))
|
||||
sys.exit(1)
|
||||
@@ -0,0 +1,66 @@
|
||||
import base64, json, os, sys, urllib.parse, urllib.request, urllib.error
|
||||
|
||||
|
||||
def _cfg():
|
||||
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
|
||||
|
||||
|
||||
def _headers(cfg):
|
||||
h = {"Accept": "application/json", "Content-Type": "application/json"}
|
||||
if str(cfg.get("auth_type") or "basic").lower() == "oauth":
|
||||
data = urllib.parse.urlencode({
|
||||
"grant_type": "password",
|
||||
"client_id": cfg.get("client_id", ""),
|
||||
"client_secret": cfg.get("client_secret", ""),
|
||||
"username": cfg.get("username", ""),
|
||||
"password": cfg.get("password", ""),
|
||||
}).encode("utf-8")
|
||||
req = urllib.request.Request(
|
||||
cfg.get("url", "").rstrip("/") + "/oauth_token.do", data=data,
|
||||
headers={"Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"}, method="POST")
|
||||
with urllib.request.urlopen(req, timeout=60) as r:
|
||||
tok = json.loads(r.read())
|
||||
if not tok.get("access_token"):
|
||||
raise Exception("OAuth token request failed: " + json.dumps(tok))
|
||||
h["Authorization"] = "Bearer " + tok["access_token"]
|
||||
else:
|
||||
cred = (cfg.get("username", "") + ":" + cfg.get("password", "")).encode("utf-8")
|
||||
h["Authorization"] = "Basic " + base64.b64encode(cred).decode("ascii")
|
||||
return h
|
||||
|
||||
|
||||
def request(method, path, params=None, body=None, root="/api/now", versioned=True):
|
||||
cfg = _cfg()
|
||||
v = str(cfg.get("api_version") or "").strip().strip("/")
|
||||
url = cfg.get("url", "").rstrip("/") + root + ("/" + v if v and versioned else "") + path
|
||||
q = {k: str(x) for k, x in (params or {}).items() if x not in (None, "")}
|
||||
if q:
|
||||
url += "?" + urllib.parse.urlencode(q)
|
||||
data = json.dumps(body).encode("utf-8") if body is not None else None
|
||||
req = urllib.request.Request(url, data=data, headers=_headers(cfg), method=method)
|
||||
with urllib.request.urlopen(req, timeout=90) as r:
|
||||
raw = r.read()
|
||||
return json.loads(raw) if raw else {}
|
||||
|
||||
|
||||
def main():
|
||||
inputs = json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
|
||||
ticket_id = str(inputs.get("id") or "")
|
||||
if not ticket_id:
|
||||
raise Exception("id is required")
|
||||
params = {
|
||||
"sysparm_query": "element_id=" + ticket_id + "^element=comments^ORelement=work_notes^ORDERBYsys_created_on",
|
||||
"sysparm_limit": inputs.get("limit") or 10,
|
||||
"sysparm_offset": inputs.get("offset") or 0,
|
||||
}
|
||||
print(json.dumps(request("GET", "/table/sys_journal_field", params=params)))
|
||||
|
||||
|
||||
try:
|
||||
main()
|
||||
except urllib.error.HTTPError as e:
|
||||
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
|
||||
sys.exit(1)
|
||||
except Exception as e:
|
||||
print(json.dumps({"error": str(e)}))
|
||||
sys.exit(1)
|
||||
@@ -0,0 +1,64 @@
|
||||
import base64, json, os, sys, urllib.parse, urllib.request, urllib.error
|
||||
|
||||
|
||||
def _cfg():
|
||||
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
|
||||
|
||||
|
||||
def _headers(cfg):
|
||||
h = {"Accept": "application/json", "Content-Type": "application/json"}
|
||||
if str(cfg.get("auth_type") or "basic").lower() == "oauth":
|
||||
data = urllib.parse.urlencode({
|
||||
"grant_type": "password",
|
||||
"client_id": cfg.get("client_id", ""),
|
||||
"client_secret": cfg.get("client_secret", ""),
|
||||
"username": cfg.get("username", ""),
|
||||
"password": cfg.get("password", ""),
|
||||
}).encode("utf-8")
|
||||
req = urllib.request.Request(
|
||||
cfg.get("url", "").rstrip("/") + "/oauth_token.do", data=data,
|
||||
headers={"Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"}, method="POST")
|
||||
with urllib.request.urlopen(req, timeout=60) as r:
|
||||
tok = json.loads(r.read())
|
||||
if not tok.get("access_token"):
|
||||
raise Exception("OAuth token request failed: " + json.dumps(tok))
|
||||
h["Authorization"] = "Bearer " + tok["access_token"]
|
||||
else:
|
||||
cred = (cfg.get("username", "") + ":" + cfg.get("password", "")).encode("utf-8")
|
||||
h["Authorization"] = "Basic " + base64.b64encode(cred).decode("ascii")
|
||||
return h
|
||||
|
||||
|
||||
def request(method, path, params=None, body=None, root="/api/now", versioned=True):
|
||||
cfg = _cfg()
|
||||
v = str(cfg.get("api_version") or "").strip().strip("/")
|
||||
url = cfg.get("url", "").rstrip("/") + root + ("/" + v if v and versioned else "") + path
|
||||
q = {k: str(x) for k, x in (params or {}).items() if x not in (None, "")}
|
||||
if q:
|
||||
url += "?" + urllib.parse.urlencode(q)
|
||||
data = json.dumps(body).encode("utf-8") if body is not None else None
|
||||
req = urllib.request.Request(url, data=data, headers=_headers(cfg), method=method)
|
||||
with urllib.request.urlopen(req, timeout=90) as r:
|
||||
raw = r.read()
|
||||
return json.loads(raw) if raw else {}
|
||||
|
||||
|
||||
def main():
|
||||
inputs = json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
|
||||
table = inputs.get("table_name") or ""
|
||||
res = request("GET", "/table/" + urllib.parse.quote(table), params={"sysparm_limit": 1})
|
||||
rows = res.get("result") or []
|
||||
if not rows:
|
||||
print(json.dumps({"table": table, "fields": []}))
|
||||
return
|
||||
print(json.dumps({"table": table, "fields": sorted(rows[0].keys())}))
|
||||
|
||||
|
||||
try:
|
||||
main()
|
||||
except urllib.error.HTTPError as e:
|
||||
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
|
||||
sys.exit(1)
|
||||
except Exception as e:
|
||||
print(json.dumps({"error": str(e)}))
|
||||
sys.exit(1)
|
||||
@@ -0,0 +1,73 @@
|
||||
import base64, json, os, sys, urllib.parse, urllib.request, urllib.error
|
||||
|
||||
|
||||
def _cfg():
|
||||
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
|
||||
|
||||
|
||||
def _headers(cfg):
|
||||
h = {"Accept": "application/json", "Content-Type": "application/json"}
|
||||
if str(cfg.get("auth_type") or "basic").lower() == "oauth":
|
||||
data = urllib.parse.urlencode({
|
||||
"grant_type": "password",
|
||||
"client_id": cfg.get("client_id", ""),
|
||||
"client_secret": cfg.get("client_secret", ""),
|
||||
"username": cfg.get("username", ""),
|
||||
"password": cfg.get("password", ""),
|
||||
}).encode("utf-8")
|
||||
req = urllib.request.Request(
|
||||
cfg.get("url", "").rstrip("/") + "/oauth_token.do", data=data,
|
||||
headers={"Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"}, method="POST")
|
||||
with urllib.request.urlopen(req, timeout=60) as r:
|
||||
tok = json.loads(r.read())
|
||||
if not tok.get("access_token"):
|
||||
raise Exception("OAuth token request failed: " + json.dumps(tok))
|
||||
h["Authorization"] = "Bearer " + tok["access_token"]
|
||||
else:
|
||||
cred = (cfg.get("username", "") + ":" + cfg.get("password", "")).encode("utf-8")
|
||||
h["Authorization"] = "Basic " + base64.b64encode(cred).decode("ascii")
|
||||
return h
|
||||
|
||||
|
||||
def request(method, path, params=None, body=None, root="/api/now", versioned=True):
|
||||
cfg = _cfg()
|
||||
v = str(cfg.get("api_version") or "").strip().strip("/")
|
||||
url = cfg.get("url", "").rstrip("/") + root + ("/" + v if v and versioned else "") + path
|
||||
q = {k: str(x) for k, x in (params or {}).items() if x not in (None, "")}
|
||||
if q:
|
||||
url += "?" + urllib.parse.urlencode(q)
|
||||
data = json.dumps(body).encode("utf-8") if body is not None else None
|
||||
req = urllib.request.Request(url, data=data, headers=_headers(cfg), method=method)
|
||||
with urllib.request.urlopen(req, timeout=90) as r:
|
||||
raw = r.read()
|
||||
return json.loads(raw) if raw else {}
|
||||
|
||||
|
||||
def main():
|
||||
inputs = json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
|
||||
cid = inputs.get("computer_id") or ""
|
||||
if cid:
|
||||
res = request("GET", "/table/cmdb_ci_computer/" + urllib.parse.quote(cid))
|
||||
else:
|
||||
if inputs.get("computer_name"):
|
||||
q = "name=" + inputs["computer_name"]
|
||||
elif inputs.get("asset_tag"):
|
||||
q = "asset_tag=" + inputs["asset_tag"]
|
||||
else:
|
||||
q = inputs.get("query") or ""
|
||||
res = request("GET", "/table/cmdb_ci_computer", params={
|
||||
"sysparm_query": q,
|
||||
"sysparm_limit": inputs.get("limit") or 10,
|
||||
"sysparm_offset": inputs.get("offset") or 0,
|
||||
})
|
||||
print(json.dumps(res))
|
||||
|
||||
|
||||
try:
|
||||
main()
|
||||
except urllib.error.HTTPError as e:
|
||||
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
|
||||
sys.exit(1)
|
||||
except Exception as e:
|
||||
print(json.dumps({"error": str(e)}))
|
||||
sys.exit(1)
|
||||
@@ -0,0 +1,71 @@
|
||||
import base64, json, os, sys, urllib.parse, urllib.request, urllib.error
|
||||
|
||||
|
||||
def _cfg():
|
||||
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
|
||||
|
||||
|
||||
def _headers(cfg):
|
||||
h = {"Accept": "application/json", "Content-Type": "application/json"}
|
||||
if str(cfg.get("auth_type") or "basic").lower() == "oauth":
|
||||
data = urllib.parse.urlencode({
|
||||
"grant_type": "password",
|
||||
"client_id": cfg.get("client_id", ""),
|
||||
"client_secret": cfg.get("client_secret", ""),
|
||||
"username": cfg.get("username", ""),
|
||||
"password": cfg.get("password", ""),
|
||||
}).encode("utf-8")
|
||||
req = urllib.request.Request(
|
||||
cfg.get("url", "").rstrip("/") + "/oauth_token.do", data=data,
|
||||
headers={"Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"}, method="POST")
|
||||
with urllib.request.urlopen(req, timeout=60) as r:
|
||||
tok = json.loads(r.read())
|
||||
if not tok.get("access_token"):
|
||||
raise Exception("OAuth token request failed: " + json.dumps(tok))
|
||||
h["Authorization"] = "Bearer " + tok["access_token"]
|
||||
else:
|
||||
cred = (cfg.get("username", "") + ":" + cfg.get("password", "")).encode("utf-8")
|
||||
h["Authorization"] = "Basic " + base64.b64encode(cred).decode("ascii")
|
||||
return h
|
||||
|
||||
|
||||
def request(method, path, params=None, body=None, root="/api/now", versioned=True):
|
||||
cfg = _cfg()
|
||||
v = str(cfg.get("api_version") or "").strip().strip("/")
|
||||
url = cfg.get("url", "").rstrip("/") + root + ("/" + v if v and versioned else "") + path
|
||||
q = {k: str(x) for k, x in (params or {}).items() if x not in (None, "")}
|
||||
if q:
|
||||
url += "?" + urllib.parse.urlencode(q)
|
||||
data = json.dumps(body).encode("utf-8") if body is not None else None
|
||||
req = urllib.request.Request(url, data=data, headers=_headers(cfg), method=method)
|
||||
with urllib.request.urlopen(req, timeout=90) as r:
|
||||
raw = r.read()
|
||||
return json.loads(raw) if raw else {}
|
||||
|
||||
|
||||
def main():
|
||||
inputs = json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
|
||||
gid = inputs.get("group_id") or ""
|
||||
if gid:
|
||||
res = request("GET", "/table/sys_user_group/" + urllib.parse.quote(gid))
|
||||
else:
|
||||
if inputs.get("group_name"):
|
||||
q = "name=" + inputs["group_name"]
|
||||
else:
|
||||
q = inputs.get("query") or ""
|
||||
res = request("GET", "/table/sys_user_group", params={
|
||||
"sysparm_query": q,
|
||||
"sysparm_limit": inputs.get("limit") or 10,
|
||||
"sysparm_offset": inputs.get("offset") or 0,
|
||||
})
|
||||
print(json.dumps(res))
|
||||
|
||||
|
||||
try:
|
||||
main()
|
||||
except urllib.error.HTTPError as e:
|
||||
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
|
||||
sys.exit(1)
|
||||
except Exception as e:
|
||||
print(json.dumps({"error": str(e)}))
|
||||
sys.exit(1)
|
||||
@@ -0,0 +1,63 @@
|
||||
import base64, json, os, sys, urllib.parse, urllib.request, urllib.error
|
||||
|
||||
|
||||
def _cfg():
|
||||
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
|
||||
|
||||
|
||||
def _headers(cfg):
|
||||
h = {"Accept": "application/json", "Content-Type": "application/json"}
|
||||
if str(cfg.get("auth_type") or "basic").lower() == "oauth":
|
||||
data = urllib.parse.urlencode({
|
||||
"grant_type": "password",
|
||||
"client_id": cfg.get("client_id", ""),
|
||||
"client_secret": cfg.get("client_secret", ""),
|
||||
"username": cfg.get("username", ""),
|
||||
"password": cfg.get("password", ""),
|
||||
}).encode("utf-8")
|
||||
req = urllib.request.Request(
|
||||
cfg.get("url", "").rstrip("/") + "/oauth_token.do", data=data,
|
||||
headers={"Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"}, method="POST")
|
||||
with urllib.request.urlopen(req, timeout=60) as r:
|
||||
tok = json.loads(r.read())
|
||||
if not tok.get("access_token"):
|
||||
raise Exception("OAuth token request failed: " + json.dumps(tok))
|
||||
h["Authorization"] = "Bearer " + tok["access_token"]
|
||||
else:
|
||||
cred = (cfg.get("username", "") + ":" + cfg.get("password", "")).encode("utf-8")
|
||||
h["Authorization"] = "Basic " + base64.b64encode(cred).decode("ascii")
|
||||
return h
|
||||
|
||||
|
||||
def request(method, path, params=None, body=None, root="/api/now", versioned=True):
|
||||
cfg = _cfg()
|
||||
v = str(cfg.get("api_version") or "").strip().strip("/")
|
||||
url = cfg.get("url", "").rstrip("/") + root + ("/" + v if v and versioned else "") + path
|
||||
q = {k: str(x) for k, x in (params or {}).items() if x not in (None, "")}
|
||||
if q:
|
||||
url += "?" + urllib.parse.urlencode(q)
|
||||
data = json.dumps(body).encode("utf-8") if body is not None else None
|
||||
req = urllib.request.Request(url, data=data, headers=_headers(cfg), method=method)
|
||||
with urllib.request.urlopen(req, timeout=90) as r:
|
||||
raw = r.read()
|
||||
return json.loads(raw) if raw else {}
|
||||
|
||||
|
||||
def main():
|
||||
inputs = json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
|
||||
res = request("GET", "/servicecatalog/items", root="/api/sn_sc", params={
|
||||
"sysparm_text": inputs.get("name"),
|
||||
"sysparm_limit": inputs.get("limit") or 10,
|
||||
"sysparm_offset": inputs.get("offset") or 0,
|
||||
})
|
||||
print(json.dumps(res))
|
||||
|
||||
|
||||
try:
|
||||
main()
|
||||
except urllib.error.HTTPError as e:
|
||||
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
|
||||
sys.exit(1)
|
||||
except Exception as e:
|
||||
print(json.dumps({"error": str(e)}))
|
||||
sys.exit(1)
|
||||
@@ -0,0 +1,69 @@
|
||||
import base64, json, os, sys, urllib.parse, urllib.request, urllib.error
|
||||
|
||||
|
||||
def _cfg():
|
||||
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
|
||||
|
||||
|
||||
def _headers(cfg):
|
||||
h = {"Accept": "application/json", "Content-Type": "application/json"}
|
||||
if str(cfg.get("auth_type") or "basic").lower() == "oauth":
|
||||
data = urllib.parse.urlencode({
|
||||
"grant_type": "password",
|
||||
"client_id": cfg.get("client_id", ""),
|
||||
"client_secret": cfg.get("client_secret", ""),
|
||||
"username": cfg.get("username", ""),
|
||||
"password": cfg.get("password", ""),
|
||||
}).encode("utf-8")
|
||||
req = urllib.request.Request(
|
||||
cfg.get("url", "").rstrip("/") + "/oauth_token.do", data=data,
|
||||
headers={"Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"}, method="POST")
|
||||
with urllib.request.urlopen(req, timeout=60) as r:
|
||||
tok = json.loads(r.read())
|
||||
if not tok.get("access_token"):
|
||||
raise Exception("OAuth token request failed: " + json.dumps(tok))
|
||||
h["Authorization"] = "Bearer " + tok["access_token"]
|
||||
else:
|
||||
cred = (cfg.get("username", "") + ":" + cfg.get("password", "")).encode("utf-8")
|
||||
h["Authorization"] = "Basic " + base64.b64encode(cred).decode("ascii")
|
||||
return h
|
||||
|
||||
|
||||
def request(method, path, params=None, body=None, root="/api/now", versioned=True):
|
||||
cfg = _cfg()
|
||||
v = str(cfg.get("api_version") or "").strip().strip("/")
|
||||
url = cfg.get("url", "").rstrip("/") + root + ("/" + v if v and versioned else "") + path
|
||||
q = {k: str(x) for k, x in (params or {}).items() if x not in (None, "")}
|
||||
if q:
|
||||
url += "?" + urllib.parse.urlencode(q)
|
||||
data = json.dumps(body).encode("utf-8") if body is not None else None
|
||||
req = urllib.request.Request(url, data=data, headers=_headers(cfg), method=method)
|
||||
with urllib.request.urlopen(req, timeout=90) as r:
|
||||
raw = r.read()
|
||||
return json.loads(raw) if raw else {}
|
||||
|
||||
|
||||
def main():
|
||||
inputs = json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
|
||||
table = inputs.get("table_name") or ""
|
||||
fields = str(inputs.get("fields") or "").strip()
|
||||
if fields and "sys_id" not in [x.strip() for x in fields.split(",")]:
|
||||
fields += ",sys_id"
|
||||
res = request("GET", "/table/" + urllib.parse.quote(table), params={
|
||||
"sysparm_query": inputs.get("query"),
|
||||
"sysparm_limit": inputs.get("limit") or 10,
|
||||
"sysparm_offset": inputs.get("offset") or 0,
|
||||
"sysparm_fields": fields or None,
|
||||
"sysparm_display_value": inputs.get("display_value"),
|
||||
})
|
||||
print(json.dumps(res))
|
||||
|
||||
|
||||
try:
|
||||
main()
|
||||
except urllib.error.HTTPError as e:
|
||||
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
|
||||
sys.exit(1)
|
||||
except Exception as e:
|
||||
print(json.dumps({"error": str(e)}))
|
||||
sys.exit(1)
|
||||
@@ -0,0 +1,69 @@
|
||||
import base64, json, os, sys, urllib.parse, urllib.request, urllib.error
|
||||
|
||||
|
||||
def _cfg():
|
||||
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
|
||||
|
||||
|
||||
def _headers(cfg):
|
||||
h = {"Accept": "application/json", "Content-Type": "application/json"}
|
||||
if str(cfg.get("auth_type") or "basic").lower() == "oauth":
|
||||
data = urllib.parse.urlencode({
|
||||
"grant_type": "password",
|
||||
"client_id": cfg.get("client_id", ""),
|
||||
"client_secret": cfg.get("client_secret", ""),
|
||||
"username": cfg.get("username", ""),
|
||||
"password": cfg.get("password", ""),
|
||||
}).encode("utf-8")
|
||||
req = urllib.request.Request(
|
||||
cfg.get("url", "").rstrip("/") + "/oauth_token.do", data=data,
|
||||
headers={"Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"}, method="POST")
|
||||
with urllib.request.urlopen(req, timeout=60) as r:
|
||||
tok = json.loads(r.read())
|
||||
if not tok.get("access_token"):
|
||||
raise Exception("OAuth token request failed: " + json.dumps(tok))
|
||||
h["Authorization"] = "Bearer " + tok["access_token"]
|
||||
else:
|
||||
cred = (cfg.get("username", "") + ":" + cfg.get("password", "")).encode("utf-8")
|
||||
h["Authorization"] = "Basic " + base64.b64encode(cred).decode("ascii")
|
||||
return h
|
||||
|
||||
|
||||
def request(method, path, params=None, body=None, root="/api/now", versioned=True):
|
||||
cfg = _cfg()
|
||||
v = str(cfg.get("api_version") or "").strip().strip("/")
|
||||
url = cfg.get("url", "").rstrip("/") + root + ("/" + v if v and versioned else "") + path
|
||||
q = {k: str(x) for k, x in (params or {}).items() if x not in (None, "")}
|
||||
if q:
|
||||
url += "?" + urllib.parse.urlencode(q)
|
||||
data = json.dumps(body).encode("utf-8") if body is not None else None
|
||||
req = urllib.request.Request(url, data=data, headers=_headers(cfg), method=method)
|
||||
with urllib.request.urlopen(req, timeout=90) as r:
|
||||
raw = r.read()
|
||||
return json.loads(raw) if raw else {}
|
||||
|
||||
|
||||
def main():
|
||||
inputs = json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
|
||||
table = str(inputs.get("ticket_type") or _cfg().get("ticket_type") or "incident")
|
||||
fields = str(inputs.get("fields") or "")
|
||||
if fields and "sys_id" not in fields:
|
||||
fields += ",sys_id"
|
||||
params = {
|
||||
"sysparm_query": inputs.get("query"),
|
||||
"sysparm_limit": inputs.get("limit") or 10,
|
||||
"sysparm_offset": inputs.get("offset") or 0,
|
||||
"sysparm_fields": fields or None,
|
||||
"sysparm_display_value": inputs.get("display_value"),
|
||||
}
|
||||
print(json.dumps(request("GET", "/table/" + urllib.parse.quote(table), params=params)))
|
||||
|
||||
|
||||
try:
|
||||
main()
|
||||
except urllib.error.HTTPError as e:
|
||||
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
|
||||
sys.exit(1)
|
||||
except Exception as e:
|
||||
print(json.dumps({"error": str(e)}))
|
||||
sys.exit(1)
|
||||
@@ -0,0 +1,71 @@
|
||||
import base64, json, os, sys, urllib.parse, urllib.request, urllib.error
|
||||
|
||||
|
||||
def _cfg():
|
||||
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
|
||||
|
||||
|
||||
def _headers(cfg):
|
||||
h = {"Accept": "application/json", "Content-Type": "application/json"}
|
||||
if str(cfg.get("auth_type") or "basic").lower() == "oauth":
|
||||
data = urllib.parse.urlencode({
|
||||
"grant_type": "password",
|
||||
"client_id": cfg.get("client_id", ""),
|
||||
"client_secret": cfg.get("client_secret", ""),
|
||||
"username": cfg.get("username", ""),
|
||||
"password": cfg.get("password", ""),
|
||||
}).encode("utf-8")
|
||||
req = urllib.request.Request(
|
||||
cfg.get("url", "").rstrip("/") + "/oauth_token.do", data=data,
|
||||
headers={"Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"}, method="POST")
|
||||
with urllib.request.urlopen(req, timeout=60) as r:
|
||||
tok = json.loads(r.read())
|
||||
if not tok.get("access_token"):
|
||||
raise Exception("OAuth token request failed: " + json.dumps(tok))
|
||||
h["Authorization"] = "Bearer " + tok["access_token"]
|
||||
else:
|
||||
cred = (cfg.get("username", "") + ":" + cfg.get("password", "")).encode("utf-8")
|
||||
h["Authorization"] = "Basic " + base64.b64encode(cred).decode("ascii")
|
||||
return h
|
||||
|
||||
|
||||
def request(method, path, params=None, body=None, root="/api/now", versioned=True):
|
||||
cfg = _cfg()
|
||||
v = str(cfg.get("api_version") or "").strip().strip("/")
|
||||
url = cfg.get("url", "").rstrip("/") + root + ("/" + v if v and versioned else "") + path
|
||||
q = {k: str(x) for k, x in (params or {}).items() if x not in (None, "")}
|
||||
if q:
|
||||
url += "?" + urllib.parse.urlencode(q)
|
||||
data = json.dumps(body).encode("utf-8") if body is not None else None
|
||||
req = urllib.request.Request(url, data=data, headers=_headers(cfg), method=method)
|
||||
with urllib.request.urlopen(req, timeout=90) as r:
|
||||
raw = r.read()
|
||||
return json.loads(raw) if raw else {}
|
||||
|
||||
|
||||
def main():
|
||||
inputs = json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
|
||||
uid = inputs.get("user_id") or ""
|
||||
if uid:
|
||||
res = request("GET", "/table/sys_user/" + urllib.parse.quote(uid))
|
||||
else:
|
||||
if inputs.get("user_name"):
|
||||
q = "user_name=" + inputs["user_name"]
|
||||
else:
|
||||
q = inputs.get("query") or ""
|
||||
res = request("GET", "/table/sys_user", params={
|
||||
"sysparm_query": q,
|
||||
"sysparm_limit": inputs.get("limit") or 10,
|
||||
"sysparm_offset": inputs.get("offset") or 0,
|
||||
})
|
||||
print(json.dumps(res))
|
||||
|
||||
|
||||
try:
|
||||
main()
|
||||
except urllib.error.HTTPError as e:
|
||||
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
|
||||
sys.exit(1)
|
||||
except Exception as e:
|
||||
print(json.dumps({"error": str(e)}))
|
||||
sys.exit(1)
|
||||
@@ -0,0 +1,62 @@
|
||||
import base64, json, os, sys, urllib.parse, urllib.request, urllib.error
|
||||
|
||||
|
||||
def _cfg():
|
||||
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
|
||||
|
||||
|
||||
def _headers(cfg):
|
||||
h = {"Accept": "application/json", "Content-Type": "application/json"}
|
||||
if str(cfg.get("auth_type") or "basic").lower() == "oauth":
|
||||
data = urllib.parse.urlencode({
|
||||
"grant_type": "password",
|
||||
"client_id": cfg.get("client_id", ""),
|
||||
"client_secret": cfg.get("client_secret", ""),
|
||||
"username": cfg.get("username", ""),
|
||||
"password": cfg.get("password", ""),
|
||||
}).encode("utf-8")
|
||||
req = urllib.request.Request(
|
||||
cfg.get("url", "").rstrip("/") + "/oauth_token.do", data=data,
|
||||
headers={"Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"}, method="POST")
|
||||
with urllib.request.urlopen(req, timeout=60) as r:
|
||||
tok = json.loads(r.read())
|
||||
if not tok.get("access_token"):
|
||||
raise Exception("OAuth token request failed: " + json.dumps(tok))
|
||||
h["Authorization"] = "Bearer " + tok["access_token"]
|
||||
else:
|
||||
cred = (cfg.get("username", "") + ":" + cfg.get("password", "")).encode("utf-8")
|
||||
h["Authorization"] = "Basic " + base64.b64encode(cred).decode("ascii")
|
||||
return h
|
||||
|
||||
|
||||
def request(method, path, params=None, body=None, root="/api/now", versioned=True):
|
||||
cfg = _cfg()
|
||||
v = str(cfg.get("api_version") or "").strip().strip("/")
|
||||
url = cfg.get("url", "").rstrip("/") + root + ("/" + v if v and versioned else "") + path
|
||||
q = {k: str(x) for k, x in (params or {}).items() if x not in (None, "")}
|
||||
if q:
|
||||
url += "?" + urllib.parse.urlencode(q)
|
||||
data = json.dumps(body).encode("utf-8") if body is not None else None
|
||||
req = urllib.request.Request(url, data=data, headers=_headers(cfg), method=method)
|
||||
with urllib.request.urlopen(req, timeout=90) as r:
|
||||
raw = r.read()
|
||||
return json.loads(raw) if raw else {}
|
||||
|
||||
|
||||
def main():
|
||||
cfg = _cfg()
|
||||
table = str(cfg.get("ticket_type") or "incident")
|
||||
res = request("GET", "/table/" + urllib.parse.quote(table), params={"sysparm_limit": 1})
|
||||
if "result" not in res:
|
||||
raise Exception("Unexpected response: " + json.dumps(res))
|
||||
print(json.dumps({"ok": True, "table": table}))
|
||||
|
||||
|
||||
try:
|
||||
main()
|
||||
except urllib.error.HTTPError as e:
|
||||
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
|
||||
sys.exit(1)
|
||||
except Exception as e:
|
||||
print(json.dumps({"error": str(e)}))
|
||||
sys.exit(1)
|
||||
@@ -0,0 +1,80 @@
|
||||
import base64, json, os, sys, urllib.parse, urllib.request, urllib.error
|
||||
|
||||
|
||||
def _cfg():
|
||||
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
|
||||
|
||||
|
||||
def _headers(cfg):
|
||||
h = {"Accept": "application/json", "Content-Type": "application/json"}
|
||||
if str(cfg.get("auth_type") or "basic").lower() == "oauth":
|
||||
data = urllib.parse.urlencode({
|
||||
"grant_type": "password",
|
||||
"client_id": cfg.get("client_id", ""),
|
||||
"client_secret": cfg.get("client_secret", ""),
|
||||
"username": cfg.get("username", ""),
|
||||
"password": cfg.get("password", ""),
|
||||
}).encode("utf-8")
|
||||
req = urllib.request.Request(
|
||||
cfg.get("url", "").rstrip("/") + "/oauth_token.do", data=data,
|
||||
headers={"Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"}, method="POST")
|
||||
with urllib.request.urlopen(req, timeout=60) as r:
|
||||
tok = json.loads(r.read())
|
||||
if not tok.get("access_token"):
|
||||
raise Exception("OAuth token request failed: " + json.dumps(tok))
|
||||
h["Authorization"] = "Bearer " + tok["access_token"]
|
||||
else:
|
||||
cred = (cfg.get("username", "") + ":" + cfg.get("password", "")).encode("utf-8")
|
||||
h["Authorization"] = "Basic " + base64.b64encode(cred).decode("ascii")
|
||||
return h
|
||||
|
||||
|
||||
def request(method, path, params=None, body=None, root="/api/now", versioned=True):
|
||||
cfg = _cfg()
|
||||
v = str(cfg.get("api_version") or "").strip().strip("/")
|
||||
url = cfg.get("url", "").rstrip("/") + root + ("/" + v if v and versioned else "") + path
|
||||
q = {k: str(x) for k, x in (params or {}).items() if x not in (None, "")}
|
||||
if q:
|
||||
url += "?" + urllib.parse.urlencode(q)
|
||||
data = json.dumps(body).encode("utf-8") if body is not None else None
|
||||
req = urllib.request.Request(url, data=data, headers=_headers(cfg), method=method)
|
||||
with urllib.request.urlopen(req, timeout=90) as r:
|
||||
raw = r.read()
|
||||
return json.loads(raw) if raw else {}
|
||||
|
||||
|
||||
def split_fields(s, prefix_custom=False):
|
||||
out = {}
|
||||
for part in str(s or "").split(";"):
|
||||
if "=" not in part:
|
||||
continue
|
||||
k, v = part.split("=", 1)
|
||||
k = k.strip()
|
||||
if prefix_custom and k and not k.startswith("u_"):
|
||||
k = "u_" + k
|
||||
if k:
|
||||
out[k] = v
|
||||
return out
|
||||
|
||||
|
||||
def main():
|
||||
inputs = json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
|
||||
table = inputs.get("table_name") or ""
|
||||
rid = inputs.get("id") or ""
|
||||
body = split_fields(inputs.get("fields"))
|
||||
body.update(split_fields(inputs.get("custom_fields"), prefix_custom=True))
|
||||
if not body:
|
||||
raise Exception("nothing to update")
|
||||
params = {"sysparm_input_display_value": "true"} if inputs.get("input_display_value") else None
|
||||
res = request("PATCH", "/table/" + urllib.parse.quote(table) + "/" + urllib.parse.quote(rid), params=params, body=body)
|
||||
print(json.dumps(res))
|
||||
|
||||
|
||||
try:
|
||||
main()
|
||||
except urllib.error.HTTPError as e:
|
||||
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
|
||||
sys.exit(1)
|
||||
except Exception as e:
|
||||
print(json.dumps({"error": str(e)}))
|
||||
sys.exit(1)
|
||||
@@ -0,0 +1,93 @@
|
||||
import base64, json, os, sys, urllib.parse, urllib.request, urllib.error
|
||||
|
||||
|
||||
def _cfg():
|
||||
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
|
||||
|
||||
|
||||
def _headers(cfg):
|
||||
h = {"Accept": "application/json", "Content-Type": "application/json"}
|
||||
if str(cfg.get("auth_type") or "basic").lower() == "oauth":
|
||||
data = urllib.parse.urlencode({
|
||||
"grant_type": "password",
|
||||
"client_id": cfg.get("client_id", ""),
|
||||
"client_secret": cfg.get("client_secret", ""),
|
||||
"username": cfg.get("username", ""),
|
||||
"password": cfg.get("password", ""),
|
||||
}).encode("utf-8")
|
||||
req = urllib.request.Request(
|
||||
cfg.get("url", "").rstrip("/") + "/oauth_token.do", data=data,
|
||||
headers={"Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"}, method="POST")
|
||||
with urllib.request.urlopen(req, timeout=60) as r:
|
||||
tok = json.loads(r.read())
|
||||
if not tok.get("access_token"):
|
||||
raise Exception("OAuth token request failed: " + json.dumps(tok))
|
||||
h["Authorization"] = "Bearer " + tok["access_token"]
|
||||
else:
|
||||
cred = (cfg.get("username", "") + ":" + cfg.get("password", "")).encode("utf-8")
|
||||
h["Authorization"] = "Basic " + base64.b64encode(cred).decode("ascii")
|
||||
return h
|
||||
|
||||
|
||||
def request(method, path, params=None, body=None, root="/api/now", versioned=True):
|
||||
cfg = _cfg()
|
||||
v = str(cfg.get("api_version") or "").strip().strip("/")
|
||||
url = cfg.get("url", "").rstrip("/") + root + ("/" + v if v and versioned else "") + path
|
||||
q = {k: str(x) for k, x in (params or {}).items() if x not in (None, "")}
|
||||
if q:
|
||||
url += "?" + urllib.parse.urlencode(q)
|
||||
data = json.dumps(body).encode("utf-8") if body is not None else None
|
||||
req = urllib.request.Request(url, data=data, headers=_headers(cfg), method=method)
|
||||
with urllib.request.urlopen(req, timeout=90) as r:
|
||||
raw = r.read()
|
||||
return json.loads(raw) if raw else {}
|
||||
|
||||
|
||||
def split_fields(s, prefix_custom=False):
|
||||
out = {}
|
||||
for part in str(s or "").split(";"):
|
||||
if "=" not in part:
|
||||
continue
|
||||
k, v = part.split("=", 1)
|
||||
k = k.strip()
|
||||
if prefix_custom and k and not k.startswith("u_"):
|
||||
k = "u_" + k
|
||||
if k:
|
||||
out[k] = v
|
||||
return out
|
||||
|
||||
|
||||
NAMED_FIELDS = [
|
||||
"short_description", "description", "urgency", "impact", "priority", "state",
|
||||
"category", "subcategory", "caller_id", "assigned_to", "assignment_group",
|
||||
"comments", "work_notes", "close_code", "close_notes",
|
||||
]
|
||||
|
||||
|
||||
def main():
|
||||
inputs = json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
|
||||
ticket_id = str(inputs.get("id") or "")
|
||||
if not ticket_id:
|
||||
raise Exception("id is required")
|
||||
table = str(inputs.get("ticket_type") or _cfg().get("ticket_type") or "incident")
|
||||
body = {}
|
||||
for k in NAMED_FIELDS:
|
||||
v = inputs.get(k)
|
||||
if v not in (None, ""):
|
||||
body[k] = v
|
||||
body.update(split_fields(inputs.get("fields")))
|
||||
body.update(split_fields(inputs.get("custom_fields"), prefix_custom=True))
|
||||
if not body:
|
||||
raise Exception("nothing to update")
|
||||
params = {"sysparm_input_display_value": "true"} if inputs.get("input_display_value") else None
|
||||
print(json.dumps(request("PATCH", "/table/" + urllib.parse.quote(table) + "/" + urllib.parse.quote(ticket_id), params=params, body=body)))
|
||||
|
||||
|
||||
try:
|
||||
main()
|
||||
except urllib.error.HTTPError as e:
|
||||
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
|
||||
sys.exit(1)
|
||||
except Exception as e:
|
||||
print(json.dumps({"error": str(e)}))
|
||||
sys.exit(1)
|
||||
@@ -0,0 +1,63 @@
|
||||
import base64, json, mimetypes, os, sys, urllib.parse, urllib.request, urllib.error
|
||||
|
||||
|
||||
def _cfg():
|
||||
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
|
||||
|
||||
|
||||
def _headers(cfg):
|
||||
h = {"Accept": "application/json", "Content-Type": "application/json"}
|
||||
if str(cfg.get("auth_type") or "basic").lower() == "oauth":
|
||||
data = urllib.parse.urlencode({
|
||||
"grant_type": "password",
|
||||
"client_id": cfg.get("client_id", ""),
|
||||
"client_secret": cfg.get("client_secret", ""),
|
||||
"username": cfg.get("username", ""),
|
||||
"password": cfg.get("password", ""),
|
||||
}).encode("utf-8")
|
||||
req = urllib.request.Request(
|
||||
cfg.get("url", "").rstrip("/") + "/oauth_token.do", data=data,
|
||||
headers={"Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"}, method="POST")
|
||||
with urllib.request.urlopen(req, timeout=60) as r:
|
||||
tok = json.loads(r.read())
|
||||
if not tok.get("access_token"):
|
||||
raise Exception("OAuth token request failed: " + json.dumps(tok))
|
||||
h["Authorization"] = "Bearer " + tok["access_token"]
|
||||
else:
|
||||
cred = (cfg.get("username", "") + ":" + cfg.get("password", "")).encode("utf-8")
|
||||
h["Authorization"] = "Basic " + base64.b64encode(cred).decode("ascii")
|
||||
return h
|
||||
|
||||
|
||||
def main():
|
||||
inputs = json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
|
||||
ticket_id = str(inputs.get("id") or "")
|
||||
if not ticket_id:
|
||||
raise Exception("id is required")
|
||||
cfg = _cfg()
|
||||
table = str(inputs.get("table_name") or cfg.get("ticket_type") or "incident")
|
||||
file_name = str(inputs.get("file_name") or "")
|
||||
if not file_name:
|
||||
raise Exception("file_name is required")
|
||||
url = cfg.get("url", "").rstrip("/") + "/api/now/attachment/file?" + urllib.parse.urlencode({
|
||||
"table_name": table,
|
||||
"table_sys_id": ticket_id,
|
||||
"file_name": file_name,
|
||||
})
|
||||
headers = _headers(cfg)
|
||||
headers["Content-Type"] = mimetypes.guess_type(file_name)[0] or "application/octet-stream"
|
||||
body = base64.b64decode(inputs["content_base64"])
|
||||
req = urllib.request.Request(url, data=body, headers=headers, method="POST")
|
||||
with urllib.request.urlopen(req, timeout=120) as r:
|
||||
raw = r.read()
|
||||
print(json.dumps(json.loads(raw) if raw else {}))
|
||||
|
||||
|
||||
try:
|
||||
main()
|
||||
except urllib.error.HTTPError as e:
|
||||
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
|
||||
sys.exit(1)
|
||||
except Exception as e:
|
||||
print(json.dumps({"error": str(e)}))
|
||||
sys.exit(1)
|
||||
Reference in New Issue
Block a user