actions.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import { debounce } from 'lodash';
  2. import { getBackendSrv, locationService } from '@grafana/runtime';
  3. import { fetchBuiltinRoles, fetchRoleOptions } from 'app/core/components/RolePicker/api';
  4. import { accessControlQueryParam } from 'app/core/utils/accessControl';
  5. import { contextSrv } from '../../../core/services/context_srv';
  6. import { ServiceAccountDTO, ThunkResult, ServiceAccountFilter, AccessControlAction } from '../../../types';
  7. import { ServiceAccountToken } from '../CreateServiceAccountTokenModal';
  8. import {
  9. acOptionsLoaded,
  10. builtInRolesLoaded,
  11. filterChanged,
  12. pageChanged,
  13. queryChanged,
  14. serviceAccountLoaded,
  15. serviceAccountsFetchBegin,
  16. serviceAccountsFetchEnd,
  17. serviceAccountsFetched,
  18. serviceAccountTokensLoaded,
  19. serviceAccountToRemoveLoaded,
  20. } from './reducers';
  21. const BASE_URL = `/api/serviceaccounts`;
  22. export function fetchACOptions(): ThunkResult<void> {
  23. return async (dispatch) => {
  24. try {
  25. if (contextSrv.licensedAccessControlEnabled() && contextSrv.hasPermission(AccessControlAction.ActionRolesList)) {
  26. const options = await fetchRoleOptions();
  27. dispatch(acOptionsLoaded(options));
  28. }
  29. if (
  30. contextSrv.accessControlBuiltInRoleAssignmentEnabled() &&
  31. contextSrv.licensedAccessControlEnabled() &&
  32. contextSrv.hasPermission(AccessControlAction.ActionBuiltinRolesList)
  33. ) {
  34. const builtInRoles = await fetchBuiltinRoles();
  35. dispatch(builtInRolesLoaded(builtInRoles));
  36. }
  37. } catch (error) {
  38. console.error(error);
  39. }
  40. };
  41. }
  42. export function setServiceAccountToRemove(serviceAccount: ServiceAccountDTO | null): ThunkResult<void> {
  43. return async (dispatch) => {
  44. try {
  45. dispatch(serviceAccountToRemoveLoaded(serviceAccount));
  46. } catch (error) {
  47. console.error(error);
  48. }
  49. };
  50. }
  51. export function loadServiceAccount(saID: number): ThunkResult<void> {
  52. return async (dispatch) => {
  53. try {
  54. const response = await getBackendSrv().get(`${BASE_URL}/${saID}`, accessControlQueryParam());
  55. dispatch(serviceAccountLoaded(response));
  56. } catch (error) {
  57. console.error(error);
  58. }
  59. };
  60. }
  61. export function createServiceAccountToken(
  62. saID: number,
  63. token: ServiceAccountToken,
  64. onTokenCreated: (key: string) => void
  65. ): ThunkResult<void> {
  66. return async (dispatch) => {
  67. const result = await getBackendSrv().post(`${BASE_URL}/${saID}/tokens`, token);
  68. onTokenCreated(result.key);
  69. dispatch(loadServiceAccountTokens(saID));
  70. };
  71. }
  72. export function deleteServiceAccountToken(saID: number, id: number): ThunkResult<void> {
  73. return async (dispatch) => {
  74. await getBackendSrv().delete(`${BASE_URL}/${saID}/tokens/${id}`);
  75. dispatch(loadServiceAccountTokens(saID));
  76. };
  77. }
  78. export function loadServiceAccountTokens(saID: number): ThunkResult<void> {
  79. return async (dispatch) => {
  80. try {
  81. const response = await getBackendSrv().get(`${BASE_URL}/${saID}/tokens`);
  82. dispatch(serviceAccountTokensLoaded(response));
  83. } catch (error) {
  84. console.error(error);
  85. }
  86. };
  87. }
  88. export function updateServiceAccount(serviceAccount: ServiceAccountDTO): ThunkResult<void> {
  89. return async (dispatch) => {
  90. const response = await getBackendSrv().patch(`${BASE_URL}/${serviceAccount.id}?accesscontrol=true`, {
  91. ...serviceAccount,
  92. });
  93. dispatch(serviceAccountLoaded(response));
  94. };
  95. }
  96. export function removeServiceAccount(serviceAccountId: number): ThunkResult<void> {
  97. return async (dispatch) => {
  98. await getBackendSrv().delete(`${BASE_URL}/${serviceAccountId}`);
  99. dispatch(fetchServiceAccounts());
  100. };
  101. }
  102. // search / filtering of serviceAccounts
  103. const getFilters = (filters: ServiceAccountFilter[]) => {
  104. return filters
  105. .map((filter) => {
  106. if (Array.isArray(filter.value)) {
  107. return filter.value.map((v) => `${filter.name}=${v.value}`).join('&');
  108. }
  109. return `${filter.name}=${filter.value}`;
  110. })
  111. .join('&');
  112. };
  113. export function fetchServiceAccounts(): ThunkResult<void> {
  114. return async (dispatch, getState) => {
  115. try {
  116. const { perPage, page, query, filters } = getState().serviceAccounts;
  117. const result = await getBackendSrv().get(
  118. `/api/serviceaccounts/search?perpage=${perPage}&page=${page}&query=${query}&${getFilters(
  119. filters
  120. )}&accesscontrol=true`
  121. );
  122. dispatch(serviceAccountsFetched(result));
  123. } catch (error) {
  124. serviceAccountsFetchEnd();
  125. console.error(error);
  126. }
  127. };
  128. }
  129. const fetchServiceAccountsWithDebounce = debounce((dispatch) => dispatch(fetchServiceAccounts()), 500);
  130. export function changeQuery(query: string): ThunkResult<void> {
  131. return async (dispatch) => {
  132. dispatch(serviceAccountsFetchBegin());
  133. dispatch(queryChanged(query));
  134. fetchServiceAccountsWithDebounce(dispatch);
  135. };
  136. }
  137. export function changeFilter(filter: ServiceAccountFilter): ThunkResult<void> {
  138. return async (dispatch) => {
  139. dispatch(serviceAccountsFetchBegin());
  140. dispatch(filterChanged(filter));
  141. fetchServiceAccountsWithDebounce(dispatch);
  142. };
  143. }
  144. export function changePage(page: number): ThunkResult<void> {
  145. return async (dispatch) => {
  146. dispatch(serviceAccountsFetchBegin());
  147. dispatch(pageChanged(page));
  148. dispatch(fetchServiceAccounts());
  149. };
  150. }
  151. export function deleteServiceAccount(serviceAccountId: number): ThunkResult<void> {
  152. return async () => {
  153. await getBackendSrv().delete(`${BASE_URL}/${serviceAccountId}`);
  154. locationService.push('/org/serviceaccounts');
  155. };
  156. }