Skip to content
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

Add convenience layer to generated code for Event Grid Track 2 #14442

Merged
merged 17 commits into from
Sep 5, 2020

Conversation

Soreloser2
Copy link
Contributor

This PR is based on #14440, and new commits start at 51b5908.

  • Create Clients and public event model classes on top of generated system events.
  • Write tests for publish and consumption of events
  • Document with readme, samples, javadocs

Autorest regenerated using up to date version for track 2
pom file incorporated to jacoco and dependencies for module handled
issue around capitalization of etag discrepancy fixed
no test coverage yet
Autorest regenerated using up to date version for track 2
pom file incorporated to jacoco and dependencies for module handled
issue around capitalization of etag discrepancy fixed
no test coverage yet
Autogenerate EventGrid track 2 classes using the swagger. All files except EventGridPublisherImplTests.java, pom.xml, and the swagger readme.md are autogenerated.
Convenience layer for EventGrid Track 2 on top of autogenerated code.
Add a few missing javadoc comments and remove a few comments from private methods.
test and pom edits mostly
Rename methods and use StepVerifier instead of blocking
@Soreloser2 Soreloser2 self-assigned this Aug 28, 2020
@Soreloser2 Soreloser2 added Client This issue points to a problem in the data-plane of the library. Event Grid labels Aug 28, 2020
@Soreloser2 Soreloser2 added this to the [2020] September milestone Aug 28, 2020
Drop explicit support for non-JSON data. Modify/fix tests slightly.
SharedAccessSignature -> SAS, refactor methods and classes
@Soreloser2
Copy link
Contributor Author

/azp run

@azure-pipelines
Copy link

You have several pipelines (over 10) configured to build pull requests in this repository. Specify which pipelines you would like to run by using /azp run [pipelines] command. You can specify multiple pipelines using a comma separated list.

@Soreloser2
Copy link
Contributor Author

/azp run java - eventgrid - ci, java - eventgrid - ci (Build Test Windows - Java 11)

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

conforms to live test expected custom event properties
In order to start sending events, we need an `EventGridPublisherClient`. Here is code to
create the synchronous and the asynchronous versions. Note that a shared access signature can
be used instead of a key in any of these samples by calling the `sharedAccessSignatureCredential`
method instead of `keyCredential'.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
method instead of `keyCredential'.
method instead of `keyCredential`.

Comment on lines 182 to 190
public <T> T getData(Class<T> clazz, JsonSerializer dataDeserializer) {
if (!parsed) {
// data was set instead of parsed, throw exception because we don't know how the data relates to clazz
throw new IllegalStateException("This method should only be called on events created through the parse method");
}

return dataDeserializer.deserialize(new ByteArrayInputStream((byte[]) this.event.getData()),
TypeReference.createInstance(clazz));
}
Copy link
Member

Choose a reason for hiding this comment

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

We should consider adding async overloads for these since dataDeserializer supports async deserialization.

Comment on lines 50 to 52
public EventGridServiceVersion getServiceVersion() {
return EventGridServiceVersion.V2018_01_01;
}
Copy link
Member

Choose a reason for hiding this comment

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

Instead of hardcoding the version here, the builder (since the builder should have a setter for service version) should pass this to the constructor and the client should use that as an instance variable that will be returned here.

return withContext(context -> publishEvents(events, context));
}

