6548740890
Sumo Logic REST API, 4 commands: search (job create+poll+messages, cookie session), list/get collectors. Access-key (Basic) auth, stdlib-only. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
66 lines
2.0 KiB
Python
66 lines
2.0 KiB
Python
import json, os, sys, time, base64, http.cookiejar
|
|
import urllib.parse, urllib.request, urllib.error
|
|
|
|
|
|
def _cfg():
|
|
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
|
|
|
|
|
|
def _inputs():
|
|
return json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
|
|
|
|
|
|
def _auth(cfg):
|
|
raw = str(cfg.get("access_id", "")) + ":" + str(cfg.get("access_key", ""))
|
|
return "Basic " + base64.b64encode(raw.encode("utf-8")).decode("utf-8")
|
|
|
|
|
|
def _base(cfg):
|
|
return str(cfg.get("base_url", "")).rstrip("/") + "/api/v1"
|
|
|
|
|
|
class Client:
|
|
def __init__(self, cfg):
|
|
self.cfg = cfg
|
|
self.opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(http.cookiejar.CookieJar()))
|
|
|
|
def call(self, method, path, body=None, params=None):
|
|
url = _base(self.cfg) + path
|
|
if params:
|
|
clean = {k: v for k, v in params.items() if v not in (None, "")}
|
|
if clean:
|
|
url += "?" + urllib.parse.urlencode(clean)
|
|
data = json.dumps(body).encode("utf-8") if body is not None else None
|
|
headers = {"Authorization": _auth(self.cfg), "Accept": "application/json"}
|
|
if data is not None:
|
|
headers["Content-Type"] = "application/json"
|
|
req = urllib.request.Request(url, data=data, headers=headers, method=method)
|
|
with self.opener.open(req, timeout=60) as r:
|
|
raw = r.read()
|
|
return json.loads(raw) if raw else {}
|
|
|
|
|
|
def _run(fn):
|
|
try:
|
|
client = Client(_cfg())
|
|
print(json.dumps(fn(client, _inputs())))
|
|
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)
|
|
|
|
|
|
q = lambda v: urllib.parse.quote(str(v), safe="")
|
|
|
|
|
|
def main(client, inputs):
|
|
collector_id = inputs.get("collector_id")
|
|
if not collector_id:
|
|
raise Exception("collector_id is required")
|
|
return client.call("GET", "/collectors/" + q(collector_id))
|
|
|
|
|
|
_run(main)
|