Skip to content

Commit

Permalink
feat(pci-object-storage): download user policy json
Browse files Browse the repository at this point in the history
ref: DTCORE-2880
Signed-off-by: LIDRISSI Hamid <[email protected]>
  • Loading branch information
seven-amid committed Nov 28, 2024
1 parent f486ebe commit a52511a
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 40 deletions.
10 changes: 10 additions & 0 deletions packages/manager/apps/pci-object-storage/src/api/data/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,13 @@ export const getS3Credentials = async (
);
return data;
};

export const getUserStoragePolicy = async (
projectId: string,
userId: number,
): Promise<any> => {
const { data } = await v6.get(
`/cloud/project/${projectId}/user/${userId}/policy`,
);
return data;
};

This file was deleted.

3 changes: 3 additions & 0 deletions packages/manager/apps/pci-object-storage/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ export const AVAILABILITY = {
LOCALZONE: 'public-cloud:object-storage:localzone',
'3AZ': 'public-cloud:object-storage:3az',
};

export const DOWNLOAD_FILENAME = 'import.json';
export const DOWNLOAD_TYPE = 'application/json"';
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { Translation, useTranslation } from 'react-i18next';
import { ActionMenu, useNotifications } from '@ovh-ux/manager-react-components';
import { useParams } from 'react-router-dom';
import { ApiError } from '@ovh-ux/manager-core-api';
import { useContext } from 'react';
import { ShellContext } from '@ovh-ux/manager-react-shell-client';
import { getUserStoragePolicy, TUser } from '@/api/data/user';
import { DOWNLOAD_FILENAME, DOWNLOAD_TYPE } from '@/constants';
import { downloadContent } from '@/utils';

export default function ActionsComponent({ user }: { user: TUser }) {
const { t } = useTranslation('objects/users');
const { t: tPciStoragesContainers } = useTranslation(
'pci-storages-containers',
);

const { projectId } = useParams();
const { addSuccess, addError } = useNotifications();
const { tracking } = useContext(ShellContext).shell;

const downloadUserPolicyJson = () => {
tracking?.trackClick({
name: 's3-policies-users::download-json',
type: 'action',
});

getUserStoragePolicy(projectId, user?.id)
.then(({ policy }) => {
downloadContent({
fileContent: policy,
fileName: DOWNLOAD_FILENAME,
downloadType: DOWNLOAD_TYPE,
});

addSuccess(
<Translation ns="objects/users">
{(_t) =>
_t(
'pci_projects_project_storages_containers_users_import_success_message',
{ user: user.username },
)
}
</Translation>,
true,
);
})
.catch((error: ApiError) =>
addError(
<Translation ns="objects/users">
{(_t) =>
_t(
'pci_projects_project_storages_containers_users_import_error_message',
{
message:
error?.response?.data?.message || error?.message || null,
},
)
}
</Translation>,
true,
),
);
};

const items = [
{
id: 0,
label: t('pci_projects_project_storages_containers_users_import_json'),
},
{
id: 1,
label: t('pci_projects_project_storages_containers_users_download_json'),
onclick: downloadUserPolicyJson,
},
{
id: 2,
label: t(
'pci_projects_project_storages_containers_users_download_rclone_file',
),
},
{
id: 3,
label: t('pci_projects_project_storages_containers_users_see_secret_key'),
},
{
id: 4,
label: tPciStoragesContainers(
'pci_projects_project_storages_containers_delete_label',
),
},
];

return <ActionMenu items={items} isCompact />;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
} from '@ovh-ux/manager-react-components';
import { useTranslation } from 'react-i18next';
import { TUser } from '@/api/data/user';
import ActionsComponent from '@/components/Actions.component';
import ActionsComponent from '@/pages/objects/container/users/Actions';

export const useDatagridColumn = () => {
const { t } = useTranslation('objects/users');
Expand Down Expand Up @@ -33,9 +33,9 @@ export const useDatagridColumn = () => {
},
{
id: 'actions',
cell: () => (
cell: (props: TUser) => (
<div className="min-w-16">
<ActionsComponent />
<ActionsComponent user={props} />
</div>
),
label: '',
Expand Down
19 changes: 19 additions & 0 deletions packages/manager/apps/pci-object-storage/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export const downloadContent = ({
fileContent,
fileName,
downloadType,
}: {
fileContent: string;
fileName: string;
downloadType: string;
}) => {
const blob = new Blob([fileContent], { type: downloadType });
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = fileName;

document.body.appendChild(link);
link.click();

document.body.removeChild(link);
};

0 comments on commit a52511a

Please sign in to comment.