diff --git a/docs/reference/template-strings.md b/docs/reference/template-strings.md index 06a06e7018..2d26052692 100644 --- a/docs/reference/template-strings.md +++ b/docs/reference/template-strings.md @@ -144,6 +144,22 @@ project: # clusterHostname: my-cluster.example.com # providers: {} + +# A map of all variables defined in the project configuration. +# +# Type: object +# +# Example: +# team-name: bananaramallama +# some-service-endpoint: 'https://someservice.com/api/v2' +# +variables: {} + +# Alias for the variables field. +# +# Type: object +# +var: {} ``` ## Module configuration context @@ -226,6 +242,22 @@ project: # providers: {} +# A map of all variables defined in the project configuration. +# +# Type: object +# +# Example: +# team-name: bananaramallama +# some-service-endpoint: 'https://someservice.com/api/v2' +# +variables: {} + +# Alias for the variables field. +# +# Type: object +# +var: {} + # Retrieve information about modules that are defined in the project. # # Type: object @@ -266,20 +298,4 @@ runtime: # some-key: some value # tasks: {} - -# A map of all variables defined in the project configuration. -# -# Type: object -# -# Example: -# team-name: bananaramallama -# some-service-endpoint: 'https://someservice.com/api/v2' -# -variables: {} - -# Alias for the variables field. -# -# Type: object -# -var: {} ``` \ No newline at end of file diff --git a/garden-service/src/config/config-context.ts b/garden-service/src/config/config-context.ts index 46bf428e49..c698173aa7 100644 --- a/garden-service/src/config/config-context.ts +++ b/garden-service/src/config/config-context.ts @@ -315,7 +315,17 @@ export class ProviderConfigContext extends ProjectConfigContext { ) public providers: Map - constructor(garden: Garden, resolvedProviders: Provider[]) { + @schema( + joiIdentifierMap(joiPrimitive()) + .description("A map of all variables defined in the project configuration.") + .example({ "team-name": "bananaramallama", "some-service-endpoint": "https://someservice.com/api/v2" }) + ) + public variables: PrimitiveMap + + @schema(joiIdentifierMap(joiPrimitive()).description("Alias for the variables field.")) + public var: PrimitiveMap + + constructor(garden: Garden, resolvedProviders: Provider[], variables: PrimitiveMap) { super(garden.artifactsPath) const _this = this @@ -325,6 +335,8 @@ export class ProviderConfigContext extends ProjectConfigContext { this.providers = new Map( resolvedProviders.map((p) => <[string, ProviderContext]>[p.name, new ProviderContext(_this, p)]) ) + + this.var = this.variables = variables } } @@ -494,16 +506,6 @@ export class ModuleConfigContext extends ProviderConfigContext { ) public runtime: RuntimeConfigContext - @schema( - joiIdentifierMap(joiPrimitive()) - .description("A map of all variables defined in the project configuration.") - .example({ "team-name": "bananaramallama", "some-service-endpoint": "https://someservice.com/api/v2" }) - ) - public variables: PrimitiveMap - - @schema(joiIdentifierMap(joiPrimitive()).description("Alias for the variables field.")) - public var: PrimitiveMap - constructor( garden: Garden, resolvedProviders: Provider[], @@ -513,7 +515,7 @@ export class ModuleConfigContext extends ProviderConfigContext { // Otherwise we pass `${runtime.*} template strings through for later resolution. runtimeContext?: RuntimeContext ) { - super(garden, resolvedProviders) + super(garden, resolvedProviders, variables) const _this = this @@ -540,7 +542,5 @@ export class ModuleConfigContext extends ProviderConfigContext { ) this.runtime = new RuntimeConfigContext(this, runtimeContext) - - this.var = this.variables = variables } } diff --git a/garden-service/src/tasks/resolve-provider.ts b/garden-service/src/tasks/resolve-provider.ts index 0e9d90d01b..a4dcd9fd9a 100644 --- a/garden-service/src/tasks/resolve-provider.ts +++ b/garden-service/src/tasks/resolve-provider.ts @@ -92,9 +92,9 @@ export class ResolveProviderTask extends BaseTask { async process(dependencyResults: TaskResults) { const resolvedProviders: Provider[] = Object.values(dependencyResults).map((result) => result && result.output) - const context = new ProviderConfigContext(this.garden, resolvedProviders) + const context = new ProviderConfigContext(this.garden, resolvedProviders, this.garden.variables) - this.log.silly(`Resolving template strings for plugin ${this.config.name}`) + this.log.silly(`Resolving template strings for provider ${this.config.name}`) let resolvedConfig = await resolveTemplateStrings(this.config, context) const providerName = resolvedConfig.name diff --git a/garden-service/test/unit/src/garden.ts b/garden-service/test/unit/src/garden.ts index af41346151..5fc1f74324 100644 --- a/garden-service/test/unit/src/garden.ts +++ b/garden-service/test/unit/src/garden.ts @@ -1722,6 +1722,31 @@ describe("Garden", () => { expect(providerB.config.foo).to.equal("bar") }) + it("should allow providers to reference variables", async () => { + const testA = createGardenPlugin({ + name: "test-a", + }) + + const projectConfig: ProjectConfig = { + apiVersion: "garden.io/v0", + kind: "Project", + name: "test", + path: projectRootA, + defaultEnvironment: "default", + dotIgnoreFiles: defaultDotIgnoreFiles, + environments: [{ name: "default", variables: { "my-variable": "bar" } }], + providers: [{ name: "test-a", foo: "${var.my-variable}" }], + variables: {}, + } + + const plugins = [testA] + const garden = await TestGarden.factory(projectRootA, { config: projectConfig, plugins }) + + const providerB = await garden.resolveProvider("test-a") + + expect(providerB.config.foo).to.equal("bar") + }) + it("should match a dependency to a plugin base", async () => { const baseA = createGardenPlugin({ name: "base-a",