-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #229 from jleopold28/support_target
Support target for plan/apply
- Loading branch information
Showing
5 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
## [TargetPlugin](../src/TargetPlugin.groovy) | ||
|
||
Enable this plugin to run plan/apply on selective resource targets | ||
|
||
``` | ||
// Jenkinsfile | ||
@Library(['terraform-pipeline']) _ | ||
Jenkinsfile.init(this, env) | ||
TargetPlugin.init() // Optionally limit plan/apply to specific targets | ||
def validate = new TerraformValidateStage() | ||
def deployQa = new TerraformEnvironmentStage('qa') | ||
def deployUat = new TerraformEnvironmentStage('uat') | ||
def deployProd = new TerraformEnvironmentStage('prod') | ||
// Pipeline can now be built with "Build with Parameters" | ||
// New 'target' option in parameter list can limit plan/apply to specific targets | ||
// By default, builds should skip the 'target' option, and build as-normal | ||
validate.then(deployQa) | ||
.then(deployUat) | ||
.then(deployProd) | ||
.build() | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import static TerraformEnvironmentStage.ALL | ||
|
||
class TargetPlugin implements TerraformPlanCommandPlugin, TerraformApplyCommandPlugin, TerraformEnvironmentStagePlugin { | ||
public static void init() { | ||
TargetPlugin plugin = new TargetPlugin() | ||
|
||
TerraformPlanCommand.addPlugin(plugin) | ||
TerraformApplyCommand.addPlugin(plugin) | ||
TerraformEnvironmentStage.addPlugin(plugin) | ||
} | ||
|
||
@Override | ||
public void apply(TerraformPlanCommand command) { | ||
def targets = Jenkinsfile.instance.getEnv().RESOURCE_TARGETS ?: '' | ||
targets.split(',').each { item -> command.withArgument("-target ${item}") } | ||
} | ||
|
||
@Override | ||
public void apply(TerraformApplyCommand command) { | ||
def targets = Jenkinsfile.instance.getEnv().RESOURCE_TARGETS ?: '' | ||
targets.split(',').each { item -> command.withArgument("-target ${item}") } | ||
} | ||
|
||
@Override | ||
public void apply(TerraformEnvironmentStage stage) { | ||
stage.decorate(ALL, addBuildParams()) | ||
} | ||
|
||
public static Closure addBuildParams() { | ||
return { closure -> | ||
def params = [ | ||
string(name: 'RESOURCE_TARGETS', defaultValue: '', description: 'comma-separated list of resource addresses to pass to plan and apply "-target=" parameters'), | ||
] | ||
def props = [ | ||
parameters(params) | ||
] | ||
properties(props) | ||
|
||
closure() | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import static org.hamcrest.Matchers.containsString | ||
import static org.hamcrest.Matchers.hasItem | ||
import static org.hamcrest.Matchers.instanceOf | ||
import static org.junit.Assert.assertThat | ||
import static org.mockito.Mockito.spy; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.any; | ||
import static org.mockito.Mockito.eq; | ||
import static org.mockito.Mockito.times; | ||
import static TerraformEnvironmentStage.ALL; | ||
import static org.mockito.Mockito.when; | ||
import static org.mockito.Mockito.mock; | ||
|
||
import org.junit.Test | ||
import org.junit.Before | ||
import org.junit.After | ||
import org.junit.runner.RunWith | ||
import de.bechte.junit.runners.context.HierarchicalContextRunner | ||
|
||
@RunWith(HierarchicalContextRunner.class) | ||
class TargetPluginTest { | ||
@Before | ||
void resetJenkinsEnv() { | ||
Jenkinsfile.instance = mock(Jenkinsfile.class) | ||
when(Jenkinsfile.instance.getEnv()).thenReturn([:]) | ||
} | ||
|
||
private configureJenkins(Map config = [:]) { | ||
Jenkinsfile.instance = mock(Jenkinsfile.class) | ||
when(Jenkinsfile.instance.getEnv()).thenReturn(config.env ?: [:]) | ||
} | ||
|
||
public class Init { | ||
@After | ||
void resetPlugins() { | ||
TerraformPlanCommand.resetPlugins() | ||
TerraformApplyCommand.resetPlugins() | ||
TerraformEnvironmentStage.resetPlugins() | ||
} | ||
|
||
@Test | ||
void modifiesTerraformPlanCommand() { | ||
TargetPlugin.init() | ||
|
||
Collection actualPlugins = TerraformPlanCommand.getPlugins() | ||
assertThat(actualPlugins, hasItem(instanceOf(TargetPlugin.class))) | ||
} | ||
|
||
@Test | ||
void modifiesTerraformApplyCommand() { | ||
TargetPlugin.init() | ||
|
||
Collection actualPlugins = TerraformApplyCommand.getPlugins() | ||
assertThat(actualPlugins, hasItem(instanceOf(TargetPlugin.class))) | ||
} | ||
|
||
@Test | ||
void modifiesTerraformEnvironmentStageCommand() { | ||
TargetPlugin.init() | ||
|
||
Collection actualPlugins = TerraformEnvironmentStage.getPlugins() | ||
assertThat(actualPlugins, hasItem(instanceOf(TargetPlugin.class))) | ||
} | ||
} | ||
|
||
public class Apply { | ||
|
||
@Test | ||
void addsTargetArgumentToTerraformPlan() { | ||
TargetPlugin plugin = new TargetPlugin() | ||
TerraformPlanCommand command = new TerraformPlanCommand() | ||
configureJenkins(env: [ | ||
'RESOURCE_TARGETS': 'aws_dynamodb_table.test-table-2,aws_dynamodb_table.test-table-3' | ||
]) | ||
|
||
plugin.apply(command) | ||
|
||
String result = command.toString() | ||
assertThat(result, containsString(" -target aws_dynamodb_table.test-table-2 -target aws_dynamodb_table.test-table-3")) | ||
} | ||
|
||
@Test | ||
void addsTargetArgumentToTerraformApply() { | ||
TargetPlugin plugin = new TargetPlugin() | ||
TerraformApplyCommand command = new TerraformApplyCommand() | ||
configureJenkins(env: [ | ||
'RESOURCE_TARGETS': 'aws_dynamodb_table.test-table-2,aws_dynamodb_table.test-table-3' | ||
]) | ||
|
||
plugin.apply(command) | ||
|
||
String result = command.toString() | ||
assertThat(result, containsString(" -target aws_dynamodb_table.test-table-2 -target aws_dynamodb_table.test-table-3")) | ||
} | ||
|
||
@Test | ||
void decoratesTheTerraformEnvironmentStage() { | ||
TargetPlugin plugin = new TargetPlugin() | ||
def environment = spy(new TerraformEnvironmentStage()) | ||
configureJenkins(env: [ | ||
'RESOURCE_TARGETS': 'aws_dynamodb_table.test-table-2,aws_dynamodb_table.test-table-3' | ||
]) | ||
|
||
plugin.apply(environment) | ||
|
||
verify(environment, times(1)).decorate(eq(TerraformEnvironmentStage.ALL), any(Closure.class)) | ||
} | ||
} | ||
} |