AMBARI-18332: Blueprints: API should make available "setting" property from 
blueprint


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/1b89d686
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/1b89d686
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/1b89d686

Branch: refs/heads/branch-dev-patch-upgrade
Commit: 1b89d6866e369906902f759a7a5d2ba865e8cbe0
Parents: 9496772
Author: Nahappan Somasundaram <nsomasunda...@hortonworks.com>
Authored: Tue Sep 13 11:06:32 2016 -0700
Committer: Nahappan Somasundaram <nsomasunda...@hortonworks.com>
Committed: Wed Sep 14 14:42:16 2016 -0700

----------------------------------------------------------------------
 .../internal/BlueprintResourceProvider.java     |  31 ++++++
 .../internal/BlueprintResourceProviderTest.java | 104 +++++++++++++++++++
 2 files changed, 135 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/1b89d686/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/BlueprintResourceProvider.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/BlueprintResourceProvider.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/BlueprintResourceProvider.java
index 775827b..1c127c0 100644
--- 
a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/BlueprintResourceProvider.java
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/BlueprintResourceProvider.java
@@ -21,6 +21,7 @@ package org.apache.ambari.server.controller.internal;
 import com.google.common.base.Preconditions;
 import com.google.common.base.Strings;
 import com.google.gson.Gson;
+import org.apache.ambari.server.orm.entities.BlueprintSettingEntity;
 import org.apache.ambari.server.utils.SecretReference;
 import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.DuplicateResourceException;
@@ -96,6 +97,10 @@ public class BlueprintResourceProvider extends 
AbstractControllerResourceProvide
 
   // Configurations
   public static final String CONFIGURATION_PROPERTY_ID = "configurations";
+
+  // Setting
+  public static final String SETTING_PROPERTY_ID = "settings";
+
   public static final String PROPERTIES_PROPERTY_ID = "properties";
   public static final String PROPERTIES_ATTRIBUTES_PROPERTY_ID = 
"properties_attributes";
   public static final String SCHEMA_IS_NOT_SUPPORTED_MESSAGE =
@@ -320,6 +325,8 @@ public class BlueprintResourceProvider extends 
AbstractControllerResourceProvide
     setResourceProperty(resource, HOST_GROUP_PROPERTY_ID, listGroupProps, 
requestedIds);
     setResourceProperty(resource, CONFIGURATION_PROPERTY_ID,
       populateConfigurationList(entity.getConfigurations()), requestedIds);
