ConfigGroupResourceProvider extends
  AbstractControllerResourceProvider implements ResourcePredicateEvaluator { @SuppressWarnings("unchecked") ConfigGroupRequest getConfigGroupRequest(Map<String, Object> properties) { Object groupIdObj = properties.get(CONFIGGROUP_ID_PROPERTY_ID); Long groupId = null; if (groupIdObj != null) { groupId = groupIdObj instanceof Long ? (Long) groupIdObj : Long.parseLong((String) groupIdObj); } ConfigGroupRequest request = new ConfigGroupRequest( groupId, (String) properties.get(CONFIGGROUP_CLUSTER_NAME_PROPERTY_ID), (String) properties.get(CONFIGGROUP_NAME_PROPERTY_ID), (String) properties.get(CONFIGGROUP_TAG_PROPERTY_ID), (String) properties.get(CONFIGGROUP_DESC_PROPERTY_ID), null, null); request.setServiceConfigVersionNote((String) properties.get(CONFIGGROUP_SCV_NOTE_ID)); Map<String, Config> configurations = new HashMap<String, Config>(); Set<String> hosts = new HashSet<String>(); String hostnameKey = CONFIGGROUP_HOSTNAME_PROPERTY_ID; Object hostObj = properties.get(CONFIGGROUP_HOSTS_PROPERTY_ID); if (hostObj == null) { hostnameKey = CONFIGGROUP_HOSTS_HOSTNAME_PROPERTY_ID; hostObj = properties.get(CONFIGGROUP_HOSTS_HOSTNAME_PROPERTY_ID); } if (hostObj != null) { if (hostObj instanceof HashSet<?>) { try { Set<Map<String, String>> hostsSet = (Set<Map<String, String>>) hostObj; for (Map<String, String> hostMap : hostsSet) { if (hostMap.containsKey(hostnameKey)) { String hostname = hostMap.get(hostnameKey); hosts.add(hostname); } } } catch (Exception e) { LOG.warn("Host json in unparseable format. " + hostObj, e); } } else { if (hostObj instanceof String) { hosts.add((String) hostObj); } } } Object configObj = properties.get(CONFIGGROUP_CONFIGS_PROPERTY_ID); if (configObj != null && configObj instanceof HashSet<?>) { try { Set<Map<String, Object>> configSet = (Set<Map<String, Object>>) configObj; for (Map<String, Object> configMap : configSet) { String type = (String) configMap.get(ConfigurationResourceProvider .CONFIGURATION_CONFIG_TYPE_PROPERTY_ID); String tag = (String) configMap.get(ConfigurationResourceProvider .CONFIGURATION_CONFIG_TAG_PROPERTY_ID); Map<String, String> configProperties = new HashMap<String, String>(); Map<String, Map<String, String>> configAttributes = new HashMap<String, Map<String, String>>(); for (Map.Entry<String, Object> entry : configMap.entrySet()) { String propertyCategory = PropertyHelper.getPropertyCategory(entry.getKey()); if (propertyCategory != null && entry.getValue() != null) { if ("properties".equals(propertyCategory)) { configProperties.put(PropertyHelper.getPropertyName(entry.getKey()), entry.getValue().toString()); } else if ("properties_attributes".equals(PropertyHelper.getPropertyCategory(propertyCategory))) { String attributeName = PropertyHelper.getPropertyName(propertyCategory); if (!configAttributes.containsKey(attributeName)) { configAttributes.put(attributeName, new HashMap<String, String>()); } Map<String, String> attributeValues = configAttributes.get(attributeName); attributeValues.put(PropertyHelper.getPropertyName(entry.getKey()), entry.getValue().toString()); } } } Config config = configFactory.createReadOnly(type, tag, configProperties, configAttributes); configurations.put(config.getType(), config); } } catch (Exception e) { LOG.warn("Config json in unparseable format. " + configObj, e); } } request.setConfigs(configurations); request.setHosts(hosts); return request; } protected  ConfigGroupResourceProvider(Set<String> propertyIds,
       Map<Resource.Type, String> keyPropertyIds,
       AmbariManagementController managementController); @Override RequestStatus createResourcesAuthorized(Request request); @Override Set<Resource> getResourcesAuthorized(Request request, Predicate predicate); @Override RequestStatus updateResourcesAuthorized(Request request, Predicate predicate); @Override RequestStatus deleteResourcesAuthorized(Request request, Predicate predicate); @Override Set<String> checkPropertyIds(Set<String> propertyIds); RequestStatus createResources(final Set<ConfigGroupRequest> requests); RequestStatus updateResources(final Set<ConfigGroupRequest> requests); @Override boolean evaluate(Predicate predicate, Resource resource); static final String CONFIGGROUP_HOSTS_PROPERTY_ID; static final String CONFIGGROUP_CONFIGS_PROPERTY_ID; }
@Test public void testGetConfigGroupRequest_populatesConfigAttributes() throws Exception { AmbariManagementController managementController = createMock(AmbariManagementController.class); ConfigGroupResourceProvider resourceProvider = getConfigGroupResourceProvider (managementController); Set<Map<String, String>> desiredConfigProperties = new HashSet<Map<String, String>>(); Map<String, String> desiredConfig1 = new HashMap<String, String>(); desiredConfig1.put("tag", "version2"); desiredConfig1.put("type", "type1"); desiredConfig1.put("properties/key1", "value1"); desiredConfig1.put("properties/key2", "value2"); desiredConfig1.put("properties_attributes/attr1/key1", "true"); desiredConfig1.put("properties_attributes/attr1/key2", "false"); desiredConfig1.put("properties_attributes/attr2/key1", "15"); desiredConfigProperties.add(desiredConfig1); Map<String, Object> properties = new HashMap<String, Object>(); properties.put("ConfigGroup/hosts", new HashMap<String, String>() {{ put("host_name", "ambari1"); }}); properties.put("ConfigGroup/cluster_name", "c"); properties.put("ConfigGroup/desired_configs", desiredConfigProperties); ConfigGroupRequest request = resourceProvider.getConfigGroupRequest(properties); assertNotNull(request); Map<String, Config> configMap = request.getConfigs(); assertNotNull(configMap); assertEquals(1, configMap.size()); assertTrue(configMap.containsKey("type1")); Config config = configMap.get("type1"); assertEquals("type1", config.getType()); Map<String, String> configProperties = config.getProperties(); assertNotNull(configProperties); assertEquals(2, configProperties.size()); assertEquals("value1", configProperties.get("key1")); assertEquals("value2", configProperties.get("key2")); Map<String, Map<String, String>> configAttributes = config.getPropertiesAttributes(); assertNotNull(configAttributes); assertEquals(2, configAttributes.size()); assertTrue(configAttributes.containsKey("attr1")); Map<String, String> attr1 = configAttributes.get("attr1"); assertNotNull(attr1); assertEquals(2, attr1.size()); assertEquals("true", attr1.get("key1")); assertEquals("false", attr1.get("key2")); assertTrue(configAttributes.containsKey("attr2")); Map<String, String> attr2 = configAttributes.get("attr2"); assertNotNull(attr2); assertEquals(1, attr2.size()); assertEquals("15", attr2.get("key1")); }