Search in sources :

Example 1 with Parameter

use of com.amazonaws.services.simplesystemsmanagement.model.Parameter in project spring-cloud-aws by awspring.

the class AwsParamStorePropertySource method getParameters.

private void getParameters(GetParametersByPathRequest paramsRequest) {
    GetParametersByPathResult paramsResult = this.source.getParametersByPath(paramsRequest);
    for (Parameter parameter : paramsResult.getParameters()) {
        String key = parameter.getName().replace(this.context, "").replace('/', '.').replaceAll("_(\\d)_", "[$1]");
        LOG.debug("Populating property retrieved from AWS Parameter Store: " + key);
        this.properties.put(key, parameter.getValue());
    }
    if (paramsResult.getNextToken() != null) {
        getParameters(paramsRequest.withNextToken(paramsResult.getNextToken()));
    }
}
Also used : GetParametersByPathResult(com.amazonaws.services.simplesystemsmanagement.model.GetParametersByPathResult) Parameter(com.amazonaws.services.simplesystemsmanagement.model.Parameter)

Example 2 with Parameter

use of com.amazonaws.services.simplesystemsmanagement.model.Parameter in project spring-cloud-aws by awspring.

the class AwsParamStorePropertySourceTest method followsNextToken.

@Test
void followsNextToken() {
    GetParametersByPathResult firstResult = new GetParametersByPathResult().withNextToken("next").withParameters(new Parameter().withName("/config/myservice/key1").withValue("value1"), new Parameter().withName("/config/myservice/key2").withValue("value2"));
    GetParametersByPathResult nextResult = new GetParametersByPathResult().withParameters(new Parameter().withName("/config/myservice/key3").withValue("value3"), new Parameter().withName("/config/myservice/key4").withValue("value4"));
    when(ssmClient.getParametersByPath(any(GetParametersByPathRequest.class))).thenReturn(firstResult, nextResult);
    propertySource.init();
    assertThat(propertySource.getPropertyNames()).containsExactly("key1", "key2", "key3", "key4");
    assertThat(propertySource.getProperty("key3")).isEqualTo("value3");
}
Also used : GetParametersByPathResult(com.amazonaws.services.simplesystemsmanagement.model.GetParametersByPathResult) Parameter(com.amazonaws.services.simplesystemsmanagement.model.Parameter) GetParametersByPathRequest(com.amazonaws.services.simplesystemsmanagement.model.GetParametersByPathRequest) Test(org.junit.jupiter.api.Test)

Example 3 with Parameter

use of com.amazonaws.services.simplesystemsmanagement.model.Parameter in project spring-cloud-config by spring-cloud.

the class AwsParameterStoreEnvironmentRepository method addParametersToProperties.

private void addParametersToProperties(String path, List<Parameter> parameters, Map<String, String> properties) {
    for (Parameter parameter : parameters) {
        String name = StringUtils.delete(parameter.getName(), path).replace(DEFAULT_PATH_SEPARATOR, ".");
        properties.put(name, parameter.getValue());
    }
}
Also used : Parameter(com.amazonaws.services.simplesystemsmanagement.model.Parameter)

Example 4 with Parameter

use of com.amazonaws.services.simplesystemsmanagement.model.Parameter in project spring-cloud-config by spring-cloud.

the class AwsParameterStoreEnvironmentRepositoryTests method splitParametersIntoChunks.

private List<Set<Parameter>> splitParametersIntoChunks(Set<Parameter> parameters) {
    AtomicInteger counter = new AtomicInteger();
    Collector<Parameter, ?, Map<Integer, Set<Parameter>>> collector = Collectors.groupingBy(p -> counter.getAndIncrement() / environmentProperties.getMaxResults(), Collectors.toSet());
    return new ArrayList<>(parameters.stream().collect(collector).values());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) Parameter(com.amazonaws.services.simplesystemsmanagement.model.Parameter) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with Parameter

use of com.amazonaws.services.simplesystemsmanagement.model.Parameter in project spring-cloud-config by spring-cloud.

the class AwsParameterStoreEnvironmentRepositoryTests method setupAwsSsmClientMocks.

private void setupAwsSsmClientMocks(Environment environment, boolean withSlashesForPropertyName, boolean paginatedResponse) {
    for (PropertySource ps : environment.getPropertySources()) {
        String path = StringUtils.delete(ps.getName(), environmentProperties.getOrigin());
        GetParametersByPathRequest request = new GetParametersByPathRequest().withPath(path).withRecursive(environmentProperties.isRecursive()).withWithDecryption(environmentProperties.isDecryptValues()).withMaxResults(environmentProperties.getMaxResults());
        Set<Parameter> parameters = getParameters(ps, path, withSlashesForPropertyName);
        GetParametersByPathResult response = new GetParametersByPathResult().withParameters(parameters);
        if (paginatedResponse && environmentProperties.getMaxResults() < parameters.size()) {
            List<Set<Parameter>> chunks = splitParametersIntoChunks(parameters);
            String nextToken = null;
            for (int i = 0; i < chunks.size(); i++) {
                Set<Parameter> chunk = chunks.get(i);
                if (i == 0) {
                    nextToken = generateNextToken();
                    GetParametersByPathResult responseClone = response.clone().withParameters(chunk).withNextToken(nextToken);
                    when(awsSsmClientMock.getParametersByPath(eq(request))).thenReturn(responseClone);
                } else if (i == chunks.size() - 1) {
                    GetParametersByPathRequest requestClone = request.clone().withNextToken(nextToken);
                    GetParametersByPathResult responseClone = response.clone().withParameters(chunk);
                    when(awsSsmClientMock.getParametersByPath(eq(requestClone))).thenReturn(responseClone);
                } else {
                    String newNextToken = generateNextToken();
                    GetParametersByPathRequest requestClone = request.clone().withNextToken(nextToken);
                    GetParametersByPathResult responseClone = response.clone().withParameters(chunk).withNextToken(newNextToken);
                    when(awsSsmClientMock.getParametersByPath(eq(requestClone))).thenReturn(responseClone);
                    nextToken = newNextToken;
                }
            }
        } else {
            when(awsSsmClientMock.getParametersByPath(eq(request))).thenReturn(response);
        }
    }
}
Also used : Set(java.util.Set) Parameter(com.amazonaws.services.simplesystemsmanagement.model.Parameter) GetParametersByPathResult(com.amazonaws.services.simplesystemsmanagement.model.GetParametersByPathResult) GetParametersByPathRequest(com.amazonaws.services.simplesystemsmanagement.model.GetParametersByPathRequest) PropertySource(org.springframework.cloud.config.environment.PropertySource)

Aggregations

Parameter (com.amazonaws.services.simplesystemsmanagement.model.Parameter)7 GetParametersByPathResult (com.amazonaws.services.simplesystemsmanagement.model.GetParametersByPathResult)4 GetParametersByPathRequest (com.amazonaws.services.simplesystemsmanagement.model.GetParametersByPathRequest)3 Test (org.junit.jupiter.api.Test)3 GetParameterRequest (com.amazonaws.services.simplesystemsmanagement.model.GetParameterRequest)1 GetParameterResult (com.amazonaws.services.simplesystemsmanagement.model.GetParameterResult)1 ECKeyGenerator (com.nimbusds.jose.jwk.gen.ECKeyGenerator)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Set (java.util.Set)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 BouncyCastleProvider (org.bouncycastle.jce.provider.BouncyCastleProvider)1 PropertySource (org.springframework.cloud.config.environment.PropertySource)1