1 /*
2 * Copyright 2016-2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15 package com.amazonaws.partitions.model;
16
17 import com.amazonaws.util.ValidationUtils;
18 import com.fasterxml.jackson.annotation.JsonProperty;
19
20 import java.util.Map;
21 import java.util.regex.Pattern;
22
23 /**
24 * This class models a AWS partition and contains all metadata about it.
25 */
26 public class Partition {
27
28 /**
29 * The name of the partition.
30 */
31 private final String partition;
32
33 /**
34 * Supported regions.
35 */
36 private final Map<String, Region> regions;
37
38 /**
39 * Supported services;
40 */
41 private final Map<String, Service> services;
42
43 /**
44 * description of the partition.
45 */
46 private String partitionName;
47
48 /**
49 * dns suffix for the endpoints in the partition.
50 */
51 private String dnsSuffix;
52
53 /**
54 * region name regex for regions in the partition.
55 */
56 private String regionRegex;
57
58 /**
59 * default endpoint configuration.
60 */
61 private Endpoint defaults;
62
63 public Partition(@JsonProperty(value = "partition") String partition,
64 @JsonProperty(value = "regions") Map<String, Region>
65 regions,
66 @JsonProperty(value = "services") Map<String,
67 Service> services) {
68 this.partition = ValidationUtils.assertNotNull(partition, "Partition");
69 this.regions = regions;
70 this.services = services;
71 }
72
73 /**
74 * Returns the name of the partition.
75 */
76 public String getPartition() {
77 return partition;
78 }
79
80 /**
81 * Returns the description of the partition.
82 */
83 public String getPartitionName() {
84 return partitionName;
85 }
86
87 /**
88 * Sets the description of the partition.
89 */
90 public void setPartitionName(String partitionName) {
91 this.partitionName = partitionName;
92 }
93
94 /**
95 * Returns the dns suffix of the partition.
96 */
97 public String getDnsSuffix() {
98 return dnsSuffix;
99 }
100
101 /**
102 * Sets the dns suffix of the partition.
103 */
104 public void setDnsSuffix(String dnsSuffix) {
105 this.dnsSuffix = dnsSuffix;
106 }
107
108 /**
109 * Returns the regex for the regions in the partition.
110 */
111 public String getRegionRegex() {
112 return regionRegex;
113 }
114
115 /**
116 * Sets the regex for the regions in the partition.
117 */
118 public void setRegionRegex(String regionRegex) {
119 this.regionRegex = regionRegex;
120 }
121
122 /**
123 * Returns the default endpoint configuration of the partition.
124 */
125 public Endpoint getDefaults() {
126 return defaults;
127 }
128
129 /**
130 * Sets the default endpoint configuration of the partition.
131 */
132 public void setDefaults(Endpoint defaults) {
133 this.defaults = defaults;
134 }
135
136 /**
137 * Returns the set of regions associated with the partition.
138 */
139 public Map<String, Region> getRegions() {
140 return regions;
141 }
142
143 /**
144 * Returns the set of services supported by the partition.
145 */
146 public Map<String, Service> getServices() {
147 return services;
148 }
149
150 /**
151 * Returns true if the region is explicitly configured in the partition
152 * or if the region matches the {@link #regionRegex} of the partition.
153 */
154 public boolean hasRegion(String region) {
155 return regions.containsKey(region) || matchesRegionRegex(region) || hasServiceEndpoint
156 (region);
157 }
158
159 private boolean matchesRegionRegex(String region) {
160 final Pattern p = Pattern.compile(regionRegex);
161 return p.matcher(region).matches();
162 }
163
164 /**
165 * returns true if any of the services in the partition has a custom endpoint
166 * like s3 having s3-external-1.
167 * TODO Remove this support as part of next major version.
168 * @Deprecated use the {@link com.amazonaws.AmazonWebServiceClient#setEndpoint(String)} method
169 * for custom endpoints.
170 */
171 @Deprecated
172 private boolean hasServiceEndpoint(String endpoint) {
173 for(Service s : services.values()) {
174 if (s.getEndpoints().containsKey(endpoint)) {
175 return true;
176 }
177 }
178 return false;
179 }
180 }
181