Skip to content

Commit 9d40ec1

Browse files
authored
IMDS service client (#3494)
* IMDS Ec2 Metada refactoring * IMDS Ec2 Metada refactoring * IMDS Ec2 Metada refactoring * IMDS Ec2 Metada refactoring * IMDS Ec2 Metada refactoring * IMDS Ec2 Metada refactoring * IMDS Ec2 Metada refactoring * fix checkstyle error * Increase IMDS test code coverage
1 parent 48de313 commit 9d40ec1

20 files changed

+1020
-1084
lines changed

core/imds/pom.xml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,6 @@
5454
<artifactId>mockito-core</artifactId>
5555
<scope>test</scope>
5656
</dependency>
57-
<dependency>
58-
<groupId>org.mockito</groupId>
59-
<artifactId>mockito-core</artifactId>
60-
<scope>test</scope>
61-
</dependency>
6257
<dependency>
6358
<groupId>software.amazon.awssdk</groupId>
6459
<artifactId>url-connection-client</artifactId>
@@ -99,6 +94,12 @@
9994
<version>${awsjavasdk.version}</version>
10095
<scope>compile</scope>
10196
</dependency>
97+
<dependency>
98+
<groupId>software.amazon.awssdk</groupId>
99+
<artifactId>third-party-jackson-core</artifactId>
100+
<version>${awsjavasdk.version}</version>
101+
<scope>compile</scope>
102+
</dependency>
102103
<dependency>
103104
<groupId>software.amazon.awssdk</groupId>
104105
<artifactId>test-utils</artifactId>

core/imds/src/main/java/software/amazon/awssdk/imds/Ec2Metadata.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424

2525
/**
26-
* Interface to represent the Ec2Metadata Client Class.
26+
* Interface to represent the Ec2Metadata Client Class. Used to access instance metadata from a running instance.
2727
*/
2828
@SdkPublicApi
2929
public interface Ec2Metadata {
@@ -84,21 +84,13 @@ interface Builder {
8484
Builder tokenTtl(Duration tokenTtl);
8585

8686
/**
87-
* Define the endpoint mode of IMDS.Supported values include IPv4 and IPv6.
87+
* Define the endpoint mode of IMDS. Supported values include IPv4 and IPv6.
8888
*
8989
* @param endpointMode The endpoint mode of IMDS.Supported values include IPv4 and IPv6.
9090
* @return Returns a reference to this builder
9191
*/
9292
Builder endpointMode(EndpointMode endpointMode);
9393

94-
/**
95-
* Define the output stream for debugging.
96-
*
97-
* @param httpDebugOutput TThe output stream for debugging.
98-
* @return Returns a reference to this builder
99-
*/
100-
Builder httpDebugOutput(String httpDebugOutput);
101-
10294
/**
10395
* Define the SdkHttpClient instance to make the http requests.
10496
*

core/imds/src/main/java/software/amazon/awssdk/imds/Ec2MetadataRetryPolicy.java

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,16 @@ private Ec2MetadataRetryPolicy(BuilderImpl builder) {
4646
}
4747

4848
@Override
49-
public boolean equals(Object o) {
50-
if (this == o) {
49+
public boolean equals(Object obj) {
50+
if (this == obj) {
5151
return true;
5252
}
53-
if (o == null || getClass() != o.getClass()) {
53+
if (obj == null || getClass() != obj.getClass()) {
5454
return false;
5555
}
56-
Ec2MetadataRetryPolicy ec2MetadataRetryPolicy = (Ec2MetadataRetryPolicy) o;
56+
Ec2MetadataRetryPolicy ec2MetadataRetryPolicy = (Ec2MetadataRetryPolicy) obj;
5757

58-
if (!Objects.equals(numRetries, ec2MetadataRetryPolicy.numRetries)) {
58+
if (numRetries != ec2MetadataRetryPolicy.numRetries) {
5959
return false;
6060
}
6161
return Objects.equals(backoffStrategy, ec2MetadataRetryPolicy.backoffStrategy);
@@ -64,7 +64,7 @@ public boolean equals(Object o) {
6464
@Override
6565
public int hashCode() {
6666

67-
int result = numRetries >= 0 ? numRetries : 0;
67+
int result = Math.max(numRetries, 0);
6868
result = 31 * result + (backoffStrategy != null ? backoffStrategy.hashCode() : 0);
6969
return result;
7070
}
@@ -103,6 +103,10 @@ public Builder toBuilder() {
103103
.backoffStrategy(backoffStrategy);
104104
}
105105

106+
public static Ec2MetadataRetryPolicy none() {
107+
return builder().numRetries(0).build();
108+
}
109+
106110
public interface Builder extends CopyableBuilder<Ec2MetadataRetryPolicy.Builder, Ec2MetadataRetryPolicy> {
107111

108112
/**
@@ -133,20 +137,12 @@ public Builder numRetries(Integer numRetries) {
133137
return this;
134138
}
135139

136-
public void setNumRetries(Integer numRetries) {
137-
numRetries(numRetries);
138-
}
139-
140140
@Override
141141
public Builder backoffStrategy(BackoffStrategy backoffStrategy) {
142142
this.backoffStrategy = backoffStrategy;
143143
return this;
144144
}
145145

146-
public void setBackoffStrategy(BackoffStrategy backoffStrategy) {
147-
backoffStrategy(backoffStrategy);
148-
}
149-
150146
@Override
151147
public Ec2MetadataRetryPolicy build() {
152148
return new Ec2MetadataRetryPolicy(this);

core/imds/src/main/java/software/amazon/awssdk/imds/MetadataResponse.java

Lines changed: 42 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -15,101 +15,89 @@
1515

1616
package software.amazon.awssdk.imds;
1717

18-
import java.io.IOException;
18+
import java.io.UncheckedIOException;
1919
import java.util.Arrays;
2020
import java.util.List;
21-
import org.slf4j.Logger;
22-
import org.slf4j.LoggerFactory;
2321
import software.amazon.awssdk.annotations.SdkPublicApi;
2422
import software.amazon.awssdk.core.document.Document;
2523
import software.amazon.awssdk.imds.internal.unmarshall.document.DocumentUnmarshaller;
2624
import software.amazon.awssdk.protocols.jsoncore.JsonNode;
2725
import software.amazon.awssdk.protocols.jsoncore.JsonNodeParser;
26+
import software.amazon.awssdk.thirdparty.jackson.core.JsonParseException;
27+
import software.amazon.awssdk.utils.ToString;
2828
import software.amazon.awssdk.utils.Validate;
2929

3030
/**
3131
* The class is used for Response Handling and Parsing the metadata fetched by the get call in the {@link Ec2Metadata} interface.
3232
* The class provides convenience methods to the users to parse the metadata as a String, List and Document.
3333
*/
3434
@SdkPublicApi
35-
public class MetadataResponse {
36-
37-
private static final Logger log = LoggerFactory.getLogger(MetadataResponse.class);
35+
public final class MetadataResponse {
3836

3937
private static final JsonNodeParser JSON_NODE_PARSER = JsonNode.parserBuilder().removeErrorLocations(true).build();
4038

4139
private final String body;
4240

43-
public MetadataResponse(String body) {
41+
private MetadataResponse(String body) {
4442
this.body = Validate.notNull(body, "Metadata is null");
4543
}
4644

4745
/**
48-
* Returns the Metadata Response body as a String. This method can be used for parsing the retrieved
49-
* singular metadata from IMDS.
50-
*
46+
* Create a {@link MetadataResponse} with the given body as it's content.
47+
* @param body the content of the response
48+
* @return a {@link MetadataResponse} with the given body as it's content.
49+
*/
50+
public static MetadataResponse create(String body) {
51+
return new MetadataResponse(body);
52+
}
53+
54+
/**
5155
* @return String Representation of the Metadata Response Body.
52-
*
53-
* <p>
54-
* Example:
55-
* <pre>
56-
* {@code
57-
*
58-
* Ec2Metadata ec2Metadata = Ec2Metadata.create();
59-
* MetadataResponse metadataResponse = client.get("/latest/meta-data/ami-id");
60-
* String response = metadataResponse.asString();
61-
* }
62-
* </pre>
6356
*/
6457
public String asString() {
6558
return body;
6659
}
6760

6861
/**
69-
* Parses the response String into a list of Strings split by delimiter ("\n"). This method can be used for parsing the
70-
* list-type metadata from IMDS.
71-
*
72-
* @return List Representation of the Metadata Response Body.
73-
*
74-
* <p>
75-
* Example:
76-
* <pre>
77-
* {@code
78-
*
79-
* Ec2Metadata ec2Metadata = Ec2Metadata.create();
80-
* MetadataResponse metadataResponse = client.get("/latest/meta-data/ancestor-ami-ids");
81-
* List<String>response = metadataResponse.asList();
82-
* }
83-
* </pre>
62+
* Splits the Metadata response body on new line character and returns it as a list.
63+
* @return The Metadata response split on each line.
8464
*/
8565
public List<String> asList() {
8666
return Arrays.asList(body.split("\n"));
8767
}
8868

8969
/**
90-
* Parses the response String into {@link Document} type. This method can be used for
70+
* Parses the response String into a {@link Document} type. This method can be used for
9171
* parsing the metadata in a String Json Format.
92-
*
93-
* @return Document Representation of the Metadata Response Body.
94-
* @throws IOException in case parsing does not happen correctly.
95-
*
96-
* <p>
97-
* Example:
98-
* <pre>
99-
* {@code
100-
*
101-
* Ec2Metadata ec2Metadata = Ec2Metadata.create();
102-
* MetadataResponse metadataResponse = client.get("/latest/dynamic/instance-identity/document");
103-
* Document document = metadataResponse.asDocument();
104-
* }
105-
* </pre>
72+
* @return Document Representation, as json, of the Metadata Response Body.
73+
* @throws UncheckedIOException (wrapping a {@link JsonParseException} if the Response body is not of JSON format.
10674
*/
107-
108-
public Document asDocument() throws IOException {
109-
75+
public Document asDocument() {
11076
JsonNode node = JSON_NODE_PARSER.parse(body);
11177
return node.visit(new DocumentUnmarshaller());
11278
}
11379

80+
@Override
81+
public boolean equals(Object o) {
82+
if (this == o) {
83+
return true;
84+
}
85+
if (o == null || getClass() != o.getClass()) {
86+
return false;
87+
}
88+
MetadataResponse that = (MetadataResponse) o;
89+
return body.equals(that.body);
90+
}
11491

92+
@Override
93+
public int hashCode() {
94+
return body.hashCode();
95+
}
96+
97+
@Override
98+
public String toString() {
99+
return ToString.builder("MetadataResponse")
100+
.add("body", body)
101+
.build();
102+
}
115103
}

0 commit comments

Comments
 (0)