The manifest now carries an authority block naming the api credential service, so the marketplace view shows the slot instead of relying on the description text. The client takes the full Authorization header: the api slot accepts a basic username/password or a bearer token, setting both is an error. Egress stays operator-supplied at accept time because the instance host comes from the instance_url setting, which a static egressHosts list cannot express.
130 lines
3.6 KiB
Go
130 lines
3.6 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/base64"
|
|
json "encoding/json/v2"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// client is the ServiceNow Table API: one instance, basic auth, JSON both
|
|
// ways. Every area of the connector talks through it, so credentials and
|
|
// error shape live here and nowhere else.
|
|
type client struct {
|
|
base string // the instance URL with no trailing slash
|
|
auth string // the Authorization header value
|
|
http *http.Client
|
|
}
|
|
|
|
// newClient takes the full Authorization header value, so callers pick the
|
|
// scheme: basicAuth for a username and password, or "Bearer "+token.
|
|
func newClient(instanceURL, authHeader string) *client {
|
|
return &client{
|
|
base: strings.TrimRight(instanceURL, "/"),
|
|
auth: authHeader,
|
|
http: &http.Client{Timeout: 30 * time.Second},
|
|
}
|
|
}
|
|
|
|
// basicAuth builds the Authorization header for a basic credential.
|
|
func basicAuth(username, password string) string {
|
|
return "Basic " + base64.StdEncoding.EncodeToString([]byte(username+":"+password))
|
|
}
|
|
|
|
// tableResult is the Table API's envelope: every read returns result rows.
|
|
type tableResult struct {
|
|
Result []map[string]any `json:"result"`
|
|
}
|
|
|
|
// rows reads rows of one table. query carries the sysparm_* parameters.
|
|
func (c *client) rows(ctx context.Context, table string, query url.Values) ([]map[string]any, error) {
|
|
u := c.base + "/api/now/table/" + table
|
|
if len(query) > 0 {
|
|
u += "?" + query.Encode()
|
|
}
|
|
var out tableResult
|
|
if err := c.do(ctx, http.MethodGet, u, nil, &out); err != nil {
|
|
return nil, err
|
|
}
|
|
return out.Result, nil
|
|
}
|
|
|
|
// one returns the first row of a query, or nil when there is none.
|
|
func (c *client) one(ctx context.Context, table string, query url.Values) (map[string]any, error) {
|
|
query.Set("sysparm_limit", "1")
|
|
rows, err := c.rows(ctx, table, query)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(rows) == 0 {
|
|
return nil, nil
|
|
}
|
|
return rows[0], nil
|
|
}
|
|
|
|
// singleResult is the envelope the Table API wraps one written row in.
|
|
type singleResult struct {
|
|
Result map[string]any `json:"result"`
|
|
}
|
|
|
|
// create inserts one row and returns it.
|
|
func (c *client) create(ctx context.Context, table string, body map[string]any) (map[string]any, error) {
|
|
var out singleResult
|
|
if err := c.do(ctx, http.MethodPost, c.base+"/api/now/table/"+table, body, &out); err != nil {
|
|
return nil, err
|
|
}
|
|
return out.Result, nil
|
|
}
|
|
|
|
// update changes one row, addressed by sys_id, and returns it.
|
|
func (c *client) update(ctx context.Context, table, sysID string, body map[string]any) (map[string]any, error) {
|
|
var out singleResult
|
|
if err := c.do(ctx, http.MethodPatch, c.base+"/api/now/table/"+table+"/"+url.PathEscape(sysID), body, &out); err != nil {
|
|
return nil, err
|
|
}
|
|
return out.Result, nil
|
|
}
|
|
|
|
func (c *client) do(ctx context.Context, method, u string, body map[string]any, out any) error {
|
|
var rdr io.Reader
|
|
if body != nil {
|
|
b, err := json.Marshal(body)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
rdr = bytes.NewReader(b)
|
|
}
|
|
req, err := http.NewRequestWithContext(ctx, method, u, rdr)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
req.Header.Set("Authorization", c.auth)
|
|
req.Header.Set("Accept", "application/json")
|
|
if body != nil {
|
|
req.Header.Set("Content-Type", "application/json")
|
|
}
|
|
resp, err := c.http.Do(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode < 200 || resp.StatusCode > 299 {
|
|
b, _ := io.ReadAll(io.LimitReader(resp.Body, 4096))
|
|
return fmt.Errorf("servicenow %s %s: %s: %s", method, u, resp.Status, strings.TrimSpace(string(b)))
|
|
}
|
|
if out == nil {
|
|
return nil
|
|
}
|
|
b, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return json.Unmarshal(b, out)
|
|
}
|