250 lines
10 KiB
TypeScript
250 lines
10 KiB
TypeScript
import {
|
|
DeviceCommunicationType,
|
|
DeviceStatus,
|
|
Host,
|
|
MutationCreateHostArgs,
|
|
MutationImportHostGroupsArgs,
|
|
MutationImportHostsArgs,
|
|
MutationImportUserRightsArgs,
|
|
Permission,
|
|
QueryAllHostGroupsArgs,
|
|
QueryAllHostsArgs,
|
|
QueryExportHostValueHistoryArgs,
|
|
QueryExportUserRightsArgs,
|
|
QueryHasPermissionsArgs,
|
|
QueryUserPermissionsArgs,
|
|
Resolvers,
|
|
StorageItemType,
|
|
} from "../schema/generated/graphql.js";
|
|
|
|
import {HostImporter} from "../execution/host_importer.js";
|
|
import {HostValueExporter} from "../execution/host_exporter.js";
|
|
import {logger} from "../logging/logger.js";
|
|
import {ParsedArgs, ZabbixRequest} from "../datasources/zabbix-request.js";
|
|
import {ZabbixCreateHostRequest, ZabbixQueryHostsRequestWithItemsAndInventory,} from "../datasources/zabbix-hosts.js";
|
|
import {ZabbixQueryHostgroupsParams, ZabbixQueryHostgroupsRequest} from "../datasources/zabbix-hostgroups.js";
|
|
import {
|
|
ZabbixExportUserGroupArgs,
|
|
ZabbixExportUserGroupsRequest,
|
|
ZabbixImportUserGroupsParams,
|
|
ZabbixImportUserGroupsRequest
|
|
} from "../datasources/zabbix-usergroups.js";
|
|
import {
|
|
ZabbixImportUserRolesParams,
|
|
ZabbixImportUserRolesRequest,
|
|
ZabbixQueryUserRolesRequest
|
|
} from "../datasources/zabbix-userroles.js";
|
|
import {ZABBIX_EDGE_DEVICE_BASE_GROUP, zabbixAPI} from "../datasources/zabbix-api.js";
|
|
import {GraphQLInterfaceType, GraphQLList} from "graphql/type/index.js";
|
|
import {isDevice} from "./resolver_helpers.js";
|
|
import {ZabbixPermissionsHelper} from "../datasources/zabbix-permissions.js";
|
|
|
|
|
|
export function createResolvers(): Resolvers {
|
|
// @ts-ignore
|
|
// @ts-ignore
|
|
return {
|
|
Query: {
|
|
userPermissions: async (_parent: any, objectNamesFilter: QueryUserPermissionsArgs, {
|
|
zabbixAuthToken,
|
|
cookie
|
|
}: any) => {
|
|
return ZabbixPermissionsHelper.getUserPermissions(zabbixAPI, zabbixAuthToken, cookie, objectNamesFilter.objectNames)
|
|
},
|
|
hasPermissions: async (_parent: any, args: QueryHasPermissionsArgs, {zabbixAuthToken, cookie}: any) => {
|
|
return ZabbixPermissionsHelper.hasUserPermissions(zabbixAPI, args, zabbixAuthToken, cookie)
|
|
},
|
|
locations: (_parent: any, args: Object, {dataSources, zabbixAuthToken}: any) => {
|
|
return dataSources.zabbixAPI.getLocations(zabbixAuthToken, new ParsedArgs(args));
|
|
},
|
|
apiVersion: () => {
|
|
return process.env.API_VERSION ?? "unknown"
|
|
},
|
|
zabbixVersion: async () => {
|
|
return await new ZabbixRequest<string>("apiinfo.version").executeRequestThrowError(
|
|
zabbixAPI)
|
|
},
|
|
login: async (_parent, args) => {
|
|
return await new ZabbixRequest<any>("user.login").executeRequestThrowError(
|
|
zabbixAPI, new ParsedArgs(args))
|
|
},
|
|
logout: async (_parent, _args, {zabbixAuthToken, cookie}: any) => {
|
|
return await new ZabbixRequest<any>("user.logout", zabbixAuthToken, cookie).executeRequestThrowError(zabbixAPI);
|
|
},
|
|
|
|
allHosts: async (_parent: any, args: QueryAllHostsArgs, {
|
|
zabbixAuthToken,
|
|
cookie, dataSources
|
|
}: any) => {
|
|
args.tag_hostType ??= [ZABBIX_EDGE_DEVICE_BASE_GROUP];
|
|
return await new ZabbixQueryHostsRequestWithItemsAndInventory(zabbixAuthToken, cookie)
|
|
.executeRequestThrowError(
|
|
dataSources.zabbixAPI, new ParsedArgs(args)
|
|
)
|
|
},
|
|
|
|
allHostGroups: async (_parent: any, args: QueryAllHostGroupsArgs, {
|
|
zabbixAuthToken,
|
|
cookie
|
|
}: any) => {
|
|
if (!args.search_name) {
|
|
args.search_name = ZABBIX_EDGE_DEVICE_BASE_GROUP + "/*"
|
|
}
|
|
return await new ZabbixQueryHostgroupsRequest(zabbixAuthToken, cookie).executeRequestThrowError(
|
|
zabbixAPI, new ZabbixQueryHostgroupsParams(args)
|
|
)
|
|
},
|
|
|
|
exportHostValueHistory: (_parent: any, args: QueryExportHostValueHistoryArgs, {
|
|
zabbixAuthToken,
|
|
cookie
|
|
}: any) => {
|
|
return HostValueExporter.exportHistory(args, zabbixAuthToken, cookie)
|
|
},
|
|
|
|
exportUserRights: async (_, args: QueryExportUserRightsArgs, {
|
|
zabbixAuthToken,
|
|
cookie
|
|
}: any) => {
|
|
let groups = await new ZabbixExportUserGroupsRequest(zabbixAuthToken, cookie)
|
|
.executeRequestThrowError(zabbixAPI, new ZabbixExportUserGroupArgs(args.name_pattern, args.exclude_hostgroups_pattern));
|
|
let roles = await new ZabbixQueryUserRolesRequest(zabbixAuthToken, cookie)
|
|
.executeRequestThrowError(zabbixAPI, new ParsedArgs(args.name_pattern ? {name_pattern: args.name_pattern} : undefined));
|
|
return {
|
|
userGroups: groups,
|
|
userRoles: roles
|
|
}
|
|
}
|
|
},
|
|
Mutation: {
|
|
|
|
createHost: async (_parent: any, args: MutationCreateHostArgs, {
|
|
zabbixAuthToken,
|
|
cookie
|
|
}: any) => {
|
|
return await new ZabbixCreateHostRequest(zabbixAuthToken, cookie).executeRequestThrowError(
|
|
zabbixAPI,
|
|
new ParsedArgs(args)
|
|
)
|
|
},
|
|
importHostGroups: async (_parent: any, args: MutationImportHostGroupsArgs, {
|
|
zabbixAuthToken,
|
|
cookie
|
|
}: any) => {
|
|
return HostImporter.importHostGroups(args.hostGroups, zabbixAuthToken, cookie)
|
|
},
|
|
importHosts: async (_parent: any, args: MutationImportHostsArgs, {
|
|
zabbixAuthToken,
|
|
cookie
|
|
}: any) => {
|
|
return HostImporter.importHosts(args.hosts, zabbixAuthToken, cookie)
|
|
},
|
|
importUserRights: async (_, args: MutationImportUserRightsArgs, {
|
|
zabbixAuthToken,
|
|
cookie
|
|
}: any) => {
|
|
let userRoleImportArgs = structuredClone(args);
|
|
let userGroupImportArgs = structuredClone(args);
|
|
let userRolesImport = userRoleImportArgs.input.userRoles ?
|
|
await new ZabbixImportUserRolesRequest(zabbixAuthToken, cookie)
|
|
.executeRequestThrowError(zabbixAPI,
|
|
new ZabbixImportUserRolesParams(userRoleImportArgs.input.userRoles, userRoleImportArgs.dryRun)) : null;
|
|
let userGroupsImport = userGroupImportArgs.input.userGroups ?
|
|
await new ZabbixImportUserGroupsRequest(zabbixAuthToken, cookie)
|
|
.executeRequestThrowError(zabbixAPI,
|
|
new ZabbixImportUserGroupsParams(userGroupImportArgs.input.userGroups, userGroupImportArgs.dryRun)) : null;
|
|
return {
|
|
userRoles: userRolesImport,
|
|
userGroups: userGroupsImport
|
|
}
|
|
}
|
|
},
|
|
|
|
Host: {
|
|
// @ts-ignore
|
|
__resolveType: function (host: Host, _context, info ): string {
|
|
|
|
if (!isDevice(host)) {
|
|
logger.info(`checking host ${host.name} for deviceType - no device type found, returning as ZabbixHost`);
|
|
|
|
return "ZabbixHost";
|
|
}
|
|
const deviceType = host.deviceType!;
|
|
logger.info(`checking host ${host.name} for deviceType - found ${deviceType}`);
|
|
let interfaceType: GraphQLInterfaceType = (info.returnType instanceof GraphQLList ?
|
|
info.returnType.ofType : info.returnType) as GraphQLInterfaceType
|
|
if (info.schema.getImplementations(interfaceType).objects.some((impl: { name: string; }) => impl.name === deviceType)) {
|
|
return deviceType;
|
|
}
|
|
return "GenericDevice"
|
|
}
|
|
},
|
|
|
|
Inventory: {
|
|
/*
|
|
Always map inventory.location,... fields to location object
|
|
*/
|
|
// @ts-ignore
|
|
location: (parent: { location: string; location_lon: string; location_lat: string; }) => {
|
|
return {
|
|
name: parent.location,
|
|
longitude: parent.location_lon,
|
|
latitude: parent.location_lat,
|
|
}
|
|
}
|
|
},
|
|
|
|
UserRoleRules: {
|
|
ui_default_access: (parent: any) => {
|
|
return parent["ui.default_access"]
|
|
},
|
|
modules_default_access: (parent: any) => {
|
|
return parent["modules.default_access"]
|
|
},
|
|
actions_default_access: (parent: any) => {
|
|
return parent["actions.default_access"]
|
|
},
|
|
api_access: (parent: any) => {
|
|
return parent["api.access"]
|
|
},
|
|
api_mode: (parent: any) => {
|
|
return parent["api.mode"]
|
|
},
|
|
},
|
|
|
|
// Enum Value Mappings
|
|
Permission: {
|
|
READ: Permission.Read,
|
|
READ_WRITE: Permission.ReadWrite,
|
|
DENY: Permission.Deny
|
|
},
|
|
|
|
DeviceCommunicationType: {
|
|
ZABBIX_AGENT: DeviceCommunicationType.ZABBIX_AGENT,
|
|
ZABBIX_AGENT_ACTIVE: DeviceCommunicationType.ZABBIX_AGENT_ACTIVE,
|
|
ZABBIX_TRAP: DeviceCommunicationType.ZABBIX_TRAP,
|
|
SIMPLE_CHECK: DeviceCommunicationType.SIMPLE_CHECK,
|
|
ZABBIX_INTERNAL_ITEM: DeviceCommunicationType.ZABBIX_INTERNAL_ITEM,
|
|
DEPENDANT_ITEM: DeviceCommunicationType.DEPENDANT_ITEM,
|
|
HTTP_AGENT: DeviceCommunicationType.HTTP_AGENT,
|
|
SIMULATOR_CALCULATED: DeviceCommunicationType.SIMULATOR_CALCULATED,
|
|
SNMP_AGENT: DeviceCommunicationType.SNMP_AGENT,
|
|
SNMP_TRAP: DeviceCommunicationType.SNMP_TRAP,
|
|
IPMI_AGENT: DeviceCommunicationType.IPMI_AGENT,
|
|
JMX_AGENT: DeviceCommunicationType.JMX_AGENT,
|
|
SIMULATOR_JAVASCRIPT: DeviceCommunicationType.SIMULATOR_JAVASCRIPT,
|
|
DATABASE_MONITOR: DeviceCommunicationType.DATABASE_MONITOR,
|
|
},
|
|
DeviceStatus: {
|
|
ENABLED: DeviceStatus.ENABLED,
|
|
DISABLED: DeviceStatus.DISABLED
|
|
},
|
|
|
|
StorageItemType: {
|
|
TEXT: StorageItemType.Text,
|
|
FLOAT: StorageItemType.Float,
|
|
INT: StorageItemType.Int,
|
|
}
|
|
}
|
|
}
|
|
|