[8.10] [Synthetics] Parse response from agent policies API in route (#165206) (#165233)

# Backport

This will backport the following commits from `main` to `8.10`:
- [[Synthetics] Parse response from agent policies API in route
(#165206)](https://github.com/elastic/kibana/pull/165206)

<!--- Backport version: 8.9.7 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT
[{"author":{"name":"Shahzad","email":"shahzad31comp@gmail.com"},"sourceCommit":{"committedDate":"2023-08-30T12:48:40Z","message":"[Synthetics]
Parse response from agent policies API in route
(#165206)","sha":"4509cbeaa3b0540cd937549f20d72b5f574dd28a","branchLabelMapping":{"^v8.11.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["Team:uptime","release_note:skip","v8.10.0","v8.11.0"],"number":165206,"url":"https://github.com/elastic/kibana/pull/165206","mergeCommit":{"message":"[Synthetics]
Parse response from agent policies API in route
(#165206)","sha":"4509cbeaa3b0540cd937549f20d72b5f574dd28a"}},"sourceBranch":"main","suggestedTargetBranches":["8.10"],"targetPullRequestStates":[{"branch":"8.10","label":"v8.10.0","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v8.11.0","labelRegex":"^v8.11.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/165206","number":165206,"mergeCommit":{"message":"[Synthetics]
Parse response from agent policies API in route
(#165206)","sha":"4509cbeaa3b0540cd937549f20d72b5f574dd28a"}}]}]
BACKPORT-->

Co-authored-by: Shahzad <shahzad31comp@gmail.com>
This commit is contained in:
Kibana Machine 2023-08-30 10:04:57 -04:00 committed by GitHub
parent 663dc2978d
commit f688faba9f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 41 additions and 56 deletions

View file

@ -20,3 +20,11 @@ export interface TestNowResponse {
configId: string;
monitor: SyntheticsMonitor;
}
export interface AgentPolicyInfo {
id: string;
name: string;
agents: number;
status: string;
description?: string;
}

View file

@ -82,9 +82,7 @@ describe('GettingStartedPage', () => {
loading: false,
},
agentPolicies: {
data: {
total: 0,
},
data: [],
isAddingNewPrivateLocation: true,
},
},
@ -109,10 +107,7 @@ describe('GettingStartedPage', () => {
loading: false,
},
agentPolicies: {
data: {
total: 1,
items: [{}],
},
data: [{}],
isAddingNewPrivateLocation: true,
},
},
@ -141,10 +136,7 @@ describe('GettingStartedPage', () => {
loading: false,
},
agentPolicies: {
data: {
total: 1,
items: [{}],
},
data: [{}],
isAddingNewPrivateLocation: true,
},
},

View file

@ -29,7 +29,7 @@ export const LocationForm = ({ privateLocations }: { privateLocations: PrivateLo
const { control, register, watch } = useFormContext<PrivateLocation>();
const { errors } = useFormState();
const selectedPolicyId = watch('agentPolicyId');
const selectedPolicy = data?.items.find((item) => item.id === selectedPolicyId);
const selectedPolicy = data?.find((item) => item.id === selectedPolicyId);
const tagsList = privateLocations.reduce((acc, item) => {
const tags = item.tags || [];
@ -38,7 +38,7 @@ export const LocationForm = ({ privateLocations }: { privateLocations: PrivateLo
return (
<>
{data?.items.length === 0 && <AgentPolicyNeeded />}
{data?.length === 0 && <AgentPolicyNeeded />}
<EuiForm component="form" noValidate>
<EuiFormRow
fullWidth

View file

@ -26,7 +26,7 @@ export const ManageEmptyState: FC<{
}) => {
const { data: agentPolicies } = useSelector(selectAgentPolicies);
if (agentPolicies?.total === 0 && showNeedAgentPolicy) {
if (agentPolicies?.length === 0 && showNeedAgentPolicy) {
return <AgentPolicyNeeded />;
}

View file

@ -46,12 +46,7 @@ describe('<ManagePrivateLocations />', () => {
const { getByText, getByRole, findByText } = render(<ManagePrivateLocations />, {
state: {
agentPolicies: {
data: {
items: [],
total: 0,
page: 1,
perPage: 20,
},
data: [],
loading: false,
error: null,
isManageFlyoutOpen: false,
@ -85,12 +80,7 @@ describe('<ManagePrivateLocations />', () => {
const { getByText, getByRole, findByText } = render(<ManagePrivateLocations />, {
state: {
agentPolicies: {
data: {
items: [{}],
total: 1,
page: 1,
perPage: 20,
},
data: [{}],
loading: false,
error: null,
isManageFlyoutOpen: false,
@ -140,12 +130,7 @@ describe('<ManagePrivateLocations />', () => {
const { getByText, getByRole, findByText } = render(<ManagePrivateLocations />, {
state: {
agentPolicies: {
data: {
items: [{}],
total: 1,
page: 1,
perPage: 20,
},
data: [{}],
loading: false,
error: null,
isManageFlyoutOpen: false,

View file

@ -34,7 +34,7 @@ export const PolicyHostsField = ({
}) => {
const { data } = useSelector(selectAgentPolicies);
const policyHostsOptions = data?.items.map((item) => {
const policyHostsOptions = data?.map((item) => {
const hasLocation = privateLocations.find((location) => location.agentPolicyId === item.id);
return {
disabled: Boolean(hasLocation),

View file

@ -20,7 +20,7 @@ export const PolicyName = ({ agentPolicyId }: { agentPolicyId: string }) => {
const { data: policies, loading } = useSelector(selectAgentPolicies);
const policy = policies?.items.find((policyT) => policyT.id === agentPolicyId);
const policy = policies?.find((policyT) => policyT.id === agentPolicyId);
if (loading) {
return <EuiLoadingSpinner size="s" />;

View file

@ -6,10 +6,10 @@
*/
import { createAction } from '@reduxjs/toolkit';
import { AgentPolicyInfo } from '../../../../../common/types';
import { createAsyncAction } from '../utils/actions';
import { AgentPoliciesList } from '.';
export const getAgentPoliciesAction = createAsyncAction<void, AgentPoliciesList>(
export const getAgentPoliciesAction = createAsyncAction<void, AgentPolicyInfo[]>(
'[AGENT POLICIES] GET'
);

View file

@ -5,12 +5,12 @@
* 2.0.
*/
import { AgentPolicyInfo } from '../../../../../common/types';
import { SYNTHETICS_API_URLS } from '../../../../../common/constants';
import { PrivateLocation, SyntheticsPrivateLocations } from '../../../../../common/runtime_types';
import { apiService } from '../../../../utils/api_service/api_service';
import { AgentPoliciesList } from '.';
export const fetchAgentPolicies = async (): Promise<AgentPoliciesList> => {
export const fetchAgentPolicies = async (): Promise<AgentPolicyInfo[]> => {
return await apiService.get(SYNTHETICS_API_URLS.AGENT_POLICIES);
};

View file

@ -6,19 +6,12 @@
*/
import { createReducer } from '@reduxjs/toolkit';
import { AgentPolicy } from '@kbn/fleet-plugin/common';
import { AgentPolicyInfo } from '../../../../../common/types';
import { IHttpSerializedFetchError } from '..';
import { getAgentPoliciesAction, setAddingNewPrivateLocation } from './actions';
export interface AgentPoliciesList {
items: AgentPolicy[];
total: number;
page: number;
perPage: number;
}
export interface AgentPoliciesState {
data: AgentPoliciesList | null;
data: AgentPolicyInfo[] | null;
loading: boolean;
error: IHttpSerializedFetchError | null;
isManageFlyoutOpen?: boolean;

View file

@ -5,6 +5,7 @@
* 2.0.
*/
import { AgentPolicyInfo } from '../../../../common/types';
import { SyntheticsServerSetup } from '../../../types';
import { SyntheticsRestApiRouteFactory } from '../../types';
import { SYNTHETICS_API_URLS } from '../../../../common/constants';
@ -13,7 +14,7 @@ export const getAgentPoliciesRoute: SyntheticsRestApiRouteFactory = () => ({
method: 'GET',
path: SYNTHETICS_API_URLS.AGENT_POLICIES,
validate: {},
handler: async ({ server, context, uptimeEsClient }): Promise<any> => {
handler: async ({ server }): Promise<AgentPolicyInfo[]> => {
return getAgentPoliciesAsInternalUser(server);
},
});
@ -22,7 +23,7 @@ export const getAgentPoliciesAsInternalUser = async (server: SyntheticsServerSet
const soClient = server.coreStart.savedObjects.createInternalRepository();
const esClient = server.coreStart.elasticsearch.client.asInternalUser;
return server.fleet?.agentPolicyService.list(soClient, {
const agentPolicies = await server.fleet?.agentPolicyService.list(soClient, {
page: 1,
perPage: 10000,
sortField: 'name',
@ -31,4 +32,12 @@ export const getAgentPoliciesAsInternalUser = async (server: SyntheticsServerSet
esClient,
withAgentCount: true,
});
return agentPolicies.items.map((agentPolicy) => ({
id: agentPolicy.id,
name: agentPolicy.name,
agents: agentPolicy.agents ?? 0,
status: agentPolicy.status,
description: agentPolicy.description,
}));
};

View file

@ -4,9 +4,9 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import type { AgentPolicy } from '@kbn/fleet-plugin/common';
import { SavedObjectsErrorHelpers } from '@kbn/core/server';
import { SavedObjectsClientContract } from '@kbn/core-saved-objects-api-server';
import { AgentPolicyInfo } from '../../../../common/types';
import { SyntheticsRestApiRouteFactory } from '../../types';
import { SyntheticsPrivateLocations } from '../../../../common/runtime_types';
import { SYNTHETICS_API_URLS } from '../../../../common/constants';
@ -33,7 +33,7 @@ export const getPrivateLocationsRoute: SyntheticsRestApiRouteFactory<
export const getPrivateLocationsAndAgentPolicies = async (
savedObjectsClient: SavedObjectsClientContract,
syntheticsMonitorClient: SyntheticsMonitorClient
): Promise<SyntheticsPrivateLocationsAttributes & { agentPolicies: AgentPolicy[] }> => {
): Promise<SyntheticsPrivateLocationsAttributes & { agentPolicies: AgentPolicyInfo[] }> => {
try {
const [privateLocations, agentPolicies] = await Promise.all([
getPrivateLocations(savedObjectsClient),

View file

@ -4,7 +4,7 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import type { AgentPolicy } from '@kbn/fleet-plugin/common';
import { AgentPolicyInfo } from '../../../../common/types';
import type { SyntheticsPrivateLocations } from '../../../../common/runtime_types';
import type {
SyntheticsPrivateLocationsAttributes,
@ -14,7 +14,7 @@ import { PrivateLocation } from '../../../../common/runtime_types';
export const toClientContract = (
attributes: SyntheticsPrivateLocationsAttributes,
agentPolicies?: AgentPolicy[]
agentPolicies?: AgentPolicyInfo[]
): SyntheticsPrivateLocations => {
return {
locations: attributes.locations.map((location) => ({

View file

@ -435,9 +435,7 @@ export class SyntheticsPrivateLocation {
}
async getAgentPolicies() {
const agentPolicies = await getAgentPoliciesAsInternalUser(this.server);
return agentPolicies.items;
return await getAgentPoliciesAsInternalUser(this.server);
}
}