You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by sv...@apache.org on 2016/02/16 11:59:45 UTC
[1/2] brooklyn-library git commit: add ability to pass DB creation
script as a url to a freemarker template
Repository: brooklyn-library
Updated Branches:
refs/heads/master a283d8dc3 -> 9fdf1dbf6
add ability to pass DB creation script as a url to a freemarker template
Project: http://git-wip-us.apache.org/repos/asf/brooklyn-library/repo
Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-library/commit/1bc5db09
Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-library/tree/1bc5db09
Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-library/diff/1bc5db09
Branch: refs/heads/master
Commit: 1bc5db092d1cd637850fe33d08ed970f1473515e
Parents: 02abbab
Author: John McCabe <jo...@johnmccabe.net>
Authored: Tue Feb 16 10:01:30 2016 +0000
Committer: John McCabe <jo...@johnmccabe.net>
Committed: Tue Feb 16 10:01:30 2016 +0000
----------------------------------------------------------------------
.../entity/database/DatastoreMixins.java | 35 ++++++++++++++++++--
1 file changed, 32 insertions(+), 3 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/brooklyn-library/blob/1bc5db09/software/database/src/main/java/org/apache/brooklyn/entity/database/DatastoreMixins.java
----------------------------------------------------------------------
diff --git a/software/database/src/main/java/org/apache/brooklyn/entity/database/DatastoreMixins.java b/software/database/src/main/java/org/apache/brooklyn/entity/database/DatastoreMixins.java
index 67eda16..5490e08 100644
--- a/software/database/src/main/java/org/apache/brooklyn/entity/database/DatastoreMixins.java
+++ b/software/database/src/main/java/org/apache/brooklyn/entity/database/DatastoreMixins.java
@@ -18,19 +18,25 @@
*/
package org.apache.brooklyn.entity.database;
+import java.io.ByteArrayInputStream;
import java.io.InputStream;
+import java.io.UnsupportedEncodingException;
import javax.annotation.Nullable;
+import com.google.common.collect.ImmutableMap;
import org.apache.brooklyn.api.effector.Effector;
import org.apache.brooklyn.api.entity.Entity;
import org.apache.brooklyn.api.sensor.AttributeSensor;
import org.apache.brooklyn.config.ConfigKey;
import org.apache.brooklyn.core.config.ConfigKeys;
import org.apache.brooklyn.core.effector.Effectors;
+import org.apache.brooklyn.core.entity.EntityInternal;
import org.apache.brooklyn.core.sensor.Sensors;
import org.apache.brooklyn.util.core.ResourceUtils;
import org.apache.brooklyn.util.core.flags.SetFromFlag;
+import org.apache.brooklyn.util.core.text.TemplateProcessor;
+import org.apache.brooklyn.util.exceptions.Exceptions;
import org.apache.brooklyn.util.stream.KnownSizeInputStream;
import org.apache.brooklyn.util.text.Strings;
@@ -58,8 +64,9 @@ public class DatastoreMixins {
}
- public static final ConfigKey<String> CREATION_SCRIPT_CONTENTS = CanGiveCreationScript.CREATION_SCRIPT_CONTENTS;
- public static final ConfigKey<String> CREATION_SCRIPT_URL = CanGiveCreationScript.CREATION_SCRIPT_URL;
+ public static final ConfigKey<String> CREATION_SCRIPT_CONTENTS = CanGiveCreationScript.CREATION_SCRIPT_CONTENTS;
+ public static final ConfigKey<String> CREATION_SCRIPT_URL = CanGiveCreationScript.CREATION_SCRIPT_URL;
+ public static final ConfigKey<String> CREATION_SCRIPT_TEMPLATE = CanGiveCreationScript.CREATION_SCRIPT_TEMPLATE;
public static interface CanGiveCreationScript {
@SetFromFlag("creationScriptContents")
@@ -67,12 +74,18 @@ public class DatastoreMixins {
"datastore.creation.script.contents",
"Contents of creation script to initialize the datastore",
"");
-
+
@SetFromFlag("creationScriptUrl")
public static final ConfigKey<String> CREATION_SCRIPT_URL = ConfigKeys.newStringConfigKey(
"datastore.creation.script.url",
"URL of creation script to use to initialize the datastore (ignored if creationScriptContents is specified)",
"");
+
+ @SetFromFlag("creationScriptTemplateUrl")
+ public static final ConfigKey<String> CREATION_SCRIPT_TEMPLATE = ConfigKeys.newStringConfigKey(
+ "datastore.creation.script.template.url",
+ "URL of creation script Freemarker template used to initialize the datastore (ignored if datastore.creation.script.contents or datastore.creation.script.url is specified)",
+ "");
}
/** returns the creation script contents, if it exists, or null if none is defined (error if it cannot be loaded) */
@@ -80,6 +93,17 @@ public class DatastoreMixins {
String url = entity.getConfig(DatastoreMixins.CREATION_SCRIPT_URL);
if (!Strings.isBlank(url))
return new ResourceUtils(entity).getResourceFromUrl(url);
+
+ String templateUrl = entity.getConfig(DatastoreMixins.CREATION_SCRIPT_TEMPLATE);
+ if (!Strings.isBlank(templateUrl)) {
+ String template = TemplateProcessor.processTemplateContents(new ResourceUtils(entity).getResourceAsString(templateUrl), (EntityInternal) entity, ImmutableMap.<String, Object>of());
+ try {
+ return new ByteArrayInputStream(template.getBytes("UTF-8"));
+ } catch (UnsupportedEncodingException e) {
+ throw Exceptions.propagate(e);
+ }
+ }
+
String contents = entity.getConfig(DatastoreMixins.CREATION_SCRIPT_CONTENTS);
if (!Strings.isBlank(contents))
return KnownSizeInputStream.of(contents);
@@ -91,6 +115,11 @@ public class DatastoreMixins {
String url = entity.getConfig(DatastoreMixins.CREATION_SCRIPT_URL);
if (!Strings.isBlank(url))
return new ResourceUtils(entity).getResourceAsString(url);
+
+ String templateUrl = entity.getConfig(DatastoreMixins.CREATION_SCRIPT_TEMPLATE);
+ if (!Strings.isBlank(templateUrl))
+ return TemplateProcessor.processTemplateContents(new ResourceUtils(entity).getResourceAsString(templateUrl), (EntityInternal) entity, ImmutableMap.<String, Object>of());
+
String contents = entity.getConfig(DatastoreMixins.CREATION_SCRIPT_CONTENTS);
if (!Strings.isBlank(contents))
return contents;
[2/2] brooklyn-library git commit: Closes #6
Posted by sv...@apache.org.
Closes #6
add ability to pass DB creation script as a url to a freemarker template
Added to `DatastoreMixins` as part of the `CanGiveCreationScript` interface, should be available to all DBs, have tested with PostgreSQL and MySQL.
Example:
```
name: DB Test
location: byon1
services:
- type: org.apache.brooklyn.entity.database.mysql.MySqlNode
id: db
name: MySQL DB
brooklyn.config:
daytrader.dbname: mydbname
daytrader.username: mydbuser
daytrader.password: mysecurepassword
datastore.creation.script.template.url: "https://myhost.domain.com/templates/mydbname.template.ddl"
```
With the template DDL using standard Freemarker markup.
Project: http://git-wip-us.apache.org/repos/asf/brooklyn-library/repo
Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-library/commit/9fdf1dbf
Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-library/tree/9fdf1dbf
Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-library/diff/9fdf1dbf
Branch: refs/heads/master
Commit: 9fdf1dbf663157d25ed2bf047191eb84e4f4c409
Parents: a283d8d 1bc5db0
Author: Svetoslav Neykov <sv...@cloudsoftcorp.com>
Authored: Tue Feb 16 12:58:33 2016 +0200
Committer: Svetoslav Neykov <sv...@cloudsoftcorp.com>
Committed: Tue Feb 16 12:58:33 2016 +0200
----------------------------------------------------------------------
.../entity/database/DatastoreMixins.java | 35 ++++++++++++++++++--
1 file changed, 32 insertions(+), 3 deletions(-)
----------------------------------------------------------------------