-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
376ebbf
commit 8f973cb
Showing
9 changed files
with
1,258 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import * as React from 'react'; | ||
import { useSafeK8sGet } from '@odf/core/hooks'; | ||
import { useODFNamespaceSelector } from '@odf/core/redux'; | ||
import { StatusBox } from '@odf/shared/generic/status-box'; | ||
import { SecretModel, RouteModel } from '@odf/shared/models'; | ||
import { S3Commands } from '@odf/shared/s3'; | ||
import { SecretKind, K8sResourceKind } from '@odf/shared/types'; | ||
import * as _ from 'lodash-es'; | ||
import { | ||
NOOBAA_ADMIN_SECRET, | ||
NOOBAA_S3_ROUTE, | ||
NOOBAA_ACCESS_KEY_ID, | ||
NOOBAA_SECRET_ACCESS_KEY, | ||
} from '../../constants'; | ||
|
||
type NoobaaS3ContextType = { | ||
noobaaS3: S3Commands; | ||
}; | ||
|
||
type NoobaaS3ProviderType = { | ||
loading?: boolean; | ||
error?: unknown; | ||
}; | ||
|
||
export const NoobaaS3Context = React.createContext<NoobaaS3ContextType>( | ||
{} as NoobaaS3ContextType | ||
); | ||
|
||
// ToDo: In case this provider is needed at too many places, consider applying it to the console's root or use redux instead | ||
export const NoobaaS3Provider: React.FC<NoobaaS3ProviderType> = ({ | ||
children, | ||
loading, | ||
error, | ||
}) => { | ||
const { isODFNsLoaded, odfNsLoadError } = useODFNamespaceSelector(); | ||
|
||
const [secretData, secretLoaded, secretError] = useSafeK8sGet<SecretKind>( | ||
SecretModel, | ||
NOOBAA_ADMIN_SECRET | ||
); | ||
// ToDo: Configure ConsolePlugin proxy instead of using Route | ||
const [routeData, routeLoaded, routeError] = useSafeK8sGet<K8sResourceKind>( | ||
RouteModel, | ||
NOOBAA_S3_ROUTE | ||
); | ||
|
||
const [noobaaS3, noobaaS3Error]: [S3Commands, unknown] = React.useMemo(() => { | ||
if (!_.isEmpty(secretData) && !_.isEmpty(routeData)) { | ||
try { | ||
// ToDo: Remove this once ConsolePlugin proxy is configured | ||
const endpoint = `http://${routeData.spec.host}`; | ||
const accessKeyId = atob(secretData.data?.[NOOBAA_ACCESS_KEY_ID]); | ||
const secretAccessKey = atob( | ||
secretData.data?.[NOOBAA_SECRET_ACCESS_KEY] | ||
); | ||
|
||
return [new S3Commands(endpoint, accessKeyId, secretAccessKey), null]; | ||
} catch (err) { | ||
return [{} as S3Commands, err]; | ||
} | ||
} | ||
return [{} as S3Commands, null]; | ||
}, [secretData, routeData]); | ||
|
||
const allLoaded = | ||
isODFNsLoaded && | ||
secretLoaded && | ||
routeLoaded && | ||
!loading && | ||
!_.isEmpty(noobaaS3); | ||
const anyError = | ||
odfNsLoadError || secretError || routeError || noobaaS3Error || error; | ||
|
||
return allLoaded && !anyError ? ( | ||
<NoobaaS3Context.Provider value={{ noobaaS3 }}> | ||
{children} | ||
</NoobaaS3Context.Provider> | ||
) : ( | ||
<StatusBox loaded={allLoaded} loadError={anyError} /> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export const NOOBAA_ADMIN_SECRET = 'noobaa-admin'; | ||
export const NOOBAA_S3_ROUTE = 's3'; | ||
export const NOOBAA_ACCESS_KEY_ID = 'AWS_ACCESS_KEY_ID'; | ||
export const NOOBAA_SECRET_ACCESS_KEY = 'AWS_SECRET_ACCESS_KEY'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { | ||
S3Client, | ||
ListBucketsCommand, | ||
ListObjectsCommand, | ||
} from '@aws-sdk/client-s3'; | ||
import { ListBuckets, ListObjects } from './types'; | ||
|
||
export class S3Commands { | ||
private s3Client: S3Client; | ||
|
||
constructor(endpoint: string, accessKeyId: string, secretAccessKey: string) { | ||
this.s3Client = new S3Client({ | ||
// "region" is a required parameter for the SDK, using "none" as a workaround | ||
region: 'none', | ||
endpoint, | ||
credentials: { | ||
accessKeyId, | ||
secretAccessKey, | ||
}, | ||
forcePathStyle: true, | ||
}); | ||
} | ||
|
||
// Bucket command members | ||
listBuckets: ListBuckets = (input) => | ||
this.s3Client.send(new ListBucketsCommand(input)); | ||
|
||
// Object command members | ||
listObjects: ListObjects = (input) => | ||
this.s3Client.send(new ListObjectsCommand(input)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './commands'; | ||
export * from './types'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { | ||
ListBucketsCommandInput, | ||
ListBucketsCommandOutput, | ||
ListObjectsCommandInput, | ||
ListObjectsCommandOutput, | ||
} from '@aws-sdk/client-s3'; | ||
|
||
// Bucket command types | ||
export type ListBuckets = ( | ||
input?: ListBucketsCommandInput | ||
) => Promise<ListBucketsCommandOutput>; | ||
|
||
// Object command types | ||
export type ListObjects = ( | ||
input: ListObjectsCommandInput | ||
) => Promise<ListObjectsCommandOutput>; |
Oops, something went wrong.