+    setResourceProperty(resource, SETTING_PROPERTY_ID,
+      populateSettingList(entity.getSettings()), requestedIds);
 
     if (entity.getSecurityType() != null) {
       Map<String, String> securityConfigMap = new LinkedHashMap<>();
@@ -387,6 +394,30 @@ public class BlueprintResourceProvider extends 
AbstractControllerResourceProvide
   }
 
   /**
+   * Populate a list of setting property maps from a collection of setting 
entities.
+   *
+   * @param settings  collection of setting entities
+   *
+   * @return list of setting property maps
+   */
+  public static List<Map<String, Object>> populateSettingList(
+          Collection<? extends BlueprintSettingEntity> settings) throws 
NoSuchResourceException {
+    List<Map<String, Object>> listSettings = new ArrayList<Map<String, 
Object>>();
+
+    if (settings != null) {
+      for (BlueprintSettingEntity setting : settings) {
+        List<Map<String, String>> propertiesList = 
jsonSerializer.<List<Map<String, String>>>fromJson(
+                setting.getSettingData(), List.class);
+        Map<String, Object> settingMap = new HashMap<>();
+        settingMap.put(setting.getSettingName(), propertiesList);
+        listSettings.add(settingMap);
+      }
+    }
+
+    return listSettings;
+  }
+
+  /**
    * Populate blueprint configurations.
    *
    * @param propertyMaps  collection of configuration property maps

http://git-wip-us.apache.org/repos/asf/ambari/blob/1b89d686/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/BlueprintResourceProviderTest.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/BlueprintResourceProviderTest.java
 
b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/BlueprintResourceProviderTest.java
index 75eff7e..0943bc9 100644
--- 
a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/BlueprintResourceProviderTest.java
+++ 
b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/BlueprintResourceProviderTest.java
@@ -40,6 +40,7 @@ import org.apache.ambari.server.orm.dao.StackDAO;
 import org.apache.ambari.server.orm.entities.BlueprintConfigEntity;
 import org.apache.ambari.server.orm.entities.BlueprintConfiguration;
 import org.apache.ambari.server.orm.entities.BlueprintEntity;
+import org.apache.ambari.server.orm.entities.BlueprintSettingEntity;
 import org.apache.ambari.server.orm.entities.HostGroupComponentEntity;
 import org.apache.ambari.server.orm.entities.HostGroupConfigEntity;
 import org.apache.ambari.server.orm.entities.HostGroupEntity;
@@ -1138,5 +1139,108 @@ public class BlueprintResourceProviderTest {
     assertEquals("attrValue1", finalAttrs.get("key3"));
     assertEquals("attrValue2", finalAttrs.get("key4"));
   }
+
+  @Test
+  public void testPopulateSettingList() throws Exception {
+    StackEntity stackEntity = new StackEntity();
+    stackEntity.setStackName("test-stack-name");
+    stackEntity.setStackVersion("test-stack-version");
+    BlueprintEntity entity = createMock(BlueprintEntity.class);
+    expect(entity.getStack()).andReturn(stackEntity).anyTimes();
+
+    HashMap<PropertyInfo.PropertyType, Set<String>> pwdProperties = new 
HashMap<PropertyInfo.PropertyType, Set<String>>() {{
+      put(PropertyInfo.PropertyType.PASSWORD, new HashSet<String>(){{
+        add("test.password");
+      }});
+    }};
+
+    StackInfo info = createMock(StackInfo.class);
+    expect(info.getConfigPropertiesTypes("type1")).andReturn(new 
HashMap<PropertyInfo.PropertyType, Set<String>>()).anyTimes();
+    expect(info.getConfigPropertiesTypes("type2")).andReturn(new 
HashMap<PropertyInfo.PropertyType, Set<String>>()).anyTimes();
+    
expect(info.getConfigPropertiesTypes("type3")).andReturn(pwdProperties).anyTimes();
+    expect(metaInfo.getStack("test-stack-name", 
"test-stack-version")).andReturn(info).anyTimes();
+
+    replay(info, metaInfo, entity);
+
+    // Blueprint setting entities
+    // Global recovery setting
+    BlueprintSettingEntity settingEntity1 = new BlueprintSettingEntity();
+    settingEntity1.setSettingName("recovery_settings");
+    settingEntity1.setSettingData("[{\"recovery_enabled\":\"true\"}]");
+    settingEntity1.setBlueprintEntity(entity);
+
+    // Service exceptions setting
+    BlueprintSettingEntity settingEntity2 = new BlueprintSettingEntity();
+    settingEntity2.setSettingName("service_settings");
+    settingEntity2.setSettingData("[{\"name\":\"HDFS\", 
\"recovery_enabled\":\"false\"}, " +
+            "{\"name\":\"ZOOKEEPER\", \"recovery_enabled\":\"false\"}]");
+    settingEntity2.setBlueprintEntity(entity);
+
+    // Service component exceptions setting
+    BlueprintSettingEntity settingEntity3 = new BlueprintSettingEntity();
+    settingEntity3.setSettingName("component_settings");
+    settingEntity3.setSettingData("[{\"name\":\"METRICS_MONITOR\", 
\"recovery_enabled\":\"false\"}," +
+            "{\"name\":\"KAFKA_CLIENT\", \"recovery_enabled\":\"false\"}]");
+    settingEntity3.setBlueprintEntity(entity);
+
+    List<BlueprintSettingEntity> settingEntities = new ArrayList();
+    settingEntities.add(settingEntity1);
+    settingEntities.add(settingEntity2);
+    settingEntities.add(settingEntity3);
+
+    List<Map<String, Object>> settings = 
provider.populateSettingList(settingEntities);
+
+    assertNotNull(settings);
+    assertEquals(settingEntities.size(), settings.size());
+
+    // Verify global recovery setting
+    Map<String, Object> setting1 = settings.get(0);
+    assertNotNull(setting1);
+    assertEquals(1, setting1.size());
+    assertTrue(setting1.containsKey("recovery_settings"));
+    List<Map<String, String>> setting1value = (List<Map<String, String>>) 
setting1.get("recovery_settings");
+    assertNotNull(setting1value);
+    assertEquals(1, setting1value.size());
+    assertTrue(setting1value.get(0).containsKey("recovery_enabled"));
+    assertEquals(setting1value.get(0).get("recovery_enabled"), "true");
+
+    // Verify service exceptions
+    Map<String, Object> setting2 = settings.get(1);
+    assertNotNull(setting2);
+    assertEquals(1, setting2.size());
+    assertTrue(setting2.containsKey("service_settings"));
+    List<Map<String, String>> setting2value = (List<Map<String, String>>) 
setting2.get("service_settings");
+    assertNotNull(setting2value);
+    assertEquals(2, setting2value.size());
+    // first service exception is HDFS
+    assertTrue(setting2value.get(0).containsKey("name"));
+    assertEquals(setting2value.get(0).get("name"), "HDFS");
+    assertTrue(setting2value.get(0).containsKey("recovery_enabled"));
+    assertEquals(setting2value.get(0).get("recovery_enabled"), "false");
+    // second service exception is ZOOKEEPER
+    assertTrue(setting2value.get(1).containsKey("name"));
+    assertEquals(setting2value.get(1).get("name"), "ZOOKEEPER");
+    assertTrue(setting2value.get(1).containsKey("recovery_enabled"));
+    assertEquals(setting2value.get(1).get("recovery_enabled"), "false");
+
+    // Verify service component exceptions
+    Map<String, Object> setting3 = settings.get(2);
+    assertNotNull(setting3);
+    assertEquals(1, setting3.size());
+    assertTrue(setting3.containsKey("component_settings"));
+    List<Map<String, String>> setting3value = (List<Map<String, String>>) 
setting3.get("component_settings");
+    assertNotNull(setting3value);
+    assertEquals(2, setting3value.size());
+    // first service component exception is METRICS_MONITOR
+    assertTrue(setting3value.get(0).containsKey("name"));
+    assertEquals(setting3value.get(0).get("name"), "METRICS_MONITOR");
+    assertTrue(setting3value.get(0).containsKey("recovery_enabled"));
+    assertEquals(setting3value.get(0).get("recovery_enabled"), "false");
+    // second service component exception is KAFKA_CLIENT
+    assertTrue(setting3value.get(1).containsKey("name"));
+    assertEquals(setting3value.get(1).get("name"), "KAFKA_CLIENT");
+    assertTrue(setting3value.get(1).containsKey("recovery_enabled"));
+    assertEquals(setting3value.get(1).get("recovery_enabled"), "false");
+  }
 }
 

Reply via email to