diff --git a/src/config/service-connections.ts b/src/config/service-connections.ts index 1fbdb98..0a7c785 100644 --- a/src/config/service-connections.ts +++ b/src/config/service-connections.ts @@ -8,14 +8,12 @@ import { ProviderConnectionConfig } from '../types/service-connections'; export const providerConnectionConfigs: Record = { // Render.com - using blueprint fromService syntax 'RND': { - // Uses Render Blueprint's fromService capability useProviderNativeReferences: true, implementationType: 'blueprint-reference' }, - // DigitalOcean App Platform - simple service name + // DigitalOcean App Platform - simple service name with transformation 'DOP': { - // DigitalOcean uses simple service names for internal communication - serviceReferenceFormat: '${serviceName}' + serviceNameTransformer: 'digitalOcean' } -}; +}; \ No newline at end of file diff --git a/src/types/service-connections.ts b/src/types/service-connections.ts index 571dfb0..e9f0982 100644 --- a/src/types/service-connections.ts +++ b/src/types/service-connections.ts @@ -8,16 +8,17 @@ export type ConnectionFormat = string; * Provider-specific service connection configuration */ export interface ProviderConnectionConfig { - // The format template to use for service references - // (can use ${serviceName} as a variable) + // The format template to use for service references serviceReferenceFormat?: ConnectionFormat; // Whether to use provider's native service reference mechanism - // instead of string replacement useProviderNativeReferences?: boolean; // What type of native implementation to use implementationType?: 'blueprint-reference' | 'service-discovery'; + + // Name of the service transformer function to use (e.g., 'digitalOcean') + serviceNameTransformer?: string; } /** diff --git a/src/utils/resolveServiceConnections.ts b/src/utils/resolveServiceConnections.ts index b278306..f77a77f 100644 --- a/src/utils/resolveServiceConnections.ts +++ b/src/utils/resolveServiceConnections.ts @@ -4,6 +4,7 @@ import { ResolvedServiceConnection, ProviderConnectionConfig } from '../types/service-connections'; +import { getServiceNameTransformer } from './serviceNameTransformers'; /** * Replace service references in environment variable values based on provider configuration @@ -33,30 +34,37 @@ export function resolveServiceConnections( toService, variables: {} }; - + // Process each environment variable that references the target service for (const varName of environmentVariables) { - if (varName in serviceEnv) { - const originalValue = serviceEnv[varName]; - let transformedValue = originalValue; - - // For providers that don't use native service references - if (!providerConfig.useProviderNativeReferences) { - // Replace exact service name with the provider-specific format - const serviceNamePattern = new RegExp(`\\b${toService}\\b`, 'g'); - const replacementValue = providerConfig.serviceReferenceFormat?.replace('${serviceName}', toService) || toService; + const matchingVarNames = Object.keys(serviceEnv).filter(envKey => + envKey === varName || envKey.includes(varName) + ); + + if (matchingVarNames.length > 0) { + for (const matchedVarName of matchingVarNames) { + const originalValue = serviceEnv[matchedVarName]; + let transformedValue = originalValue; - transformedValue = transformedValue.replace(serviceNamePattern, replacementValue); + if (!providerConfig.useProviderNativeReferences) { + // Get the appropriate transformer function + const transformerFn = getServiceNameTransformer(providerConfig.serviceNameTransformer); + + // Transform the service name + const transformedServiceName = transformerFn(toService); + + // Use the transformed name + transformedValue = transformedServiceName; + + // Update the environment variable + config.services[fromService].environment[matchedVarName] = transformedValue; + } - // Update the environment variable in the source service - config.services[fromService].environment[varName] = transformedValue; + resolvedConnection.variables[matchedVarName] = { + originalValue, + transformedValue + }; } - - // Store the original and transformed values - resolvedConnection.variables[varName] = { - originalValue, - transformedValue - }; } } diff --git a/src/utils/serviceNameTransformers.ts b/src/utils/serviceNameTransformers.ts new file mode 100644 index 0000000..dce0640 --- /dev/null +++ b/src/utils/serviceNameTransformers.ts @@ -0,0 +1,14 @@ +import { digitalOceanParserServiceName } from './digitalOceanParserServiceName'; + +// Registry of service name transformers +export const serviceNameTransformers: Record string> = { + 'digitalOcean': digitalOceanParserServiceName, + // Add other transformers here as needed + 'default': (serviceName: string) => serviceName // Identity transformer +}; + +// Function to get a transformer by name +export function getServiceNameTransformer(transformerName?: string): (serviceName: string) => string { + if (!transformerName) return serviceNameTransformers.default; + return serviceNameTransformers[transformerName] || serviceNameTransformers.default; +}