use of software.amazon.awssdk.profiles.ProfileFile.Type in project openhab-addons by openhab.
the class DynamoDBConfig method fromConfig.
/**
* @param config persistence service configuration
* @return DynamoDB configuration. Returns null in case of configuration errors
*/
@Nullable
public static DynamoDBConfig fromConfig(Map<String, Object> config) {
ExpectedTableSchema tableRevision;
try {
String regionName = (String) config.get("region");
if (regionName == null) {
return null;
}
final Region region;
if (Region.regions().stream().noneMatch(r -> r.toString().equals(regionName))) {
LOGGER.warn("Region {} is not matching known regions: {}. The region might not be supported.", regionName, Region.regions().stream().map(r -> r.toString()).collect(Collectors.joining(", ")));
}
region = Region.of(regionName);
Optional<RetryMode> retryMode = Optional.empty();
AwsCredentials credentials;
String accessKey = (String) config.get("accessKey");
String secretKey = (String) config.get("secretKey");
if (accessKey != null && !accessKey.isBlank() && secretKey != null && !secretKey.isBlank()) {
LOGGER.debug("accessKey and secretKey specified. Using those.");
credentials = AwsBasicCredentials.create(accessKey, secretKey);
} else {
LOGGER.debug("accessKey and/or secretKey blank. Checking profilesConfigFile and profile.");
String profilesConfigFile = (String) config.get("profilesConfigFile");
String profile = (String) config.get("profile");
if (profilesConfigFile == null || profilesConfigFile.isBlank() || profile == null || profile.isBlank()) {
LOGGER.error("Specify either 1) accessKey and secretKey; or 2) profilesConfigFile and " + "profile for providing AWS credentials");
return null;
}
ProfileFile profileFile = ProfileFile.builder().content(Path.of(profilesConfigFile)).type(Type.CREDENTIALS).build();
credentials = ProfileCredentialsProvider.builder().profileFile(profileFile).profileName(profile).build().resolveCredentials();
retryMode = profileFile.profile(profile).flatMap(p -> p.property(ProfileProperty.RETRY_MODE)).flatMap(retry_mode -> {
for (RetryMode value : RetryMode.values()) {
if (retry_mode.equalsIgnoreCase(value.name())) {
return Optional.of(value);
}
}
LOGGER.warn("Unknown retry_mode '{}' in profile. Ignoring and using SDK default retry mode.", retry_mode);
return Optional.empty();
});
LOGGER.debug("Retry mode {}", retryMode);
}
String table = (String) config.get("table");
String tablePrefixLegacy;
if (table == null || table.isBlank()) {
// the new parameter 'table' has not been set. Check whether the legacy parameter 'tablePrefix' is set
table = DEFAULT_TABLE_NAME;
tablePrefixLegacy = (String) config.get("tablePrefix");
if (tablePrefixLegacy == null || tablePrefixLegacy.isBlank()) {
LOGGER.debug("Using default table prefix {}", DEFAULT_TABLE_PREFIX);
// No explicit value has been specified for tablePrefix, user could be still using the legacy setup
tableRevision = ExpectedTableSchema.MAYBE_LEGACY;
tablePrefixLegacy = DEFAULT_TABLE_PREFIX;
} else {
// Explicit value for tablePrefix, user certainly prefers LEGACY
tableRevision = ExpectedTableSchema.LEGACY;
}
} else {
tableRevision = ExpectedTableSchema.NEW;
tablePrefixLegacy = DEFAULT_TABLE_PREFIX;
}
final long readCapacityUnits;
String readCapacityUnitsParam = (String) config.get("readCapacityUnits");
if (readCapacityUnitsParam == null || readCapacityUnitsParam.isBlank()) {
readCapacityUnits = DEFAULT_READ_CAPACITY_UNITS;
} else {
readCapacityUnits = Long.parseLong(readCapacityUnitsParam);
}
final long writeCapacityUnits;
String writeCapacityUnitsParam = (String) config.get("writeCapacityUnits");
if (writeCapacityUnitsParam == null || writeCapacityUnitsParam.isBlank()) {
writeCapacityUnits = DEFAULT_WRITE_CAPACITY_UNITS;
} else {
writeCapacityUnits = Long.parseLong(writeCapacityUnitsParam);
}
@Nullable final Integer expireDays;
String expireDaysString = (String) config.get("expireDays");
if (expireDaysString == null || expireDaysString.isBlank()) {
expireDays = null;
} else {
expireDays = Integer.parseInt(expireDaysString);
if (expireDays <= 0) {
LOGGER.error("expireDays should be positive integer or null");
return null;
}
}
switch(tableRevision) {
case NEW:
LOGGER.debug("Using new DynamoDB table schema");
return DynamoDBConfig.newSchema(region, credentials, retryMode.map(AwsRetryPolicy::forRetryMode), table, readCapacityUnits, writeCapacityUnits, expireDays);
case LEGACY:
LOGGER.warn("Using legacy DynamoDB table schema. It is recommended to transition to new schema by defining 'table' parameter and not configuring 'tablePrefix'");
return DynamoDBConfig.legacySchema(region, credentials, retryMode.map(AwsRetryPolicy::forRetryMode), tablePrefixLegacy, readCapacityUnits, writeCapacityUnits);
case MAYBE_LEGACY:
LOGGER.debug("Unclear whether we should use new legacy DynamoDB table schema. It is recommended to explicitly define new 'table' parameter. The correct table schema will be detected at runtime.");
return DynamoDBConfig.maybeLegacySchema(region, credentials, retryMode.map(AwsRetryPolicy::forRetryMode), table, tablePrefixLegacy, readCapacityUnits, writeCapacityUnits, expireDays);
default:
throw new IllegalStateException("Unhandled enum. Bug");
}
} catch (Exception e) {
LOGGER.error("Error with configuration: {} {}", e.getClass().getSimpleName(), e.getMessage());
return null;
}
}
Aggregations