Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
MonPote committed Apr 22, 2024
1 parent 965145d commit 7d50fe5
Show file tree
Hide file tree
Showing 9 changed files with 415 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .jest-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ import 'core-js/stable';
import 'regenerator-runtime/runtime';

Enzyme.configure({ adapter: new Adapter() });
HTMLCanvasElement.prototype.getContext = () => {
// return whatever getContext has to return
};
35 changes: 32 additions & 3 deletions src/react/DataServiceRoleProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { getRoleArnStored, setRoleArnStored } from './utils/localStorage';
import { useMutation } from 'react-query';
import {
S3ClientProvider,
S3ClientWithoutReduxProvider,
useAssumeRoleQuery,
useS3ConfigFromAssumeRoleResult,
} from './next-architecture/ui/S3ClientProvider';
Expand Down Expand Up @@ -94,7 +95,18 @@ export const useCurrentAccount = () => {
};
};

const DataServiceRoleProvider = ({ children }: { children: JSX.Element }) => {
const DataServiceRoleProvider = ({
children,
/**
* DoNotChangePropsWithRedux is a static props.
* When set, it must not be changed, otherwise it will break the hook rules.
* To be removed when we remove redux.
*/
DoNotChangePropsWithRedux = true,
}: {
children: JSX.Element;
DoNotChangePropsWithRedux?: boolean;
}) => {
const [role, setRoleState] = useState<{ roleArn: string }>({
roleArn: '',
});
Expand Down Expand Up @@ -171,8 +183,25 @@ const DataServiceRoleProvider = ({ children }: { children: JSX.Element }) => {
return <Loader>Loading...</Loader>;
}

if (DoNotChangePropsWithRedux) {
return (
<S3ClientProvider configuration={getS3Config(assumedRole)}>
<_DataServiceRoleContext.Provider
value={{
role,
setRole,
setRolePromise,
assumedRole,
}}
>
{children}
</_DataServiceRoleContext.Provider>
</S3ClientProvider>
);
}

return (
<S3ClientProvider configuration={getS3Config(assumedRole)}>
<S3ClientWithoutReduxProvider configuration={getS3Config(assumedRole)}>
<_DataServiceRoleContext.Provider
value={{
role,
Expand All @@ -183,7 +212,7 @@ const DataServiceRoleProvider = ({ children }: { children: JSX.Element }) => {
>
{children}
</_DataServiceRoleContext.Provider>
</S3ClientProvider>
</S3ClientWithoutReduxProvider>
);
};

Expand Down
44 changes: 44 additions & 0 deletions src/react/databrowser/buckets/details/Overview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import {
getLocationType,
} from '../../../utils/storageOptions';
import { useWorkflows } from '../../../workflow/Workflows';
import { SelectAccountIAMRole } from '../../../ui-elements/SelectAccountIAMRole';

function capitalize(string: string) {
return string.toLowerCase().replace(/^\w/, (c) => {
Expand Down Expand Up @@ -388,6 +389,49 @@ function Overview({ bucket, ingestionStates }: Props) {
</T.Row>
</T.GroupContent>
</T.Group>
<div
style={{ display: 'flex', gap: '1rem', flexDirection: 'column' }}
>
<div>
Normal
<SelectAccountIAMRole />

Check failure on line 397 in src/react/databrowser/buckets/details/Overview.tsx

View workflow job for this annotation

GitHub Actions / build

Property 'onChange' is missing in type '{}' but required in type '{ onChange: (account: Account, role: Role) => void; defaultValue?: { accountName: string; roleName: string; }; hideAccountRoles?: { accountName: string; roleName: string; }[]; }'.

Check failure on line 397 in src/react/databrowser/buckets/details/Overview.tsx

View workflow job for this annotation

GitHub Actions / build-ui

Property 'onChange' is missing in type '{}' but required in type '{ onChange: (account: Account, role: Role) => void; defaultValue?: { accountName: string; roleName: string; }; hideAccountRoles?: { accountName: string; roleName: string; }[]; }'.
</div>
<div>
Default Value
<SelectAccountIAMRole

Check failure on line 401 in src/react/databrowser/buckets/details/Overview.tsx

View workflow job for this annotation

GitHub Actions / build

Property 'onChange' is missing in type '{ defaultValue: { accountName: string; roleName: string; }; }' but required in type '{ onChange: (account: Account, role: Role) => void; defaultValue?: { accountName: string; roleName: string; }; hideAccountRoles?: { accountName: string; roleName: string; }[]; }'.

Check failure on line 401 in src/react/databrowser/buckets/details/Overview.tsx

View workflow job for this annotation

GitHub Actions / build-ui

Property 'onChange' is missing in type '{ defaultValue: { accountName: string; roleName: string; }; }' but required in type '{ onChange: (account: Account, role: Role) => void; defaultValue?: { accountName: string; roleName: string; }; hideAccountRoles?: { accountName: string; roleName: string; }[]; }'.
defaultValue={{
accountName: 'william',
roleName: 'backbeat-gc-1',
}}
/>
</div>

<div>
hidden
<SelectAccountIAMRole

Check failure on line 411 in src/react/databrowser/buckets/details/Overview.tsx

View workflow job for this annotation

GitHub Actions / build

Property 'onChange' is missing in type '{ hideAccountRoles: { accountName: string; roleName: string; }[]; }' but required in type '{ onChange: (account: Account, role: Role) => void; defaultValue?: { accountName: string; roleName: string; }; hideAccountRoles?: { accountName: string; roleName: string; }[]; }'.

Check failure on line 411 in src/react/databrowser/buckets/details/Overview.tsx

View workflow job for this annotation

GitHub Actions / build-ui

Property 'onChange' is missing in type '{ hideAccountRoles: { accountName: string; roleName: string; }[]; }' but required in type '{ onChange: (account: Account, role: Role) => void; defaultValue?: { accountName: string; roleName: string; }; hideAccountRoles?: { accountName: string; roleName: string; }[]; }'.
hideAccountRoles={[
{ accountName: 'william', roleName: 'backbeat-gc-1' },
{ accountName: 'william', roleName: 'data-consumer-role' },
]}
/>
</div>

<div>
wrong default value
<SelectAccountIAMRole

Check failure on line 421 in src/react/databrowser/buckets/details/Overview.tsx

View workflow job for this annotation

GitHub Actions / build

Property 'onChange' is missing in type '{ defaultValue: { accountName: string; roleName: string; }; }' but required in type '{ onChange: (account: Account, role: Role) => void; defaultValue?: { accountName: string; roleName: string; }; hideAccountRoles?: { accountName: string; roleName: string; }[]; }'.

Check failure on line 421 in src/react/databrowser/buckets/details/Overview.tsx

View workflow job for this annotation

GitHub Actions / build-ui

Property 'onChange' is missing in type '{ defaultValue: { accountName: string; roleName: string; }; }' but required in type '{ onChange: (account: Account, role: Role) => void; defaultValue?: { accountName: string; roleName: string; }; hideAccountRoles?: { accountName: string; roleName: string; }[]; }'.
defaultValue={{
accountName: 'william1',
roleName: 'backbeat-gc-1',
}}
/>
<SelectAccountIAMRole

Check failure on line 427 in src/react/databrowser/buckets/details/Overview.tsx

View workflow job for this annotation

GitHub Actions / build

Property 'onChange' is missing in type '{ defaultValue: { accountName: string; roleName: string; }; }' but required in type '{ onChange: (account: Account, role: Role) => void; defaultValue?: { accountName: string; roleName: string; }; hideAccountRoles?: { accountName: string; roleName: string; }[]; }'.

Check failure on line 427 in src/react/databrowser/buckets/details/Overview.tsx

View workflow job for this annotation

GitHub Actions / build-ui

Property 'onChange' is missing in type '{ defaultValue: { accountName: string; roleName: string; }; }' but required in type '{ onChange: (account: Account, role: Role) => void; defaultValue?: { accountName: string; roleName: string; }; hideAccountRoles?: { accountName: string; roleName: string; }[]; }'.
defaultValue={{
accountName: 'william',
roleName: 'backbeat-gc-2',
}}
/>
</div>
</div>
</T.Body>
</Table>
</TableContainer>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useAccounts } from '../../../utils/hooks';
import { noopBasedEventDispatcher, useAccounts } from '../../../utils/hooks';
import { useAccountsLocationsAndEndpoints } from '../../domain/business/accounts';
import { AccountInfo, Role } from '../../domain/entities/account';
import { PromiseResult } from '../../domain/entities/promise';
Expand All @@ -8,6 +8,7 @@ import { IAccountsLocationsEndpointsAdapter } from '../accounts-locations/IAccou
export class IAMPensieveAccessibleAccounts implements IAccessibleAccounts {
constructor(
private accountsLocationsAndEndpointsAdapter: IAccountsLocationsEndpointsAdapter,
private withEventDispatcher = true,
) {}
useListAccessibleAccounts(): {
accountInfos: PromiseResult<(AccountInfo & { assumableRoles: Role[] })[]>;
Expand All @@ -17,7 +18,12 @@ export class IAMPensieveAccessibleAccounts implements IAccessibleAccounts {
accountsLocationsEndpointsAdapter:
this.accountsLocationsAndEndpointsAdapter,
});
const { accounts: accessibleAccounts, status } = useAccounts();
const eventDispatcher = this.withEventDispatcher
? undefined
: noopBasedEventDispatcher;
console.log('withEventDispatcher', this.withEventDispatcher);
const { accounts: accessibleAccounts, status } =
useAccounts(eventDispatcher);

if (accountStatus === 'error' || status === 'error') {
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,20 @@ export const useAccessibleAccountsAdapter = (): IAccessibleAccounts => {

export const AccessibleAccountsAdapterProvider = ({
children,
/**
* DoNotChangePropsWithEventDispatcher is a static props.
* When set, it must not be changed, otherwise it will break the hook rules.
* To be removed when we remove redux.
*/
DoNotChangePropsWithEventDispatcher = true,
}: {
children: JSX.Element;
DoNotChangePropsWithEventDispatcher?: boolean;
}) => {
const accountAdapter = useAccountsLocationsEndpointsAdapter();
const accessibleAccountsAdapter = new IAMPensieveAccessibleAccounts(
accountAdapter,
DoNotChangePropsWithEventDispatcher,
);

return (
Expand Down
54 changes: 54 additions & 0 deletions src/react/next-architecture/ui/S3ClientProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,60 @@ export const S3ClientProvider = ({
);
};

export const S3ClientWithoutReduxProvider = ({
configuration,
children,
}: PropsWithChildren<{
configuration: S3.Types.ClientConfiguration;
}>) => {
const { iamEndpoint, iamInternalFQDN, s3InternalFQDN, basePath } =
useConfig();
const { s3Client, zenkoClient, iamClient } = useMemo(() => {
const s3Config = {
...configuration,
endpoint: genClientEndpoint(configuration.endpoint as string),
};
const s3Client = new S3(s3Config);
const zenkoClient = new ZenkoClient(
s3Config.endpoint,
iamInternalFQDN,
s3InternalFQDN,
process.env.NODE_ENV === 'development' ? '' : basePath,
);
const iamClient = new IAMClient(iamEndpoint);

if (
configuration.credentials?.accessKeyId &&
configuration.credentials?.secretAccessKey &&
configuration.credentials?.sessionToken
) {
zenkoClient.login({
accessKey: configuration.credentials.accessKeyId,
secretKey: configuration.credentials.secretAccessKey,
sessionToken: configuration.credentials.sessionToken,
});

iamClient.login({
accessKey: configuration.credentials.accessKeyId,
secretKey: configuration.credentials.secretAccessKey,
sessionToken: configuration.credentials.sessionToken,
});
}

return { s3Client, zenkoClient, iamClient };
}, [configuration]);

return (
<S3ClientContext.Provider value={s3Client}>
<ZenkoClientContext.Provider value={zenkoClient}>
<_IAMContext.Provider value={{ iamClient }}>
{children}
</_IAMContext.Provider>
</ZenkoClientContext.Provider>
</S3ClientContext.Provider>
);
};

export const useAssumeRoleQuery = () => {
const { stsEndpoint } = useConfig();
const token = useAccessToken();
Expand Down
89 changes: 86 additions & 3 deletions src/react/ui-elements/SelectAccountIAMRole.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,49 @@
import { Stack } from '@scality/core-ui';
import { Select } from '@scality/core-ui/dist/next';
import { IAM } from 'aws-sdk';
import { Bucket } from 'aws-sdk/clients/s3';
import { useState } from 'react';
import { useQuery } from 'react-query';
import { MemoryRouter } from 'react-router-dom';
import { Account } from '../../types/iam';
import DataServiceRoleProvider, {
useCurrentAccount,
useSetAssumedRole,
} from '../DataServiceRoleProvider';
import { useIAMClient } from '../IAMProvider';
import { IMetricsAdapter } from '../next-architecture/adapters/metrics/IMetricsAdapter';
import { useListAccounts } from '../next-architecture/domain/business/accounts';
import { LatestUsedCapacity } from '../next-architecture/domain/entities/metrics';
import {
AccessibleAccountsAdapterProvider,
useAccessibleAccountsAdapter,
} from '../next-architecture/ui/AccessibleAccountsAdapterProvider';
import { AccountsLocationsEndpointsAdapterProvider } from '../next-architecture/ui/AccountsLocationsEndpointsAdapterProvider';
import { getListRolesQuery } from '../queries';
import { useAccounts } from '../utils/hooks';
import { noopBasedEventDispatcher, useAccounts } from '../utils/hooks';

class NoOppMetricsAdapter implements IMetricsAdapter {
async listBucketsLatestUsedCapacity(
buckets: Bucket[],
): Promise<Record<string, LatestUsedCapacity>> {
return {};
}
async listLocationsLatestUsedCapacity(
locationIds: string[],
): Promise<Record<string, LatestUsedCapacity>> {
return {};
}
async listAccountLocationsLatestUsedCapacity(
accountCanonicalId: string,
): Promise<Record<string, LatestUsedCapacity>> {
return {};
}
async listAccountsLatestUsedCapacity(
accountCanonicalIds: string[],
): Promise<Record<string, LatestUsedCapacity>> {
return {};
}
}

const filterRoles = (
accountName: string,
Expand All @@ -24,7 +61,24 @@ const filterRoles = (
};

const defaultOnChange = () => ({});
export const SelectAccountIAMRole = (props: {

const InternalProvider = ({ children }) => {
return (
<MemoryRouter>
<DataServiceRoleProvider DoNotChangePropsWithRedux={false}>
<AccountsLocationsEndpointsAdapterProvider>
<AccessibleAccountsAdapterProvider
DoNotChangePropsWithEventDispatcher={false}
>
{children}
</AccessibleAccountsAdapterProvider>
</AccountsLocationsEndpointsAdapterProvider>
</DataServiceRoleProvider>
</MemoryRouter>
);
};

export const _SelectAccountIAMRole = (props: {
onChange: (account: Account, role: IAM.Role) => void;
defaultValue?: { accountName: string; roleName: string };
hideAccountRoles?: { accountName: string; roleName: string }[];
Expand All @@ -34,9 +88,25 @@ export const SelectAccountIAMRole = (props: {
hideAccountRoles = [],
defaultValue,
} = props;
const { accounts } = useAccounts();
const { accounts } = useAccounts(noopBasedEventDispatcher);

const accessibleAccountsAdapter = useAccessibleAccountsAdapter();
const metricsAdapter = new NoOppMetricsAdapter();
const accounts2 = useListAccounts({
accessibleAccountsAdapter,
metricsAdapter,
});
console.log('accounts2', accounts2.accounts);
const IAMClient = useIAMClient();

const setAssumedRole = useSetAssumedRole();

const currentAccount = useCurrentAccount();

console.log('currentAccount', currentAccount);

console.log('accounts', accounts);

const defaultAccount =
accounts.find((account) => account.Name === defaultValue?.accountName) ??
null;
Expand All @@ -50,6 +120,7 @@ export const SelectAccountIAMRole = (props: {
enabled: !!IAMClient && !!IAMClient.client && accountName !== '',
};
const roleQueryData = useQuery(listRolesQuery);
// console.log('roleQueryData', roleQueryData);
const roles = filterRoles(
accountName,
roleQueryData?.data?.Roles ?? [],
Expand Down Expand Up @@ -106,3 +177,15 @@ export const SelectAccountIAMRole = (props: {
</Stack>
);
};

export const SelectAccountIAMRole = (props: {
onChange: (account: Account, role: IAM.Role) => void;
defaultValue?: { accountName: string; roleName: string };
hideAccountRoles?: { accountName: string; roleName: string }[];
}) => {
return (
<InternalProvider>
<_SelectAccountIAMRole {...props} />
</InternalProvider>
);
};
Loading

0 comments on commit 7d50fe5

Please sign in to comment.