Skip to content

Commit

Permalink
Merge branch 'branch/v17' into tcsc/branch/v17/idc-backport-compendium
Browse files Browse the repository at this point in the history
  • Loading branch information
smallinsky authored Nov 8, 2024
2 parents 7961750 + 94cc854 commit 47575fc
Showing 1 changed file with 30 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ export default function useAccessRequestCheckout() {
const ctx = useAppContext();
useWorkspaceServiceState();
ctx.clustersService.useState();
/**
* @deprecated Do not use it here. This value comes from the cluster selector next to the search
* bar. Changing the cluster should not affect the request checkout in any way.
* clusterUri is kept here just to not break existing code that depends on it.
* See https://github.com/gravitational/teleport/issues/48510.
*
* Instead, in most cases you want to use rootClusterUri or the cluster URI derived from a URI of
* a resource that you're operating on.
*/
const clusterUri =
ctx.workspacesService?.getActiveWorkspace()?.localClusterUri;
const rootClusterUri = ctx.workspacesService?.getRootClusterUri();
Expand Down Expand Up @@ -87,6 +96,12 @@ export default function useAccessRequestCheckout() {

const { attempt: createRequestAttempt, setAttempt: setCreateRequestAttempt } =
useAttempt('');
// isCreatingRequest is an auxiliary variable that helps to differentiate between a dry run being
// performed vs an actual request being created, as both types of requests use the same attempt
// object (createRequestAttempt).
// TODO(ravicious): Remove this in React 19 when useSyncExternalStore updates are batched with
// other updates.
const [isCreatingRequest, setIsCreatingRequest] = useState(false);

const { attempt: fetchResourceRolesAttempt, run: runFetchResourceRoles } =
useAttempt('success');
Expand All @@ -112,18 +127,23 @@ export default function useAccessRequestCheckout() {
// suggested reviewers.
// Options and reviewers can change depending on the selected
// roles or resources.
if (showCheckout && requestedCount == 0) {
if (showCheckout && requestedCount == 0 && !isCreatingRequest) {
performDryRun();
}
}, [showCheckout, pendingAccessRequestRequest]);
}, [
showCheckout,
pendingAccessRequestRequest,
requestedCount,
isCreatingRequest,
]);

useEffect(() => {
if (!pendingAccessRequestRequest || requestedCount > 0) {
return;
}

runFetchResourceRoles(() =>
retryWithRelogin(ctx, clusterUri, async () => {
retryWithRelogin(ctx, rootClusterUri, async () => {
const { response } = await ctx.tshd.getRequestableRoles({
clusterUri: rootClusterUri,
resourceIds: pendingAccessRequestsWithoutParentResource
Expand All @@ -142,11 +162,11 @@ export default function useAccessRequestCheckout() {
setSelectedResourceRequestRoles(response.applicableRoles);
})
);
}, [pendingAccessRequestRequest]);
}, [pendingAccessRequestRequest, requestedCount]);

useEffect(() => {
clearCreateAttempt();
}, [clusterUri]);
}, [rootClusterUri]);

useEffect(() => {
if (
Expand Down Expand Up @@ -266,7 +286,7 @@ export default function useAccessRequestCheckout() {
}

function getAssumedRequests() {
if (!clusterUri) {
if (!rootClusterUri) {
return [];
}
const assumed = ctx.clustersService.getAssumedRequests(rootClusterUri);
Expand Down Expand Up @@ -315,7 +335,7 @@ export default function useAccessRequestCheckout() {

setCreateRequestAttempt({ status: 'processing' });

return retryWithRelogin(ctx, clusterUri, () =>
return retryWithRelogin(ctx, rootClusterUri, () =>
ctx.clustersService.createAccessRequest(params).then(({ response }) => {
return {
accessRequest: response.request,
Expand Down Expand Up @@ -349,6 +369,7 @@ export default function useAccessRequestCheckout() {
}

async function createRequest(req: CreateRequest) {
setIsCreatingRequest(true);
let requestedCount: number;
try {
const response = await prepareAndCreateRequest(req);
Expand All @@ -360,6 +381,7 @@ export default function useAccessRequestCheckout() {
setRequestedCount(requestedCount);
reset();
setCreateRequestAttempt({ status: 'success' });
setIsCreatingRequest(false);
}

function clearCreateAttempt() {
Expand Down Expand Up @@ -408,7 +430,7 @@ export default function useAccessRequestCheckout() {
useSearchAsRoles: true,
nextKey: '',
resourceType: 'namespace',
clusterUri,
clusterUri: clusterUri,
predicateExpression: '',
kubernetesCluster: kubeCluster,
kubernetesNamespace: '',
Expand All @@ -435,11 +457,9 @@ export default function useAccessRequestCheckout() {
goToRequestsList,
requestedCount,
clearCreateAttempt,
clusterUri,
selectedResourceRequestRoles,
setSelectedResourceRequestRoles,
resourceRequestRoles,
rootClusterUri,
fetchResourceRolesAttempt,
createRequestAttempt,
collapseBar,
Expand Down

0 comments on commit 47575fc

Please sign in to comment.