Skip to content

Commit

Permalink
Add various missing check options (#156)
Browse files Browse the repository at this point in the history
Closes #154
  • Loading branch information
silas authored Oct 20, 2023
1 parent fe75b4a commit 2bb6a2a
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 29 deletions.
12 changes: 11 additions & 1 deletion lib/agent/check.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,40 @@ interface Check {

type ListResult = Record<string, Check>;

interface RegisterOptions extends CommonOptions {
export interface CheckOptions {
name: string;
id?: string;
serviceid?: string;
http?: string;
body?: string;
header?: Record<string, string>;
disableredirects?: boolean;
h2ping?: string;
h2pingusetls?: boolean;
tlsskipverify?: boolean;
tcp?: string;
udp?: string;
args?: string[];
script?: string;
dockercontainerid?: string;
grpc?: string;
grpcusetls?: boolean;
shell?: string;
timeout: string;
interval?: string;
ttl?: string;
aliasnode?: string;
aliasservice?: string;
notes?: string;
status?: string;
deregistercriticalserviceafter?: string;
failuresbeforewarning?: number;
successbeforepassing?: number;
failuresbeforecritical?: number;
}

interface RegisterOptions extends CheckOptions, CommonOptions {}

type RegisterResult = any;

interface DeregisterOptions extends CommonOptions {
Expand Down
19 changes: 3 additions & 16 deletions lib/agent/service.d.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,12 @@
import { CommonOptions, Consul } from "../consul";
import { CheckOptions } from "./check";

interface ListOptions extends CommonOptions {
filter?: string;
}

type ListResult = Record<string, any>;

interface RegisterCheck {
http?: string;
tcp?: string;
script?: string;
dockercontainerid?: string;
shell?: string;
interval?: string;
timeout?: string;
ttl?: string;
notes?: string;
status?: string;
deregistercriticalserviceafter?: string;
}

interface RegisterConnect {
native?: boolean;
proxy?: any;
Expand All @@ -38,8 +25,8 @@ interface RegisterOptions extends CommonOptions {
kind?: string;
proxy?: any;
connect?: RegisterConnect;
check?: RegisterCheck;
checks?: RegisterCheck[];
check?: CheckOptions;
checks?: CheckOptions[];
}

type RegisterResult = any;
Expand Down
38 changes: 34 additions & 4 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,18 +302,36 @@ function _createServiceCheck(src) {
const dst = {};

if (
(src.grpc || src.http || src.tcp || src.args || src.script) &&
(src.grpc ||
src.h2ping ||
src.http ||
src.tcp ||
src.udp ||
src.args ||
src.script) &&
src.interval
) {
if (src.grpc) {
dst.GRPC = src.grpc;
if (src.hasOwnProperty("grpcusetls")) dst.GRPCUseTLS = src.grpcusetls;
} else if (src.h2ping) {
dst.H2Ping = src.h2ping;
if (src.hasOwnProperty("h2pingusetls")) {
dst.H2PingUseTLS = src.h2pingusetls;
}
} else if (src.http) {
dst.HTTP = src.http;
if (src.hasOwnProperty("tlsskipverify"))
dst.TLSSkipVerify = src.tlsskipverify;
if (src.hasOwnProperty("body")) dst.Body = src.body;
if (src.hasOwnProperty("disableredirects")) {
dst.DisableRedirects = src.disableredirects;
}
if (src.hasOwnProperty("header")) dst.Header = src.header;
if (src.hasOwnProperty("method")) dst.Method = src.method;
} else if (src.tcp) {
dst.TCP = src.tcp;
if (src.hasOwnProperty("tcpusetls")) dst.TCPUseTLS = src.tcpusetls;
} else if (src.udp) {
dst.UDP = src.udp;
} else {
if (src.args) {
dst.Args = src.args;
Expand All @@ -325,22 +343,34 @@ function _createServiceCheck(src) {
if (src.hasOwnProperty("shell")) dst.Shell = src.shell;
}
dst.Interval = src.interval;
if (src.hasOwnProperty("outputmaxsize")) {
dst.OutputMaxSize = src.outputmaxsize;
}
if (src.hasOwnProperty("timeout")) dst.Timeout = src.timeout;
if (src.hasOwnProperty("tlsservername")) {
dst.TLSServerName = src.tlsservername;
}
if (src.hasOwnProperty("tlsskipverify")) {
dst.TLSSkipVerify = src.tlsskipverify;
}
} else if (src.ttl) {
dst.TTL = src.ttl;
} else if (src.aliasnode || src.aliasservice) {
if (src.hasOwnProperty("aliasnode")) dst.AliasNode = src.aliasnode;
if (src.hasOwnProperty("aliasservice")) dst.AliasService = src.aliasservice;
} else {
throw new Error(
"args/grpc/http/tcp and interval, ttl, or aliasnode/aliasservice"
"args/grpc/h2ping/http/tcp/udp and interval, ttl, or aliasnode/aliasservice"
);
}
if (src.hasOwnProperty("notes")) dst.Notes = src.notes;
if (src.hasOwnProperty("status")) dst.Status = src.status;
if (src.hasOwnProperty("deregistercriticalserviceafter")) {
dst.DeregisterCriticalServiceAfter = src.deregistercriticalserviceafter;
}
if (src.hasOwnProperty("failuresbeforewarning")) {
dst.FailuresBeforeWarning = src.failuresbeforewarning;
}
if (src.hasOwnProperty("failuresbeforecritical")) {
dst.FailuresBeforeCritical = src.failuresbeforecritical;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "consul",
"version": "2.0.0-next.2",
"version": "2.0.0-next.3",
"description": "Consul client",
"main": "./lib",
"types": "./lib/index.d.ts",
Expand Down
4 changes: 2 additions & 2 deletions test/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ describe("Agent", function () {
} catch (err) {
should(err).property(
"message",
"consul: agent.check.register: args/grpc/http/tcp and interval, " +
"consul: agent.check.register: args/grpc/h2ping/http/tcp/udp and interval, " +
"ttl, or aliasnode/aliasservice"
);
}
Expand Down Expand Up @@ -300,7 +300,7 @@ describe("Agent", function () {
} catch (err) {
should(err).property(
"message",
"consul: agent.service.register: args/grpc/http/tcp and interval, " +
"consul: agent.service.register: args/grpc/h2ping/http/tcp/udp and interval, " +
"ttl, or aliasnode/aliasservice"
);
}
Expand Down
92 changes: 87 additions & 5 deletions test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,9 @@ describe("utils", function () {
interval: "60s",
notes: "Just a note.",
status: "passing",
failuresbeforecritical: 1,
successBeforePassing: 2,
failuresbeforewarning: 1,
failuresbeforecritical: 2,
successBeforePassing: 3,
})
).eql({
ID: "id",
Expand All @@ -362,8 +363,9 @@ describe("utils", function () {
Interval: "60s",
Notes: "Just a note.",
Status: "passing",
FailuresBeforeCritical: 1,
SuccessBeforePassing: 2,
FailuresBeforeWarning: 1,
FailuresBeforeCritical: 2,
SuccessBeforePassing: 3,
});

should(
Expand All @@ -372,6 +374,7 @@ describe("utils", function () {
name: "name",
service_id: "service",
tcp: "localhost:22",
tcpusetls: true,
interval: "10s",
notes: "SSH TCP on port 22",
status: "passing",
Expand All @@ -382,6 +385,7 @@ describe("utils", function () {
Name: "name",
ServiceID: "service",
TCP: "localhost:22",
TCPUseTLS: true,
Interval: "10s",
Notes: "SSH TCP on port 22",
Status: "passing",
Expand Down Expand Up @@ -422,10 +426,56 @@ describe("utils", function () {
utils.createServiceCheck({
grpc: "localhost:50051",
interval: "5s",
tlsservername: "server",
tlsskipverify: true,
outputmaxsize: 4096,
})
).eql({
GRPC: "localhost:50051",
Interval: "5s",
TLSSkipVerify: true,
TLSServerName: "server",
OutputMaxSize: 4096,
});

should(
utils.createServiceCheck({
http: "https://example.com/test",
body: "{}",
disableredirects: true,
header: { authorization: ["one"] },
method: "POST",
interval: "5s",
})
).eql({
HTTP: "https://example.com/test",
Body: "{}",
DisableRedirects: true,
Header: { authorization: ["one"] },
Method: "POST",
Interval: "5s",
});

should(
utils.createServiceCheck({
h2ping: "https://example.com/test",
interval: "5s",
})
).eql({
H2Ping: "https://example.com/test",
Interval: "5s",
});

should(
utils.createServiceCheck({
h2ping: "http://example.com/test",
h2pingusetls: false,
interval: "5s",
})
).eql({
H2Ping: "http://example.com/test",
Interval: "5s",
H2PingUseTLS: false,
});

should(
Expand All @@ -440,6 +490,38 @@ describe("utils", function () {
Interval: "10s",
});

should(
utils.createServiceCheck({
udp: "localhost:50051",
interval: "10s",
})
).eql({
UDP: "localhost:50051",
Interval: "10s",
});

should(
utils.createServiceCheck({
tcp: "localhost:50051",
interval: "10s",
})
).eql({
TCP: "localhost:50051",
Interval: "10s",
});

should(
utils.createServiceCheck({
tcp: "localhost:50051",
interval: "10s",
tcpusetls: true,
})
).eql({
TCP: "localhost:50051",
Interval: "10s",
TCPUseTLS: true,
});

should(
utils.createServiceCheck({
ttl: "15s",
Expand Down Expand Up @@ -472,7 +554,7 @@ describe("utils", function () {
should(() => {
utils.createCheck();
}).throw(
"args/grpc/http/tcp and interval, ttl, or aliasnode/aliasservice"
"args/grpc/h2ping/http/tcp/udp and interval, ttl, or aliasnode/aliasservice"
);
}
);
Expand Down

0 comments on commit 2bb6a2a

Please sign in to comment.