fleet/frontend/services/entities/hosts.ts

342 lines
8.9 KiB
TypeScript
Raw Normal View History

/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import sendRequest from "services";
import endpoints from "utilities/endpoints";
import { IHost, HostStatus } from "interfaces/host";
import {
buildQueryStringFromParams,
getLabelParam,
reconcileMutuallyExclusiveHostParams,
reconcileMutuallyInclusiveHostParams,
} from "utilities/url";
import { ISelectedPlatform } from "interfaces/platform";
import { DiskEncryptionStatus } from "utilities/constants";
import { ISoftware } from "interfaces/software";
import { IMdmSolution } from "interfaces/mdm";
import { IMunkiIssuesAggregate } from "interfaces/macadmins";
export interface ISortOption {
key: string;
direction: string;
}
export interface ILoadHostsResponse {
hosts: IHost[];
software: ISoftware;
munki_issue: IMunkiIssuesAggregate;
mobile_device_management_solution: IMdmSolution;
}
export interface ILoadHostsQueryKey extends ILoadHostsOptions {
scope: "hosts";
}
export type MacSettingsStatusQueryParam = "latest" | "pending" | "failing";
export interface ILoadHostsOptions {
page?: number;
perPage?: number;
selectedLabels?: string[];
globalFilter?: string;
sortBy?: ISortOption[];
teamId?: number;
policyId?: number;
policyResponse?: string;
macSettingsStatus?: MacSettingsStatusQueryParam;
softwareId?: number;
status?: HostStatus;
mdmId?: number;
mdmEnrollmentStatus?: string;
lowDiskSpaceHosts?: number;
osId?: number;
osName?: string;
osVersion?: string;
munkiIssueId?: number;
device_mapping?: boolean;
columns?: string;
visibleColumns?: string;
diskEncryptionStatus?: DiskEncryptionStatus;
}
2022-05-25 23:48:48 +00:00
export interface IExportHostsOptions {
sortBy: ISortOption[];
page?: number;
perPage?: number;
selectedLabels?: string[];
globalFilter?: string;
teamId?: number;
policyId?: number;
policyResponse?: string;
macSettingsStatus?: MacSettingsStatusQueryParam;
2022-05-25 23:48:48 +00:00
softwareId?: number;
status?: HostStatus;
mdmId?: number;
munkiIssueId?: number;
mdmEnrollmentStatus?: string;
lowDiskSpaceHosts?: number;
osId?: number;
osName?: string;
osVersion?: string;
2022-05-25 23:48:48 +00:00
device_mapping?: boolean;
columns?: string;
visibleColumns?: string;
diskEncryptionStatus?: DiskEncryptionStatus;
2022-05-25 23:48:48 +00:00
}
export interface IActionByFilter {
teamId: number | null;
query: string;
status: string;
labelId?: number;
}
export type ILoadHostDetailsExtension = "device_mapping" | "macadmins";
const LABEL_PREFIX = "labels/";
const getLabel = (selectedLabels?: string[]) => {
if (selectedLabels === undefined) return undefined;
return selectedLabels.find((filter) => filter.includes(LABEL_PREFIX));
};
const getHostEndpoint = (selectedLabels?: string[]) => {
const { HOSTS, LABEL_HOSTS } = endpoints;
if (selectedLabels === undefined) return HOSTS;
const label = getLabel(selectedLabels);
if (label) {
const labelId = label.substr(LABEL_PREFIX.length);
return LABEL_HOSTS(parseInt(labelId, 10));
}
return HOSTS;
};
const getSortParams = (sortOptions?: ISortOption[]) => {
if (sortOptions === undefined || sortOptions.length === 0) {
return {};
}
const sortItem = sortOptions[0];
return {
order_key: sortItem.key,
order_direction: sortItem.direction,
};
};
const createMdmParams = (platform?: ISelectedPlatform, teamId?: number) => {
if (platform === "all") {
return buildQueryStringFromParams({ team_id: teamId });
}
return buildQueryStringFromParams({ platform, team_id: teamId });
};
export default {
destroy: (host: IHost) => {
const { HOSTS } = endpoints;
const path = `${HOSTS}/${host.id}`;
return sendRequest("DELETE", path);
},
destroyBulk: (hostIds: number[]) => {
const { HOSTS_DELETE } = endpoints;
return sendRequest("POST", HOSTS_DELETE, { ids: hostIds });
},
destroyByFilter: ({ teamId, query, status, labelId }: IActionByFilter) => {
const { HOSTS_DELETE } = endpoints;
return sendRequest("POST", HOSTS_DELETE, {
filters: {
query,
status,
label_id: labelId,
team_id: teamId,
},
});
},
2022-05-25 23:48:48 +00:00
exportHosts: (options: IExportHostsOptions) => {
const sortBy = options.sortBy;
const selectedLabels = options?.selectedLabels || [];
const globalFilter = options?.globalFilter || "";
const teamId = options?.teamId;
const policyId = options?.policyId;
2022-05-25 23:48:48 +00:00
const policyResponse = options?.policyResponse || "passing";
const softwareId = options?.softwareId;
const macSettingsStatus = options?.macSettingsStatus;
const status = options?.status;
const mdmId = options?.mdmId;
const mdmEnrollmentStatus = options?.mdmEnrollmentStatus;
const lowDiskSpaceHosts = options?.lowDiskSpaceHosts;
const visibleColumns = options?.visibleColumns;
const label = getLabelParam(selectedLabels);
const munkiIssueId = options?.munkiIssueId;
const diskEncryptionStatus = options?.diskEncryptionStatus;
2022-05-25 23:48:48 +00:00
if (!sortBy.length) {
throw Error("sortBy is a required field.");
}
const queryParams = {
order_key: sortBy[0].key,
order_direction: sortBy[0].direction,
query: globalFilter,
...reconcileMutuallyInclusiveHostParams({ teamId, macSettingsStatus }),
...reconcileMutuallyExclusiveHostParams({
label,
policyId,
policyResponse,
mdmId,
mdmEnrollmentStatus,
munkiIssueId,
softwareId,
lowDiskSpaceHosts,
diskEncryptionStatus,
}),
status,
label_id: label,
columns: visibleColumns,
format: "csv",
};
const queryString = buildQueryStringFromParams(queryParams);
const endpoint = endpoints.HOSTS_REPORT;
const path = `${endpoint}?${queryString}`;
return sendRequest("GET", path);
},
loadHosts: ({
page = 0,
perPage = 100,
globalFilter,
teamId,
policyId,
policyResponse = "passing",
macSettingsStatus,
softwareId,
status,
mdmId,
mdmEnrollmentStatus,
munkiIssueId,
lowDiskSpaceHosts,
osId,
osName,
osVersion,
device_mapping,
selectedLabels,
sortBy,
diskEncryptionStatus,
}: ILoadHostsOptions): Promise<ILoadHostsResponse> => {
const label = getLabel(selectedLabels);
const sortParams = getSortParams(sortBy);
const queryParams = {
page,
per_page: perPage,
query: globalFilter,
device_mapping,
order_key: sortParams.order_key,
order_direction: sortParams.order_direction,
status,
...reconcileMutuallyInclusiveHostParams({
teamId,
macSettingsStatus,
}),
...reconcileMutuallyExclusiveHostParams({
label,
policyId,
policyResponse,
mdmId,
mdmEnrollmentStatus,
munkiIssueId,
softwareId,
lowDiskSpaceHosts,
osId,
osName,
osVersion,
diskEncryptionStatus,
}),
};
const queryString = buildQueryStringFromParams(queryParams);
const endpoint = getHostEndpoint(selectedLabels);
const path = `${endpoint}?${queryString}`;
return sendRequest("GET", path);
},
loadHostDetails: (hostID: number) => {
const { HOSTS } = endpoints;
const path = `${HOSTS}/${hostID}`;
return sendRequest("GET", path);
},
loadHostDetailsExtension: (
hostID: number,
extension: ILoadHostDetailsExtension
) => {
const { HOSTS } = endpoints;
const path = `${HOSTS}/${hostID}/${extension}`;
return sendRequest("GET", path);
},
refetch: (host: IHost) => {
const { HOSTS } = endpoints;
const path = `${HOSTS}/${host.id}/refetch`;
return sendRequest("POST", path);
},
1497 improved query experience (#1998) * Step 1 for improving query experience (#1591) * fake change to create draft PR * temp routes to work and not modify old query page * created new API abstraction for query * refactored App.jsx to prepare react-query * fixed flow of redirects after page refresh; functional component added * setup for getting data on edit * implementing functions for query page * Old form showing on new setup * improving and breaking up query form * no need for the helpers anymore; clean up * added type for button component variant * step toward new save modal; have to switch gears to #1619 * creating new query works * clean up * linting cleanup * added default value for new query * will address dynamic save disabled in edit step * Step 2 for improving query experience (select targets) (#1732) * fake change to create draft PR * temp routes to work and not modify old query page * created new API abstraction for query * refactored App.jsx to prepare react-query * fixed flow of redirects after page refresh; functional component added * setup for getting data on edit * implementing functions for query page * Old form showing on new setup * improving and breaking up query form * no need for the helpers anymore; clean up * added type for button component variant * step toward new save modal; have to switch gears to #1619 * creating new query works * clean up * linting cleanup * added default value for new query * split steps into separate files for readability * components laid out * new targets picker * function clean up * styling tables * fixing logic * fixed logic to keep getting related hosts * formatting targets for API * fixed default query * clean up * styled target selectors; fixed target input styles * began total count * forgot to remove debugging code * lint fixes * added target count from API * clean up * able to remove selected host targets from table * lint fixes * Improving query experience - Step 3 (query results) (#1766) * fake change to create draft PR * temp routes to work and not modify old query page * created new API abstraction for query * refactored App.jsx to prepare react-query * fixed flow of redirects after page refresh; functional component added * setup for getting data on edit * implementing functions for query page * Old form showing on new setup * improving and breaking up query form * no need for the helpers anymore; clean up * added type for button component variant * step toward new save modal; have to switch gears to #1619 * creating new query works * clean up * linting cleanup * added default value for new query * split steps into separate files for readability * components laid out * new targets picker * function clean up * styling tables * fixing logic * fixed logic to keep getting related hosts * formatting targets for API * fixed default query * clean up * styled target selectors; fixed target input styles * began total count * forgot to remove debugging code * lint fixes * added target count from API * clean up * able to remove selected host targets from table * lint fixes * connected run query with modern React/JS; clean up * linting fixes * fixed logic to retrieve results from live query * linting fixes * created new, simpler query progress * populating results and errors tables as expected * syntax fixes * fixing styles for query results * more styling for query results * manual merge from main * Rename core->free and basic->premium * Fix lint js * Comment out portion of test that seems to timeout * Rename tier to premium if basic is still loaded * go sum * Query Experience Cleanup Tasks (#1807) * fixes to get merged main branch to build and work * moved screens for query pages; clean up * updated and typed react ace for query form; clean up * using console error instead * added real types instead of `any` except for errors * query side panel ts and functional. prep for close task. * ability to hide, show query table sidebar * improved live query status warning * added loading and error state for targets search * error screen for targets; improved loading display * now using API-created label for all linux * missed some files on previous commit * able to edit query * clean up * lint fixes * query results showing as they come * remove unused code * removed old query page. major file cleanup. * removed selectedTargets redux implementation * removed unused redux actions and reducers * removed unused keys in initial state * selectedOsqueryTable is now using context API * removed all querypages redux code * set up context for app and user * fixed auth with temp fix for wrapper * completed redux removal from query page * fixed var names coming from main branch * fixed var name changes coming from issue 1501 * fixed save popup bug; clean up * added permissions * fixed login redirect * removed unused props * linting fix * clean up * removed unused component, refactor, and clean up * fixed styles for step 1 as admin * fixed styles for step 1 as observer * fixed percentage of online hosts * added loading progress to query stop button * reset query status on run again * added download icon to export button text * fixed error reset on name input; fixed styles * fixed bug where query value wasn't saving * fixed query value when blank * fixed bug - default query was running every time * auto adding host from url to targets * fixed flows for repeating run and save steps * fleet ace is now TS and functional * fixed a couple of tests * fixed issues with query value text inconsistencies * fixed query side panel not showing * hiding error count if not > 0 * fixed showing editor for different roles * using integer for targets * go sum * fixed targets param * catching all errors while running query * fixed hover state for title and description * ignore unit test for now; lint fixes * locking react-ace version * ignoring tests breaking in github actions * brought tests back * fixing file name * fixing file name again * fixed e2e test * have to ignore tests for now * ignore certain premium tests for now * one last test to revamp * another test * fixed teamflow test * fixed observer query 403 * lint fixes * fixed maintainer test * added changes file Co-authored-by: Tomas Touceda <chiiph@gmail.com>
2021-09-10 19:06:37 +00:00
search: (searchText: string) => {
const { HOSTS } = endpoints;
const path = `${HOSTS}?query=${searchText}`;
return sendRequest("GET", path);
},
transferToTeam: (teamId: number | null, hostIds: number[]) => {
const { HOSTS_TRANSFER } = endpoints;
return sendRequest("POST", HOSTS_TRANSFER, {
team_id: teamId,
hosts: hostIds,
});
},
2021-08-30 23:02:53 +00:00
// TODO confirm interplay with policies
transferToTeamByFilter: ({
teamId,
query,
status,
labelId,
}: IActionByFilter) => {
const { HOSTS_TRANSFER_BY_FILTER } = endpoints;
return sendRequest("POST", HOSTS_TRANSFER_BY_FILTER, {
team_id: teamId,
filters: {
query,
status,
label_id: labelId,
},
});
},
getMdm: (id: number) => {
const { HOST_MDM } = endpoints;
return sendRequest("GET", HOST_MDM(id));
},
getMdmSummary: (platform?: ISelectedPlatform, teamId?: number) => {
const { MDM_SUMMARY } = endpoints;
if (!platform || platform === "linux") {
throw new Error("mdm not supported for this platform");
}
const params = createMdmParams(platform, teamId);
const fullPath = params !== "" ? `${MDM_SUMMARY}?${params}` : MDM_SUMMARY;
return sendRequest("GET", fullPath);
},
getEncryptionKey: (id: number) => {
const { HOST_ENCRYPTION_KEY } = endpoints;
return sendRequest("GET", HOST_ENCRYPTION_KEY(id));
},
};