Skip to content

Commit

Permalink
Update ProfileFile to support reading sub-properties. (#5560)
Browse files Browse the repository at this point in the history
Sub-properties are those nested under another property. E.g.
```
[default]
s3 =
  endpoint_url = foo
```

Before this change, only `s3` would be available, and would contain the raw string underneath the property. After this change, `s3` remains the same, but `foo` can be retrieved through the property `s3.endpoint_url`.

This change also adds the property for `endpoint_url` and a convenience setter for profile content strings.
  • Loading branch information
millems committed Sep 9, 2024
1 parent a117b2a commit 6c8c348
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import software.amazon.awssdk.profiles.internal.ProfileFileReader;
import software.amazon.awssdk.utils.FunctionalUtils;
import software.amazon.awssdk.utils.IoUtils;
import software.amazon.awssdk.utils.StringInputStream;
import software.amazon.awssdk.utils.ToString;
import software.amazon.awssdk.utils.Validate;
import software.amazon.awssdk.utils.builder.SdkBuilder;
Expand Down Expand Up @@ -233,6 +234,14 @@ public enum Type {
* required fields.
*/
public interface Builder extends SdkBuilder<Builder, ProfileFile> {
/**
* Configure the content of the profile file. This stream will be read from and then closed when {@link #build()} is
* invoked.
*/
default Builder content(String content) {
return content(new StringInputStream(content));
}

/**
* Configure the content of the profile file. This stream will be read from and then closed when {@link #build()} is
* invoked.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@ public final class ProfileProperty {
*/
public static final String REQUEST_MIN_COMPRESSION_SIZE_BYTES = "request_min_compression_size_bytes";

/**
* The endpoint override to use. This may also be specified under a service-specific parent property to override the
* endpoint just for that one service.
*/
public static final String ENDPOINT_URL = "endpoint_url";

private ProfileProperty() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,10 @@ private static void readPropertyContinuationLine(ParserState state, String line)

// If this is a sub-property, make sure it can be parsed correctly by the CLI.
if (state.validatingContinuationsAsSubProperties) {
parsePropertyDefinition(state, line);
parsePropertyDefinition(state, line).ifPresent(p -> {
String subPropertyDefinition = state.currentPropertyBeingRead + "." + p.left();
profileProperties.put(subPropertyDefinition, p.right());
});
}

profileProperties.put(state.currentPropertyBeingRead, newPropertyValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,9 @@ public void propertiesCanHaveSubProperties() {
assertThat(configFileProfiles("[profile foo]\n" +
"s3 =\n" +
" name = value"))
.isEqualTo(profiles(profile("foo", property("s3", "\nname = value"))));
.isEqualTo(profiles(profile("foo",
property("s3", "\nname = value"),
property("s3.name", "value"))));
}

@Test
Expand All @@ -359,7 +361,9 @@ public void subPropertiesCanHaveEmptyValues() {
assertThat(configFileProfiles("[profile foo]\n" +
"s3 =\n" +
" name ="))
.isEqualTo(profiles(profile("foo", property("s3", "\nname ="))));
.isEqualTo(profiles(profile("foo",
property("s3", "\nname ="),
property("s3.name", ""))));
}

@Test
Expand All @@ -385,7 +389,10 @@ public void subPropertiesCanHaveBlankLines() {
" name = value\n" +
"\t \n" +
" name2 = value2"))
.isEqualTo(profiles(profile("foo", property("s3", "\nname = value\nname2 = value2"))));
.isEqualTo(profiles(profile("foo",
property("s3", "\nname = value\nname2 = value2"),
property("s3.name", "value"),
property("s3.name2", "value2"))));
}

@Test
Expand Down Expand Up @@ -565,7 +572,7 @@ public void returnsEmptyMap_when_AwsFilesDoNotExist() {

private ProfileFile configFile(String configFile) {
return ProfileFile.builder()
.content(new StringInputStream(configFile))
.content(configFile)
.type(ProfileFile.Type.CONFIGURATION)
.build();
}
Expand All @@ -576,7 +583,7 @@ private Map<String, Profile> configFileProfiles(String configFile) {

private ProfileFile credentialFile(String credentialFile) {
return ProfileFile.builder()
.content(new StringInputStream(credentialFile))
.content(credentialFile)
.type(ProfileFile.Type.CREDENTIALS)
.build();
}
Expand Down

0 comments on commit 6c8c348

Please sign in to comment.