-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathCredentialsPlugin.groovy
75 lines (59 loc) · 2.43 KB
/
CredentialsPlugin.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
class CredentialsPlugin implements BuildStagePlugin, RegressionStagePlugin, TerraformEnvironmentStagePlugin, TerraformValidateStagePlugin, Resettable {
private static bindings = []
public static void init() {
def plugin = new CredentialsPlugin()
BuildStage.addPlugin(plugin)
RegressionStage.addPlugin(plugin)
TerraformEnvironmentStage.addPlugin(plugin)
TerraformValidateStage.addPlugin(plugin)
}
public static withBinding(Closure binding) {
bindings << binding
return this
}
// Deprecated: Remove this with Issue #404 and the next major release
public static withBuildCredentials(Map options = [:], String credentialsId) {
Map optionsWithDefaults = populateDefaults(options, credentialsId)
bindings << { usernamePassword(optionsWithDefaults) }
return this
}
@Override
public void apply(BuildStage buildStage) {
buildStage.decorate(addBuildCredentials())
}
@Override
public void apply(RegressionStage regressionStage) {
regressionStage.decorate(addBuildCredentials())
}
@Override
public void apply(TerraformEnvironmentStage environmentStage) {
environmentStage.decorate(addBuildCredentials())
}
@Override
public void apply(TerraformValidateStage validateStage) {
validateStage.decorate(addBuildCredentials())
}
private addBuildCredentials() {
return { innerClosure ->
def workflowScript = delegate
def appliedBindings = getBindings().collect { it -> it.delegate = workflowScript; it() }
withCredentials(appliedBindings, innerClosure)
}
}
public static Map populateDefaults(Map options = [:], String credentialsId) {
def credentialsOptions = options.clone()
credentialsOptions['credentialsId'] = credentialsId
credentialsOptions['usernameVariable'] = credentialsOptions['usernameVariable'] ?: "${toEnvironmentVariable(credentialsId)}_USERNAME".toString()
credentialsOptions['passwordVariable'] = credentialsOptions['passwordVariable'] ?: "${toEnvironmentVariable(credentialsId)}_PASSWORD".toString()
return credentialsOptions
}
public static String toEnvironmentVariable(String value) {
value.toUpperCase().replaceAll('-', '_')
}
public static getBindings() {
return bindings
}
public static void reset() {
bindings = []
}
}