feat: add template and template group management via GraphQL
- Implemented GraphQL endpoints for importing, querying, and deleting Zabbix templates and template groups. - Added support for full template data import, including items, preprocessing steps, tags, and linked templates. - Implemented dependent item support by deferred creation logic in the template importer. - Added ability to query templates and template groups with name pattern filtering (supporting Zabbix wildcards). - Implemented batch deletion for templates and template groups by ID or name pattern. - Improved error reporting by including detailed Zabbix API error data in GraphQL responses. - Added comprehensive unit and integration tests covering all new functionality. - Provided GraphQL sample queries and mutations in the 'docs' directory for all new endpoints.
This commit is contained in:
parent
e641f8e610
commit
a3ed4886a3
22 changed files with 2450 additions and 20 deletions
112
src/test/template_query.test.ts
Normal file
112
src/test/template_query.test.ts
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
|
||||
import {createResolvers} from "../api/resolvers.js";
|
||||
import {zabbixAPI} from "../datasources/zabbix-api.js";
|
||||
import {QueryTemplatesArgs} from "../schema/generated/graphql.js";
|
||||
|
||||
// Mocking ZabbixAPI
|
||||
jest.mock("../datasources/zabbix-api.js", () => ({
|
||||
zabbixAPI: {
|
||||
executeRequest: jest.fn(),
|
||||
post: jest.fn(),
|
||||
baseURL: "http://mock-zabbix"
|
||||
},
|
||||
ZABBIX_EDGE_DEVICE_BASE_GROUP: "Baustellen-Devices"
|
||||
}));
|
||||
|
||||
describe("Template Resolver", () => {
|
||||
let resolvers: any;
|
||||
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
resolvers = createResolvers();
|
||||
});
|
||||
|
||||
test("templates query - returns all templates", async () => {
|
||||
const mockTemplates = [
|
||||
{ templateid: "1", name: "Template 1", uuid: "uuid1" },
|
||||
{ templateid: "2", name: "Template 2", uuid: "uuid2" }
|
||||
];
|
||||
|
||||
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce(mockTemplates);
|
||||
|
||||
const args: QueryTemplatesArgs = {};
|
||||
const context = { zabbixAuthToken: "test-token" };
|
||||
|
||||
const result = await resolvers.Query.templates(null, args, context);
|
||||
|
||||
expect(result).toEqual(mockTemplates);
|
||||
expect(zabbixAPI.post).toHaveBeenCalledWith("template.get", expect.objectContaining({
|
||||
body: expect.objectContaining({
|
||||
method: "template.get",
|
||||
params: {}
|
||||
})
|
||||
}));
|
||||
});
|
||||
|
||||
test("templates query - filters by hostids", async () => {
|
||||
const mockTemplates = [{ templateid: "1", name: "Template 1" }];
|
||||
|
||||
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce(mockTemplates);
|
||||
|
||||
const args: QueryTemplatesArgs = { hostids: [1] };
|
||||
const context = { zabbixAuthToken: "test-token" };
|
||||
|
||||
const result = await resolvers.Query.templates(null, args, context);
|
||||
|
||||
expect(result).toEqual(mockTemplates);
|
||||
expect(zabbixAPI.post).toHaveBeenCalledWith("template.get", expect.objectContaining({
|
||||
body: expect.objectContaining({
|
||||
method: "template.get",
|
||||
params: expect.objectContaining({
|
||||
templateids: [1]
|
||||
})
|
||||
})
|
||||
}));
|
||||
});
|
||||
|
||||
test("templates query - filters by name_pattern", async () => {
|
||||
const mockTemplates = [{ templateid: "1", name: "Template 1" }];
|
||||
|
||||
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce(mockTemplates);
|
||||
|
||||
const args: QueryTemplatesArgs = { name_pattern: "Template" };
|
||||
const context = { zabbixAuthToken: "test-token" };
|
||||
|
||||
const result = await resolvers.Query.templates(null, args, context);
|
||||
|
||||
expect(result).toEqual(mockTemplates);
|
||||
expect(zabbixAPI.post).toHaveBeenCalledWith("template.get", expect.objectContaining({
|
||||
body: expect.objectContaining({
|
||||
method: "template.get",
|
||||
params: expect.objectContaining({
|
||||
search: {
|
||||
name: "Template"
|
||||
}
|
||||
})
|
||||
})
|
||||
}));
|
||||
});
|
||||
|
||||
test("templates query - filters by name_pattern with % wildcard", async () => {
|
||||
const mockTemplates = [{ templateid: "1", name: "Template 1" }];
|
||||
|
||||
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce(mockTemplates);
|
||||
|
||||
const args: QueryTemplatesArgs = { name_pattern: "Temp%1" };
|
||||
const context = { zabbixAuthToken: "test-token" };
|
||||
|
||||
const result = await resolvers.Query.templates(null, args, context);
|
||||
|
||||
expect(result).toEqual(mockTemplates);
|
||||
expect(zabbixAPI.post).toHaveBeenCalledWith("template.get", expect.objectContaining({
|
||||
body: expect.objectContaining({
|
||||
method: "template.get",
|
||||
params: expect.objectContaining({
|
||||
search: {
|
||||
name: "Temp%1"
|
||||
}
|
||||
})
|
||||
})
|
||||
}));
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue