forked from manheim/terraform-pipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParameterStoreBuildWrapperPlugin.groovy
92 lines (74 loc) · 2.93 KB
/
ParameterStoreBuildWrapperPlugin.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import static TerraformValidateStage.ALL
import static TerraformEnvironmentStage.PLAN
import static TerraformEnvironmentStage.APPLY
class ParameterStoreBuildWrapperPlugin implements TerraformValidateStagePlugin, TerraformEnvironmentStagePlugin {
private static globalPathPattern
private static List globalParameterOptions = []
private static defaultPathPattern = { options -> "/${options['organization']}/${options['repoName']}/${options['environment']}/" }
public static void init() {
TerraformEnvironmentStage.addPlugin(new ParameterStoreBuildWrapperPlugin())
TerraformValidateStage.addPlugin(new ParameterStoreBuildWrapperPlugin())
}
public static withPathPattern(Closure newPathPattern) {
globalPathPattern = newPathPattern
return this
}
public static withGlobalParameter(String path, Map options = [:]) {
globalParameterOptions << [path: path] + options
return this
}
@Override
public void apply(TerraformValidateStage stage) {
globalParameterOptions.each { gp ->
stage.decorate(ALL, addParameterStoreBuildWrapper(gp))
}
}
@Override
public void apply(TerraformEnvironmentStage stage) {
def environment = stage.getEnvironment()
List options = getParameterOptions(environment)
options.each { option ->
stage.decorate(PLAN, addParameterStoreBuildWrapper(option))
stage.decorate(APPLY, addParameterStoreBuildWrapper(option))
}
}
List getParameterOptions(String environment) {
List options = []
options.add(getEnvironmentParameterOptions(environment))
options.addAll(getGlobalParameterOptions())
return options
}
List getGlobalParameterOptions() {
return globalParameterOptions
}
Map getEnvironmentParameterOptions(String environment) {
return [
path: pathForEnvironment(environment),
credentialsId: "${environment.toUpperCase()}_PARAMETER_STORE_ACCESS"
]
}
String pathForEnvironment(String environment) {
String organization = Jenkinsfile.instance.getOrganization()
String repoName = Jenkinsfile.instance.getRepoName()
def patternOptions = [ environment: environment,
repoName: repoName,
organization: organization ]
def pathPattern = globalPathPattern ?: defaultPathPattern
return pathPattern(patternOptions)
}
public Closure addParameterStoreBuildWrapper(Map options = [:]) {
def Map defaultOptions = [
naming: 'basename'
]
def parameterStoreOptions = defaultOptions + options
return { closure ->
withAWSParameterStore(parameterStoreOptions) {
closure()
}
}
}
public static reset() {
globalPathPattern = null
globalParameterOptions = []
}
}