Skip to content

Commit b52bffe

Browse files
authored
Merge pull request #196 from deploystackio/fix/service-connections
fix(service-connections): add service name transformation support for DigitalOcean
2 parents eb38775 + 24844fc commit b52bffe

4 files changed

Lines changed: 48 additions & 27 deletions

File tree

src/config/service-connections.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@ import { ProviderConnectionConfig } from '../types/service-connections';
88
export const providerConnectionConfigs: Record<string, ProviderConnectionConfig> = {
99
// Render.com - using blueprint fromService syntax
1010
'RND': {
11-
// Uses Render Blueprint's fromService capability
1211
useProviderNativeReferences: true,
1312
implementationType: 'blueprint-reference'
1413
},
1514

16-
// DigitalOcean App Platform - simple service name
15+
// DigitalOcean App Platform - simple service name with transformation
1716
'DOP': {
18-
// DigitalOcean uses simple service names for internal communication
19-
serviceReferenceFormat: '${serviceName}'
17+
serviceNameTransformer: 'digitalOcean'
2018
}
21-
};
19+
};

src/types/service-connections.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,17 @@ export type ConnectionFormat = string;
88
* Provider-specific service connection configuration
99
*/
1010
export interface ProviderConnectionConfig {
11-
// The format template to use for service references
12-
// (can use ${serviceName} as a variable)
11+
// The format template to use for service references
1312
serviceReferenceFormat?: ConnectionFormat;
1413

1514
// Whether to use provider's native service reference mechanism
16-
// instead of string replacement
1715
useProviderNativeReferences?: boolean;
1816

1917
// What type of native implementation to use
2018
implementationType?: 'blueprint-reference' | 'service-discovery';
19+
20+
// Name of the service transformer function to use (e.g., 'digitalOcean')
21+
serviceNameTransformer?: string;
2122
}
2223

2324
/**

src/utils/resolveServiceConnections.ts

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
ResolvedServiceConnection,
55
ProviderConnectionConfig
66
} from '../types/service-connections';
7+
import { getServiceNameTransformer } from './serviceNameTransformers';
78

89
/**
910
* Replace service references in environment variable values based on provider configuration
@@ -33,30 +34,37 @@ export function resolveServiceConnections(
3334
toService,
3435
variables: {}
3536
};
36-
37+
3738
// Process each environment variable that references the target service
3839
for (const varName of environmentVariables) {
39-
if (varName in serviceEnv) {
40-
const originalValue = serviceEnv[varName];
41-
let transformedValue = originalValue;
42-
43-
// For providers that don't use native service references
44-
if (!providerConfig.useProviderNativeReferences) {
45-
// Replace exact service name with the provider-specific format
46-
const serviceNamePattern = new RegExp(`\\b${toService}\\b`, 'g');
47-
const replacementValue = providerConfig.serviceReferenceFormat?.replace('${serviceName}', toService) || toService;
40+
const matchingVarNames = Object.keys(serviceEnv).filter(envKey =>
41+
envKey === varName || envKey.includes(varName)
42+
);
43+
44+
if (matchingVarNames.length > 0) {
45+
for (const matchedVarName of matchingVarNames) {
46+
const originalValue = serviceEnv[matchedVarName];
47+
let transformedValue = originalValue;
4848

49-
transformedValue = transformedValue.replace(serviceNamePattern, replacementValue);
49+
if (!providerConfig.useProviderNativeReferences) {
50+
// Get the appropriate transformer function
51+
const transformerFn = getServiceNameTransformer(providerConfig.serviceNameTransformer);
52+
53+
// Transform the service name
54+
const transformedServiceName = transformerFn(toService);
55+
56+
// Use the transformed name
57+
transformedValue = transformedServiceName;
58+
59+
// Update the environment variable
60+
config.services[fromService].environment[matchedVarName] = transformedValue;
61+
}
5062

51-
// Update the environment variable in the source service
52-
config.services[fromService].environment[varName] = transformedValue;
63+
resolvedConnection.variables[matchedVarName] = {
64+
originalValue,
65+
transformedValue
66+
};
5367
}
54-
55-
// Store the original and transformed values
56-
resolvedConnection.variables[varName] = {
57-
originalValue,
58-
transformedValue
59-
};
6068
}
6169
}
6270

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { digitalOceanParserServiceName } from './digitalOceanParserServiceName';
2+
3+
// Registry of service name transformers
4+
export const serviceNameTransformers: Record<string, (serviceName: string) => string> = {
5+
'digitalOcean': digitalOceanParserServiceName,
6+
// Add other transformers here as needed
7+
'default': (serviceName: string) => serviceName // Identity transformer
8+
};
9+
10+
// Function to get a transformer by name
11+
export function getServiceNameTransformer(transformerName?: string): (serviceName: string) => string {
12+
if (!transformerName) return serviceNameTransformers.default;
13+
return serviceNameTransformers[transformerName] || serviceNameTransformers.default;
14+
}

0 commit comments

Comments
 (0)