Files
Eric Leijonmarck 9d6ab92e39 Service accounts: Remove Add API keys buttons and remove one state of migrating for API keys tab (#63411)
* add: hide apikeys tab on start

* make use of store method

* added hiding of apikeys tab for new org creation

* missing err check

* removed unused files

* implemennted fake to make tests run

* move check for globalHideApikeys from org to admin

* refactor to remove the fake

* removed unused method calls for interface

* Update pkg/services/serviceaccounts/manager/service.go

Co-authored-by: Alexander Zobnin <alexanderzobnin@gmail.com>

* Update pkg/services/serviceaccounts/manager/service.go

Co-authored-by: Alexander Zobnin <alexanderzobnin@gmail.com>

* remove the checkglobal method

* removed duplicate global set const

* add count of apikeys for performance

* remove apikeys adding in UI

* added back deleted file

* added comment on component

* changed wording and copy for hiding and migrating service accounts

* refactor: remove migrationstatus in front/backend

This removes the migrationstatus state from the UI in favor of only
looking at the number of API keys to determine what to show to the user.
This simplifies the logic and makes less calls to the backend with each
page load. This was called both on the API keys page and the Service
accounts page.

- removes the state of migrationstatus from the UI
- removes the backend call
- removes the backend endpoint for migrationstatus

* Update pkg/services/apikey/apikeyimpl/xorm_store.go

Co-authored-by: Karl Persson <kalle.persson@grafana.com>

* changes the contet to also be primary

* change id of version for footer component

---------

Co-authored-by: Alexander Zobnin <alexanderzobnin@gmail.com>
Co-authored-by: Karl Persson <kalle.persson@grafana.com>
2023-03-01 15:34:53 +00:00

128 lines
3.8 KiB
TypeScript

import { debounce } from 'lodash';
import { getBackendSrv } from '@grafana/runtime';
import { fetchRoleOptions } from 'app/core/components/RolePicker/api';
import { contextSrv } from 'app/core/services/context_srv';
import { AccessControlAction, ServiceAccountDTO, ServiceAccountStateFilter, ThunkResult } from 'app/types';
import { ServiceAccountToken } from '../components/CreateTokenModal';
import {
acOptionsLoaded,
pageChanged,
queryChanged,
serviceAccountsFetchBegin,
serviceAccountsFetched,
serviceAccountsFetchEnd,
stateFilterChanged,
} from './reducers';
const BASE_URL = `/api/serviceaccounts`;
export function fetchACOptions(): ThunkResult<void> {
return async (dispatch) => {
try {
if (contextSrv.licensedAccessControlEnabled() && contextSrv.hasPermission(AccessControlAction.ActionRolesList)) {
const options = await fetchRoleOptions();
dispatch(acOptionsLoaded(options));
}
} catch (error) {
console.error(error);
}
};
}
interface FetchServiceAccountsParams {
withLoadingIndicator: boolean;
}
export function fetchServiceAccounts(
{ withLoadingIndicator }: FetchServiceAccountsParams = { withLoadingIndicator: false }
): ThunkResult<void> {
return async (dispatch, getState) => {
try {
if (contextSrv.hasPermission(AccessControlAction.ServiceAccountsRead)) {
if (withLoadingIndicator) {
dispatch(serviceAccountsFetchBegin());
}
const { perPage, page, query, serviceAccountStateFilter } = getState().serviceAccounts;
const result = await getBackendSrv().get(
`/api/serviceaccounts/search?perpage=${perPage}&page=${page}&query=${query}${getStateFilter(
serviceAccountStateFilter
)}&accesscontrol=true`
);
dispatch(serviceAccountsFetched(result));
}
} catch (error) {
console.error(error);
} finally {
dispatch(serviceAccountsFetchEnd());
}
};
}
const fetchServiceAccountsWithDebounce = debounce((dispatch) => dispatch(fetchServiceAccounts()), 500, {
leading: true,
});
export function updateServiceAccount(serviceAccount: ServiceAccountDTO): ThunkResult<void> {
return async (dispatch) => {
await getBackendSrv().patch(`${BASE_URL}/${serviceAccount.id}?accesscontrol=true`, {
...serviceAccount,
});
dispatch(fetchServiceAccounts());
};
}
export function deleteServiceAccount(serviceAccountId: number): ThunkResult<void> {
return async (dispatch) => {
await getBackendSrv().delete(`${BASE_URL}/${serviceAccountId}`);
dispatch(fetchServiceAccounts());
};
}
export function createServiceAccountToken(
saID: number,
token: ServiceAccountToken,
onTokenCreated: (key: string) => void
): ThunkResult<void> {
return async (dispatch) => {
const result = await getBackendSrv().post(`${BASE_URL}/${saID}/tokens`, token);
onTokenCreated(result.key);
dispatch(fetchServiceAccounts());
};
}
// search / filtering of serviceAccounts
const getStateFilter = (value: ServiceAccountStateFilter) => {
switch (value) {
case ServiceAccountStateFilter.WithExpiredTokens:
return '&expiredTokens=true';
case ServiceAccountStateFilter.Disabled:
return '&disabled=true';
default:
return '';
}
};
export function changeQuery(query: string): ThunkResult<void> {
return async (dispatch) => {
dispatch(queryChanged(query));
fetchServiceAccountsWithDebounce(dispatch);
};
}
export function changeStateFilter(filter: ServiceAccountStateFilter): ThunkResult<void> {
return async (dispatch) => {
dispatch(stateFilterChanged(filter));
dispatch(fetchServiceAccounts());
};
}
export function changePage(page: number): ThunkResult<void> {
return async (dispatch) => {
dispatch(pageChanged(page));
dispatch(fetchServiceAccounts());
};
}