/** * @deprecated since 0.10.0; use {@link #getFilteredLocationProperties(String, String, Map, Map)} */ @Deprecated protected Map<String, Object> getFilteredLocationProperties(String provider, String namedLocation, Map<String, ?> globalProperties) { return new LocationPropertiesFromBrooklynProperties().getLocationProperties(getPrefix(), namedLocation, globalProperties); }
protected Map<String, Object> transformDeprecated(Map<String, ?> properties) { Map<String,Object> result = Maps.newLinkedHashMap(); Map<String, String> deprecatedKeysMapping = getDeprecatedKeysMapping(); for (Map.Entry<String,?> entry : properties.entrySet()) { String key = entry.getKey(); Object value = entry.getValue(); if (deprecatedKeysMapping.containsKey(key)) { String transformedKey = deprecatedKeysMapping.get(key); LOG.warn("Deprecated key {}, transformed to {}; will not be supported in future versions", new Object[] {key, transformedKey}); result.put(transformedKey, value); } else { result.put(key, value); } } return result; }
/** * Finds the properties that apply to location, stripping off the prefixes. * * Order of preference (in ascending order) is: * <ol> * <li>brooklyn.location.* * <li>brooklyn.location.provider.* * <li>brooklyn.location.named.namedlocation.* * </ol> * <p> * Converts deprecated hyphenated properties to the non-deprecated camelCase format. */ public Map<String, Object> getLocationProperties(String provider, String namedLocation, Map<String, ?> properties) { ConfigBag result = ConfigBag.newInstance(); if (!Strings.isNullOrEmpty(provider)) result.put(LocationConfigKeys.CLOUD_PROVIDER, provider); // named properties are preferred over providerOrApi properties result.putAll(transformDeprecated(getGenericLocationSingleWordProperties(properties))); if (!Strings.isNullOrEmpty(provider)) result.putAll(transformDeprecated(getScopedLocationProperties(provider, properties))); if (!Strings.isNullOrEmpty(namedLocation)) result.putAll(transformDeprecated(getNamedLocationProperties(namedLocation, properties))); setLocalTempDir(properties, result); return result.getAllConfigRaw(); }
@Test public void testConvertsDeprecatedFormats() throws Exception { String provider = "myprovider"; String namedLocation = "mynamed"; Map<String, String> properties = Maps.newLinkedHashMap(); properties.put("brooklyn.location.named.mynamed", "myprovider"); // prefer those in "named" over everything else properties.put("brooklyn.location.named.mynamed.private-key-file", "privateKeyFile-inNamed"); properties.put("brooklyn.location.myprovider.public-key-file", "publicKeyFile-inProviderSpecific"); properties.put("brooklyn.location.private-key-data", "privateKeyData-inGeneric"); Map<String, Object> conf = parser.getLocationProperties(provider, namedLocation, properties); assertEquals(conf.get("privateKeyFile"), "privateKeyFile-inNamed"); assertEquals(conf.get("publicKeyFile"), "publicKeyFile-inProviderSpecific"); assertEquals(conf.get("privateKeyData"), "privateKeyData-inGeneric"); }
@BeforeMethod(alwaysRun=true) public void setUp() throws Exception { parser = new LocationPropertiesFromBrooklynProperties(); }
/** * Returns those properties in the form "brooklyn.location.xyz", where "xyz" is any * key that does not contain dots. We do this special (sub-optimal!) filtering * because we want to exclude brooklyn.location.named.*, brooklyn.location.jclouds.*, etc. * We only want those properties that are to be generic for all locations. * * Strips off the prefix in the returned map. */ protected Map<String, Object> getGenericLocationSingleWordProperties(Map<String, ?> properties) { return getMatchingSingleWordProperties("brooklyn.location.", properties); }
@Override public LocationSpec<?> newLocationSpecFromString(String spec, Map<?,?> locationFlags, LocationRegistry registry) { ConfigBag config = extractConfig(locationFlags, spec, registry); @SuppressWarnings("unchecked") Map<String,?> globalProperties = registry.getProperties(); String namedLocation = (String) locationFlags.get(LocationInternal.NAMED_SPEC_NAME.getName()); if (registry != null) { LocationPropertiesFromBrooklynProperties.setLocalTempDir(globalProperties, config); } return LocationSpec.create(getLocationType()) .configure(config.getAllConfig()) .configure(LocationConfigUtils.finalAndOriginalSpecs(spec, locationFlags, globalProperties, namedLocation)); }
/** * Gets all properties that start with either of the given prefixes. The {@code fullPreferredPrefix} * properties will override any duplicates in {@code fullDeprecatedPrefix}. If there are any * properties that match the {@code fullDeprecatedPrefix}, then a warning will be logged. * * @see #getMatchingProperties(String, Map) */ protected Map<String, Object> getMatchingProperties(String fullPreferredPrefix, String fullDeprecatedPrefix, Map<String, ?> properties) { Map<String, Object> deprecatedResults = getMatchingProperties(fullDeprecatedPrefix, properties); Map<String, Object> results = getMatchingProperties(fullPreferredPrefix, properties); if (deprecatedResults.size() > 0) { LOG.warn("Deprecated use of properties prefix "+fullDeprecatedPrefix+"; instead use "+fullPreferredPrefix); return MutableMap.<String, Object>builder() .putAll(deprecatedResults) .putAll(results) .build(); } else { return results; } }
@Test public void testExtractProviderProperties() throws Exception { String provider = "myprovider"; String namedLocation = null; Map<String, String> properties = Maps.newLinkedHashMap(); // prefer those in "named" over everything else properties.put("brooklyn.location.myprovider.privateKeyFile", "privateKeyFile-inProviderSpecific"); properties.put("brooklyn.location.privateKeyFile", "privateKeyFile-inLocationGeneric"); // prefer location-generic if nothing else properties.put("brooklyn.location.publicKeyFile", "publicKeyFile-inLocationGeneric"); Map<String, Object> conf = parser.getLocationProperties(provider, namedLocation, properties); assertEquals(conf.get("privateKeyFile"), "privateKeyFile-inProviderSpecific"); assertEquals(conf.get("publicKeyFile"), "publicKeyFile-inLocationGeneric"); }
/** * Gets all single-word properties that start with either of the given prefixes. The {@code fullPreferredPrefix} * properties will override any duplicates in {@code fullDeprecatedPrefix}. If there are any * properties that match the {@code fullDeprecatedPrefix}, then a warning will be logged. * * @see #getMatchingSingleWordProperties(String, Map) */ protected Map<String, Object> getMatchingSingleWordProperties(String fullPreferredPrefix, String fullDeprecatedPrefix, Map<String, ?> properties) { Map<String, Object> deprecatedResults = getMatchingSingleWordProperties(fullDeprecatedPrefix, properties); Map<String, Object> results = getMatchingSingleWordProperties(fullPreferredPrefix, properties); if (deprecatedResults.size() > 0) { LOG.warn("Deprecated use of properties prefix "+fullDeprecatedPrefix+"; instead use "+fullPreferredPrefix); return MutableMap.<String, Object>builder() .putAll(deprecatedResults) .putAll(results) .build(); } else { return results; } }
@Override public LocationSpec<?> newLocationSpecFromString(String spec, Map<?, ?> locationFlags, LocationRegistry registry) { ConfigBag config = extractConfig(locationFlags, spec, registry); @SuppressWarnings("rawtypes") Map globalProperties = registry.getProperties(); String namedLocation = (String) locationFlags.get(LocationInternal.NAMED_SPEC_NAME.getName()); if (registry != null) { LocationPropertiesFromBrooklynProperties.setLocalTempDir(globalProperties, config); } if (config.getStringKey("target") == null) { throw new IllegalArgumentException("target must be specified in single-machine spec"); } String target = config.getStringKey("target").toString(); config.remove("target"); Maybe<LocationSpec<?>> testResolve = managementContext.getLocationRegistry().getLocationSpec(target); if (!testResolve.isPresent()) { throw new IllegalArgumentException("Invalid target location '" + target + "' for location '"+SINGLE+"': "+ Exceptions.collapseText( Maybe.getException(testResolve) ), Maybe.getException(testResolve)); } return LocationSpec.create(SingleMachineProvisioningLocation.class) .configure("location", target) .configure("locationFlags", config.getAllConfig()) .configure(LocationConfigUtils.finalAndOriginalSpecs(spec, locationFlags, globalProperties, namedLocation)); }
@Override protected Map<String, Object> getFilteredLocationProperties(String provider, String namedLocation, Map<String, ?> prioritisedProperties, Map<String, ?> globalProperties) { Map<String, Object> dockerConf = new LocationPropertiesFromBrooklynProperties().getLocationProperties(getPrefix(), namedLocation, globalProperties); Object providerInConf = dockerConf.get("provider"); if (providerInConf != null && !provider.equals(providerInConf)) { throw new IllegalArgumentException(provider + " location configured with provider '" + providerInConf + "', but must be blank or '" + provider + "'"); } String providerOrApi = "docker"; String regionName = (String) prioritisedProperties.get("region"); Map<String, Object> jcloudsConf = new JcloudsPropertiesFromBrooklynProperties().getJcloudsProperties(providerOrApi, regionName, namedLocation, globalProperties); return MutableMap.<String, Object>builder() .putAll(jcloudsConf) .putAll(dockerConf) .put("provider", providerOrApi) .build(); } }
@Test public void testThrowsIfProviderDoesNotMatchNamed() throws Exception { String provider = "myprovider"; String namedLocation = "mynamed"; Map<String, String> properties = Maps.newLinkedHashMap(); properties.put("brooklyn.location.named.mynamed", "completelydifferent"); try { Map<String, Object> conf = parser.getLocationProperties(provider, namedLocation, properties); } catch (IllegalStateException e) { if (!e.toString().contains("Conflicting configuration")) throw e; } } }
Map<String, Object> filteredProperties = new LocationPropertiesFromBrooklynProperties().getLocationProperties(null, namedLocation, globalProperties); ConfigBag flags = ConfigBag.newInstance(locationFlags).putIfAbsent(filteredProperties); flags.remove(LocationInternal.NAMED_SPEC_NAME);
@Test public void testExtractNamedLocationProperties() throws Exception { String provider = "myprovider"; String namedLocation = "mynamed"; Map<String, String> properties = Maps.newLinkedHashMap(); properties.put("brooklyn.location.named.mynamed", "myprovider"); // prefer those in "named" over everything else properties.put("brooklyn.location.named.mynamed.privateKeyFile", "privateKeyFile-inNamed"); properties.put("brooklyn.location.myprovider.privateKeyFile", "privateKeyFile-inProviderSpecific"); properties.put("brooklyn.location.privateKeyFile", "privateKeyFile-inGeneric"); // prefer those in provider-specific over generic properties.put("brooklyn.location.myprovider.publicKeyFile", "publicKeyFile-inProviderSpecific"); properties.put("brooklyn.location.publicKeyFile", "publicKeyFile-inGeneric"); // prefer location-generic if nothing else properties.put("brooklyn.location.privateKeyData", "privateKeyData-inGeneric"); Map<String, Object> conf = parser.getLocationProperties(provider, namedLocation, properties); assertEquals(conf.get("privateKeyFile"), "privateKeyFile-inNamed"); assertEquals(conf.get("publicKeyFile"), "publicKeyFile-inProviderSpecific"); assertEquals(conf.get("privateKeyData"), "privateKeyData-inGeneric"); }
Map<String, Object> filteredProperties = new LocationPropertiesFromBrooklynProperties().getLocationProperties(null, namedLocation, (Map)globalProperties); MutableMap<Object, Object> flags = MutableMap.<Object, Object>builder() .putAll(filteredProperties)