Skip to content

Commit

Permalink
improvement(config): allow provider configs to reference variables
Browse files Browse the repository at this point in the history
  • Loading branch information
edvald committed Nov 29, 2019
1 parent 7f74a46 commit 56175ee
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 32 deletions.
48 changes: 32 additions & 16 deletions docs/reference/template-strings.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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: {}
```
28 changes: 14 additions & 14 deletions garden-service/src/config/config-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,17 @@ export class ProviderConfigContext extends ProjectConfigContext {
)
public providers: Map<string, ProviderContext>

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

Expand All @@ -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
}
}

Expand Down Expand Up @@ -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[],
Expand All @@ -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

Expand All @@ -540,7 +542,5 @@ export class ModuleConfigContext extends ProviderConfigContext {
)

this.runtime = new RuntimeConfigContext(this, runtimeContext)

this.var = this.variables = variables
}
}
4 changes: 2 additions & 2 deletions garden-service/src/tasks/resolve-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions garden-service/test/unit/src/garden.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1702,6 +1702,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",
Expand Down

0 comments on commit 56175ee

Please sign in to comment.