-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathConsulBackendPlugin.groovy
38 lines (29 loc) · 1.18 KB
/
ConsulBackendPlugin.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
class ConsulBackendPlugin implements TerraformInitCommandPlugin {
public static String defaultAddress
public static Closure pathPattern
public static void init() {
ConsulBackendPlugin plugin = new ConsulBackendPlugin()
TerraformInitCommand.addPlugin(plugin)
}
@Override
public void apply(TerraformInitCommand command) {
String environment = command.getEnvironment()
String backendPath = getBackendPath(environment)
command.withBackendConfig("path=${backendPath}")
String consulAddress = getConsulAddress()
if (consulAddress) {
command.withBackendConfig("address=${consulAddress}")
}
}
public String getBackendPath(String environment) {
Closure backendPathPattern = pathPattern
if (backendPathPattern == null) {
String repoSlug = Jenkinsfile.instance.getStandardizedRepoSlug()
backendPathPattern = { String env -> "terraform/${repoSlug}_${env}" }
}
return backendPathPattern.call(environment)
}
public String getConsulAddress() {
return defaultAddress ?: Jenkinsfile.instance.getEnv().DEFAULT_CONSUL_ADDRESS
}
}