Skip to content

IMDS Release #3723

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/next-release/feature-AWSSDKforJavav2-65b1d43.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "feature",
"category": "AWS SDK for Java v2",
"contributor": "",
"description": "EC2 Instance Metadata Client is now generally available - https://github.com/aws/aws-sdk-java-v2/issues/61"
}
5 changes: 5 additions & 0 deletions bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1693,6 +1693,11 @@
<artifactId>redshiftserverless</artifactId>
<version>${awsjavasdk.version}</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>imds</artifactId>
<version>${awsjavasdk.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
138 changes: 138 additions & 0 deletions core/imds/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
~
~ Licensed under the Apache License, Version 2.0 (the "License").
~ You may not use this file except in compliance with the License.
~ A copy of the License is located at
~
~ http://aws.amazon.com/apache2.0
~
~ or in the "license" file accompanying this file. This file is distributed
~ on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
~ express or implied. See the License for the specific language governing
~ permissions and limitations under the License.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>core</artifactId>
<groupId>software.amazon.awssdk</groupId>
<version>2.17.209-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>imds</artifactId>
<name>AWS Java SDK :: Core :: Imds</name>
<url>https://aws.amazon.com/sdkforjava</url>

<dependencies>
<dependency>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: can we organize the dependencies a bit by separating compile and test dependencies ? Non-test dependencies should be put to the top.

<groupId>software.amazon.awssdk</groupId>
<artifactId>annotations</artifactId>
<version>${awsjavasdk.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this module only have JUnit5 tests only? If so, let's remove JUnit4 dependencies to avoid the chances of mixing JUnit4 and JUnit5 annotations. See https://github.com/aws/aws-sdk-java-v2/blob/master/services-custom/s3-transfer-manager/pom.xml#L121-L130

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, will do that to prevent any junit4 dependency in the classpath

<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>url-connection-client</artifactId>
<version>${awsjavasdk.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>http-client-spi</artifactId>
<version>${awsjavasdk.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>nl.jqno.equalsverifier</groupId>
<artifactId>equalsverifier</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>sdk-core</artifactId>
<version>${awsjavasdk.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-jre8</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>utils</artifactId>
<version>${awsjavasdk.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>json-utils</artifactId>
<version>${awsjavasdk.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>third-party-jackson-core</artifactId>
<version>${awsjavasdk.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>test-utils</artifactId>
<version>${awsjavasdk.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>profiles</artifactId>
<version>${awsjavasdk.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>netty-nio-client</artifactId>
<version>${awsjavasdk.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<Automatic-Module-Name>software.amazon.awssdk.imds</Automatic-Module-Name>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package software.amazon.awssdk.imds;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import software.amazon.awssdk.annotations.SdkPublicApi;
import software.amazon.awssdk.core.exception.SdkClientException;
import software.amazon.awssdk.http.async.SdkAsyncHttpClient;
import software.amazon.awssdk.imds.internal.DefaultEc2MetadataAsyncClient;
import software.amazon.awssdk.utils.SdkAutoCloseable;

/**
* Interface to represent the Ec2Metadata Client Class. Used to access instance metadata from a running instance.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add some code snippets using {@snippet}

*/
@SdkPublicApi
public interface Ec2MetadataAsyncClient extends SdkAutoCloseable {

/**
* Gets the specified instance metadata value by the given path. For more information about instance metadata, check the
* <a href=https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html>Instance metadata documentation</a>.
*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a code sample here as well. Same as sync client.

* @param path Input path
* @return A CompletableFuture that completes when the MetadataResponse is made available.
*/
CompletableFuture<Ec2MetadataResponse> get(String path);

/**
* Create an {@link Ec2MetadataAsyncClient} instance using the default values.
*
* @return the client instance.
*/
static Ec2MetadataAsyncClient create() {
return builder().build();
}

/**
* Creates a builder for an async client instance.
* @return the newly created builder instance.
*/
static Ec2MetadataAsyncClient.Builder builder() {
return DefaultEc2MetadataAsyncClient.builder();
}

/**
* The builder definition for a {@link Ec2MetadataClient}. All parameters are optional and have default values if not
* specified. Therefore, an instance can be simply created with {@code Ec2MetadataAsyncClient.builder().build()} or
* {@code Ec2MetadataAsyncClient.create()}, both having the same result.
*/
interface Builder extends Ec2MetadataClientBuilder<Ec2MetadataAsyncClient.Builder, Ec2MetadataAsyncClient> {

/**
* Define the {@link ScheduledExecutorService} used to schedule asynchronous retry attempts. If provided, the
* Ec2MetadataClient will <em>NOT</em> manage the lifetime if the httpClient and must therefore be
* closed explicitly by calling the {@link SdkAsyncHttpClient#close()} method on it.
* <p>
* If not specified, defaults to {@link Executors#newScheduledThreadPool} with a default value of 3 thread in the
* pool.
*
* @param scheduledExecutorService the ScheduledExecutorService to use for retry attempt.
* @return a reference to this builder
*/
Builder scheduledExecutorService(ScheduledExecutorService scheduledExecutorService);

/**
* Define the http client used by the Ec2 Metadata client. If provided, the Ec2MetadataClient will <em>NOT</em> manage the
* lifetime if the httpClient and must therefore be closed explicitly by calling the {@link SdkAsyncHttpClient#close()}
* method on it.
* <p>
* If not specified, the IMDS client will look for a SdkAsyncHttpClient class included in the classpath of the
* application and creates a new instance of that class, managed by the IMDS Client, that will be closed when the IMDS
* Client is closed. If no such class can be found, will throw a {@link SdkClientException}.
*
* @param httpClient the http client
* @return a reference to this builder
*/
Builder httpClient(SdkAsyncHttpClient httpClient);

/**
* An http client builder used to retrieve an instance of an {@link SdkAsyncHttpClient}. If specified, the Ec2
* Metadata Client will use the instance returned by the builder and manage its lifetime by closing the http client
* once the Ec2 Client itself is closed.
*
* @param builder the builder to used to retrieve an instance.
* @return a reference to this builder
*/
Builder httpClient(SdkAsyncHttpClient.Builder<?> builder);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package software.amazon.awssdk.imds;

import software.amazon.awssdk.annotations.SdkPublicApi;
import software.amazon.awssdk.core.exception.SdkClientException;
import software.amazon.awssdk.http.SdkHttpClient;
import software.amazon.awssdk.http.async.SdkAsyncHttpClient;
import software.amazon.awssdk.imds.internal.DefaultEc2MetadataClient;
import software.amazon.awssdk.utils.SdkAutoCloseable;


/**
* Interface to represent the Ec2Metadata Client Class. Used to access instance metadata from a running instance.
*/
@SdkPublicApi
public interface Ec2MetadataClient extends SdkAutoCloseable {

/**
* Gets the specified instance metadata value by the given path. For more information about instance metadata, check the
* <a href=https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html>Instance metadata documentation</a>
*
* @param path Input path
* @return Instance metadata value as part of MetadataResponse Object
*/
Ec2MetadataResponse get(String path);

/**
* Create an {@link Ec2MetadataClient} instance using the default values.
*
* @return the client instance.
*/
static Ec2MetadataClient create() {
return builder().build();
}

/**
* Creates a default builder for {@link Ec2MetadataClient}.
*/
static Builder builder() {
return DefaultEc2MetadataClient.builder();
}

/**
* The builder definition for a {@link Ec2MetadataClient}.
*/
interface Builder extends Ec2MetadataClientBuilder<Ec2MetadataClient.Builder, Ec2MetadataClient> {

/**
* Define the http client used by the Ec2 Metadata client. If provided, the Ec2MetadataClient will <em>NOT</em> manage the
* lifetime if the httpClient and must therefore be closed explicitly by calling the {@link SdkAsyncHttpClient#close()}
* method on it.
* <p>
* If not specified, the IMDS client will look for a SdkHttpClient class included in the classpath of the
* application and creates a new instance of that class, managed by the IMDS Client, that will be closed when the IMDS
* Client is closed. If no such class can be found, will throw a {@link SdkClientException}.
*
* @param httpClient the http client
* @return a reference to this builder
*/
Builder httpClient(SdkHttpClient httpClient);

/**
* A http client builder used to retrieve an instance of an {@link SdkHttpClient}. If specified, the Ec2 Metadata Client
* will use the instance returned by the builder and manage its lifetime by closing the http client once the Ec2 Client
* itself is closed.
*
* @param builder the builder to used to retrieve an instance.
* @return a reference to this builder
*/
Builder httpClient(SdkHttpClient.Builder<?> builder);
}

}
Loading