chore: Add allDevices query resolver, update Zabbix device query handling, and enhance schema with DeviceConfig and WidgetPreview types
This commit is contained in:
parent
c1035cd614
commit
e641f8e610
7 changed files with 200 additions and 33 deletions
|
|
@ -1,4 +1,4 @@
|
|||
import {CreateHostResponse, Host, ZabbixHost} from "../schema/generated/graphql.js";
|
||||
import {CreateHostResponse, Device, Host, ZabbixHost} from "../schema/generated/graphql.js";
|
||||
import {ZabbixAPI} from "./zabbix-api.js";
|
||||
import {
|
||||
isZabbixErrorResult,
|
||||
|
|
@ -9,16 +9,17 @@ import {
|
|||
ZabbixResult
|
||||
} from "./zabbix-request.js";
|
||||
import {ZabbixHistoryGetParams, ZabbixQueryHistoryRequest} from "./zabbix-history.js";
|
||||
import {isArray} from "node:util";
|
||||
|
||||
|
||||
export class ZabbixQueryHostsGenericRequest<T extends ZabbixResult> extends ZabbixRequest<T> {
|
||||
export class ZabbixQueryHostsGenericRequest<T extends ZabbixResult, A extends ParsedArgs = ParsedArgs> extends ZabbixRequest<T, A> {
|
||||
public static PATH = "host.get";
|
||||
|
||||
constructor(path: string, authToken?: string | null, cookie?: string | null) {
|
||||
super(path, authToken, cookie);
|
||||
}
|
||||
|
||||
createZabbixParams(args?: ParsedArgs): ZabbixParams {
|
||||
createZabbixParams(args?: A): ZabbixParams {
|
||||
return {
|
||||
...super.createZabbixParams(args),
|
||||
selectParentTemplates: [
|
||||
|
|
@ -63,12 +64,12 @@ export class ZabbixQueryHostsMetaRequest extends ZabbixQueryHostsGenericRequest<
|
|||
}
|
||||
|
||||
|
||||
export class ZabbixQueryHostsGenericRequestWithItems<T extends ZabbixResult> extends ZabbixQueryHostsGenericRequest<T> {
|
||||
export class ZabbixQueryHostsGenericRequestWithItems<T extends ZabbixResult, A extends ParsedArgs = ParsedArgs> extends ZabbixQueryHostsGenericRequest<T, A> {
|
||||
constructor(path: string, authToken?: string | null, cookie?: string) {
|
||||
super(path, authToken, cookie);
|
||||
}
|
||||
|
||||
createZabbixParams(args?: ParsedArgs): ZabbixParams {
|
||||
createZabbixParams(args?: A): ZabbixParams {
|
||||
return {
|
||||
...super.createZabbixParams(args),
|
||||
selectItems: [
|
||||
|
|
@ -93,7 +94,7 @@ export class ZabbixQueryHostsGenericRequestWithItems<T extends ZabbixResult> ext
|
|||
};
|
||||
}
|
||||
|
||||
async executeRequestReturnError(zabbixAPI: ZabbixAPI, args?: ParsedArgs): Promise<ZabbixErrorResult | T> {
|
||||
async executeRequestReturnError(zabbixAPI: ZabbixAPI, args?: A): Promise<ZabbixErrorResult | T> {
|
||||
let result = await super.executeRequestReturnError(zabbixAPI, args);
|
||||
|
||||
if (result && !isZabbixErrorResult(result)) {
|
||||
|
|
@ -123,12 +124,12 @@ export class ZabbixQueryHostsGenericRequestWithItems<T extends ZabbixResult> ext
|
|||
}
|
||||
}
|
||||
|
||||
export class ZabbixQueryHostsGenericRequestWithItemsAndInventory<T extends ZabbixResult> extends ZabbixQueryHostsGenericRequestWithItems<T> {
|
||||
export class ZabbixQueryHostsGenericRequestWithItemsAndInventory<T extends ZabbixResult, A extends ParsedArgs = ParsedArgs> extends ZabbixQueryHostsGenericRequestWithItems<T, A> {
|
||||
constructor(path: string, authToken?: string | null, cookie?: string) {
|
||||
super(path, authToken, cookie);
|
||||
}
|
||||
|
||||
createZabbixParams(args?: ParsedArgs): ZabbixParams {
|
||||
createZabbixParams(args?: A): ZabbixParams {
|
||||
return {
|
||||
...super.createZabbixParams(args),
|
||||
selectInventory: [
|
||||
|
|
@ -144,6 +145,22 @@ export class ZabbixQueryHostsRequestWithItemsAndInventory extends ZabbixQueryHos
|
|||
}
|
||||
}
|
||||
|
||||
export class ZabbixQueryDevicesArgs extends ParsedArgs {
|
||||
constructor(public args?: any) {
|
||||
if (!args?.tag_deviceType ||
|
||||
(Array.isArray(args.tag_deviceType) && !args.tag_deviceType.length)) {
|
||||
args.tag_deviceType_exists = true;
|
||||
}
|
||||
super(args);
|
||||
}
|
||||
}
|
||||
|
||||
export class ZabbixQueryDevices extends ZabbixQueryHostsGenericRequestWithItemsAndInventory<Device[], ZabbixQueryDevicesArgs> {
|
||||
constructor(authToken?: string | null, cookie?: string) {
|
||||
super("host.get.with_items", authToken, cookie);
|
||||
}
|
||||
}
|
||||
|
||||
const isZabbixCreateHostInputParams = (value: ZabbixParams): value is ZabbixCreateHostInputParams => "host" in value && !!value.host;
|
||||
|
||||
export interface ZabbixCreateHostInputParams extends ZabbixParams {
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ export interface ZabbixParams {
|
|||
}
|
||||
|
||||
export interface ZabbixWithTagsParams extends ZabbixParams {
|
||||
tags?: { tag: string; operator: number; value: any; }[]
|
||||
tags?: { tag: string; operator: number; value?: any; }[]
|
||||
}
|
||||
|
||||
export class ParsedArgs {
|
||||
|
|
@ -72,20 +72,28 @@ export class ParsedArgs {
|
|||
}
|
||||
delete args.groupidsbase
|
||||
}
|
||||
let filterTagStatements: { tag: string; operator: number; value: any; }[] = []
|
||||
let filterTagStatements: { tag: string; operator: number; value?: any; }[] = []
|
||||
let filterStatements = {}
|
||||
for (let argsKey in args) {
|
||||
// @ts-ignore
|
||||
let argsValue = args[argsKey]
|
||||
if (argsKey.startsWith("tag_") && argsValue !== null) {
|
||||
let argsArray = Array.isArray(argsValue) ? argsValue : [argsValue]
|
||||
argsArray.forEach((tagValue) => {
|
||||
if (argsKey.startsWith("tag_")) {
|
||||
if (argsKey.endsWith("_exists")) {
|
||||
filterTagStatements.push({
|
||||
"tag": argsKey.slice(4),
|
||||
"operator": 1,
|
||||
"value": tagValue,
|
||||
"tag": argsKey.slice(4, -7),
|
||||
"operator": argsValue ? 4 : 5
|
||||
})
|
||||
})
|
||||
} else if (argsValue !== null) {
|
||||
let argsArray = Array.isArray(argsValue) ? argsValue : [argsValue]
|
||||
argsArray.forEach((tagValue) => {
|
||||
filterTagStatements.push({
|
||||
"tag": argsKey.slice(4),
|
||||
"operator": 1,
|
||||
"value": tagValue,
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
// @ts-ignore
|
||||
delete args[argsKey]
|
||||
}
|
||||
|
|
@ -121,9 +129,9 @@ export class ParsedArgs {
|
|||
|
||||
if (this.name_pattern) {
|
||||
if ("search" in result) {
|
||||
(<any> result.search).name = this.name_pattern
|
||||
(<any>result.search).name = this.name_pattern
|
||||
} else {
|
||||
(<any> result).search = {
|
||||
(<any>result).search = {
|
||||
name: this.name_pattern,
|
||||
}
|
||||
}
|
||||
|
|
@ -184,7 +192,6 @@ export class ZabbixRequest<T extends ZabbixResult, A extends ParsedArgs = Parsed
|
|||
}
|
||||
|
||||
|
||||
|
||||
async prepare(zabbixAPI: ZabbixAPI, _args?: A): Promise<T | ZabbixErrorResult | undefined> {
|
||||
// If prepare returns something else than undefined, the execution will be skipped and the
|
||||
// result returned
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue