Commit 1621fea4 authored by Phil Hughes's avatar Phil Hughes

Merge branch 'network-policy-management-store-update' into 'master'

Add updatePolicy action to the network policies store

See merge request gitlab-org/gitlab!32034
parents 759ba64a 85a0f111
import { s__ } from '~/locale'; import { s__, sprintf } from '~/locale';
import axios from '~/lib/utils/axios_utils'; import axios from '~/lib/utils/axios_utils';
import createFlash from '~/flash'; import createFlash, { FLASH_TYPES } from '~/flash';
import { joinPaths } from '~/lib/utils/url_utility';
import * as types from './mutation_types'; import * as types from './mutation_types';
export const setEndpoints = ({ commit }, endpoints) => { export const setEndpoints = ({ commit }, endpoints) => {
commit(types.SET_ENDPOINT, endpoints.networkPoliciesEndpoint); commit(types.SET_ENDPOINT, endpoints.networkPoliciesEndpoint);
}; };
export const fetchPolicies = ({ state, commit }, environmentId) => { const commitReceivePoliciesError = (commit, payload) => {
const commitError = payload => { const error =
const error = payload?.error || s__('NetworkPolicies|Something went wrong, unable to fetch policies');
payload?.error || s__('NetworkPolicies|Something went wrong, unable to fetch policies'); commit(types.RECEIVE_POLICIES_ERROR, error);
commit(types.RECEIVE_POLICIES_ERROR, error); createFlash(error);
createFlash(error); };
};
if (!state.policiesEndpoint || !environmentId) return commitError(); export const fetchPolicies = ({ state, commit }, environmentId) => {
if (!state.policiesEndpoint || !environmentId) return commitReceivePoliciesError(commit);
commit(types.REQUEST_POLICIES); commit(types.REQUEST_POLICIES);
return axios return axios
.get(state.policiesEndpoint, { params: { environment_id: environmentId } }) .get(state.policiesEndpoint, { params: { environment_id: environmentId } })
.then(({ data }) => commit(types.RECEIVE_POLICIES_SUCCESS, data)) .then(({ data }) => commit(types.RECEIVE_POLICIES_SUCCESS, data))
.catch(error => commitError(error?.response?.data)); .catch(error => commitReceivePoliciesError(commit, error?.response?.data));
};
const commitUpdatePolicyError = (commit, payload) => {
const error =
payload?.error || s__('NetworkPolicies|Something went wrong, failed to update policy');
commit(types.RECEIVE_UPDATE_POLICY_ERROR, error);
createFlash(error);
};
export const updatePolicy = ({ state, commit }, { environmentId, policy, manifest }) => {
if (!state.policiesEndpoint || !environmentId || !manifest) {
return commitUpdatePolicyError(commit);
}
commit(types.REQUEST_UPDATE_POLICY);
return axios
.put(joinPaths(state.policiesEndpoint, policy.name), {
environment_id: environmentId,
manifest,
})
.then(({ data }) => {
commit(types.RECEIVE_UPDATE_POLICY_SUCCESS, {
policy,
updatedPolicy: data,
});
createFlash(
sprintf(s__('NetworkPolicies|Policy %{policyName} was successfully changed'), {
policyName: policy.name,
}),
FLASH_TYPES.SUCCESS,
);
})
.catch(error => commitUpdatePolicyError(commit, error?.response?.data));
}; };
...@@ -3,3 +3,7 @@ export const SET_ENDPOINT = 'SET_ENDPOINT'; ...@@ -3,3 +3,7 @@ export const SET_ENDPOINT = 'SET_ENDPOINT';
export const REQUEST_POLICIES = 'REQUEST_POLICIES'; export const REQUEST_POLICIES = 'REQUEST_POLICIES';
export const RECEIVE_POLICIES_SUCCESS = 'RECEIVE_POLICIES_SUCCESS'; export const RECEIVE_POLICIES_SUCCESS = 'RECEIVE_POLICIES_SUCCESS';
export const RECEIVE_POLICIES_ERROR = 'RECEIVE_POLICIES_ERROR'; export const RECEIVE_POLICIES_ERROR = 'RECEIVE_POLICIES_ERROR';
export const REQUEST_UPDATE_POLICY = 'REQUEST_UPDATE_POLICY';
export const RECEIVE_UPDATE_POLICY_SUCCESS = 'RECEIVE_UPDATE_POLICY_SUCCESS';
export const RECEIVE_UPDATE_POLICY_ERROR = 'RECEIVE_UPDATE_POLICY_ERROR';
...@@ -18,4 +18,18 @@ export default { ...@@ -18,4 +18,18 @@ export default {
state.isLoadingPolicies = false; state.isLoadingPolicies = false;
state.errorLoadingPolicies = true; state.errorLoadingPolicies = true;
}, },
[types.REQUEST_UPDATE_POLICY](state) {
state.isUpdatingPolicy = true;
state.errorUpdatingPolicy = false;
},
[types.RECEIVE_UPDATE_POLICY_SUCCESS](state, { policy, updatedPolicy }) {
const newPolicy = convertObjectPropsToCamelCase(updatedPolicy);
state.policies = state.policies.map(pol => (pol.name === policy.name ? newPolicy : pol));
state.isUpdatingPolicy = false;
state.errorUpdatingPolicy = false;
},
[types.RECEIVE_UPDATE_POLICY_ERROR](state) {
state.isUpdatingPolicy = false;
state.errorUpdatingPolicy = true;
},
}; };
...@@ -3,4 +3,6 @@ export default () => ({ ...@@ -3,4 +3,6 @@ export default () => ({
policies: [], policies: [],
isLoadingPolicies: false, isLoadingPolicies: false,
errorLoadingPolicies: false, errorLoadingPolicies: false,
isUpdatingPolicy: false,
errorUpdatingPolicy: false,
}); });
...@@ -4,6 +4,7 @@ import axios from '~/lib/utils/axios_utils'; ...@@ -4,6 +4,7 @@ import axios from '~/lib/utils/axios_utils';
import httpStatus from '~/lib/utils/http_status'; import httpStatus from '~/lib/utils/http_status';
import createFlash from '~/flash'; import createFlash from '~/flash';
import testAction from 'helpers/vuex_action_helper'; import testAction from 'helpers/vuex_action_helper';
import { joinPaths } from '~/lib/utils/url_utility';
import * as actions from 'ee/threat_monitoring/store/modules/network_policies/actions'; import * as actions from 'ee/threat_monitoring/store/modules/network_policies/actions';
import * as types from 'ee/threat_monitoring/store/modules/network_policies/mutation_types'; import * as types from 'ee/threat_monitoring/store/modules/network_policies/mutation_types';
...@@ -11,7 +12,7 @@ import getInitialState from 'ee/threat_monitoring/store/modules/network_policies ...@@ -11,7 +12,7 @@ import getInitialState from 'ee/threat_monitoring/store/modules/network_policies
import { mockPoliciesResponse } from '../../../mock_data'; import { mockPoliciesResponse } from '../../../mock_data';
jest.mock('~/flash', () => jest.fn()); jest.mock('~/flash');
const networkPoliciesEndpoint = 'networkPoliciesEndpoint'; const networkPoliciesEndpoint = 'networkPoliciesEndpoint';
...@@ -136,4 +137,111 @@ describe('Network Policy actions', () => { ...@@ -136,4 +137,111 @@ describe('Network Policy actions', () => {
)); ));
}); });
}); });
describe('updatePolicy', () => {
let mock;
const environmentId = 3;
const policy = { name: 'policy', manifest: 'foo' };
const updatedPolicy = { name: 'policy', manifest: 'bar' };
beforeEach(() => {
state.policiesEndpoint = networkPoliciesEndpoint;
mock = new MockAdapter(axios);
});
afterEach(() => {
mock.restore();
});
describe('on success', () => {
beforeEach(() => {
mock
.onPut(joinPaths(networkPoliciesEndpoint, policy.name), {
environment_id: environmentId,
manifest: updatedPolicy.manifest,
})
.replyOnce(httpStatus.OK, updatedPolicy);
});
it('should dispatch the request and success actions', () =>
testAction(
actions.updatePolicy,
{ environmentId, policy, manifest: updatedPolicy.manifest },
state,
[
{ type: types.REQUEST_UPDATE_POLICY },
{
type: types.RECEIVE_UPDATE_POLICY_SUCCESS,
payload: { policy, updatedPolicy },
},
],
[],
));
});
describe('on error', () => {
const error = { error: 'foo' };
beforeEach(() => {
mock
.onPut(joinPaths(networkPoliciesEndpoint, policy.name), {
environment_id: environmentId,
manifest: updatedPolicy.manifest,
})
.replyOnce(500, error);
});
it('should dispatch the request and error actions', () =>
testAction(
actions.updatePolicy,
{ environmentId, policy, manifest: updatedPolicy.manifest },
state,
[
{ type: types.REQUEST_UPDATE_POLICY },
{ type: types.RECEIVE_UPDATE_POLICY_ERROR, payload: 'foo' },
],
[],
));
});
describe('with an empty endpoint', () => {
beforeEach(() => {
state.policiesEndpoint = '';
});
it('should dispatch RECEIVE_UPDATE_POLICY_ERROR', () =>
testAction(
actions.updatePolicy,
{ environmentId, policy, manifest: updatedPolicy.manifest },
state,
[
{
type: types.RECEIVE_UPDATE_POLICY_ERROR,
payload: s__('NetworkPolicies|Something went wrong, failed to update policy'),
},
],
[],
));
});
describe('without environment id', () => {
it('should dispatch RECEIVE_UPDATE_POLICY_ERROR', () =>
testAction(
actions.updatePolicy,
{
environmentId: undefined,
policy,
manifest: updatedPolicy.manifest,
},
state,
[
{
type: types.RECEIVE_UPDATE_POLICY_ERROR,
payload: s__('NetworkPolicies|Something went wrong, failed to update policy'),
},
],
[],
));
});
});
}); });
...@@ -5,7 +5,7 @@ describe('Network Policies mutations', () => { ...@@ -5,7 +5,7 @@ describe('Network Policies mutations', () => {
let state; let state;
beforeEach(() => { beforeEach(() => {
state = {}; state = { policies: [] };
}); });
describe(types.SET_ENDPOINT, () => { describe(types.SET_ENDPOINT, () => {
...@@ -54,4 +54,49 @@ describe('Network Policies mutations', () => { ...@@ -54,4 +54,49 @@ describe('Network Policies mutations', () => {
expect(state.errorLoadingPolicies).toBe(true); expect(state.errorLoadingPolicies).toBe(true);
}); });
}); });
describe(types.REQUEST_UPDATE_POLICY, () => {
beforeEach(() => {
mutations[types.REQUEST_UPDATE_POLICY](state);
});
it('sets isUpdatingPolicy to true and sets errorUpdatingPolicy to false', () => {
expect(state.isUpdatingPolicy).toBe(true);
expect(state.errorUpdatingPolicy).toBe(false);
});
});
describe(types.RECEIVE_UPDATE_POLICY_SUCCESS, () => {
const policy = { id: 1, name: 'production', manifest: 'foo' };
const updatedPolicy = { id: 1, name: 'production', manifest: 'bar' };
beforeEach(() => {
state.policies.push(policy);
mutations[types.RECEIVE_UPDATE_POLICY_SUCCESS](state, {
policy,
updatedPolicy,
});
});
it('replaces policies with the updatedPolicy', () => {
expect(state.policies).not.toEqual(expect.objectContaining(policy));
expect(state.policies).toEqual(expect.objectContaining([updatedPolicy]));
});
it('sets isUpdatingPolicy to false and sets errorUpdatingPolicy to false', () => {
expect(state.isUpdatingPolicy).toBe(false);
expect(state.errorUpdatingPolicy).toBe(false);
});
});
describe(types.RECEIVE_UPDATE_POLICY_ERROR, () => {
beforeEach(() => {
mutations[types.RECEIVE_UPDATE_POLICY_ERROR](state);
});
it('sets isUpdatingPolicy to false and sets errorUpdatingPolicy to true', () => {
expect(state.isUpdatingPolicy).toBe(false);
expect(state.errorUpdatingPolicy).toBe(true);
});
});
}); });
...@@ -14061,6 +14061,12 @@ msgstr "" ...@@ -14061,6 +14061,12 @@ msgstr ""
msgid "NetworkPolicies|Kubernetes error: %{error}" msgid "NetworkPolicies|Kubernetes error: %{error}"
msgstr "" msgstr ""
msgid "NetworkPolicies|Policy %{policyName} was successfully changed"
msgstr ""
msgid "NetworkPolicies|Something went wrong, failed to update policy"
msgstr ""
msgid "NetworkPolicies|Something went wrong, unable to fetch policies" msgid "NetworkPolicies|Something went wrong, unable to fetch policies"
msgstr "" msgstr ""
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment