Skip to content

Commit cdea7d2

Browse files
committed
Split changes into multiple PRs
1 parent a4bf575 commit cdea7d2

9 files changed

+31
-165
lines changed

CHANGELOG.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# Unreleased
22
- [Issue #379](https://github.com/manheim/terraform-pipeline/issues/379) Support Terraform 0.15. Added `-chdir` argument.
33
- [Issue #395](https://github.com/manheim/terraform-pipeline/issues/395) Support Terraform 1.0. Added `-chdir` argument.
4-
- Modified ``PassPlanFilePlugin``
5-
- Additional argument ``withDirectory``. This allows stash and unstash commands to be run in subdirectory.
64

75

86
# v5.18

docs/PassPlanFilePlugin.md

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ Enable this plugin to pass the plan file output to `terraform apply`.
55
This plugin stashes the plan file during the `plan` step.
66
When `apply` is called, the plan file is unstashed and passed as an argument.
77

8-
For stash and unstash commands, you can either specify a directory when initializing the plugin, or it will default to the `./` directory.
9-
If you are using the TerraformDirectoryPlugin, you must specify the same directory to support stash and unstash.
10-
118

129
```
1310
// Jenkinsfile
@@ -18,12 +15,6 @@ Jenkinsfile.init(this, env)
1815
// Pass the plan file to 'terraform apply'
1916
PassPlanFilePlugin.init()
2017
21-
// When using TerraformDirectoryPlugin,
22-
// Pass the plan file to 'terraform apply' using withDirectory
23-
PassPlanFilePlugin.withDirectory('./tf/').init()
24-
TerraformDirectoryPlugin.withDirectory('./tf/').init()
25-
26-
2718
def validate = new TerraformValidateStage()
2819
2920
def destroyQa = new TerraformEnvironmentStage('qa')

src/PassPlanFilePlugin.groovy

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import static TerraformEnvironmentStage.APPLY_COMMAND
33

44
class PassPlanFilePlugin implements TerraformPlanCommandPlugin, TerraformApplyCommandPlugin, TerraformEnvironmentStagePlugin {
55

6-
private static String directory = "./"
7-
86
public static void init() {
97
PassPlanFilePlugin plugin = new PassPlanFilePlugin()
108

@@ -13,11 +11,6 @@ class PassPlanFilePlugin implements TerraformPlanCommandPlugin, TerraformApplyCo
1311
TerraformApplyCommand.addPlugin(plugin)
1412
}
1513

16-
public static withDirectory(String directory) {
17-
PassPlanFilePlugin.directory = directory
18-
return this
19-
}
20-
2114
@Override
2215
public void apply(TerraformEnvironmentStage stage) {
2316
stage.decorate(PLAN_COMMAND, stashPlan(stage.getEnvironment()))
@@ -33,33 +26,25 @@ class PassPlanFilePlugin implements TerraformPlanCommandPlugin, TerraformApplyCo
3326
@Override
3427
public void apply(TerraformApplyCommand command) {
3528
String env = command.getEnvironment()
36-
command.withPlanFile("tfplan-" + env)
29+
command.withDirectory("tfplan-" + env)
3730
}
3831

3932
public Closure stashPlan(String env) {
4033
return { closure ->
4134
closure()
4235
String planFile = "tfplan-" + env
4336
echo "Stashing ${planFile} file"
44-
dir(directory) {
45-
stash name: planFile, includes: planFile
46-
}
37+
stash name: planFile, includes: planFile
4738
}
4839
}
4940

5041
public Closure unstashPlan(String env) {
5142
return { closure ->
5243
String planFile = "tfplan-" + env
5344
echo "Unstashing ${planFile} file"
54-
dir(directory) {
55-
unstash planFile
56-
}
45+
unstash planFile
5746
closure()
5847
}
5948
}
6049

61-
public static reset() {
62-
directory = "./"
63-
}
64-
6550
}

src/TerraformApplyCommand.groovy

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ class TerraformApplyCommand implements TerraformCommand, Resettable {
88
private args = []
99
private String directory
1010
private boolean chdir_flag = false
11-
private String planFile
1211
private Closure variablePattern
1312
private Closure mapPattern
1413
private static plugins = []
@@ -82,11 +81,6 @@ class TerraformApplyCommand implements TerraformCommand, Resettable {
8281
return this
8382
}
8483

85-
public TerraformApplyCommand withPlanFile(String planFile) {
86-
this.planFile = planFile
87-
return this
88-
}
89-
9084
public String toString() {
9185
applyPlugins()
9286
def pieces = []
@@ -100,14 +94,10 @@ class TerraformApplyCommand implements TerraformCommand, Resettable {
10094
pieces << "-input=false"
10195
}
10296
pieces += args
103-
if (directory && !chdir_flag && !planFile) {
97+
if (directory && !chdir_flag) {
10498
pieces << directory
10599
}
106100

107-
if (planFile) {
108-
pieces << planFile
109-
}
110-
111101
pieces += suffixes
112102

113103
return pieces.join(' ')

src/TerraformPlugin.groovy

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
*/
1212
class TerraformPlugin implements TerraformValidateCommandPlugin,
1313
TerraformFormatCommandPlugin,
14-
TerraformInitCommandPlugin,
1514
TerraformPlanCommandPlugin,
1615
TerraformApplyCommandPlugin,
1716
TerraformValidateStagePlugin,
@@ -26,7 +25,6 @@ class TerraformPlugin implements TerraformValidateCommandPlugin,
2625

2726
TerraformValidateCommand.addPlugin(plugin)
2827
TerraformFormatCommand.addPlugin(plugin)
29-
TerraformInitCommand.addPlugin(plugin)
3028
TerraformPlanCommand.addPlugin(plugin)
3129
TerraformApplyCommand.addPlugin(plugin)
3230
TerraformValidateStage.addPlugin(plugin)
@@ -81,7 +79,6 @@ class TerraformPlugin implements TerraformValidateCommandPlugin,
8179

8280
TerraformValidateCommand.reset()
8381
TerraformFormatCommand.reset()
84-
TerraformInitCommand.reset()
8582
TerraformPlanCommand.reset()
8683
TerraformApplyCommand.reset()
8784
TerraformValidateStage.reset()
@@ -97,11 +94,6 @@ class TerraformPlugin implements TerraformValidateCommandPlugin,
9794
applyToCommand(command)
9895
}
9996

100-
@Override
101-
void apply(TerraformInitCommand command) {
102-
applyToCommand(command)
103-
}
104-
10597
@Override
10698
void apply(TerraformPlanCommand command) {
10799
applyToCommand(command)

src/TerraformPluginVersion.groovy

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
abstract class TerraformPluginVersion implements TerraformValidateStagePlugin,
22
TerraformValidateCommandPlugin,
3-
TerraformInitCommandPlugin,
43
TerraformPlanCommandPlugin,
54
TerraformApplyCommandPlugin,
65
TerraformFormatCommandPlugin {
@@ -12,10 +11,6 @@ abstract class TerraformPluginVersion implements TerraformValidateStagePlugin,
1211
public void apply(TerraformValidateCommand command) {
1312
}
1413

15-
@Override
16-
public void apply(TerraformInitCommand command) {
17-
}
18-
1914
@Override
2015
public void apply(TerraformPlanCommand command) {
2116
}

test/PassPlanFilePluginTest.groovy

Lines changed: 27 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -45,73 +45,35 @@ class PassPlanFilePluginTest {
4545

4646
@Nested
4747
public class Apply {
48-
@Nested
49-
public class WithDirectoryProvided {
50-
@Test
51-
void decoratesTheTerraformEnvironmentStage() {
52-
PassPlanFilePlugin.withDirectory('./terraform/')
53-
PassPlanFilePlugin plugin = new PassPlanFilePlugin()
54-
def environment = spy(new TerraformEnvironmentStage())
55-
plugin.apply(environment)
56-
57-
verify(environment, times(1)).decorate(eq(TerraformEnvironmentStage.PLAN_COMMAND), any(Closure.class))
58-
verify(environment, times(1)).decorate(eq(TerraformEnvironmentStage.APPLY_COMMAND), any(Closure.class))
59-
}
60-
61-
@Test
62-
void addsArgumentToTerraformPlan() {
63-
PassPlanFilePlugin.withDirectory('./terraform/')
64-
PassPlanFilePlugin plugin = new PassPlanFilePlugin()
65-
TerraformPlanCommand command = new TerraformPlanCommand("dev")
66-
plugin.apply(command)
67-
68-
String result = command.toString()
69-
assertThat(result, containsString("-out=tfplan-dev"))
70-
}
71-
72-
@Test
73-
void addsArgumentToTerraformApply() {
74-
PassPlanFilePlugin.withDirectory('./terraform/')
75-
PassPlanFilePlugin plugin = new PassPlanFilePlugin()
76-
TerraformApplyCommand command = new TerraformApplyCommand("dev")
77-
plugin.apply(command)
78-
79-
String result = command.toString()
80-
assertThat(result, containsString("tfplan-dev"))
81-
}
48+
49+
@Test
50+
void decoratesTheTerraformEnvironmentStage() {
51+
PassPlanFilePlugin plugin = new PassPlanFilePlugin()
52+
def environment = spy(new TerraformEnvironmentStage())
53+
plugin.apply(environment)
54+
55+
verify(environment, times(1)).decorate(eq(TerraformEnvironmentStage.PLAN_COMMAND), any(Closure.class))
56+
verify(environment, times(1)).decorate(eq(TerraformEnvironmentStage.APPLY_COMMAND), any(Closure.class))
8257
}
8358

84-
@Nested
85-
public class WithoutDirectoryProvided {
86-
@Test
87-
void decoratesTheTerraformEnvironmentStage() {
88-
PassPlanFilePlugin plugin = new PassPlanFilePlugin()
89-
def environment = spy(new TerraformEnvironmentStage())
90-
plugin.apply(environment)
91-
92-
verify(environment, times(1)).decorate(eq(TerraformEnvironmentStage.PLAN_COMMAND), any(Closure.class))
93-
verify(environment, times(1)).decorate(eq(TerraformEnvironmentStage.APPLY_COMMAND), any(Closure.class))
94-
}
95-
96-
@Test
97-
void addsArgumentToTerraformPlan() {
98-
PassPlanFilePlugin plugin = new PassPlanFilePlugin()
99-
TerraformPlanCommand command = new TerraformPlanCommand("dev")
100-
plugin.apply(command)
101-
102-
String result = command.toString()
103-
assertThat(result, containsString("-out=tfplan-dev"))
104-
}
105-
106-
@Test
107-
void addsArgumentToTerraformApply() {
108-
PassPlanFilePlugin plugin = new PassPlanFilePlugin()
109-
TerraformApplyCommand command = new TerraformApplyCommand("dev")
110-
plugin.apply(command)
111-
112-
String result = command.toString()
113-
assertThat(result, containsString("tfplan-dev"))
114-
}
59+
@Test
60+
void addsArgumentToTerraformPlan() {
61+
PassPlanFilePlugin plugin = new PassPlanFilePlugin()
62+
TerraformPlanCommand command = new TerraformPlanCommand("dev")
63+
plugin.apply(command)
64+
65+
String result = command.toString()
66+
assertThat(result, containsString("-out=tfplan-dev"))
67+
}
68+
69+
@Test
70+
void addsArgumentToTerraformApply() {
71+
PassPlanFilePlugin plugin = new PassPlanFilePlugin()
72+
TerraformApplyCommand command = new TerraformApplyCommand("dev")
73+
plugin.apply(command)
74+
75+
String result = command.toString()
76+
assertThat(result, containsString("tfplan-dev"))
11577
}
11678

11779
}

test/TerraformApplyCommandTest.groovy

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -166,37 +166,6 @@ class TerraformApplyCommandTest {
166166
}
167167
}
168168

169-
@Nested
170-
public class WithPlanFile {
171-
@Test
172-
void addsPlanFileArgument() {
173-
def command = new TerraformApplyCommand().withPlanFile("foobar")
174-
175-
def actualCommand = command.toString()
176-
assertThat(actualCommand, endsWith(" foobar"))
177-
}
178-
179-
@Test
180-
void addsPlanFileArgumentInPlaceOfDirectory() {
181-
def command = new TerraformApplyCommand().withDirectory("dir")
182-
.withPlanFile("foobar")
183-
184-
def actualCommand = command.toString()
185-
assertThat(actualCommand, endsWith(" foobar"))
186-
}
187-
188-
@Test
189-
void addsPlanFileArgumentWithChangeDirectoryFlag() {
190-
def command = new TerraformApplyCommand().withDirectory("dir")
191-
.withPlanFile("foobar")
192-
.withChangeDirectoryFlag()
193-
194-
def actualCommand = command.toString()
195-
assertThat(actualCommand, containsString(" -chdir=dir"))
196-
assertThat(actualCommand, endsWith(" foobar"))
197-
}
198-
}
199-
200169
@Nested
201170
public class WithPrefix {
202171
@Test

test/TerraformPluginTest.groovy

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -155,22 +155,6 @@ class TerraformPluginTest {
155155
}
156156
}
157157

158-
@Nested
159-
class ApplyTerraformInitCommand {
160-
@Test
161-
void shouldApplyTheCorrectStrategyToTerraformInitCommand() {
162-
def initCommand = mock(TerraformInitCommand.class)
163-
def strategy = mock(TerraformPluginVersion.class)
164-
def plugin = spy(new TerraformPlugin())
165-
doReturn('someVersion').when(plugin).detectVersion()
166-
doReturn(strategy).when(plugin).strategyFor('someVersion')
167-
168-
plugin.apply(initCommand)
169-
170-
verify(strategy, times(1)).apply(initCommand)
171-
}
172-
}
173-
174158
@Nested
175159
class ApplyTerraformFormatCommand {
176160
@Test

0 commit comments

Comments
 (0)