Skip to content

Commit 5ed6c25

Browse files
authored
Fixes default state not updating when changing state name (opensearch-project#145)
Signed-off-by: Drew Baugher <dbbaughe@amazon.com>
1 parent d1b46a8 commit 5ed6c25

File tree

8 files changed

+60
-35
lines changed

8 files changed

+60
-35
lines changed

.github/workflows/cypress-workflow.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ on:
99
- development-*
1010
env:
1111
OPENSEARCH_DASHBOARDS_VERSION: '1.x'
12-
OPENSEARCH_VERSION: '1.2.0-SNAPSHOT'
12+
OPENSEARCH_VERSION: '1.3.0-SNAPSHOT'
1313
jobs:
1414
tests:
1515
name: Run Cypress E2E tests

cypress/fixtures/sample_rollover_policy.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
"name": "hot",
88
"actions": [
99
{
10-
"rollover": {}
10+
"rollover": {},
11+
"retry": {
12+
"count": 0
13+
}
1114
}
1215
],
1316
"transitions": [

opensearch_dashboards.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "indexManagementDashboards",
3-
"version": "1.2.0.0",
4-
"opensearchDashboardsVersion": "1.2.0",
3+
"version": "1.3.0.0",
4+
"opensearchDashboardsVersion": "1.3.0",
55
"configPath": ["opensearch_index_management"],
66
"requiredPlugins": ["navigation"],
77
"server": true,

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "opensearch_index_management_dashboards",
3-
"version": "1.2.0.0",
3+
"version": "1.3.0.0",
44
"description": "Opensearch Dashboards plugin for Index Management",
55
"main": "index.js",
66
"license": "Apache-2.0",

public/pages/VisualCreatePolicy/containers/CreateState/CreateState.tsx

+2-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import { actionRepoSingleton, getOrderInfo } from "../../utils/helpers";
3434

3535
interface CreateStateProps {
3636
policy: Policy;
37-
onSaveState: (state: State, editingState: State | null, states: State[], order: string, afterBeforeState: string) => void;
37+
onSaveState: (state: State, states: State[], order: string, afterBeforeState: string) => void;
3838
onCloseFlyout: () => void;
3939
state: State | null;
4040
}
@@ -176,14 +176,13 @@ export default class CreateState extends Component<CreateStateProps, CreateState
176176

177177
onClickSaveState = () => {
178178
const { order, afterBeforeState } = this.state;
179-
const { onSaveState, state, policy } = this.props;
179+
const { onSaveState, policy } = this.props;
180180
onSaveState(
181181
{
182182
name: this.state.name,
183183
actions: this.state.actions.map((action) => action.toAction()),
184184
transitions: this.state.transitions.map((transition) => transition.transition),
185185
},
186-
state,
187186
policy.states,
188187
order,
189188
afterBeforeState

public/pages/VisualCreatePolicy/containers/VisualCreatePolicy/VisualCreatePolicy.tsx

+13-25
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import ISMTemplates from "../../components/ISMTemplates";
1717
import States from "../../components/States";
1818
import CreateState from "../CreateState";
1919
import { getErrorMessage } from "../../../../utils/helpers";
20-
import { getUpdatedStates } from "../../utils/helpers";
20+
import { getUpdatedPolicy, getUpdatedStates } from "../../utils/helpers";
2121
import ErrorNotification from "../ErrorNotification";
2222

2323
interface VisualCreatePolicyProps extends RouteComponentProps {
@@ -171,13 +171,13 @@ export default class VisualCreatePolicy extends Component<VisualCreatePolicyProp
171171
this.context.notifications.toasts.addSuccess(`Created policy: ${response.response._id}`);
172172
this.props.history.push(ROUTES.INDEX_POLICIES);
173173
} else {
174-
this.setState({ submitError: response.error });
174+
this.setState({ submitError: response.error, isSubmitting: false });
175175
this.context.notifications.toasts.addDanger(`Failed to create policy: ${response.error}`);
176176
}
177177
} catch (err) {
178178
const errorMessage = getErrorMessage(err, "There was a problem creating the policy");
179179
this.context.notifications.toasts.addDanger(errorMessage);
180-
this.setState({ submitError: errorMessage });
180+
this.setState({ submitError: errorMessage, isSubmitting: false });
181181
}
182182
};
183183

@@ -195,12 +195,12 @@ export default class VisualCreatePolicy extends Component<VisualCreatePolicyProp
195195
this.props.history.push(ROUTES.INDEX_POLICIES);
196196
} else {
197197
this.context.notifications.toasts.addDanger(`Failed to update policy: ${response.error}`);
198-
this.setState({ submitError: response.error });
198+
this.setState({ submitError: response.error, isSubmitting: false });
199199
}
200200
} catch (err) {
201201
const errorMessage = getErrorMessage(err, "There was a problem updating the policy");
202202
this.context.notifications.toasts.addDanger(errorMessage);
203-
this.setState({ submitError: errorMessage });
203+
this.setState({ submitError: errorMessage, isSubmitting: false });
204204
}
205205
};
206206

