Skip to content

Commit

Permalink
remove generate link button
Browse files Browse the repository at this point in the history
  • Loading branch information
yuvalyaron committed Feb 6, 2023
1 parent 5ac2b8c commit 3226302
Showing 1 changed file with 81 additions and 100 deletions.
181 changes: 81 additions & 100 deletions ui/app/src/components/shared/airlock/AirlockRequestFilesSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,131 +14,112 @@ interface AirlockRequestFilesSectionProps {

export const AirlockRequestFilesSection: React.FunctionComponent<AirlockRequestFilesSectionProps> = (props: AirlockRequestFilesSectionProps) => {

const [filesLink, setFilesLink] = useState<string>();
const [cliCommand, setCliCommand] = useState<string>("");

const [filesLinkError, setFilesLinkError] = useState(false);
const [apiFilesLinkError, setApiFilesLinkError] = useState({} as APIError);
const [cliCommandError, setCliCommandError] = useState(false);
const [apiCliCommandError, setApiCliCommandError] = useState({} as APIError);
const [sasUrl, setSasUrl] = useState<string>();
const [sasUrlError, setSasUrlError] = useState(false);
const [apiSasUrlError, setApiSasUrlError] = useState({} as APIError);

const apiCall = useAuthApiCall();

const parseSasUrl = (sasUrl: string) => {
const match = sasUrl.match(/https:\/\/(.*?).blob.core.windows.net\/(.*)\?(.*)$/);
if (!match) {
return
}

return {
StorageAccountName: match[1],
containerName: match[2],
sasToken: match[3]
}
}

const generateFilesLink = useCallback(async () => {
const generateSasUrl = useCallback(async () => {
if (props.request && props.request.workspaceId) {
try {
const linkObject = await apiCall(
`${ApiEndpoint.Workspaces}/${props.request.workspaceId}/${ApiEndpoint.AirlockRequests}/${props.request.id}/${ApiEndpoint.AirlockLink}`,
HttpMethod.Get,
props.workspaceApplicationIdURI
);
setFilesLink(linkObject.containerUrl);
setSasUrl(linkObject.containerUrl);
} catch (err: any) {
err.userMessage = 'Error retrieving storage link';
setApiFilesLinkError(err);
setFilesLinkError(true);
setApiSasUrlError(err);
setSasUrlError(true);
}
}
}, [apiCall, props.request, props.workspaceApplicationIdURI]);

const generateAzureCliCommand = useCallback(async () => {
if (props.request && props.request.workspaceId) {
try {
const linkObject = await apiCall(
`${ApiEndpoint.Workspaces}/${props.request.workspaceId}/${ApiEndpoint.AirlockRequests}/${props.request.id}/${ApiEndpoint.AirlockLink}`,
HttpMethod.Get,
props.workspaceApplicationIdURI
);
const parseSasUrl = (sasUrl: string) => {
const match = sasUrl.match(/https:\/\/(.*?).blob.core.windows.net\/(.*)\?(.*)$/);
if (!match) {
return
}

let containerDetails = parseSasUrl(linkObject.containerUrl)
if (!containerDetails) {
return
}
return {
StorageAccountName: match[1],
containerName: match[2],
sasToken: match[3]
}
};

let cliCommand = "";
if (props.request.status === AirlockRequestStatus.Draft) {
cliCommand = `az storage blob upload --file <~/path/to/file> --name <filename.filetype> --account-name ${containerDetails.StorageAccountName} --type block --container-name ${containerDetails.containerName} --sas-token "${containerDetails.sasToken}"`
} else {
cliCommand = `az storage blob download-batch --destination <~/destination/path/for/file> --source ${containerDetails.containerName} --account-name ${containerDetails.StorageAccountName} --sas-token "${containerDetails.sasToken}"`
}
const getAzureCliCommand = (sasUrl: string) => {
let containerDetails = parseSasUrl(sasUrl)
if (!containerDetails) {
return '';
}

setCliCommand(cliCommand);
} catch (err: any) {
err.userMessage = 'Error generating CLI command';
setApiCliCommandError(err);
setCliCommandError(true);
}
let cliCommand = "";
if (props.request.status === AirlockRequestStatus.Draft) {
cliCommand = `az storage blob upload --file <~/path/to/file> --name <filename.filetype> --account-name ${containerDetails.StorageAccountName} --type block --container-name ${containerDetails.containerName} --sas-token "${containerDetails.sasToken}"`
} else {
cliCommand = `az storage blob download-batch --destination <~/destination/path/for/file> --source ${containerDetails.containerName} --account-name ${containerDetails.StorageAccountName} --sas-token "${containerDetails.sasToken}"`
}
}, [apiCall, props.request, props.workspaceApplicationIdURI]);

return cliCommand;
};

useEffect(() => {
generateAzureCliCommand()
}, [generateAzureCliCommand]);
generateSasUrl()
}, [generateSasUrl]);

return (
<Pivot aria-label="Storage options">
<PivotItem headerText="SAS URL">
<Stack>
<Stack.Item style={{ paddingTop: '10px', paddingBottom: '10px' }}>
{
props.request.status === AirlockRequestStatus.Draft
? <small>Generate a storage container SAS URL to upload your request file.</small>
: <small>Generate a storage container SAS URL to view the request file.</small>
}
<Stack horizontal styles={{ root: { alignItems: 'center', paddingTop: '7px' } }}>
<Stack.Item grow>
<TextField readOnly value={filesLink} defaultValue="Click generate to create a link" />
</Stack.Item>
<Stack>
<Pivot aria-label="Storage options">
<PivotItem headerText="SAS URL">
<Stack>
<Stack.Item style={{ paddingTop: '10px', paddingBottom: '10px' }}>
{
filesLink ? <PrimaryButton
iconProps={{ iconName: 'copy' }}
styles={{ root: { minWidth: '40px' } }}
onClick={() => { navigator.clipboard.writeText(filesLink) }}
/> : <PrimaryButton onClick={() => { setFilesLinkError(false); generateFilesLink() }}>Generate</PrimaryButton>
props.request.status === AirlockRequestStatus.Draft
? <small>Use the storage container SAS URL to upload your request file.</small>
: <small>Use the storage container SAS URL to view the request file.</small>
}
</Stack>
</Stack.Item>
{
props.request.status === AirlockRequestStatus.Draft && <MessageBar messageBarType={MessageBarType.info}>
Please upload a single file. Only single-file imports (including zip files) are supported.
</MessageBar>
}
{
filesLinkError && <ExceptionLayout e={apiFilesLinkError} />
}
</Stack>
</PivotItem>
<PivotItem headerText="CLI">
<Stack>
<Stack.Item style={{ paddingTop: '10px', paddingBottom: '10px' }}>
<small>Use Azure command-line interface (Azure CLI) to interact with the storage container.</small>
<hr style={{ border: "1px solid #faf9f8", borderRadius: "1px" }} />
</Stack.Item>
<Stack.Item style={{ paddingTop: '10px' }}>
<CliCommand
command={cliCommand}
title={props.request.status === AirlockRequestStatus.Draft ? "Upload a file to the storage container" : "Download the file from the storage container"}
isLoading={!cliCommand && !cliCommandError}
/>
</Stack.Item>
{
cliCommandError && <ExceptionLayout e={apiCliCommandError} />
}
</Stack>
</PivotItem>
</Pivot>
<Stack horizontal styles={{ root: { alignItems: 'center', paddingTop: '7px' } }}>
<Stack.Item grow>
<TextField readOnly value={sasUrl} />
</Stack.Item>
{
<PrimaryButton
iconProps={{ iconName: 'copy' }}
styles={{ root: { minWidth: '40px' } }}
onClick={() => { sasUrl && navigator.clipboard.writeText(sasUrl) }}
/>
}
</Stack>
</Stack.Item>
{
props.request.status === AirlockRequestStatus.Draft && <MessageBar messageBarType={MessageBarType.info}>
Please upload a single file. Only single-file imports (including zip files) are supported.
</MessageBar>
}
</Stack>
</PivotItem>
<PivotItem headerText="CLI">
<Stack>
<Stack.Item style={{ paddingTop: '10px', paddingBottom: '10px' }}>
<small>Use Azure command-line interface (Azure CLI) to interact with the storage container.</small>
<hr style={{ border: "1px solid #faf9f8", borderRadius: "1px" }} />
</Stack.Item>
<Stack.Item style={{ paddingTop: '10px' }}>
<CliCommand
command={sasUrl ? getAzureCliCommand(sasUrl) : ''}
title={props.request.status === AirlockRequestStatus.Draft ? "Upload a file to the storage container" : "Download the file from the storage container"}
isLoading={!sasUrl && !sasUrlError}
/>
</Stack.Item>
</Stack>
</PivotItem>
</Pivot>
{
sasUrlError && <ExceptionLayout e={apiSasUrlError} />
}
</Stack>
);
};

0 comments on commit 3226302

Please sign in to comment.