Files
supabase/apps/studio/data/storage/iceberg-wrapper-create-mutation.ts
Gildas Garcia 43300d43ce chore: consolidate useAPIKeysQuery + getKeys into a single useAPIKeys hook (#46761)
## Problem

- API may return a non-array shape that can crash `getKeys` because of
an hard coded cast
- getting API keys is cumbersome as consumers have to call two functions

## Solution

- consolidate `useAPIKeysQuery` + `getKeys` into a single `useAPIKeys`
hook
- guard `getKeys` so that it doesn't crash if passed a non array value
- update usages

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **Refactor**
* Unified how project API keys are retrieved across the studio,
resulting in more consistent loading/error handling and slight
responsiveness improvements when showing keys and related command
snippets. UI and permissions behavior remain unchanged for end users.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-06-09 15:49:10 +02:00

83 lines
3.1 KiB
TypeScript

import { PermissionAction } from '@supabase/shared-types/out/constants'
import { useS3AccessKeyCreateMutation } from './s3-access-key-create-mutation'
import { WRAPPERS } from '@/components/interfaces/Integrations/Wrappers/Wrappers.constants'
import {
getAnalyticsBucketFDWName,
getAnalyticsBucketS3KeyName,
} from '@/components/interfaces/Storage/AnalyticsBuckets/AnalyticsBucketDetails/AnalyticsBucketDetails.utils'
import {
getCatalogURI,
getConnectionURL,
} from '@/components/interfaces/Storage/StorageSettings/StorageSettings.utils'
import { useAPIKeys } from '@/data/api-keys/api-keys-query'
import { useProjectSettingsV2Query } from '@/data/config/project-settings-v2-query'
import { FDWCreateVariables, useFDWCreateMutation } from '@/data/fdw/fdw-create-mutation'
import { useAsyncCheckPermissions } from '@/hooks/misc/useCheckPermissions'
import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
export const useIcebergWrapperCreateMutation = () => {
const { data: project } = useSelectedProjectQuery()
const { can: canReadAPIKeys } = useAsyncCheckPermissions(PermissionAction.SECRETS_READ, '*')
const { data: apiKeysData } = useAPIKeys(
{ projectRef: project?.ref, reveal: true },
{ enabled: canReadAPIKeys }
)
const { secretKey, serviceKey } = apiKeysData ?? {}
const { data: settings } = useProjectSettingsV2Query({ projectRef: project?.ref })
const protocol = settings?.app_config?.protocol ?? 'https'
const endpoint = settings?.app_config?.storage_endpoint || settings?.app_config?.endpoint
const apiKey = secretKey?.api_key ?? serviceKey?.api_key ?? 'SUPABASE_CLIENT_API_KEY'
const wrapperMeta = WRAPPERS.find((wrapper) => wrapper.name === 'iceberg_wrapper')
const { can: canCreateCredentials } = useAsyncCheckPermissions(
PermissionAction.STORAGE_ADMIN_WRITE,
'*'
)
const { mutateAsync: createS3AccessKey, isPending: isCreatingS3AccessKey } =
useS3AccessKeyCreateMutation()
const { mutateAsync: createFDW, isPending: isCreatingFDW } = useFDWCreateMutation()
const mutateAsync = async ({ bucketName }: { bucketName: string }) => {
const createS3KeyData = await createS3AccessKey({
projectRef: project?.ref,
description: getAnalyticsBucketS3KeyName(bucketName),
})
const wrapperName = getAnalyticsBucketFDWName(bucketName)
const params: FDWCreateVariables = {
projectRef: project?.ref,
connectionString: project?.connectionString,
wrapperMeta: wrapperMeta!,
formState: {
wrapper_name: wrapperName,
server_name: `${wrapperName}_server`,
vault_aws_access_key_id: createS3KeyData?.access_key,
vault_aws_secret_access_key: createS3KeyData?.secret_key,
vault_token: apiKey,
warehouse: bucketName,
's3.endpoint': getConnectionURL(project?.ref ?? '', protocol, endpoint),
catalog_uri: getCatalogURI(project?.ref ?? '', protocol, endpoint),
},
mode: 'skip',
tables: [],
sourceSchema: '',
targetSchema: '',
}
await createFDW(params)
}
return {
mutateAsync,
isPending: isCreatingFDW || isCreatingS3AccessKey,
hasPermission: canCreateCredentials,
}
}