2022-06-15 07:59:40 -05:00
|
|
|
|
import { config } from '@grafana/runtime';
|
|
|
|
|
import { getBackendSrv } from 'app/core/services/backend_srv';
|
2020-01-13 01:03:22 -06:00
|
|
|
|
import { ApiKey, ThunkResult } from 'app/types';
|
2022-04-22 08:33:13 -05:00
|
|
|
|
|
2022-06-15 07:59:40 -05:00
|
|
|
|
import {
|
|
|
|
|
apiKeysLoaded,
|
|
|
|
|
includeExpiredToggled,
|
|
|
|
|
isFetching,
|
|
|
|
|
apiKeysMigrationStatusLoaded,
|
|
|
|
|
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()));
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-15 07:59:40 -05:00
|
|
|
|
export function migrateApiKey(id: number): ThunkResult<void> {
|
|
|
|
|
return async (dispatch) => {
|
|
|
|
|
try {
|
|
|
|
|
await getBackendSrv().post(`/api/serviceaccounts/migrate/${id}`);
|
|
|
|
|
} finally {
|
|
|
|
|
dispatch(loadApiKeys());
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function migrateAll(): ThunkResult<void> {
|
|
|
|
|
return async (dispatch) => {
|
|
|
|
|
try {
|
|
|
|
|
await getBackendSrv().post('/api/serviceaccounts/migrate');
|
|
|
|
|
} finally {
|
|
|
|
|
dispatch(getApiKeysMigrationStatus());
|
|
|
|
|
dispatch(loadApiKeys());
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getApiKeysMigrationStatus(): ThunkResult<void> {
|
|
|
|
|
return async (dispatch) => {
|
|
|
|
|
// TODO: remove when service account enabled by default (or use another way to detect if it's enabled)
|
|
|
|
|
if (config.featureToggles.serviceAccounts) {
|
|
|
|
|
const result = await getBackendSrv().get('/api/serviceaccounts/migrationstatus');
|
|
|
|
|
dispatch(apiKeysMigrationStatusLoaded(!!result?.migrated));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function hideApiKeys(): ThunkResult<void> {
|
|
|
|
|
return async (dispatch) => {
|
|
|
|
|
await getBackendSrv().post('/api/serviceaccounts/hideApiKeys');
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-16 05:46:09 -06:00
|
|
|
|
export function toggleIncludeExpired(): ThunkResult<void> {
|
|
|
|
|
return (dispatch) => {
|
|
|
|
|
dispatch(includeExpiredToggled());
|
2018-09-25 09:23:43 -05:00
|
|
|
|
};
|
|
|
|
|
}
|