Mono<Void> publishEvents(Iterable<EventGridEvent> events, Context context) {
Copy link
Member

Choose a reason for hiding this comment

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

Why is this called publish? It's better to use the same terminology even for non-public methods.

Comment on lines 85 to 88
List<com.azure.messaging.eventgrid.implementation.models.CloudEvent> implList = new ArrayList<>();
for (CloudEvent event : events) {
implList.add(event.toImpl());
}
Copy link
Member

Choose a reason for hiding this comment

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

Converting to inner types should happen lazily when the Mono is subscribed. Do the same at other places too where the list operations are performed eagerly.

*
* @return the builder itself.
*/
public EventGridPublisherClientBuilder keyCredential(AzureKeyCredential credential) {
Copy link
Member

Choose a reason for hiding this comment

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

The method should be named credential to be consistent with other SDK builders.

return String.format("%s&%s=%s", unsignedSas, signKey, encodedSignature);

} catch (NoSuchAlgorithmException | UnsupportedEncodingException | InvalidKeyException e) {
e.printStackTrace();
Copy link
Member

Choose a reason for hiding this comment

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

Use logs instead of e.printStackTrace().

*/
public EventGridSasCredential(String sas) {
if (CoreUtils.isNullOrEmpty(sas)) {
throw new IllegalArgumentException("the access signature cannot be null or empty");
Copy link
Member

Choose a reason for hiding this comment

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

Log and throw.

import java.util.Locale;
import java.util.Map;

public final class SystemEventMappings {
Copy link
Member

Choose a reason for hiding this comment

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

Missing javadoc.

import java.time.ZoneOffset;
import java.util.Random;

public class ClassTime {
Copy link
Member

Choose a reason for hiding this comment

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

Add javadocs for all samples to explain what the sample does.

Copy link
Member

@srnagar srnagar left a comment

Choose a reason for hiding this comment

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

Overall, looks great and it's good to see all design guidelines being followed! Thanks @Soreloser2

import com.azure.core.annotation.Fluent;
import com.azure.core.serializer.json.jackson.JacksonJsonSerializerBuilder;
import com.azure.core.util.CoreUtils;
import com.azure.core.util.serializer.*;
Copy link
Member

Choose a reason for hiding this comment

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

Generally I think we try to avoid * imports, I know I had to go dive into IntelliJ settings to make it stop doing this by default:

Settings > Editor > Code Style > Java > set both count to use import with '*' settings to 99.

public Object getData() {
if (!parsed) {
// data was set instead of parsed, throw error
throw new IllegalStateException("This method should only be called on events created through the parse method");
Copy link
Member

Choose a reason for hiding this comment

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

I think Srikanta already called this out, but instead of throwing directly, we should get a ClientLogger instance and call throw logger.logExceptionAsWarning/Error(...) instead.

@@ -0,0 +1,55 @@
package com.azure.messaging.eventgrid;
Copy link
Member

Choose a reason for hiding this comment

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

Make sure all source files have the copyright & license header.

Variety of changes, major ones include:
- Async methods on generic getData
- Lazy list creation for send methods
- Credential methods both renamed to `credential`
- Logging errors instead of throwing
- Additional missing javadoc comments
@Soreloser2
Copy link
Contributor Author

Thanks for all the feedback, @srnagar and @bsiegel! I'm pretty sure I addressed all the comments, but let me know if there's anything else.

Copy link
Member

@srnagar srnagar left a comment

Choose a reason for hiding this comment

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

LGTM! Some minor cosmetic changes required before merging.

import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
import java.time.OffsetDateTime;
import java.util.*;
Copy link
Member

Choose a reason for hiding this comment

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

Enumerate all imports instead of using *. Wonder why this is happening on generated code though. Is this how autorest generated? If that's the case, let me know. I will take a look.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I believe this has to do with my IntelliJ settings, because this is code written over the autogenerated model in implementation. Brandon mentioned this in a different file as well but my settings should be fixed now.

public <T> Mono<T> getDataAsync(Class<T> clazz, JsonSerializer dataDeserializer) {
if (!parsed) {
// data was set instead of parsed, throw exception because we don't know how the data relates to clazz
throw logger.logExceptionAsError(new IllegalStateException(
Copy link
Member

Choose a reason for hiding this comment

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

For async methods, exceptions should be passed through the publisher. Use FluxUtil.monoError().

Comment on lines +107 to +109
return Flux.fromIterable(events)
.collectList()
.flatMap(list -> this.impl.publishCustomEventEventsAsync(this.hostname, list, context));
Copy link
Member

Choose a reason for hiding this comment

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

Unfortunately, we'll have to iterate through the collection twice - once to convert Iterable to list and then when serializing. This impacts performance. Not sure, if we can avoid this without changing the API to take a List instead.

return String.format("%s&%s=%s", unsignedSas, signKey, encodedSignature);

} catch (NoSuchAlgorithmException | UnsupportedEncodingException | InvalidKeyException e) {
throw new RuntimeException(logger.logThrowableAsError(e));
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
throw new RuntimeException(logger.logThrowableAsError(e));
throw logger.logThrowableAsError(new RuntimeException(e));

Comment on lines 78 to 80
if (CoreUtils.isNullOrEmpty(sas)) {
throw logger.logExceptionAsError(new IllegalArgumentException("the access signature cannot be null or empty"));
}
Copy link
Member

Choose a reason for hiding this comment

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

https://azure.github.io/azure-sdk/java_implementation.html#java-errors-system-errors
Split the exception into NPE and IllegalArgumentException when the input is null and empty respectively.

Comment on lines 1 to 3
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for
// license information.
Copy link
Member

Choose a reason for hiding this comment

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

License headers should be the following in all Java files:
https://docs.opensource.microsoft.com/content/releasing/copyright-headers.html#java

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

mostly minor changes, except a small license change to all source files, including generated ones.
@Soreloser2 Soreloser2 merged commit 18528bd into Azure:master Sep 5, 2020
@Soreloser2 Soreloser2 deleted the tsdahl/written branch September 5, 2020 00:42
openapi-sdkautomation bot pushed a commit to AzureSDKAutomation/azure-sdk-for-java that referenced this pull request Aug 26, 2021
Mitryakh/network march (Azure#15709)

* Adds base for updating Microsoft.Network from version stable/2021-02-01 to version 2021-03-01

* Updates readme

* Updates API version in new specs and examples

* Add new parameter to enable/disable BGP route translation for NAT VirtualWan VpnGateway (Azure#14462)

* Adding Bastion Host Configuration Features (Azure#14442)

Co-authored-by: Abhishek Shah <shabhis@microsoft.com>

* Add PL ASG (Azure#14620)

* Add PL ASG

* Prettier

* [Private Link] Private Endpoint Ipconfigurations swagger for 2021-03-01 (Azure#14662)

* Ipconfigurations swagger

* Ran prettier check

* fixing conflicting files

* fixing lint

* Fixing linter diff

* Lint diff

* resolving comments

* fixing conflict

* Fioxing

* running prettier

* Fixing linter comment

Co-authored-by: Shane Baden <shbaden@microsoft.com>

* Add missing properties of ServiceEndpointPolicy (Azure#14948)

* Add missing 'type' and 'serviceAlias' for ServiceEndpointPolicy related API response

* Add missing 'contextualServiceEndpointPolicies'

* fix bug

* fix type

* Add missing properties to ServiceEndpointPolicy

* Revert "fix type"

This reverts commit 3a49beb66d9bbb288fa0d3baaeaf5aaf2bbc6ee9.

* Revert "fix bug"

This reverts commit a4fff4ae925b01ed0cfb07b1594fb95e2c6649c7.

* Revert "Add missing 'contextualServiceEndpointPolicies'"

This reverts commit ccf19167abc61b90bce6b5204dc3b2a64fdf4ca4.

* Revert "Add missing 'type' and 'serviceAlias' for ServiceEndpointPolicy related API response"

This reverts commit e532cc1118241a74902e4362c32c25c11a764933.

Co-authored-by: Xu Wang <wax@microsoft.com>

* add new service tag details api (Azure#14958)

* add new service tag details api

* prettier check fixes

* fixing lint issue, additional properties issue

* Add Load distribution policy and global configuration to AppGW (Azure#14790)

* add Load distribution policy and global configuration to Application Gateway resource

* prettify

* remove provisioning state

* add public ssh key to network virtual appliance (Azure#14988)

* Changes needed for BgpEndpoint (Azure#14800)

* changes needed for BgpEndpoint

* prettier check

* model validation fix

* fix description

* update again

* Multi QoS support for DSCP configuration (Azure#15120)

* Updating the DSCP jsons to reflect the  multi-qos support

* Adding support to multi-qos

* adding the type object to NrpQos

* fixing the prettier
removing my update to the settings

* fixing the missing type object error. seeing if this also fixes the additional attributes exception

* fixing the prettier exception

* almost forgot this, it was refactor from NrpQos to QosDefinition. NRPQos is already being used in Networking repo

* copying what I did with my other objects

* names didn't match with what I have in NRP's src

* forgot to update the examples with the updates in the specification. updating it

* fixing the merge exceptions

* Add SQL Setting to Firewall Policy (Azure#15110)

* Add SQL Setting to Firewall Policy

* fix lint check except for object type

* resolve linter error

* network: fix newly added targets field to an array and missing sub-resource properties in applicationGateways.json (Azure#15318)

* fix: change target to an array and add subresource properties

* fix target examples

* Support to update tags for BastionHost resource (Azure#15446)

* Initial commit

* Validation fixes

* Update bastionHost.json

Co-authored-by: Abhishek Shah <shabhis@microsoft.com>

* Azure FW - Explicit Proxy feature swagger change (Azure#15017)

* explicit proxy swagger change

* prettier check fix

* lint check

* prettier fix

* revert unnecessary file change

* Add kind to virtual hub (Azure#15562)

* Adding customnetworkinterfacename attribute to Private Endpoint (Azure#15574)

Co-authored-by: Shane Baden <shbaden@microsoft.com>

* Added 3 new properties to InboundNatRule resource (Azure#15611)

* Added the missins properties to fix the change breaking update (Azure#15732)

Co-authored-by: Nilambari <nilamd@microsoft.com>
Co-authored-by: Abhishek Shah <shah.abhi7860@gmail.com>
Co-authored-by: Abhishek Shah <shabhis@microsoft.com>
Co-authored-by: Yang Shi <yangshi93@gmail.com>
Co-authored-by: Shane Baden <naruto.shane@gmail.com>
Co-authored-by: Shane Baden <shbaden@microsoft.com>
Co-authored-by: Xu Wang <wangxu724@gmail.com>
Co-authored-by: Xu Wang <wax@microsoft.com>
Co-authored-by: guptas14 <71726901+guptas14@users.noreply.github.com>
Co-authored-by: Akshay Gupta <aksgupta@microsoft.com>
Co-authored-by: litchiyangMSFT <64560090+litchiyangMSFT@users.noreply.github.com>
Co-authored-by: Ritvika Reddy Nagula <rinagula@microsoft.com>
Co-authored-by: David Velasco <davelasc@microsoft.com>
Co-authored-by: Jiejiong Wu <b564518128@users.noreply.github.com>
Co-authored-by: tinawu6 <78238424+tinawu6@users.noreply.github.com>
Co-authored-by: irrogozh <irrogozh@microsoft.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Client This issue points to a problem in the data-plane of the library. Event Grid
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants