Skip to content

Commit

Permalink
fix(terraform): allow map outputs from modules
Browse files Browse the repository at this point in the history
Fixes #1947
  • Loading branch information
edvald committed Aug 3, 2020
1 parent 7215f94 commit 4dcb70a
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 12 deletions.
2 changes: 1 addition & 1 deletion garden-service/src/config/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ export const joiVariables = () =>
.object()
.pattern(variableNameRegex, joi.alternatives(joiPrimitive(), joi.link("..."), joi.array().items(joi.link("..."))))
.default(() => ({}))
.unknown(false)
.unknown(true)
.description("Key/value map. " + joiVariablesDescription)

export const joiEnvVars = () =>
Expand Down
7 changes: 2 additions & 5 deletions garden-service/src/plugins/terraform/terraform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { pathExists } from "fs-extra"
import { createGardenPlugin } from "../../types/plugin/plugin"
import { getEnvironmentStatus, prepareEnvironment } from "./init"
import { providerConfigBaseSchema, ProviderConfig, Provider } from "../../config/provider"
import { joi } from "../../config/common"
import { joi, joiVariables } from "../../config/common"
import { dedent } from "../../util/string"
import { supportedVersions, defaultTerraformVersion, terraformCliSpecs } from "./cli"
import { ConfigureProviderParams, ConfigureProviderResult } from "../../types/plugin/provider/configureProvider"
Expand Down Expand Up @@ -88,10 +88,7 @@ export const gardenPlugin = createGardenPlugin({
See the [Terraform guide](${DOCS_BASE_URL}/advanced/terraform) for a high-level introduction to the \`terraform\` provider.
`,
serviceOutputsSchema: joi
.object()
.pattern(/.+/, joi.any())
.description("A map of all the outputs defined in the Terraform stack."),
serviceOutputsSchema: joiVariables().description("A map of all the outputs defined in the Terraform stack."),
schema,
handlers: {
suggestModules: async ({ name, path }: SuggestModulesParams): Promise<SuggestModulesResult> => {
Expand Down
15 changes: 10 additions & 5 deletions garden-service/src/types/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@

import normalizeUrl from "normalize-url"
import { format } from "url"
import { joiUserIdentifier, joi, joiIdentifier, joiArray, PrimitiveMap, joiPrimitive } from "../config/common"
import {
joiUserIdentifier,
joi,
joiIdentifier,
joiArray,
PrimitiveMap,
joiPrimitive,
joiVariables,
} from "../config/common"
import { Module } from "./module"
import { ServiceConfig, serviceConfigSchema } from "../config/service"
import dedent = require("dedent")
Expand Down Expand Up @@ -231,10 +239,7 @@ export const serviceStatusSchema = () =>
.allow("")
.description("Latest status message of the service (if any)."),
lastError: joi.string().description("Latest error status message of the service (if any)."),
outputs: joi
.object()
.pattern(/.+/, joiPrimitive())
.description("A map of primitive values, output from the service."),
outputs: joiVariables().description("A map of values output from the service."),
runningReplicas: joi.number().description("How many replicas of the service are currently running."),
state: joi
.string()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ output "test-file-path" {

output "my-output" {
value = "input: ${var.my-variable}"
}
}

output "map-output" {
value = map("first", "second")
}
29 changes: 29 additions & 0 deletions garden-service/test/unit/src/plugins/terraform/terraform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { LogLevel } from "../../../../../src/logger/log-node"
import { ConfigGraph } from "../../../../../src/config-graph"
import { TerraformProvider } from "../../../../../src/plugins/terraform/terraform"
import { DeployTask } from "../../../../../src/tasks/deploy"
import { emptyRuntimeContext } from "../../../../../src/runtime-context"

describe("Terraform provider", () => {
const testRoot = getDataDir("test-projects", "terraform-provider")
Expand Down Expand Up @@ -257,6 +258,34 @@ describe("Terraform module type", () => {
expect(result["task.test-task"]!.output.log).to.equal("input: foo")
expect(result["task.test-task"]!.output.outputs.log).to.equal("input: foo")
})

it("should should return outputs with the service status", async () => {
const provider = await garden.resolveProvider(garden.log, "terraform")
const ctx = garden.getPluginContext(provider)
const applyCommand = findByName(terraformCommands, "apply-module")!
await applyCommand.handler({
ctx,
args: ["tf", "-auto-approve", "-input=false"],
log: garden.log,
modules: graph.getModules(),
})

const actions = await garden.getActionRouter()
const status = await actions.getServiceStatus({
service: graph.getService("tf"),
hotReload: false,
log: garden.log,
runtimeContext: emptyRuntimeContext,
})

expect(status.outputs).to.eql({
"map-output": {
first: "second",
},
"my-output": "input: foo",
"test-file-path": "./test.log",
})
})
})

context("autoApply=true", () => {
Expand Down

0 comments on commit 4dcb70a

Please sign in to comment.