use of com.walmartlabs.concord.plugins.terraform.backend.BackendFactoryV1 in project concord-plugins by walmartlabs.
the class TerraformTaskTest method test.
@Test
@SuppressWarnings("unchecked")
public void test() throws Exception {
Map<String, Object> args = baseArguments(workDir, dstDir, Action.PLAN.name());
args.put(TaskConstants.VARS_FILES, varFiles());
args.put(TaskConstants.EXTRA_VARS_KEY, extraVars());
args.put(TaskConstants.GIT_SSH_KEY, gitSsh());
Context ctx = new MockContext(args);
backendManager = new BackendFactoryV1(ctx, lockService, objectStorage);
TerraformTask t = new TerraformTask(secretService, lockService, objectStorage, dependencyManager);
t.execute(ctx);
// ---
Map<String, Object> result = (Map<String, Object>) ctx.getVariable(TaskConstants.RESULT_KEY);
assertTrue((boolean) result.get("ok"));
assertNotNull(result.get("planPath"));
// ---
verify(lockService, times(1)).projectLock(any(), any());
verify(lockService, times(1)).projectUnlock(any(), any());
// ---
System.out.println("===================================================================================");
args = baseArguments(workDir, dstDir, Action.APPLY.name());
args.put(TaskConstants.DESTROY_KEY, true);
args.put(TaskConstants.PLAN_KEY, result.get("planPath"));
ctx = new MockContext(args);
t.execute(ctx);
result = (Map<String, Object>) ctx.getVariable(TaskConstants.RESULT_KEY);
System.out.println(result);
//
// Check the output contains our two variables populated with values from our var files
//
// Outputs:
//
// message = hello!
// name_from_varfile0 = bob
// time_from_varfile1 = now
//
assertTrue(((String) result.get("output")).contains("name_from_varfile0 = bob"));
assertTrue(((String) result.get("output")).contains("time_from_varfile1 = now"));
// ---
System.out.println("===================================================================================");
//
// Simulate having some outputs being created in the terraform state. The output action will not work
// correctly without outputs present in the terraform state.
//
String terraformStateWithOutputs = responseTemplate("terraform.tfstate");
wireMockRule.stubFor(get("/test").willReturn(aResponse().withBody(terraformStateWithOutputs).withStatus(200)));
args = baseArguments(workDir, dstDir, Action.OUTPUT.name());
ctx = new MockContext(args);
t.execute(ctx);
}
use of com.walmartlabs.concord.plugins.terraform.backend.BackendFactoryV1 in project concord-plugins by walmartlabs.
the class TerraformTask method execute.
public void execute(Context ctx) throws Exception {
String instanceId = (String) ctx.getVariable(com.walmartlabs.concord.sdk.Constants.Context.TX_ID_KEY);
Map<String, Object> cfg = createCfg(ctx);
Path workDir = ContextUtils.getWorkDir(ctx);
boolean debug = MapUtils.get(cfg, TaskConstants.DEBUG_KEY, false, Boolean.class);
Action action = getAction(cfg);
// configure the state backend and populate the environment with necessary parameters
Backend backend = new BackendFactoryV1(ctx, lockService, objectStorage).getBackend(cfg);
Map<String, String> env = getEnv(cfg, backend);
// configure the environment to support Terraform's git modules using Concord Secrets for authentication
GitSshWrapper.SecretProvider secretProvider = (orgName, secretName, password) -> {
Map<String, String> result = secretService.exportKeyAsFile(ctx, instanceId, workDir.toAbsolutePath().toString(), orgName, secretName, password);
return workDir.resolve(result.get("private"));
};
GitSshWrapper gitSshWrapper = GitSshWrapper.createFrom(secretProvider, workDir, cfg, debug);
Map<String, String> baseEnv = gitSshWrapper.updateEnv(workDir, new HashMap<>());
// configure the Terraform's binary
TerraformBinaryResolver binaryResolver = new TerraformBinaryResolver(cfg, workDir, debug, url -> dependencyManager.resolve(URI.create(url)));
Terraform terraform = new Terraform(binaryResolver, debug, baseEnv);
if (debug) {
terraform.exec(workDir, "version", "version");
}
try {
TerraformActionResult result = TerraformTaskCommon.execute(terraform, action, backend, workDir, cfg, env);
ctx.setVariable(TaskConstants.RESULT_KEY, objectMapper.convertValue(result, Map.class));
} finally {
gitSshWrapper.cleanup();
}
}
Aggregations