diff --git a/src/juju/api.test.ts b/src/juju/api.test.ts index 0ddfa97a9..e43a3a147 100644 --- a/src/juju/api.test.ts +++ b/src/juju/api.test.ts @@ -734,12 +734,8 @@ describe("Juju API", () => { .spyOn(jujuLibVersions, "jujuUpdateAvailable") .mockImplementationOnce(async () => true) .mockImplementationOnce(async () => false); - await fetchControllerList( - "wss://example.com/api", - conn, - true, - dispatch, - () => rootStateFactory.build(), + await fetchControllerList("wss://example.com/api", conn, dispatch, () => + rootStateFactory.build(), ); expect(dispatch).toHaveBeenCalledWith( jujuActions.updateControllerList({ @@ -773,12 +769,8 @@ describe("Juju API", () => { .spyOn(jujuLibVersions, "jujuUpdateAvailable") .mockImplementationOnce(async () => true) .mockImplementationOnce(async () => false); - await fetchControllerList( - "wss://example.com/api", - conn, - true, - dispatch, - () => rootStateFactory.build(), + await fetchControllerList("wss://example.com/api", conn, dispatch, () => + rootStateFactory.build(), ); expect(dispatch).toHaveBeenCalledWith( jujuActions.updateControllerList({ @@ -849,12 +841,8 @@ describe("Juju API", () => { jest .spyOn(jujuLibVersions, "jujuUpdateAvailable") .mockImplementationOnce(async () => null); - await fetchControllerList( - "wss://example.com/api", - conn, - true, - dispatch, - () => rootStateFactory.build(), + await fetchControllerList("wss://example.com/api", conn, dispatch, () => + rootStateFactory.build(), ); expect(dispatch).toHaveBeenCalledWith( jujuActions.updateControllerList({ diff --git a/src/store/app/actions.ts b/src/store/app/actions.ts index 0a07fef0b..1400b0455 100644 --- a/src/store/app/actions.ts +++ b/src/store/app/actions.ts @@ -17,7 +17,7 @@ export type ControllerArgs = [ // credentials Credential | undefined, // identityProviderAvailable - boolean | undefined + boolean | undefined, ]; export const connectAndPollControllers = createAction<{ diff --git a/src/store/middleware/model-poller.ts b/src/store/middleware/model-poller.ts index 1e32f0dff..22ab2d6ab 100644 --- a/src/store/middleware/model-poller.ts +++ b/src/store/middleware/model-poller.ts @@ -26,8 +26,6 @@ export enum LoginError { NO_INFO = "Unable to retrieve controller details", } -type ControllerOptions = [string, Credential, boolean]; - const checkJIMMRelation = async ( conn: ConnectionWithFacades, identity: string, @@ -64,39 +62,38 @@ export const modelPollerMiddleware: Middleware< // Each time we try to log in to a controller we get new macaroons, so // first clean up any old auth requests: reduxStore.dispatch(generalActions.clearVisitURLs()); - action.payload.controllers.forEach( - async (controllerData: ControllerOptions) => { - const [wsControllerURL, credentials, identityProviderAvailable] = - controllerData; - let conn: ConnectionWithFacades | undefined; - let juju: Client | undefined; - let error: unknown; - let intervalId: number | null | undefined; - try { - ({ conn, error, juju, intervalId } = await loginWithBakery( - wsControllerURL, - credentials, - identityProviderAvailable - )); - if (conn) { - controllers.set(wsControllerURL, conn); - } - if (error && typeof error === "string") { - reduxStore.dispatch( - generalActions.storeLoginError({ wsControllerURL, error }) - ); - return; - } - } catch (e) { + for (const controllerData of action.payload.controllers) { + const [wsControllerURL, credentials, identityProviderAvailable] = + controllerData; + let conn: ConnectionWithFacades | undefined; + let juju: Client | undefined; + let error: unknown; + let intervalId: number | null | undefined; + try { + ({ conn, error, juju, intervalId } = await loginWithBakery( + wsControllerURL, + credentials, + identityProviderAvailable, + )); + if (conn) { + controllers.set(wsControllerURL, conn); + } + if (error && typeof error === "string") { reduxStore.dispatch( - generalActions.storeLoginError({ - wsControllerURL, - error: - "Unable to log into the controller, check that the controller address is correct and that it is online.", - }) + generalActions.storeLoginError({ wsControllerURL, error }), ); - return console.log(LoginError.LOG, e, controllerData); + return; } + } catch (e) { + reduxStore.dispatch( + generalActions.storeLoginError({ + wsControllerURL, + error: + "Unable to log into the controller, check that the controller address is correct and that it is online.", + }), + ); + return console.log(LoginError.LOG, e, controllerData); + } if (!conn?.info || !Object.keys(conn.info).length) { reduxStore.dispatch( @@ -171,22 +168,22 @@ export const modelPollerMiddleware: Middleware< ); } - await fetchControllerList( - wsControllerURL, - conn, - reduxStore.dispatch, - reduxStore.getState - ); - if (identityProviderAvailable) { - // This call will be a noop if the user isn't an administrator - // on the JIMM controller we're connected to. - try { - await disableControllerUUIDMasking(conn); - } catch (e) { - // Silently fail, if this doesn't work then the user isn't authorized - // to perform the action. - } + await fetchControllerList( + wsControllerURL, + conn, + reduxStore.dispatch, + reduxStore.getState, + ); + if (identityProviderAvailable) { + // This call will be a noop if the user isn't an administrator + // on the JIMM controller we're connected to. + try { + await disableControllerUUIDMasking(conn); + } catch (e) { + // Silently fail, if this doesn't work then the user isn't authorized + // to perform the action. } + } let pollCount = 0; do {