Skip to content

Commit

Permalink
Merge pull request #349 from duckpuppy/use_traits
Browse files Browse the repository at this point in the history
Create a Pluggable trait to DRY up terraform commands
  • Loading branch information
kmanning committed Mar 23, 2021
2 parents 7ca47df + 00aec41 commit 4a56d34
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 159 deletions.
5 changes: 2 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

* [Issue #344](https://github.com/manheim/terraform-pipeline/issues/344) Add PLAN_ONLY parameter to PlanOnlyPlugin
* **BREAKING CHANGE** This change is a breaking change. Prior to this update, applying the PlanOnlyPlugin would restrict the pipeline to only running `terraform plan`. This update changes behavior to simply providing a `PLAN_ONLY` boolean parameter that can be set to restrict the build behavior. It defaults to `false`.
* [Issue #347](https://github.com/manheim/terraform-pipeline/pull/348)
Feature: TerraformOutputOnlyPlugin - can restrict a pipeline run to
displaying the current state outputs only via new job parameters.
* [Issue #347](https://github.com/manheim/terraform-pipeline/pull/348) Feature: TerraformOutputOnlyPlugin - can restrict a pipeline run to displaying the current state outputs only via new job parameters.
* [Issue #318](https://github.com/manheim/terraform-pipeline/issues/318) Feature: Show environment on confirm command page
* [Issue #349](https://github.com/manheim/terraform-pipeline/pull/349) Extract plugin handling to a `Pluggable` trait to DRY up the existing TerraformCommand classes.

# v5.15

Expand Down
51 changes: 51 additions & 0 deletions src/Pluggable.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* The `Pluggable` trait can be used to add plugin management to a class. It
* takes as a type parameter the plugin type it accepts.
*/
trait Pluggable<T> implements Resettable {
private static plugins = []
private appliedPlugins = []

/**
* Assures that all plugins are applied, and are applied at most once. It
* can be safely called multiple times.
*/
public applyPlugins() {
def remainingPlugins = plugins - appliedPlugins

for (T plugin in remainingPlugins) {
plugin.apply(this)
appliedPlugins << plugin
}
}

/**
* Accepts a plugin of the appropriate type and adds it to the list of plugins.
*
* @param plugin The plugin to add
*/
public static void addPlugin(T plugin) {
plugins << plugin
}

public static void setPlugins(plugins) {
this.plugins = plugins
}

public static getPlugins() {
return plugins
}

/**
* Reset plugins will reset the plugin list to a default set of plugins.
*
* @param defaultPlugins list of plugins to set, default: []
*/
public static void resetPlugins(defaultPlugins = []) {
this.plugins = defaultPlugins.clone()
}

public static void reset() {
this.resetPlugins()
}
}
28 changes: 2 additions & 26 deletions src/TerraformApplyCommand.groovy
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
class TerraformApplyCommand implements TerraformCommand, Resettable {
class TerraformApplyCommand implements TerraformCommand, Pluggable<TerraformApplyCommandPlugin> {
private boolean input = false
private String terraformBinary = "terraform"
private String command = "apply"
String environment
private prefixes = []
private suffixes = []
private args = []
private static plugins = []
private appliedPlugins = []
private String directory
private Closure variablePattern
private Closure mapPattern
Expand Down Expand Up @@ -76,8 +74,7 @@ class TerraformApplyCommand implements TerraformCommand, Resettable {
}

public String toString() {
applyPluginsOnce()

applyPlugins()
def pieces = []
pieces += prefixes
pieces << terraformBinary
Expand All @@ -95,33 +92,12 @@ class TerraformApplyCommand implements TerraformCommand, Resettable {
return pieces.join(' ')
}

private applyPluginsOnce() {
def remainingPlugins = plugins - appliedPlugins

for (TerraformApplyCommandPlugin plugin in remainingPlugins) {
plugin.apply(this)
appliedPlugins << plugin
}
}

public static void addPlugin(TerraformApplyCommandPlugin plugin) {
plugins << plugin
}

public static TerraformApplyCommand instanceFor(String environment) {
return new TerraformApplyCommand(environment)
.withInput(false)
.withArgument("-auto-approve")
}

public static getPlugins() {
return plugins
}

public static reset() {
this.plugins = []
}

public String getEnvironment() {
return environment
}
Expand Down
3 changes: 3 additions & 0 deletions src/TerraformCommand.groovy
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* `TerraformCommand` is an interface that can be implemented by a class that wraps a Terraform command.
*/
interface TerraformCommand {
public String getEnvironment()
}
24 changes: 4 additions & 20 deletions src/TerraformFormatCommand.groovy
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
class TerraformFormatCommand implements Resettable {
private static globalPlugins = []
private appliedPlugins = []
class TerraformFormatCommand implements Pluggable<TerraformFormatCommandPlugin> {
private static boolean check = false
private static boolean recursive = false
private static boolean diff = false
Expand All @@ -10,8 +8,7 @@ class TerraformFormatCommand implements Resettable {
private Closure diffOptionPattern

public String toString() {
applyPluginsOnce()

applyPlugins()
def pattern
def parts = []
parts << 'terraform fmt'
Expand All @@ -29,19 +26,6 @@ class TerraformFormatCommand implements Resettable {
return parts.join(' ')
}

public static addPlugin(TerraformFormatCommandPlugin plugin) {
this.globalPlugins << plugin
}

private applyPluginsOnce() {
def remainingPlugins = globalPlugins - appliedPlugins

for (TerraformFormatCommandPlugin plugin in remainingPlugins) {
plugin.apply(this)
appliedPlugins << plugin
}
}

public static withCheck(newValue = true) {
check = newValue
return this
Expand Down Expand Up @@ -76,10 +60,10 @@ class TerraformFormatCommand implements Resettable {
return this
}

public static reset() {
public static void reset() {
check = false
recursive = false
diff = false
globalPlugins = []
resetPlugins()
}
}
30 changes: 2 additions & 28 deletions src/TerraformInitCommand.groovy
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class TerraformInitCommand implements Resettable {
class TerraformInitCommand implements TerraformCommand, Pluggable<TerraformInitCommandPlugin> {
private boolean input = false
private String terraformBinary = "terraform"
private String command = "init"
Expand All @@ -9,9 +9,6 @@ class TerraformInitCommand implements Resettable {
private boolean doBackend = true
private String directory

private static globalPlugins = []
private appliedPlugins = []

public TerraformInitCommand(String environment) {
this.environment = environment
}
Expand Down Expand Up @@ -47,8 +44,7 @@ class TerraformInitCommand implements Resettable {
}

public String toString() {
applyPluginsOnce()

applyPlugins()
def pieces = []
pieces += prefixes
pieces << terraformBinary
Expand All @@ -72,29 +68,7 @@ class TerraformInitCommand implements Resettable {
return pieces.join(' ')
}

private applyPluginsOnce() {
def remainingPlugins = globalPlugins - appliedPlugins

for (TerraformInitCommandPlugin plugin in remainingPlugins) {
plugin.apply(this)
appliedPlugins << plugin
}
}

public static void addPlugin(TerraformInitCommandPlugin plugin) {
this.globalPlugins << plugin
}

public static TerraformInitCommand instanceFor(String environment) {
return new TerraformInitCommand(environment)
}

public static getPlugins() {
return this.globalPlugins
}

public static reset() {
globalPlugins = []
// This is awkward - what about the applied plugins...?
}
}
33 changes: 4 additions & 29 deletions src/TerraformOutputCommand.groovy
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
class TerraformOutputCommand implements TerraformCommand, Resettable {
private static final DEFAULT_PLUGINS = []
class TerraformOutputCommand implements TerraformCommand, Pluggable<TerraformOutputCommandPlugin> {
private String command = "output"
private boolean json = false
private String redirectFile
private String terraformBinary = "terraform"
String environment
private boolean json = false
private String redirectFile
private String stateFilePath
private static plugins = DEFAULT_PLUGINS.clone()
private appliedPlugins = []

public TerraformOutputCommand(String environment) {
this.environment = environment
Expand All @@ -24,8 +21,7 @@ class TerraformOutputCommand implements TerraformCommand, Resettable {
}

public String toString() {
applyPluginsOnce()

applyPlugins()
def pieces = []
pieces << terraformBinary
pieces << command
Expand All @@ -41,31 +37,10 @@ class TerraformOutputCommand implements TerraformCommand, Resettable {
return pieces.join(' ')
}

private applyPluginsOnce() {
def remainingPlugins = plugins - appliedPlugins

for (TerraformOutputCommandPlugin plugin in remainingPlugins) {
plugin.apply(this)
appliedPlugins << plugin
}
}

public static addPlugin(TerraformOutputCommandPlugin plugin) {
plugins << plugin
}

public static TerraformOutputCommand instanceFor(String environment) {
return new TerraformOutputCommand(environment).withJson(false)
}

public static getPlugins() {
return plugins
}

public static reset() {
this.plugins = DEFAULT_PLUGINS.clone()
}

public String getEnvironment() {
return environment
}
Expand Down
29 changes: 2 additions & 27 deletions src/TerraformPlanCommand.groovy
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
class TerraformPlanCommand implements TerraformCommand, Resettable {
private static final DEFAULT_PLUGINS = []
class TerraformPlanCommand implements TerraformCommand, Pluggable<TerraformPlanCommandPlugin> {
private boolean input = false
private String terraformBinary = "terraform"
private String command = "plan"
String environment
private prefixes = []
private suffixes = []
private arguments = []
private static plugins = DEFAULT_PLUGINS.clone()
private appliedPlugins = []
private String directory
private String errorFile
private Closure variablePattern
Expand Down Expand Up @@ -78,8 +75,7 @@ class TerraformPlanCommand implements TerraformCommand, Resettable {
}

public String toString() {
applyPluginsOnce()

applyPlugins()
def pieces = []
pieces = pieces + prefixes
pieces << terraformBinary
Expand All @@ -103,32 +99,11 @@ class TerraformPlanCommand implements TerraformCommand, Resettable {
return pieces.join(' ')
}

private applyPluginsOnce() {
def remainingPlugins = plugins - appliedPlugins

for (TerraformPlanCommandPlugin plugin in remainingPlugins) {
plugin.apply(this)
appliedPlugins << plugin
}
}

public static addPlugin(TerraformPlanCommandPlugin plugin) {
plugins << plugin
}

public static TerraformPlanCommand instanceFor(String environment) {
return new TerraformPlanCommand(environment)
.withInput(false)
}

public static getPlugins() {
return plugins
}

public static reset() {
this.plugins = DEFAULT_PLUGINS.clone()
}

public String getEnvironment() {
return environment
}
Expand Down
Loading

0 comments on commit 4a56d34

Please sign in to comment.