Skip to content

Conversation

@srsaikumarreddy
Copy link
Contributor

Created Metadata Response class to handle the response and provide convenient method.

Motivation and Context

This change is required for response handling and parsing outputs.

Modifications

Created Metadata Response class to handle the response and provide convenient method. Wrote Test cases for it.

Testing

Wrote Test cases for the changes and tested in IDE.

Screenshots (if appropriate)

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • [X ] New feature (non-breaking change which adds functionality)

Checklist

  • [X ] I have read the CONTRIBUTING document
  • [ X] Local run of mvn install succeeds
  • [ X] My code follows the code style of this project
  • [X ] My change requires a change to the Javadoc documentation
  • [ X] I have updated the Javadoc documentation accordingly
  • [ X] I have added tests to cover my changes
  • [ X] All new and existing tests passed
  • I have added a changelog entry. Adding a new entry must be accomplished by running the scripts/new-change script and following the instructions. Commit the new file created by the script in .changes/next-release with your changes.
  • My change is to implement 1.11 parity feature and I have updated LaunchChangelog

License

  • [ X] I confirm that this pull request can be released under the Apache 2 license

@srsaikumarreddy srsaikumarreddy requested a review from a team as a code owner July 11, 2022 22:04
Comment on lines 29 to 32
* The class is used for Response Handling and Parsing the metadata fetched by the get call in the {@link Ec2Metadata} interface.
* Metadata is stored in the instance variable <b>body</b>. The class provides convenience methods to the users to parse the
* metadata as a String, List and to parse the metadata in the JsonResponse according to the key.
*/
Copy link
Contributor

Choose a reason for hiding this comment

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

We can exclude information that is internal to the class that might change, like that data is stored in the body field. That information isn't particularly relevant to a user of the class and it might change without someone updating the docs. It could be included as a comment in the field, if it's useful.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Noted.

Comment on lines 87 to 92
if (null != body && body.contains("\n")) {

return Arrays.asList(body.split("\n"));
}

return Collections.emptyList();
Copy link
Contributor

Choose a reason for hiding this comment

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

If it's a single line, should we return that single line in a collection instead of not returning anything?


public String[] getStringArrayValuesFromJson(String key) {

if (null != key) {
Copy link
Contributor

Choose a reason for hiding this comment

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

should we throw on a null key?

*/
public List<String> asList() {

if (null != body && body.contains("\n")) {
Copy link
Contributor

Choose a reason for hiding this comment

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

should we just validate that body is not null when an instance is created? or is there a use-case where body needs to be null?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just learned about the Validate class. Will use it.

* </pre>
*/

public String[] getStringArrayValuesFromJson(String key) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of having these json-like methods, maybe we should just have one to parse the body into a software.amazon.awssdk.core.document.Document? That would require making some currently-internal stuff protected (parsing JSON into a document), but it would be more flexible.

E.g.

Ec2MetadataClient ec2Metadata = Ec2MetadataClient.create();
MetadataResponse metadataResponse = client.get("/latest/dynamic/instance-identity/document");
Document instanceIdentity = metadataResponse.asDocument();

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great insight. Will implement it.


private static final Logger log = LoggerFactory.getLogger(MetadataResponse.class);

private static final JsonNodeParser JSON_NODE_PARSER = JsonNode.parser();
Copy link
Contributor

Choose a reason for hiding this comment

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

We probably want removeErrorLocations to be true on this parser, so that if credentials are in the metadata it won't include them in the exception message (which might be logged).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Noted.

Comment on lines +128 to +129
MetadataResponse metadataResponse = ec2Metadata.get("/latest/meta-data/ami-id");
assertThat(metadataResponse).isNull();
Copy link
Contributor

Choose a reason for hiding this comment

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

Returning null seems weird here. Any reason we wouldn't throw an exception? I'd think we'd never want get() to return null.

.filter(JsonNode::isString)
.map(JsonNode::asString)
.toArray(String[]::new);
} catch (Exception e) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Any specific reason we're swallowing this exception instead of throwing it?

Comment on lines +102 to +107
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>aws-json-protocol</artifactId>
<version>${awsjavasdk.version}</version>
<scope>compile</scope>
</dependency>
Copy link
Contributor

Choose a reason for hiding this comment

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

We should put something in the backlog to avoid needing this dependency at GA. I know it was needed for the DocumentMarshaller, but I think it's relatively small and we could copy it to avoid the dependency.

@sonarqubecloud
Copy link

SonarCloud Quality Gate failed.    Quality Gate failed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 8 Code Smells

26.2% 26.2% Coverage
3.4% 3.4% Duplication

@millems millems merged commit 9aabc3e into aws:feature/master/imds Jul 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants