You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by he...@apache.org on 2021/11/23 17:22:59 UTC

[brooklyn-server] 01/04: take initial deployment metadata from config or tags

This is an automated email from the ASF dual-hosted git repository.

heneveld pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brooklyn-server.git

commit c51471cdb8cf0b12ec98e70f4ee6c9db9ec9e3ff
Author: Alex Heneveld <al...@cloudsoftcorp.com>
AuthorDate: Tue Nov 23 16:29:41 2021 +0000

    take initial deployment metadata from config or tags
---
 .../core/effector/AddDeploySensorsInitializer.java | 60 ++++++++++++++++++----
 1 file changed, 51 insertions(+), 9 deletions(-)

diff --git a/core/src/main/java/org/apache/brooklyn/core/effector/AddDeploySensorsInitializer.java b/core/src/main/java/org/apache/brooklyn/core/effector/AddDeploySensorsInitializer.java
index 1e9d402..f494540 100644
--- a/core/src/main/java/org/apache/brooklyn/core/effector/AddDeploySensorsInitializer.java
+++ b/core/src/main/java/org/apache/brooklyn/core/effector/AddDeploySensorsInitializer.java
@@ -25,13 +25,48 @@ import org.apache.brooklyn.api.entity.EntityInitializer;
 import org.apache.brooklyn.api.entity.EntityLocal;
 import org.apache.brooklyn.api.mgmt.entitlement.EntitlementContext;
 import org.apache.brooklyn.api.sensor.AttributeSensor;
+import org.apache.brooklyn.core.config.ConfigKeys;
 import org.apache.brooklyn.core.entity.EntityInternal;
+import org.apache.brooklyn.core.mgmt.BrooklynTags;
 import org.apache.brooklyn.core.mgmt.entitlement.Entitlements;
 import org.apache.brooklyn.core.sensor.Sensors;
 
 import java.util.Map;
+import org.apache.brooklyn.util.collections.MutableMap;
+import org.apache.brooklyn.util.core.flags.TypeCoercions;
+import org.apache.brooklyn.util.text.Strings;
 
 public class AddDeploySensorsInitializer implements EntityInitializer {
+
+    public static final String DEPLOYMENT_METADATA = "deployment.metadata";
+
+    public static class DeploymentMetadata {
+        String user;
+        Instant created;
+
+        public void read(Object inputO, boolean overwrite) {
+            Map input;
+            if (inputO==null) return;
+            if (inputO instanceof DeploymentMetadata) {
+                input = MutableMap.of("user", ((DeploymentMetadata)inputO).user,
+                        "created", ((DeploymentMetadata)inputO).created);
+            } else if (!(inputO instanceof Map)) {
+                return;
+            } else {
+                input = (Map)inputO;
+            }
+
+            if (overwrite || Strings.isBlank(user)) {
+                String value = Strings.toString( input.get("user") );
+                if (Strings.isNonBlank(value)) user = value;
+            }
+            if (overwrite || created==null) {
+                Instant value = TypeCoercions.tryCoerce(input.get("created"), Instant.class ).orNull();
+                if (value!=null) created = value;
+            }
+        }
+    }
+
     @Override
     public void apply(EntityLocal entity) {
         // We want to set the metadata only on the root node of an application
@@ -39,18 +74,25 @@ public class AddDeploySensorsInitializer implements EntityInitializer {
             return;
         }
         EntitlementContext entitlementContext = Entitlements.getEntitlementContext();
-        AttributeSensor<Map<String, Object>> sensor = Sensors.newSensor(
-                new TypeToken<Map<String, Object>>() {},
-                "deployment.metadata",
+        AttributeSensor<DeploymentMetadata> sensor = Sensors.newSensor(
+                DeploymentMetadata.class,
+                DEPLOYMENT_METADATA,
                 "A map of metadata information about this particular deployment. Contains at least who triggered it and when.");
         ((EntityInternal) entity).getMutableEntityType().addSensor(sensor);
-        entity.sensors().set(sensor, ImmutableMap.of(
-                "user", entitlementContext != null ? entitlementContext.user() : "Unknown",
 
-                "created", Instant.now()
-                // previously used the below instead
-//                "deploy_time", System.currentTimeMillis()
-        ));
+        DeploymentMetadata result = new DeploymentMetadata();
+
+        // will convert config, then tag, and then republish
+
+        result.read( entity.config().get(ConfigKeys.newConfigKey(Object.class, DEPLOYMENT_METADATA)), false );
+        result.read(BrooklynTags.findSingleKeyMapValue(DEPLOYMENT_METADATA, Object.class, entity.tags().getTags()), false);
+        result.read(ImmutableMap.of(
+                "user", entitlementContext != null
+                        ? entitlementContext.user()
+                        : "Unknown",
+                "created", Instant.now()), false);
+
+        entity.sensors().set(sensor, result);
 
     }
 }