Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

9.1 KB
Raw Blame
import { MakeLogicType, connect, kea, listeners, path, props } from 'kea'
import { forms } from 'kea-forms'
import type { DeepPartial, DeepPartialMap, FieldName, ValidationErrorType } from 'kea-forms'

import api from 'lib/api'
import { integrationsLogic } from 'lib/integrations/integrationsLogic'
import { lemonToast } from 'lib/lemon-ui/LemonToast'

import { IntegrationType } from '~/types'

export interface SnowflakeSetupModalLogicProps {
    isOpen: boolean
    integration?: IntegrationType | null
    onComplete: (integrationId?: number) => void
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface snowflakeSetupModalLogicValues {
    isSnowflakeIntegrationSubmitting: boolean
    isSnowflakeIntegrationValid: boolean
    showSnowflakeIntegrationErrors: boolean
    snowflakeIntegration: {
        account: string
        authentication_type: string
        name: string
        password: string
        private_key: string
        private_key_file: File[] | null
        private_key_passphrase: string
        user: string
    }
    snowflakeIntegrationAllErrors: Record<string, any>
    snowflakeIntegrationChanged: boolean
    snowflakeIntegrationErrors: DeepPartialMap<
        {
            account: string
            authentication_type: string
            name: string
            password: string
            private_key: string
            private_key_file: File[] | null
            private_key_passphrase: string
            user: string
        },
        ValidationErrorType
    >
    snowflakeIntegrationHasErrors: boolean
    snowflakeIntegrationManualErrors: Record<string, any>
    snowflakeIntegrationTouched: boolean
    snowflakeIntegrationTouches: Record<string, boolean>
    snowflakeIntegrationValidationErrors: DeepPartialMap<
        {
            account: string
            authentication_type: string
            name: string
            password: string
            private_key: string
            private_key_file: File[] | null
            private_key_passphrase: string
            user: string
        },
        ValidationErrorType
    >
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface snowflakeSetupModalLogicActions {
    loadIntegrations: (_?: any) => any // integrationsLogic
    resetSnowflakeIntegration: (values?: {
        account: string
        authentication_type: string
        name: string
        password: string
        private_key: string
        private_key_file: File[] | null
        private_key_passphrase: string
        user: string
    }) => {
        values?: {
            account: string
            authentication_type: string
            name: string
            password: string
            private_key: string
            private_key_file: File[] | null
            private_key_passphrase: string
            user: string
        }
    }
    setSnowflakeIntegrationManualErrors: (errors: Record<string, any>) => {
        errors: Record<string, any>
    }
    setSnowflakeIntegrationValue: (
        key: FieldName,
        value: any
    ) => {
        name: FieldName
        value: any
    }
    setSnowflakeIntegrationValues: (
        values: DeepPartial<{
            account: string
            authentication_type: string
            name: string
            password: string
            private_key: string
            private_key_file: File[] | null
            private_key_passphrase: string
            user: string
        }>
    ) => {
        values: DeepPartial<{
            account: string
            authentication_type: string
            name: string
            password: string
            private_key: string
            private_key_file: File[] | null
            private_key_passphrase: string
            user: string
        }>
    }
    submitSnowflakeIntegration: () => {
        value: boolean
    }
    submitSnowflakeIntegrationFailure: (
        error: Error,
        errors: Record<string, any>
    ) => {
        error: Error
        errors: Record<string, any>
    }
    submitSnowflakeIntegrationRequest: (snowflakeIntegration: {
        account: string
        authentication_type: string
        name: string
        password: string
        private_key: string
        private_key_file: File[] | null
        private_key_passphrase: string
        user: string
    }) => {
        snowflakeIntegration: {
            account: string
            authentication_type: string
            name: string
            password: string
            private_key: string
            private_key_file: File[] | null
            private_key_passphrase: string
            user: string
        }
    }
    submitSnowflakeIntegrationSuccess: (snowflakeIntegration: {
        account: string
        authentication_type: string
        name: string
        password: string
        private_key: string
        private_key_file: File[] | null
        private_key_passphrase: string
        user: string
    }) => {
        snowflakeIntegration: {
            account: string
            authentication_type: string
            name: string
            password: string
            private_key: string
            private_key_file: File[] | null
            private_key_passphrase: string
            user: string
        }
    }
    touchSnowflakeIntegrationField: (key: string) => {
        key: string
    }
}

export type snowflakeSetupModalLogicType = MakeLogicType<
    snowflakeSetupModalLogicValues,
    snowflakeSetupModalLogicActions,
    SnowflakeSetupModalLogicProps
>

export const snowflakeSetupModalLogic = kea<snowflakeSetupModalLogicType>([
    path(['integrations', 'snowflake', 'snowflakeSetupModalLogic']),
    props({} as SnowflakeSetupModalLogicProps),
    connect(() => ({
        actions: [integrationsLogic, ['loadIntegrations']],
    })),
    forms(({ props, actions, values }) => ({
        snowflakeIntegration: {
            defaults: {
                name: '',
                account: '',
                user: '',
                authentication_type: 'keypair',
                password: '',
                private_key: '',
                private_key_passphrase: '',
                // Bound to the file input only — its contents are read into `private_key`, it isn't sent.
                private_key_file: null as File[] | null,
            },
            errors: ({ name, account, user, authentication_type, password, private_key }) => ({
                name: name.trim() ? undefined : 'Name is required',
                account: account.trim() ? undefined : 'Account is required',
                user: user.trim() ? undefined : 'User is required',
                password: authentication_type === 'password' && !password.trim() ? 'Password is required' : undefined,
                private_key:
                    authentication_type === 'keypair' && !private_key.trim() ? 'Private key is required' : undefined,
            }),
            submit: async () => {
                const { snowflakeIntegration } = values
                try {
                    const integration = await api.integrations.create({
                        kind: 'snowflake',
                        config: {
                            name: snowflakeIntegration.name,
                            account: snowflakeIntegration.account,
                            user: snowflakeIntegration.user,
                            authentication_type: snowflakeIntegration.authentication_type,
                            password: snowflakeIntegration.password,
                            private_key: snowflakeIntegration.private_key,
                            private_key_passphrase: snowflakeIntegration.private_key_passphrase,
                        },
                    })
                    actions.loadIntegrations()
                    lemonToast.success('Snowflake connection created successfully!')
                    props.onComplete(integration.id)
                } catch (error: any) {
                    lemonToast.error(error.detail || 'Failed to create Snowflake connection')
                    throw error
                }
            },
        },
    })),
    listeners(({ actions }) => ({
        setSnowflakeIntegrationValue: async ({ name, value }) => {
            // When a key file is uploaded, read its contents into the private_key field so users
            // following Snowflake's key-pair workflow can upload the generated file instead of pasting.
            const fieldName = Array.isArray(name) ? name[0] : name
            if (fieldName === 'private_key_file' && value?.[0]) {
                try {
                    const contents: string = await new Promise((resolve, reject) => {
                        const reader = new FileReader()
                        reader.onload = (e) => resolve(e.target?.result as string)
                        reader.onerror = (e) => reject(e)
                        reader.readAsText(value[0])
                    })
                    actions.setSnowflakeIntegrationValue('private_key', contents)
                } catch {
                    actions.setSnowflakeIntegrationManualErrors({ private_key_file: 'Could not read the key file' })
                }
            }
        },
    })),
])