Skip to content

Commit 4a2ac32

Browse files
committed
Add AWS_PARTITIONS_FILE_OVERRIDE env variable
This allows for easily overriding the partitions.json file used to resolve partition metadata for the endpoints rules engine.
1 parent 54e6468 commit 4a2ac32

File tree

1 file changed

+25
-4
lines changed
  • smithy-aws-endpoints/src/main/java/software/amazon/smithy/rulesengine/aws/language/functions

1 file changed

+25
-4
lines changed

smithy-aws-endpoints/src/main/java/software/amazon/smithy/rulesengine/aws/language/functions/AwsPartition.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
*/
55
package software.amazon.smithy.rulesengine.aws.language.functions;
66

7+
import java.io.IOException;
8+
import java.io.InputStream;
9+
import java.io.UncheckedIOException;
10+
import java.nio.file.Files;
11+
import java.nio.file.Paths;
712
import java.util.ArrayList;
813
import java.util.Collections;
914
import java.util.HashMap;
@@ -32,6 +37,7 @@
3237
@SmithyUnstableApi
3338
public final class AwsPartition extends LibraryFunction {
3439
public static final String ID = "aws.partition";
40+
public static final String AWS_PARTITIONS_FILE_OVERRIDE = "AWS_PARTITIONS_FILE_OVERRIDE";
3541
public static final Identifier NAME = Identifier.of("name");
3642
public static final Identifier DNS_SUFFIX = Identifier.of("dnsSuffix");
3743
public static final Identifier DUAL_STACK_DNS_SUFFIX = Identifier.of("dualStackDnsSuffix");
@@ -51,10 +57,25 @@ public final class AwsPartition extends LibraryFunction {
5157
private static Partition AWS_PARTITION;
5258

5359
static {
54-
PARTITIONS.addAll(Partitions.fromNode(
55-
Node.parse(Partitions.class.getResourceAsStream("partitions.json")))
56-
.getPartitions());
57-
initializeRegionMap();
60+
// Use the override if present in the environment. Ignore an empty string value.
61+
String override = System.getenv(AWS_PARTITIONS_FILE_OVERRIDE);
62+
if (override != null && override.isEmpty()) {
63+
override = null;
64+
}
65+
66+
try (InputStream in = override != null
67+
? Files.newInputStream(Paths.get(override))
68+
: Partitions.class.getResourceAsStream("partitions.json")) {
69+
if (in == null) {
70+
throw new IllegalStateException("partitions.json not found in JAR"); // should never happen
71+
}
72+
PARTITIONS.addAll(Partitions.fromNode(Node.parse(in)).getPartitions());
73+
initializeRegionMap();
74+
} catch (IOException io) {
75+
throw new UncheckedIOException(
76+
"Failed to load partitions data from " + (override != null ? override : "JAR"),
77+
io);
78+
}
5879
}
5980

6081
private AwsPartition(FunctionNode functionNode) {

0 commit comments

Comments
 (0)