Skip to content

Commit

Permalink
fix(terraform): make sure terraform init is run before workspace list
Browse files Browse the repository at this point in the history
Fixes #2190
  • Loading branch information
edvald authored and thsig committed Jan 20, 2021
1 parent bc2de8f commit 9950c1e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
15 changes: 12 additions & 3 deletions core/src/plugins/terraform/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export async function tfValidate(params: TerraformParams) {
) {
// We need to run `terraform init` and retry validation
log.debug("Initializing Terraform")
await terraform(ctx, provider).exec({ log, args: ["init"], cwd: root, timeoutSec: 300 })
await tfInit(params)

const retryRes = await terraform(ctx, provider).json({
log,
Expand Down Expand Up @@ -175,10 +175,10 @@ export async function getStackStatus(params: TerraformParamsWithVariables): Prom

export async function applyStack(params: TerraformParamsWithVariables) {
const { ctx, log, provider, root, variables } = params
const args = ["apply", "-auto-approve", "-input=false", ...(await prepareVariables(root, variables))]

await setWorkspace(params)

const args = ["apply", "-auto-approve", "-input=false", ...(await prepareVariables(root, variables))]
const proc = await terraform(ctx, provider).spawn({ log, args, cwd: root })

const statusLine = log.info("→ Applying Terraform stack...")
Expand Down Expand Up @@ -241,7 +241,12 @@ export async function prepareVariables(targetDir: string, variables?: object): P
/**
* Lists the created workspaces for the given Terraform `root`, and returns which one is selected.
*/
export async function getWorkspaces({ ctx, log, provider, root }: TerraformParams) {
export async function getWorkspaces(params: TerraformParams) {
const { ctx, log, provider, root } = params

// Must in some cases ensure init is complete before listing workspaces
await tfInit(params)

const res = await terraform(ctx, provider).stdout({ args: ["workspace", "list"], cwd: root, log })
let selected = "default"

Expand Down Expand Up @@ -287,3 +292,7 @@ export async function setWorkspace(params: TerraformParamsWithWorkspace) {
await terraform(ctx, provider).stdout({ args: ["workspace", "new", workspace], cwd: root, log })
}
}

export async function tfInit({ ctx, log, provider, root }: TerraformParams) {
await terraform(ctx, provider).exec({ log, args: ["init"], cwd: root, timeoutSec: 600 })
}
4 changes: 4 additions & 0 deletions core/test/unit/src/plugins/terraform/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { terraform } from "../../../../../src/plugins/terraform/cli"
describe("Terraform common", () => {
const testRoot = getDataDir("test-projects", "terraform-provider")
const root = join(testRoot, "tf")
const terraformDirPath = join(root, ".terraform")
const stateDirPath = join(root, "terraform.tfstate.d")
const testFilePath = join(root, "test.log")

Expand All @@ -29,6 +30,9 @@ describe("Terraform common", () => {
let provider: TerraformProvider

async function reset() {
if (await pathExists(terraformDirPath)) {
await remove(terraformDirPath)
}
if (await pathExists(testFilePath)) {
await remove(testFilePath)
}
Expand Down

0 comments on commit 9950c1e

Please sign in to comment.