Skip to content

Commit

Permalink
Added policies and policy managed indices
Browse files Browse the repository at this point in the history
  • Loading branch information
Prabhat Sharma committed Mar 27, 2024
1 parent b151b85 commit cfc2985
Show file tree
Hide file tree
Showing 22 changed files with 287 additions and 157 deletions.
30 changes: 27 additions & 3 deletions public/pages/CreatePolicy/containers/CreatePolicy/CreatePolicy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

import React, { ChangeEvent, Component, Fragment } from "react";
import React, { ChangeEvent, Component, Fragment, useContext } from "react";
import { EuiSpacer, EuiTitle, EuiFlexGroup, EuiFlexItem, EuiButton, EuiButtonEmpty, EuiCallOut, EuiLink, EuiIcon } from "@elastic/eui";
import queryString from "query-string";
import { RouteComponentProps } from "react-router-dom";
Expand All @@ -15,8 +15,10 @@ import { PolicyService } from "../../../../services";
import { BREADCRUMBS, DOCUMENTATION_URL, ROUTES } from "../../../../utils/constants";
import { getErrorMessage } from "../../../../utils/helpers";
import { CoreServicesContext } from "../../../../components/core_services";
import { DataSourceMenuContext, DataSourceMenuProperties } from "../../../../services/DataSourceMenuContext";
import { useUpdateUrlWithDataSourceProperties } from "../../../../components/MDSEnabledComponent";

