Search in sources :

Example 1 with Type

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;
    }
}
Also used : RetryMode(software.amazon.awssdk.core.retry.RetryMode) ProfileFile(software.amazon.awssdk.profiles.ProfileFile) AwsRetryPolicy(software.amazon.awssdk.awscore.retry.AwsRetryPolicy) NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) Logger(org.slf4j.Logger) Type(software.amazon.awssdk.profiles.ProfileFile.Type) AwsCredentials(software.amazon.awssdk.auth.credentials.AwsCredentials) LoggerFactory(org.slf4j.LoggerFactory) ProfileCredentialsProvider(software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider) Collectors(java.util.stream.Collectors) RetryPolicy(software.amazon.awssdk.core.retry.RetryPolicy) Nullable(org.eclipse.jdt.annotation.Nullable) Map(java.util.Map) Optional(java.util.Optional) ProfileProperty(software.amazon.awssdk.profiles.ProfileProperty) AwsBasicCredentials(software.amazon.awssdk.auth.credentials.AwsBasicCredentials) RetryMode(software.amazon.awssdk.core.retry.RetryMode) Region(software.amazon.awssdk.regions.Region) Path(java.nio.file.Path) AwsCredentials(software.amazon.awssdk.auth.credentials.AwsCredentials) Region(software.amazon.awssdk.regions.Region) Nullable(org.eclipse.jdt.annotation.Nullable) ProfileFile(software.amazon.awssdk.profiles.ProfileFile) Nullable(org.eclipse.jdt.annotation.Nullable)

Aggregations

Path (java.nio.file.Path)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 NonNullByDefault (org.eclipse.jdt.annotation.NonNullByDefault)1 Nullable (org.eclipse.jdt.annotation.Nullable)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 AwsBasicCredentials (software.amazon.awssdk.auth.credentials.AwsBasicCredentials)1 AwsCredentials (software.amazon.awssdk.auth.credentials.AwsCredentials)1 ProfileCredentialsProvider (software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider)1 AwsRetryPolicy (software.amazon.awssdk.awscore.retry.AwsRetryPolicy)1 RetryMode (software.amazon.awssdk.core.retry.RetryMode)1 RetryPolicy (software.amazon.awssdk.core.retry.RetryPolicy)1 ProfileFile (software.amazon.awssdk.profiles.ProfileFile)1 Type (software.amazon.awssdk.profiles.ProfileFile.Type)1 ProfileProperty (software.amazon.awssdk.profiles.ProfileProperty)1 Region (software.amazon.awssdk.regions.Region)1