2020-01-13 01:03:22 -06:00
|
|
|
|
import { getBackendSrv } from 'app/core/services/backend_srv';
|
|
|
|
|
import { ApiKey, ThunkResult } from 'app/types';
|
2022-04-22 08:33:13 -05:00
|
|
|
|
|
2021-12-16 05:46:09 -06:00
|
|
|
|
import { apiKeysLoaded, includeExpiredToggled, isFetching, setSearchQuery } from './reducers';
|
2018-09-25 09:23:43 -05:00
|
|
|
|
|
2021-12-16 05:46:09 -06:00
|
|
|
|
export function addApiKey(apiKey: ApiKey, openModal: (key: string) => void): ThunkResult<void> {
|
2021-01-20 00:59:48 -06:00
|
|
|
|
return async (dispatch) => {
|
2018-09-27 04:26:47 -05:00
|
|
|
|
const result = await getBackendSrv().post('/api/auth/keys', apiKey);
|
|
|
|
|
dispatch(setSearchQuery(''));
|
2021-12-16 05:46:09 -06:00
|
|
|
|
dispatch(loadApiKeys());
|
2018-09-27 04:26:47 -05:00
|
|
|
|
openModal(result.key);
|
2018-09-26 06:45:04 -05:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-16 05:46:09 -06:00
|
|
|
|
export function loadApiKeys(): ThunkResult<void> {
|
2021-01-20 00:59:48 -06:00
|
|
|
|
return async (dispatch) => {
|
2021-12-16 05:46:09 -06:00
|
|
|
|
dispatch(isFetching());
|
|
|
|
|
const [keys, keysIncludingExpired] = await Promise.all([
|
2022-04-29 08:30:24 -05:00
|
|
|
|
getBackendSrv().get('/api/auth/keys?includeExpired=false&accesscontrol=true'),
|
|
|
|
|
getBackendSrv().get('/api/auth/keys?includeExpired=true&accesscontrol=true'),
|
2021-12-16 05:46:09 -06:00
|
|
|
|
]);
|
|
|
|
|
dispatch(apiKeysLoaded({ keys, keysIncludingExpired }));
|
2018-09-25 09:23:43 -05:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-16 05:46:09 -06:00
|
|
|
|
export function deleteApiKey(id: number): ThunkResult<void> {
|
2021-01-20 00:59:48 -06:00
|
|
|
|
return async (dispatch) => {
|
2018-09-25 09:23:43 -05:00
|
|
|
|
getBackendSrv()
|
2020-01-21 03:08:07 -06:00
|
|
|
|
.delete(`/api/auth/keys/${id}`)
|
2021-12-16 05:46:09 -06:00
|
|
|
|
.then(() => dispatch(loadApiKeys()));
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function toggleIncludeExpired(): ThunkResult<void> {
|
|
|
|
|
return (dispatch) => {
|
|
|
|
|
dispatch(includeExpiredToggled());
|
2018-09-25 09:23:43 -05:00
|
|
|
|
};
|
|
|
|
|
}
|