Skip to content

Commit

Permalink
Merge pull request #4 from AzBuilder/terraform-json
Browse files Browse the repository at this point in the history
Add support for -json flag in terraform plan, apply and destroy
  • Loading branch information
alfespa17 authored Jul 14, 2021
2 parents d947a37 + 6e0e589 commit 6a4af34
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ target/
.terraform/

Main.java
.DS_Store
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Simply add the following dependency to your project's `pom.xml` will enable you
<dependency>
<groupId>org.azbuilder.terraform</groupId>
<artifactId>terraform-client</artifactId>
<version>0.3.0</version>
<version>0.4.0</version>
</dependency>
```

Expand Down Expand Up @@ -81,7 +81,7 @@ Let's still use the terraform file `storage.tf` under `/some/local/path/` folder
<dependency>
<groupId>org.azbuilder.terraform</groupId>
<artifactId>terraform-spring-boot-starter</artifactId>
<version>0.3.0</version>
<version>0.4.0</version>
</dependency>
```

Expand All @@ -91,6 +91,14 @@ You can also enable or disable terraform output colors using the `application.pr
org.azbuilder.terraform.flags.enableColor=true
```

You can also enable or disable terraform output in json format using the `application.properties` or `application-${spring.profiles.active}.properties`:

```
org.azbuilder.terraform.flags.jsonOutput=true
```

> This feature is only supported in terraform >= 0.15.3
The final step is to let the Spring framework wire up everything in your spring boot application:

Example 1: `Not thread safe`
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<revision>0.3.0</revision>
<revision>0.4.0</revision>
<maven.deploy.skip>true</maven.deploy.skip>
</properties>

Expand Down
2 changes: 1 addition & 1 deletion terraform-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<revision>0.3.0</revision>
<revision>0.4.0</revision>
<maven.deploy.skip>false</maven.deploy.skip>
<okhttp.version>4.9.1</okhttp.version>
<commons-io.version>2.8.0</commons-io.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class TerraformClient implements AutoCloseable {
private File workingDirectory;
private boolean inheritIO;
private boolean showColor;
private boolean jsonOutput;
private String terraformVersion;
private String backendConfig;

Expand Down Expand Up @@ -162,6 +163,7 @@ private ProcessLauncher getTerraformLauncher(TerraformCommand command) throws IO
launcher.setEnvironmentVariable(entry.getKey(), entry.getValue());
}

ComparableVersion version = new ComparableVersion(this.terraformVersion);
switch (command) {
case init:
if (this.backendConfig != null) {
Expand All @@ -182,9 +184,8 @@ private ProcessLauncher getTerraformLauncher(TerraformCommand command) throws IO
launcher.appendCommands("-auto-approve");
break;
case destroy:
ComparableVersion version0_15_0 = new ComparableVersion("0.15.0"); //https://www.terraform.io/upgrade-guides/0-15.html#other-minor-command-line-behavior-changes
ComparableVersion version = new ComparableVersion(this.terraformVersion);
if (version.compareTo(version0_15_0) < 0)
//https://www.terraform.io/upgrade-guides/0-15.html#other-minor-command-line-behavior-changes
if (version.compareTo(new ComparableVersion("0.15.0")) < 0)
launcher.appendCommands("-force");
else
launcher.appendCommands("-auto-approve");
Expand All @@ -193,6 +194,16 @@ private ProcessLauncher getTerraformLauncher(TerraformCommand command) throws IO
if (!this.showColor)
launcher.appendCommands("-no-color");

//https://www.terraform.io/docs/internals/machine-readable-ui.html
if (this.jsonOutput && version.compareTo(new ComparableVersion("0.15.2")) > 0)
switch(command) {
case plan:
case apply:
case destroy:
launcher.appendCommands("-json");
break;
}

launcher.setOutputListener(this.getOutputListener());
launcher.setErrorListener(this.getErrorListener());
return launcher;
Expand All @@ -209,6 +220,7 @@ private ProcessLauncher getTerraformLauncher(String terraformVersion, File worki
launcher.setEnvironmentVariable(entry.getKey(), entry.getValue());
}

ComparableVersion version = new ComparableVersion(terraformVersion);
switch (command) {
case init:
if (terraformBackendConfigFileName != null) {
Expand All @@ -227,9 +239,8 @@ private ProcessLauncher getTerraformLauncher(String terraformVersion, File worki
launcher.appendCommands("-auto-approve");
break;
case destroy:
ComparableVersion version0_15_0 = new ComparableVersion("0.15.0"); //https://www.terraform.io/upgrade-guides/0-15.html#other-minor-command-line-behavior-changes
ComparableVersion version = new ComparableVersion(terraformVersion);
if (version.compareTo(version0_15_0) < 0)
//https://www.terraform.io/upgrade-guides/0-15.html#other-minor-command-line-behavior-changes
if (version.compareTo(new ComparableVersion("0.15.0")) < 0)
launcher.appendCommands("-force");
else
launcher.appendCommands("-auto-approve");
Expand All @@ -239,6 +250,16 @@ private ProcessLauncher getTerraformLauncher(String terraformVersion, File worki
if (!this.showColor)
launcher.appendCommands("-no-color");

//https://www.terraform.io/docs/internals/machine-readable-ui.html
if (this.jsonOutput && version.compareTo(new ComparableVersion("0.15.2")) > 0)
switch(command) {
case plan:
case apply:
case destroy:
launcher.appendCommands("-json");
break;
}

launcher.setOutputListener(outputListener);
launcher.setErrorListener(errorListener);
return launcher;
Expand Down
2 changes: 1 addition & 1 deletion terraform-spring-boot-autoconfigure/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<revision>0.3.0</revision>
<revision>0.4.0</revision>
<maven.deploy.skip>false</maven.deploy.skip>
<lombok.version>1.18.20</lombok.version>
</properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ public class TerraformAutoConfiguration {

@Bean
public TerraformClient terraformClient(@NonNull TerraformProperties tfProperties) {
if (tfProperties.isEnableColor()) {
return TerraformClient.builder().showColor(true).build();
}else{
return TerraformClient.builder().showColor(false).build();
}
return TerraformClient.builder()
.showColor(tfProperties.isEnableColor())
.jsonOutput(tfProperties.isJsonOutput())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@
public class TerraformProperties {

private boolean enableColor;
private boolean jsonOutput;
}
2 changes: 1 addition & 1 deletion terraform-spring-boot-samples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<revision>0.3.0</revision>
<revision>0.4.0</revision>
<maven.deploy.skip>true</maven.deploy.skip>
</properties>

Expand Down
2 changes: 1 addition & 1 deletion terraform-spring-boot-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<revision>0.3.0</revision>
<revision>0.4.0</revision>
<maven.deploy.skip>false</maven.deploy.skip>
</properties>

Expand Down

0 comments on commit 6a4af34

Please sign in to comment.