: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import { MakeLogicType, actions, reducers, connect, listeners, kea, 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 type ServiceAccountMode = 'impersonated' | 'key_file'
export interface GoogleCloudServiceAccountSetupModalLogicProps {
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 googleCloudServiceAccountSetupModalLogicValues {
googleCloudServiceAccountIntegration: {
jsonKeyFile: null
privateKey: string | null
privateKeyId: string | null
projectId: string
serviceAccountEmail: string
tokenUri: string | null
}
googleCloudServiceAccountIntegrationAllErrors: Record<string, any>
googleCloudServiceAccountIntegrationChanged: boolean
googleCloudServiceAccountIntegrationErrors: DeepPartialMap<
{
jsonKeyFile: null
privateKey: string | null
privateKeyId: string | null
projectId: string
serviceAccountEmail: string
tokenUri: string | null
},
ValidationErrorType
>
googleCloudServiceAccountIntegrationHasErrors: boolean
googleCloudServiceAccountIntegrationManualErrors: Record<string, any>
googleCloudServiceAccountIntegrationTouched: boolean
googleCloudServiceAccountIntegrationTouches: Record<string, boolean>
googleCloudServiceAccountIntegrationValidationErrors: DeepPartialMap<
{
jsonKeyFile: null
privateKey: string | null
privateKeyId: string | null
projectId: string
serviceAccountEmail: string
tokenUri: string | null
},
ValidationErrorType
>
isGoogleCloudServiceAccountIntegrationSubmitting: boolean
isGoogleCloudServiceAccountIntegrationValid: boolean
serviceAccountMode: ServiceAccountMode
showGoogleCloudServiceAccountIntegrationErrors: boolean
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface googleCloudServiceAccountSetupModalLogicActions {
loadIntegrations: (_?: any) => any // integrationsLogic
resetGoogleCloudServiceAccountIntegration: (values?: {
jsonKeyFile: null
privateKey: string | null
privateKeyId: string | null
projectId: string
serviceAccountEmail: string
tokenUri: string | null
}) => {
values?: {
jsonKeyFile: null
privateKey: string | null
privateKeyId: string | null
projectId: string
serviceAccountEmail: string
tokenUri: string | null
}
}
setGoogleCloudServiceAccountIntegrationManualErrors: (errors: Record<string, any>) => {
errors: Record<string, any>
}
setGoogleCloudServiceAccountIntegrationValue: (
key: FieldName,
value: any
) => {
name: FieldName
value: any
}
setGoogleCloudServiceAccountIntegrationValues: (
values: DeepPartial<{
jsonKeyFile: null
privateKey: string | null
privateKeyId: string | null
projectId: string
serviceAccountEmail: string
tokenUri: string | null
}>
) => {
values: DeepPartial<{
jsonKeyFile: null
privateKey: string | null
privateKeyId: string | null
projectId: string
serviceAccountEmail: string
tokenUri: string | null
}>
}
setServiceAccountMode: (mode: ServiceAccountMode) => {
mode: ServiceAccountMode
}
submitGoogleCloudServiceAccountIntegration: () => {
value: boolean
}
submitGoogleCloudServiceAccountIntegrationFailure: (
error: Error,
errors: Record<string, any>
) => {
error: Error
errors: Record<string, any>
}
submitGoogleCloudServiceAccountIntegrationRequest: (googleCloudServiceAccountIntegration: {
jsonKeyFile: null
privateKey: string | null
privateKeyId: string | null
projectId: string
serviceAccountEmail: string
tokenUri: string | null
}) => {
googleCloudServiceAccountIntegration: {
jsonKeyFile: null
privateKey: string | null
privateKeyId: string | null
projectId: string
serviceAccountEmail: string
tokenUri: string | null
}
}
submitGoogleCloudServiceAccountIntegrationSuccess: (googleCloudServiceAccountIntegration: {
jsonKeyFile: null
privateKey: string | null
privateKeyId: string | null
projectId: string
serviceAccountEmail: string
tokenUri: string | null
}) => {
googleCloudServiceAccountIntegration: {
jsonKeyFile: null
privateKey: string | null
privateKeyId: string | null
projectId: string
serviceAccountEmail: string
tokenUri: string | null
}
}
touchGoogleCloudServiceAccountIntegrationField: (key: string) => {
key: string
}
}
export type googleCloudServiceAccountSetupModalLogicType = MakeLogicType<
googleCloudServiceAccountSetupModalLogicValues,
googleCloudServiceAccountSetupModalLogicActions,
GoogleCloudServiceAccountSetupModalLogicProps
>
export const googleCloudServiceAccountSetupModalLogic = kea<googleCloudServiceAccountSetupModalLogicType>([
path(['integrations', 'googleCloudServiceAccount', 'googleCloudServiceAccountSetupModalLogic']),
props({} as GoogleCloudServiceAccountSetupModalLogicProps),
connect(() => ({
actions: [integrationsLogic, ['loadIntegrations']],
})),
actions({
setServiceAccountMode: (mode: ServiceAccountMode) => ({ mode }),
}),
reducers({
serviceAccountMode: [
'impersonated' as ServiceAccountMode,
{
setServiceAccountMode: (_, { mode }) => mode,
},
],
}),
forms(({ props, actions, values }) => ({
googleCloudServiceAccountIntegration: {
defaults: {
projectId: '',
serviceAccountEmail: '',
jsonKeyFile: null,
privateKey: null as string | null,
privateKeyId: null as string | null,
tokenUri: null as string | null,
},
errors: ({ projectId, serviceAccountEmail }) => ({
projectId: projectId.trim() ? undefined : 'Project ID is required',
serviceAccountEmail: serviceAccountEmail.trim() ? undefined : 'Service account email is required',
}),
submit: async () => {
try {
const { projectId, serviceAccountEmail, privateKey, privateKeyId, tokenUri } =
values.googleCloudServiceAccountIntegration
const integration = await api.integrations.create({
kind: 'google-cloud-service-account',
config: {
project_id: projectId,
service_account_email: serviceAccountEmail,
...(privateKey && {
private_key: privateKey,
private_key_id: privateKeyId,
token_uri: tokenUri,
}),
},
})
actions.loadIntegrations()
lemonToast.success('Google Cloud service account integration created successfully!')
props.onComplete(integration.id)
} catch (error: any) {
lemonToast.error(error.detail || 'Failed to create Google Cloud service account integration')
throw error
}
},
},
})),
listeners(({ values, actions }) => ({
setServiceAccountMode: () => {
actions.setGoogleCloudServiceAccountIntegrationValues({
jsonKeyFile: null,
projectId: '',
serviceAccountEmail: '',
privateKey: null,
privateKeyId: null,
tokenUri: null,
})
},
setGoogleCloudServiceAccountIntegrationValue: async ({ name, value }) => {
const fieldName = Array.isArray(name) ? name[0] : name
if (fieldName === 'jsonKeyFile' && value) {
try {
const loadedFile: string = await new Promise((resolve, reject) => {
const filereader = new FileReader()
filereader.onload = (e) => resolve(e.target?.result as string)
filereader.onerror = (e) => reject(e)
filereader.readAsText(value[0])
})
const loadedKeyFile = JSON.parse(loadedFile)
const { jsonKeyFile, ...remaining } = values.googleCloudServiceAccountIntegration
actions.setGoogleCloudServiceAccountIntegrationValues({
...remaining,
projectId: loadedKeyFile.project_id ?? '',
serviceAccountEmail: loadedKeyFile.client_email ?? '',
privateKey: loadedKeyFile.private_key ?? null,
privateKeyId: loadedKeyFile.private_key_id ?? null,
tokenUri: loadedKeyFile.token_uri ?? null,
})
} catch {
actions.setGoogleCloudServiceAccountIntegrationManualErrors({
jsonKeyFile: 'The JSON key file is not valid',
})
}
}
},
})),
])