@@ -223,9 +223,15 @@ export default class VisualCreatePolicy extends Component<VisualCreatePolicyProp
223223
} catch (err) {
224224
this.context.notifications.toasts.addDanger("Invalid Policy");
225225
console.error(err);
226+
this.setState({ isSubmitting: false });
226227
}
228+
};
227229

228-
this.setState({ isSubmitting: false });
230+
onSaveState = (state: State, states: State[], order: string, afterBeforeState: string) => {
231+
const { policy, editingState } = this.state;
232+
const updatedPolicy = getUpdatedPolicy(policy, state, editingState, states, order, afterBeforeState);
233+
this.setState({ policy: updatedPolicy });
234+
this.onCloseFlyout();
229235
};
230236

231237
render() {
@@ -272,25 +278,7 @@ export default class VisualCreatePolicy extends Component<VisualCreatePolicyProp
272278
<EuiSpacer size="m" />
273279

274280
{showFlyout && (
275-
<CreateState
276-
state={editingState}
277-
policy={policy}
278-
onSaveState={(state: State, editingState: State | null, states: State[], order: string, afterBeforeState: string) => {
279-
const updatedStates = getUpdatedStates(state, editingState, states, order, afterBeforeState);
280-
let defaultState = policy.default_state;
281-
// If we are creating the first state, set the default state to it
282-
if (updatedStates.length === 1) defaultState = updatedStates[0].name;
283-
this.setState({
284-
policy: {
285-
...policy,
286-
states: updatedStates,
287-
default_state: defaultState,
288-
},
289-
});
290-
this.onCloseFlyout();
291-
}}
292-
onCloseFlyout={this.onCloseFlyout}
293-
/>
281+
<CreateState state={editingState} policy={policy} onSaveState={this.onSaveState} onCloseFlyout={this.onCloseFlyout} />
294282
)}
295283

296284
<EuiFlexGroup alignItems="center" justifyContent="flexEnd">

public/pages/VisualCreatePolicy/utils/helpers.test.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
import { actionRepoSingleton, capitalizeFirstLetter, convertTemplatesToArray, getOrderInfo } from "./helpers";
6+
import { actionRepoSingleton, capitalizeFirstLetter, convertTemplatesToArray, getOrderInfo, getUpdatedPolicy } from "./helpers";
77
import { Action, ISMTemplate, UIAction } from "../../../../models/interfaces";
88
import { makeId } from "../../../utils/helpers";
9+
import { DEFAULT_POLICY } from "./constants";
910

1011
test("converts all ism template formats into a list of ism templates", () => {
1112
expect(convertTemplatesToArray(null)).toEqual([]);
@@ -80,3 +81,10 @@ test("action repository usage", () => {
8081
expect(actionRepoSingleton.getUIAction("dummy") instanceof DummyUIAction).toBe(true);
8182
expect(actionRepoSingleton.getUIActionFromData(DEFAULT_DUMMY) instanceof DummyUIAction).toBe(true);
8283
});
84+
85+
test("changing the default state name correctly updates default_state", () => {
86+
const policy = DEFAULT_POLICY;
87+
const updatedState = { ...policy.states[0], name: "new_hot" };
88+
const newPolicy = getUpdatedPolicy(policy, updatedState, policy.states[0], policy.states, "before", policy.states[1].name);
89+
expect(newPolicy.default_state).toBe("new_hot");
90+
});

public/pages/VisualCreatePolicy/utils/helpers.ts

+28-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
import { UIAction, Action, Transition, ISMTemplate, State } from "../../../../models/interfaces";
6+
import { UIAction, Action, Transition, ISMTemplate, State, Policy } from "../../../../models/interfaces";
77
import {
88
ActionType,
99
DEFAULT_ALLOCATION,
@@ -212,3 +212,30 @@ export const getUpdatedStates = (
212212

213213
return someStates;
214214
};
215+
216+
export const getUpdatedPolicy = (
217+
currentPolicy: Policy,
218+
updatedState: State,
219+
editingState: State | null,
220+
currentStates: State[],
221+
order: string,
222+
afterBeforeState: string
223+
): Policy => {
224+
const updatedStates = getUpdatedStates(updatedState, editingState, currentStates, order, afterBeforeState);
225+
let defaultState = currentPolicy.default_state;
226+
// If there is 1 state, change it to this state
227+
if (updatedStates.length === 1) {
228+
defaultState = updatedStates[0].name;
229+
}
230+
// Change the default state if the state being edited was the default state
231+
if (editingState && editingState.name === defaultState) {
232+
// don't bother checking if the name itself changed, just set it regardless to the value from the new state
233+
defaultState = updatedState.name;
234+
}
235+
236+
return {
237+
...currentPolicy,
238+
states: updatedStates,
239+
default_state: defaultState,
240+
};
241+
};

0 commit comments

Comments
 (0)