Search in sources :

Example 1 with BackendFactoryV1

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);
}
Also used : BackendFactoryV1(com.walmartlabs.concord.plugins.terraform.backend.BackendFactoryV1) Test(org.junit.Test)

Example 2 with BackendFactoryV1

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();
    }
}
Also used : Path(java.nio.file.Path) com.walmartlabs.concord.sdk(com.walmartlabs.concord.sdk) BackendFactoryV1(com.walmartlabs.concord.plugins.terraform.backend.BackendFactoryV1) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HashMap(java.util.HashMap) Inject(javax.inject.Inject) TerraformTaskCommon(com.walmartlabs.concord.plugins.terraform.TerraformTaskCommon) Map(java.util.Map) TerraformActionResult(com.walmartlabs.concord.plugins.terraform.actions.TerraformActionResult) Named(javax.inject.Named) URI(java.net.URI) Path(java.nio.file.Path) Collections(java.util.Collections) Backend(com.walmartlabs.concord.plugins.terraform.backend.Backend) Backend(com.walmartlabs.concord.plugins.terraform.backend.Backend) TerraformActionResult(com.walmartlabs.concord.plugins.terraform.actions.TerraformActionResult) BackendFactoryV1(com.walmartlabs.concord.plugins.terraform.backend.BackendFactoryV1) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

BackendFactoryV1 (com.walmartlabs.concord.plugins.terraform.backend.BackendFactoryV1)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 TerraformTaskCommon (com.walmartlabs.concord.plugins.terraform.TerraformTaskCommon)1 TerraformActionResult (com.walmartlabs.concord.plugins.terraform.actions.TerraformActionResult)1 Backend (com.walmartlabs.concord.plugins.terraform.backend.Backend)1 com.walmartlabs.concord.sdk (com.walmartlabs.concord.sdk)1 URI (java.net.URI)1 Path (java.nio.file.Path)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Inject (javax.inject.Inject)1 Named (javax.inject.Named)1 Test (org.junit.Test)1