-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for so caled interpolation of values in a properties map
- Loading branch information
Showing
6 changed files
with
192 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
connection.project.dir=../spring-aws-serverless | ||
connection.project.dir=../s3deploy | ||
eclipse.preferences.version=1 |
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
60 changes: 60 additions & 0 deletions
60
src/main/groovy/se/solrike/cloudformation/ParameterResolver.groovy
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,60 @@ | ||
package se.solrike.cloudformation | ||
|
||
/** | ||
* @author Lucas Persson | ||
*/ | ||
public class ParameterResolver { | ||
|
||
public static Map<String, String> resolve(Map<String, String> parameters, ClassLoader parentClassLoader) { | ||
Binding binding = new Binding() | ||
|
||
Properties p = new Properties() | ||
p.putAll(parameters) | ||
// ConfigSlurper will create the properties as beans in case a dot notation is used | ||
ConfigObject conf = new ConfigSlurper().parse(p) | ||
conf.each { key, value -> | ||
binding.setVariable(key, value) | ||
} | ||
GroovyShell shell = new GroovyShell(parentClassLoader, binding) | ||
|
||
// multipass so nested references (in two hops) can be used. | ||
resolveValues(shell, binding) | ||
resolveValues(shell, binding) | ||
|
||
Map flatMap = [:] | ||
flatternMap(binding.getVariables(), flatMap, null) | ||
|
||
return flatMap | ||
} | ||
|
||
static void resolveValues(GroovyShell shell, Binding binding) { | ||
visitMap(binding.getVariables(), { | ||
it.value = shell.evaluate("\"$it.value\"") | ||
}) | ||
} | ||
|
||
static void visitMap(Map map, Closure action) { | ||
map.each { | ||
if (it.value instanceof Map) { | ||
visitMap(it.value, action) | ||
} | ||
else { | ||
action.call(it) | ||
} | ||
} | ||
} | ||
|
||
// flattern the map and re-create the dot notation or the keys | ||
static void flatternMap(Map source, Map target, String prefix) { | ||
source.each { | ||
String key = prefix ? prefix + '.' + it.key : it.key | ||
if (it.value instanceof Map) { | ||
flatternMap(it.value, target, key) | ||
} | ||
else { | ||
target[key] = it.value | ||
} | ||
} | ||
} | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
src/main/groovy/se/solrike/cloudformation/PrintEnviromentParametersTask.groovy
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,57 @@ | ||
package se.solrike.cloudformation | ||
|
||
import org.gradle.api.DefaultTask | ||
import org.gradle.api.provider.MapProperty | ||
import org.gradle.api.provider.Property | ||
import org.gradle.api.tasks.Input | ||
import org.gradle.api.tasks.Internal | ||
import org.gradle.api.tasks.Optional | ||
import org.gradle.api.tasks.TaskAction | ||
|
||
import software.amazon.awssdk.core.waiters.WaiterResponse | ||
import software.amazon.awssdk.services.cloudformation.CloudFormationClient | ||
import software.amazon.awssdk.services.cloudformation.model.Capability | ||
import software.amazon.awssdk.services.cloudformation.model.CloudFormationException | ||
import software.amazon.awssdk.services.cloudformation.model.CreateStackRequest | ||
import software.amazon.awssdk.services.cloudformation.model.CreateStackResponse | ||
import software.amazon.awssdk.services.cloudformation.model.DeleteStackResponse | ||
import software.amazon.awssdk.services.cloudformation.model.DescribeStacksResponse | ||
import software.amazon.awssdk.services.cloudformation.model.Parameter | ||
import software.amazon.awssdk.services.cloudformation.model.Tag | ||
import software.amazon.awssdk.services.cloudformation.model.UpdateStackRequest | ||
import software.amazon.awssdk.services.cloudformation.model.UpdateStackResponse | ||
|
||
/** | ||
* Task to print the resolved parameters for an environment. | ||
* <p> | ||
* The task will first resolve parameters using Groovy's evaluation support. | ||
* | ||
* @author Lucas Persson | ||
*/ | ||
abstract class PrintEnviromentParametersTask extends DefaultTask { | ||
|
||
/** | ||
* The build script's class loader so that this task can find e.g. classes defined in the build.gradle. | ||
*/ | ||
@Input | ||
@Optional | ||
public abstract Property<ClassLoader> getParentClassLoader() | ||
|
||
/** | ||
* The parameters. | ||
*/ | ||
@Input | ||
public abstract MapProperty<String, String> getParameters() | ||
|
||
|
||
|
||
@TaskAction | ||
void execute() { | ||
Map resolvedParameters = ParameterResolver.resolve(getParameters().get(), | ||
getParentClassLoader().getOrElse(getClass().getClassLoader())) | ||
|
||
resolvedParameters.sort { it.key }.each {key, value -> | ||
println "$key : $value" | ||
} | ||
} | ||
} |