interface CreatePolicyProps extends RouteComponentProps {
interface CreatePolicyProps extends RouteComponentProps, DataSourceMenuProperties {
isEdit: boolean;
policyService: PolicyService;
}
Expand All @@ -32,7 +34,7 @@ interface CreatePolicyState {
hasSubmitted: boolean;
}

export default class CreatePolicy extends Component<CreatePolicyProps, CreatePolicyState> {
export class CreatePolicy extends Component<CreatePolicyProps, CreatePolicyState> {
static contextType = CoreServicesContext;
_isMount: boolean;
constructor(props: CreatePolicyProps) {
Expand Down Expand Up @@ -78,6 +80,22 @@ export default class CreatePolicy extends Component<CreatePolicyProps, CreatePol
this._isMount = false;
}

componentDidUpdate(prevProps: CreatePolicyProps, prevState: Readonly<CreatePolicyState>) {
if (prevProps.dataSourceId != this.props.dataSourceId) {
// reset the state, if dataSourceId changes, i.e., clear state
this.setState({
policySeqNo: null,
policyPrimaryTerm: null,
policyId: "",
policyIdError: "",
submitError: "",
jsonString: DEFAULT_POLICY,
isSubmitting: false,
hasSubmitted: false,
});
}
}

getPolicyToEdit = async (policyId: string): Promise<void> => {
try {
const { policyService } = this.props;
Expand Down Expand Up @@ -250,3 +268,9 @@ export default class CreatePolicy extends Component<CreatePolicyProps, CreatePol
);
}
}

export default function (props: Omit<CreatePolicyProps, keyof DataSourceMenuProperties>) {
const dataSourceMenuProperties = useContext(DataSourceMenuContext);
useUpdateUrlWithDataSourceProperties();
return <CreatePolicy {...props} {...dataSourceMenuProperties} />;
}
14 changes: 14 additions & 0 deletions public/pages/Main/Main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ const dataSourceEnabledPaths: string[] = [
ROUTES.SPLIT_INDEX,
ROUTES.ROLLOVER,
ROUTES.INDEX_DETAIL,
ROUTES.INDEX_POLICIES,
ROUTES.MANAGED_INDICES,
ROUTES.CREATE_POLICY,
ROUTES.EDIT_POLICY,
ROUTES.POLICY_DETAILS,
ROUTES.INDICES,
ROUTES.CREATE_INDEX,
ROUTES.ALIASES,
Expand Down Expand Up @@ -203,6 +208,9 @@ export default class Main extends Component<MainProps, MainState> {
if (this.props.multiDataSourceEnabled && this.isDataSourceEnabledForPath(pathname)) {
services.indexService = new IndexService(http, this.state.dataSourceId, this.props.multiDataSourceEnabled);
services.commonService = new CommonService(http, this.state.dataSourceId, this.props.multiDataSourceEnabled);
services.managedIndexService = new ManagedIndexService(http, this.state.dataSourceId, this.props.multiDataSourceEnabled);
services.policyService = new PolicyService(http, this.state.dataSourceId, this.props.multiDataSourceEnabled);
services.notificationService = new NotificationService(http, this.state.dataSourceId, this.props.multiDataSourceEnabled);
}
return services;
}
Expand Down Expand Up @@ -340,6 +348,8 @@ export default class Main extends Component<MainProps, MainState> {
ROUTES.SPLIT_INDEX,
ROUTES.ROLLOVER,
ROUTES.INDEX_DETAIL,
ROUTES.EDIT_POLICY,
ROUTES.POLICY_DETAILS,
ROUTES.REINDEX,
]}
render={(props) => (
Expand Down Expand Up @@ -374,6 +384,10 @@ export default class Main extends Component<MainProps, MainState> {
ROUTES.CREATE_TEMPLATE,
ROUTES.COMPOSABLE_TEMPLATES,
ROUTES.CREATE_COMPOSABLE_TEMPLATE,
ROUTES.MANAGED_INDICES,
ROUTES.INDEX_POLICIES,
ROUTES.CREATE_POLICY,
ROUTES.TRANSFORMS,
]}
render={() => (
<DataSourceMenu
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

import React, { Component } from "react";
import React, { Component, useContext } from "react";
import { RouteComponentProps } from "react-router-dom";
import {
EuiBasicTable,
Expand Down Expand Up @@ -46,12 +46,14 @@ import RolloverAliasModal from "../../components/RolloverAliasModal";
import { CoreServicesContext } from "../../../../components/core_services";
import { DataStream } from "../../../../../server/models/interfaces";
import { SECURITY_EXCEPTION_PREFIX } from "../../../../../server/utils/constants";
import { DataSourceMenuContext, DataSourceMenuProperties } from "../../../../services/DataSourceMenuContext";
import MDSEnabledComponent from "../../../../components/MDSEnabledComponent";

interface ManagedIndicesProps extends RouteComponentProps {
interface ManagedIndicesProps extends RouteComponentProps, DataSourceMenuProperties {
managedIndexService: ManagedIndexService;
}

interface ManagedIndicesState {
interface ManagedIndicesState extends DataSourceMenuProperties {
totalManagedIndices: number;
from: number;
size: number;
Expand All @@ -66,7 +68,7 @@ interface ManagedIndicesState {
isDataStreamColumnVisible: boolean;
}

export default class ManagedIndices extends Component<ManagedIndicesProps, ManagedIndicesState> {
export class ManagedIndices extends MDSEnabledComponent<ManagedIndicesProps, ManagedIndicesState> {
static contextType = CoreServicesContext;
columns: EuiTableFieldDataColumnType<ManagedIndexItem>[];

Expand All @@ -76,6 +78,7 @@ export default class ManagedIndices extends Component<ManagedIndicesProps, Manag
const { from, size, search, sortField, sortDirection, showDataStreams } = getURLQueryParams(this.props.location);

this.state = {
...this.state,
totalManagedIndices: 0,
from,
size,
Expand Down Expand Up @@ -193,8 +196,25 @@ export default class ManagedIndices extends Component<ManagedIndicesProps, Manag
}
}

static getQueryObjectFromState({ from, size, search, sortField, sortDirection, showDataStreams }: ManagedIndicesState) {
return { from, size, search, sortField, sortDirection, showDataStreams };
static getQueryObjectFromState({
from,
size,
search,
sortField,
sortDirection,
showDataStreams,
dataSourceId,
multiDataSourceEnabled,
}: ManagedIndicesState) {
return {
from,
size,
search,
sortField,
sortDirection,
showDataStreams,
...(multiDataSourceEnabled ? { dataSourceId } : {}),
};
}

renderPolicyId = (policyId: string, item: ManagedIndexItem) => {
Expand Down Expand Up @@ -227,7 +247,7 @@ export default class ManagedIndices extends Component<ManagedIndicesProps, Manag
try {
const { managedIndexService, history } = this.props;
const queryObject = ManagedIndices.getQueryObjectFromState(this.state);
const queryParamsString = queryString.stringify(queryObject);
const queryParamsString = queryString.stringify({ ...queryObject, dataSourceLabel: this.state.dataSourceLabel });
history.replace({ ...this.props.location, search: queryParamsString });

const getManagedIndicesResponse = await managedIndexService.getManagedIndices({
Expand Down Expand Up @@ -256,7 +276,7 @@ export default class ManagedIndices extends Component<ManagedIndicesProps, Manag

getDataStreams = async (): Promise<DataStream[]> => {
const { managedIndexService } = this.props;
const serverResponse = await managedIndexService.getDataStreams();
const serverResponse = await managedIndexService.getDataStreams(undefined);
if (!serverResponse.ok) {
if (serverResponse.error.startsWith(SECURITY_EXCEPTION_PREFIX)) {
this.context.notifications.toasts.addWarning(serverResponse.error);
Expand Down Expand Up @@ -284,11 +304,12 @@ export default class ManagedIndices extends Component<ManagedIndicesProps, Manag
try {
if (!indices.length) return;
const { managedIndexService } = this.props;
const removePolicyResponse = await managedIndexService.removePolicy(indices);
const removePolicyResponse = await managedIndexService.removePolicy(indices, undefined);
if (removePolicyResponse.ok) {
const { updatedIndices, failedIndices, failures } = removePolicyResponse.response;
if (updatedIndices) {
this.context.notifications.toasts.addSuccess(`Removed policy from ${updatedIndices} managed indexes`);
await this.getManagedIndices();
}
if (failures) {
this.context.notifications.toasts.addDanger(
Expand Down Expand Up @@ -479,3 +500,8 @@ export default class ManagedIndices extends Component<ManagedIndicesProps, Manag
);
}
}

export default function (props: Omit<ManagedIndicesProps, keyof DataSourceMenuProperties>) {
const dataSourceMenuProps = useContext(DataSourceMenuContext);
return <ManagedIndices {...props} {...dataSourceMenuProps} />;
}
31 changes: 24 additions & 7 deletions public/pages/Policies/containers/Policies/Policies.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

import React, { Component } from "react";
import React, { Component, useContext } from "react";
import { RouteComponentProps } from "react-router-dom";
import queryString from "query-string";
import {
Expand Down Expand Up @@ -34,12 +34,15 @@ import { PolicyService } from "../../../../services";
import { getErrorMessage } from "../../../../utils/helpers";
import ConfirmationModal from "../../../../components/ConfirmationModal";
import { CoreServicesContext } from "../../../../components/core_services";
import { DataSourceMenuContext, DataSourceMenuProperties } from "../../../../services/DataSourceMenuContext";
import MDSEnabledComponent from "../../../../components/MDSEnabledComponent";
import { DataSource } from "src/plugins/data/public";

interface PoliciesProps extends RouteComponentProps {
interface PoliciesProps extends RouteComponentProps, DataSourceMenuProperties {
policyService: PolicyService;
}

interface PoliciesState {
interface PoliciesState extends DataSourceMenuProperties {
totalPolicies: number;
from: number;
size: number;
Expand All @@ -51,7 +54,7 @@ interface PoliciesState {
loadingPolicies: boolean;
}

export default class Policies extends Component<PoliciesProps, PoliciesState> {
export class Policies extends MDSEnabledComponent<PoliciesProps, PoliciesState> {
static contextType = CoreServicesContext;
columns: EuiTableFieldDataColumnType<PolicyItem>[];

Expand All @@ -61,6 +64,7 @@ export default class Policies extends Component<PoliciesProps, PoliciesState> {
const { from, size, search, sortField, sortDirection } = getURLQueryParams(this.props.location);

this.state = {
...this.state,
totalPolicies: 0,
from,
size,
Expand Down Expand Up @@ -121,16 +125,24 @@ export default class Policies extends Component<PoliciesProps, PoliciesState> {
}
}

static getQueryObjectFromState({ from, size, search, sortField, sortDirection }: PoliciesState): PoliciesQueryParams {
return { from, size, search, sortField, sortDirection };
static getQueryObjectFromState({
from,
size,
search,
sortField,
sortDirection,
dataSourceId,
multiDataSourceEnabled,
}: PoliciesState): PoliciesQueryParams {
return { from, size, search, sortField, sortDirection, ...(multiDataSourceEnabled ? { dataSourceId } : {}) };
}

getPolicies = async (): Promise<void> => {
this.setState({ loadingPolicies: true });
try {
const { policyService, history } = this.props;
const queryObject = Policies.getQueryObjectFromState(this.state);
const queryParamsString = queryString.stringify(queryObject);
const queryParamsString = queryString.stringify({ ...queryObject, dataSourceLabel: this.state.dataSourceLabel });
history.replace({ ...this.props.location, search: queryParamsString });
const getPoliciesResponse = await policyService.getPolicies(queryObject);
if (getPoliciesResponse.ok) {
Expand Down Expand Up @@ -312,3 +324,8 @@ export default class Policies extends Component<PoliciesProps, PoliciesState> {
);
}
}

export default function (props: Omit<PoliciesProps, keyof DataSourceMenuProperties>) {
const dataSourceMenuProps = useContext(DataSourceMenuContext);
return <Policies {...props} {...dataSourceMenuProps} />;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

import React, { Component } from "react";
import React, { Component, useContext } from "react";
import {
EuiSpacer,
EuiTitle,
Expand Down Expand Up @@ -33,8 +33,10 @@ import { ContentPanel } from "../../../../components/ContentPanel";
import { convertTemplatesToArray } from "../../../VisualCreatePolicy/utils/helpers";
import CreatePolicyModal from "../../../../components/CreatePolicyModal";
import { ModalConsumer } from "../../../../components/Modal";
import { DataSourceMenuContext, DataSourceMenuProperties } from "../../../../services/DataSourceMenuContext";
import { useUpdateUrlWithDataSourceProperties } from "../../../../components/MDSEnabledComponent";

interface PolicyDetailsProps extends RouteComponentProps {
interface PolicyDetailsProps extends RouteComponentProps, DataSourceMenuProperties {
policyService: PolicyService;
}

Expand All @@ -48,7 +50,7 @@ interface PolicyDetailsState {
showPerPageOptions: boolean;
}

export default class PolicyDetails extends Component<PolicyDetailsProps, PolicyDetailsState> {
export class PolicyDetails extends Component<PolicyDetailsProps, PolicyDetailsState> {
static contextType = CoreServicesContext;
constructor(props: PolicyDetailsProps) {
super(props);
Expand Down Expand Up @@ -76,6 +78,19 @@ export default class PolicyDetails extends Component<PolicyDetailsProps, PolicyD
}
};

componentDidUpdate(prevProps: Readonly<PolicyDetailsProps>, prevState: Readonly<PolicyDetailsState>, snapshot?: any): void {
if (prevProps.dataSourceId !== this.props.dataSourceId) {
this.setState({
policyId: "",
policy: null,
isJSONModalOpen: false,
isDeleteModalVisible: false,
pageIndex: 0,
pageSize: 10,
showPerPageOptions: true,
});
}
}
getPolicy = async (policyId: string): Promise<void> => {
try {
const { policyService } = this.props;
Expand Down Expand Up @@ -244,3 +259,9 @@ export default class PolicyDetails extends Component<PolicyDetailsProps, PolicyD
);
}
}

export default function (props: Omit<PolicyDetailsProps, keyof DataSourceMenuProperties>) {
const dataSourceMenuProperties = useContext(DataSourceMenuContext);
useUpdateUrlWithDataSourceProperties();
return <PolicyDetails {...props} {...dataSourceMenuProperties} />;
}
Loading

0 comments on commit cfc2985

Please sign in to comment.