Skip to content

Commit 00bd027

Browse files
authored
Update ProfileFile to support reading sub-properties. (#5560)
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.
1 parent 5253ae3 commit 00bd027

File tree

4 files changed

+31
-6
lines changed

4 files changed

+31
-6
lines changed

core/profiles/src/main/java/software/amazon/awssdk/profiles/ProfileFile.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import software.amazon.awssdk.profiles.internal.ProfileFileReader;
3232
import software.amazon.awssdk.utils.FunctionalUtils;
3333
import software.amazon.awssdk.utils.IoUtils;
34+
import software.amazon.awssdk.utils.StringInputStream;
3435
import software.amazon.awssdk.utils.ToString;
3536
import software.amazon.awssdk.utils.Validate;
3637
import software.amazon.awssdk.utils.builder.SdkBuilder;
@@ -233,6 +234,14 @@ public enum Type {
233234
* required fields.
234235
*/
235236
public interface Builder extends SdkBuilder<Builder, ProfileFile> {
237+
/**
238+
* Configure the content of the profile file. This stream will be read from and then closed when {@link #build()} is
239+
* invoked.
240+
*/
241+
default Builder content(String content) {
242+
return content(new StringInputStream(content));
243+
}
244+
236245
/**
237246
* Configure the content of the profile file. This stream will be read from and then closed when {@link #build()} is
238247
* invoked.

core/profiles/src/main/java/software/amazon/awssdk/profiles/ProfileProperty.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,12 @@ public final class ProfileProperty {
171171
*/
172172
public static final String REQUEST_MIN_COMPRESSION_SIZE_BYTES = "request_min_compression_size_bytes";
173173

174+
/**
175+
* The endpoint override to use. This may also be specified under a service-specific parent property to override the
176+
* endpoint just for that one service.
177+
*/
178+
public static final String ENDPOINT_URL = "endpoint_url";
179+
174180
private ProfileProperty() {
175181
}
176182
}

core/profiles/src/main/java/software/amazon/awssdk/profiles/internal/ProfileFileReader.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,10 @@ private static void readPropertyContinuationLine(ParserState state, String line)
170170

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

176179
profileProperties.put(state.currentPropertyBeingRead, newPropertyValue);

core/profiles/src/test/java/software/amazon/awssdk/profiles/ProfileFileTest.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,9 @@ public void propertiesCanHaveSubProperties() {
343343
assertThat(configFileProfiles("[profile foo]\n" +
344344
"s3 =\n" +
345345
" name = value"))
346-
.isEqualTo(profiles(profile("foo", property("s3", "\nname = value"))));
346+
.isEqualTo(profiles(profile("foo",
347+
property("s3", "\nname = value"),
348+
property("s3.name", "value"))));
347349
}
348350

349351
@Test
@@ -359,7 +361,9 @@ public void subPropertiesCanHaveEmptyValues() {
359361
assertThat(configFileProfiles("[profile foo]\n" +
360362
"s3 =\n" +
361363
" name ="))
362-
.isEqualTo(profiles(profile("foo", property("s3", "\nname ="))));
364+
.isEqualTo(profiles(profile("foo",
365+
property("s3", "\nname ="),
366+
property("s3.name", ""))));
363367
}
364368

365369
@Test
@@ -385,7 +389,10 @@ public void subPropertiesCanHaveBlankLines() {
385389
" name = value\n" +
386390
"\t \n" +
387391
" name2 = value2"))
388-
.isEqualTo(profiles(profile("foo", property("s3", "\nname = value\nname2 = value2"))));
392+
.isEqualTo(profiles(profile("foo",
393+
property("s3", "\nname = value\nname2 = value2"),
394+
property("s3.name", "value"),
395+
property("s3.name2", "value2"))));
389396
}
390397

391398
@Test
@@ -565,7 +572,7 @@ public void returnsEmptyMap_when_AwsFilesDoNotExist() {
565572

566573
private ProfileFile configFile(String configFile) {
567574
return ProfileFile.builder()
568-
.content(new StringInputStream(configFile))
575+
.content(configFile)
569576
.type(ProfileFile.Type.CONFIGURATION)
570577
.build();
571578
}
@@ -576,7 +583,7 @@ private Map<String, Profile> configFileProfiles(String configFile) {
576583

577584
private ProfileFile credentialFile(String credentialFile) {
578585
return ProfileFile.builder()
579-
.content(new StringInputStream(credentialFile))
586+
.content(credentialFile)
580587
.type(ProfileFile.Type.CREDENTIALS)
581588
.build();
582589
}

0 commit comments

Comments
 (0)