AzBuilder / terraform-spring-boot

Spring Boot Starter for Terraform
MIT License
2 stars 6 forks source link

Support to handle -var-file parameters #33

Closed alfespa17 closed 1 year ago

alfespa17 commented 1 year ago

Adding varFile parameters in the different methods.

package org.terrakube.terraform;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.concurrent.ExecutionException;

public final class Main {

    public static void main(String[] args) {
        withoutVarFile();
        withVarFile();
    }

    private static void withoutVarFile(){
        String terraformVersion="1.3.9";
        TerraformClient terraformClient = TerraformClient
                .builder()
                .jsonOutput(false)
                .showColor(true)
                .build();
        try {
            File workingDirectory = new File("/home/user/somepath");

            TerraformProcessData terraformProcessData = TerraformProcessData.builder()
                    .terraformVersion(terraformVersion)
                    .workingDirectory(workingDirectory)
                    .build();

            terraformClient.init(terraformProcessData, System.out::println, System.err::println).get();

            System.out.println("Terraform Plan");
            terraformClient.plan(terraformProcessData, System.out::println, System.err::println).get();

            System.out.println("Terraform Apply");
            terraformClient.apply(terraformProcessData, System.out::println, System.err::println).get();

            System.out.println("Terraform PLan Destroy");

            terraformClient.planDestroy(terraformProcessData, System.out::println, System.err::println).get();

            System.out.println("Terraform Apply to Destroy");
            terraformClient.apply(terraformProcessData, System.out::println, System.err::println).get();

        } catch (IOException | ExecutionException | InterruptedException exception) {
            exception.printStackTrace();
        }
    }

    private static void withVarFile(){
        String terraformVersion="1.3.9";
        String varFile="testing.tfvars";
        TerraformClient terraformClient = TerraformClient
                .builder()
                .jsonOutput(false)
                .showColor(true)
                .build();
        try {
            File workingDirectory = new File("/home/user/somepath");

            TerraformProcessData terraformProcessData = TerraformProcessData.builder()
                    .terraformVersion(terraformVersion)
                    .workingDirectory(workingDirectory)
                    .build();

            terraformClient.init(terraformProcessData, System.out::println, System.err::println).get();

            terraformProcessData = TerraformProcessData.builder()
                    .terraformVersion(terraformVersion)
                    .workingDirectory(workingDirectory)
                    .varFileName(varFile)
                    .build();

            System.out.println("Terraform Plan");
            terraformClient.plan(terraformProcessData, System.out::println, System.err::println).get();

            System.out.println("Terraform Apply");
            terraformClient.apply(terraformProcessData, System.out::println, System.err::println).get();

            System.out.println("Terraform PLan Destroy");
            terraformClient.planDestroy(terraformProcessData,System.out::println, System.err::println).get();

            System.out.println("Terraform Apply to Destroy");
            terraformClient.apply(terraformProcessData, System.out::println, System.err::println).get();

        } catch (IOException | ExecutionException | InterruptedException exception) {
            exception.printStackTrace();
        }
    }
}

Fixes #32

sonarcloud[bot] commented 1 year ago

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 1 Code Smell

0.0% 0.0% Coverage
0.0% 0.0% Duplication