Convert Braintree configuration to YAML

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=146477578
This commit is contained in:
mcilwain 2017-02-03 08:32:35 -08:00 committed by Ben McIlwain
parent 223e8c2316
commit 8e9b2d3483
7 changed files with 75 additions and 38 deletions

View file

@ -31,6 +31,8 @@ import java.lang.annotation.Documented;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.net.URI; import java.net.URI;
import java.net.URL; import java.net.URL;
import java.util.Map;
import java.util.Map.Entry;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import javax.inject.Named; import javax.inject.Named;
import javax.inject.Qualifier; import javax.inject.Qualifier;
@ -736,17 +738,13 @@ public final class RegistryConfig {
@Provides @Provides
@Config("braintreeMerchantAccountIds") @Config("braintreeMerchantAccountIds")
public static ImmutableMap<CurrencyUnit, String> provideBraintreeMerchantAccountId( public static ImmutableMap<CurrencyUnit, String> provideBraintreeMerchantAccountId(
RegistryEnvironment environment) { RegistryConfigSettings config) {
switch (environment) { Map<String, String> merchantAccountIds = config.braintree.merchantAccountIdsMap;
case PRODUCTION: ImmutableMap.Builder<CurrencyUnit, String> builder = new ImmutableMap.Builder<>();
return ImmutableMap.of( for (Entry<String, String> entry : merchantAccountIds.entrySet()) {
CurrencyUnit.USD, "charlestonregistryUSD", builder.put(CurrencyUnit.of(entry.getKey()), entry.getValue());
CurrencyUnit.JPY, "charlestonregistryJPY");
default:
return ImmutableMap.of(
CurrencyUnit.USD, "google",
CurrencyUnit.JPY, "google-jpy");
} }
return builder.build();
} }
/** /**
@ -758,14 +756,8 @@ public final class RegistryConfig {
*/ */
@Provides @Provides
@Config("braintreeMerchantId") @Config("braintreeMerchantId")
public static String provideBraintreeMerchantId(RegistryEnvironment environment) { public static String provideBraintreeMerchantId(RegistryConfigSettings config) {
switch (environment) { return config.braintree.merchantId;
case PRODUCTION:
return "6gm2mm48k9ty4zmx";
default:
// Valentine: Nomulus Braintree Sandbox
return "vqgn8khkq2cs6y9s";
}
} }
/** /**
@ -778,14 +770,8 @@ public final class RegistryConfig {
*/ */
@Provides @Provides
@Config("braintreePublicKey") @Config("braintreePublicKey")
public static String provideBraintreePublicKey(RegistryEnvironment environment) { public static String provideBraintreePublicKey(RegistryConfigSettings config) {
switch (environment) { return config.braintree.publicKey;
case PRODUCTION:
return "tzcfxggzgbh2jg5x";
default:
// Valentine: Nomulus Braintree Sandbox
return "tzcyzvm3mn7zkdnx";
}
} }
/** /**

View file

@ -15,6 +15,7 @@
package google.registry.config; package google.registry.config;
import java.util.List; import java.util.List;
import java.util.Map;
/** The POJO that YAML config files are deserialized into. */ /** The POJO that YAML config files are deserialized into. */
public class RegistryConfigSettings { public class RegistryConfigSettings {
@ -28,6 +29,7 @@ public class RegistryConfigSettings {
public RegistrarConsole registrarConsole; public RegistrarConsole registrarConsole;
public Monitoring monitoring; public Monitoring monitoring;
public Misc misc; public Misc misc;
public Braintree braintree;
/** Configuration options that apply to the entire App Engine project. */ /** Configuration options that apply to the entire App Engine project. */
public static class AppEngine { public static class AppEngine {
@ -105,4 +107,11 @@ public class RegistryConfigSettings {
public static class Misc { public static class Misc {
public String sheetExportId; public String sheetExportId;
} }
/** Configuration for Braintree credit card payment processing. */
public static class Braintree {
public String merchantId;
public String publicKey;
public Map<String, String> merchantAccountIdsMap;
}
} }

View file

@ -82,7 +82,9 @@ public final class YamlUtils {
* override a field that has a Map value in the default YAML with a field of any other type in the * override a field that has a Map value in the default YAML with a field of any other type in the
* custom YAML. * custom YAML.
* *
* <p>Only maps are handled recursively; lists are simply overridden in place as-is. * <p>Only maps are handled recursively; lists are simply overridden in place as-is, as are maps
* whose name is suffixed with "Map" -- this allows entire maps to be overridden, rather than
* merged.
*/ */
static String mergeYaml(String defaultYaml, String customYaml) { static String mergeYaml(String defaultYaml, String customYaml) {
Yaml yaml = new Yaml(); Yaml yaml = new Yaml();
@ -107,19 +109,22 @@ public final class YamlUtils {
* <p>All keys in the default map that are also specified in the custom map are overridden with * <p>All keys in the default map that are also specified in the custom map are overridden with
* the custom map's value. This runs recursively on all contained maps. * the custom map's value. This runs recursively on all contained maps.
*/ */
@SuppressWarnings("unchecked")
private static Map<String, Object> mergeMaps( private static Map<String, Object> mergeMaps(
Map<String, Object> defaultMap, Map<String, Object> customMap) { Map<String, Object> defaultMap, Map<String, Object> customMap) {
for (String key : defaultMap.keySet()) { for (String key : defaultMap.keySet()) {
if (!customMap.containsKey(key)) { if (!customMap.containsKey(key)) {
continue; continue;
} }
Object defaultValue = defaultMap.get(key); Object newValue;
@SuppressWarnings("unchecked") if (defaultMap.get(key) instanceof Map && !key.endsWith("Map")) {
Object newValue = newValue =
(defaultValue instanceof Map) mergeMaps(
? mergeMaps( (Map<String, Object>) defaultMap.get(key),
(Map<String, Object>) defaultValue, (Map<String, Object>) customMap.get(key)) (Map<String, Object>) customMap.get(key));
: customMap.get(key); } else {
newValue = customMap.get(key);
}
defaultMap.put(key, newValue); defaultMap.put(key, newValue);
} }
return defaultMap; return defaultMap;

View file

@ -132,3 +132,18 @@ misc:
# The ID of the Google Sheet (as found in the URL) to export registrar details # The ID of the Google Sheet (as found in the URL) to export registrar details
# to. Leave this null to disable syncing. # to. Leave this null to disable syncing.
sheetExportId: null sheetExportId: null
# Braintree is a credit card payment processor that is used on the registrar
# console to allow registrars to pay their invoices.
braintree:
# Merchant ID of the Braintree account.
merchantId: example
# Public key used for accessing Braintree API (this is found on their site).
publicKey: example
# A map of JODA Money CurrencyUnits, specified in three letter ISO-4217
# format, to Braintree account IDs (each account is limited to a single
# currency). For example, one entry might be:
# USD: accountIdUsingUSD
merchantAccountIdsMap: {}

View file

@ -42,3 +42,13 @@ registrarConsole:
misc: misc:
sheetExportId: placeholder sheetExportId: placeholder
# You only need to specify this section if using Braintree.
braintree:
merchantId: placeholder
publicKey: placeholder
# Only include currencies that you use.
merchantAccountIdsMap:
EUR: placeholder
JPY: placeholder
USD: placeholder

View file

@ -23,3 +23,8 @@ caching:
singletonCacheRefreshSeconds: 0 singletonCacheRefreshSeconds: 0
domainLabelCachingSeconds: 0 domainLabelCachingSeconds: 0
singletonCachePersistSeconds: 0 singletonCachePersistSeconds: 0
braintree:
merchantAccountIdsMap:
USD: accountIdUsd
JPY: accountIdJpy

View file

@ -53,7 +53,14 @@ public class YamlUtilsTest {
public void testSuccess_mergeEmptyMap_isNoop() { public void testSuccess_mergeEmptyMap_isNoop() {
String defaultYaml = join("one: ay", "two: bee", "three: sea"); String defaultYaml = join("one: ay", "two: bee", "three: sea");
assertThat(mergeYaml(defaultYaml, "# Just a comment\n")) assertThat(mergeYaml(defaultYaml, "# Just a comment\n"))
.isEqualTo("{one: ay, two: bee, three: sea}\n"); .isEqualTo("{one: ay, two: bee, three: sea}\n");
}
@Test
public void testSuccess_mergeNamedMap_overwritesEntirelyWithNewKey() {
String defaultYaml = join("one: ay", "two: bee", "threeMap:", " foo: bar", " baz: gak");
assertThat(mergeYaml(defaultYaml, "threeMap: {time: money}"))
.isEqualTo(join("one: ay", "two: bee", "threeMap: {time: money}"));
} }
private static String join(CharSequence... strings) { private static String join(CharSequence... strings) {