From 0cba776c5077b77f73ac55806283e399069f1074 Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Fri, 15 Apr 2016 10:14:36 -0700 Subject: [PATCH] Incorporate feedback --- SUPPORTING_NEW_SERVICES.md | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/SUPPORTING_NEW_SERVICES.md b/SUPPORTING_NEW_SERVICES.md index 56ebf073a13c..8cdb9e04db17 100644 --- a/SUPPORTING_NEW_SERVICES.md +++ b/SUPPORTING_NEW_SERVICES.md @@ -23,24 +23,28 @@ The library should contain: * An interface extending the [`ServiceFactory`](https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/gcloud-java-core/src/main/java/com/google/cloud/ServiceFactory.java) interface. Example: [`DatastoreFactory`](https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/gcloud-java-datastore/src/main/java/com/google/cloud/datastore/DatastoreFactory.java) * A runtime exception class that extends [`BaseServiceException`](https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/gcloud-java-core/src/main/java/com/google/cloud/BaseServiceException.java), which is used to wrap service-related exceptions. Example: [`DatastoreException`](https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/gcloud-java-datastore/src/main/java/com/google/cloud/datastore/DatastoreException.java) * Classes representing model objects and request-related options. Model objects that correspond to service resources should have a subclass that provides functions related to that resource. For example, see [`BlobInfo`](https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/gcloud-java-storage/src/main/java/com/google/cloud/storage/BlobInfo.java) (the metadata class) and [`Blob`](https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/gcloud-java-storage/src/main/java/com/google/cloud/storage/Blob.java) (the functional class). The builders for both objects should implement a common interface or abstract class, and the functional subclass builder should delegate to the metadata class builder. +* Optional, request-related options classes, located in the interface that extends `Service.java`. Methods should accept these options as arguments when appropriate. A common option is to request that only specific fields of a model object should be included in a response. Typically such an option is created via a `fields(...)` method which accepts a vararg of type `Field` enum. The enum should implement the [FieldSelector](https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/gcloud-java-core/src/main/java/com/google/cloud/FieldSelector.java) interface. In general, make classes immutable whenever possible, providing builders as necessary. Make model object classes `java.util.Serializable`. Prefer making classes final, with the following exceptions: (1) functional objects and (2) classes in which the user cannot set all attributes. If a class cannot be made final, then `hashCode` or `equals` overrides should be made final if possible. `gcloud-java-core` provides functionality for code patterns used across `gcloud-java` libraries. The following are some important core concepts: -* Paging: Cloud services often expose page-based listing using page tokens. The [`Page`](https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/gcloud-java-core/src/main/java/com/google/cloud/Page.java) interface should be used for page-based listing. A `Page` contains an iterator over results in that page, as well as methods to get the next page and all results in future pages. `Page` requires a `NextPageFetcher` implementation (see the `NextPageFetcher` interface in [`PageImpl`](https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/gcloud-java-core/src/main/java/com/google/cloud/PageImpl.java)). This implementation should delegate constructing request options to the `nextRequestOptions` method in `PageImpl`. +* Paging: Google Cloud services often expose page-based listing using page tokens. The [`Page`](https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/gcloud-java-core/src/main/java/com/google/cloud/Page.java) interface should be used for page-based listing. A `Page` contains an iterator over results in that page, as well as methods to get the next page and all results in future pages. `Page` requires a `NextPageFetcher` implementation (see the `NextPageFetcher` interface in [`PageImpl`](https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/gcloud-java-core/src/main/java/com/google/cloud/PageImpl.java)). This implementation should delegate constructing request options to the `nextRequestOptions` method in `PageImpl`. * Exception handling: we have the notion of retryable versus non-retryable operations. These are encapsulated by `BaseServiceException`. Retryable error codes should be listed in the service's subclass of `BaseServiceException`. An operation should be considered retryable if it makes sense to retry (e.g. if there was a transient service error, not a fundamentally invalid request) and if the operation that triggered the exception is idempotent. Exceptions also contain information regarding whether the service rejected the request, meaning the operation was not applied. The `BaseServiceException` subclass should also provide methods to translate the exceptions given by the underlying autogeneraged client library. The [`ExceptionHandler`](https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/gcloud-java-core/src/main/java/com/google/cloud/ExceptionHandler.java) class intercepts and retries RPC calls when retryable exceptions are encountered. Note that some exceptions are masked in the SPI layer. For example, `get` and `delete` operations often return "404 Not Found" if the resource doesn't exist. Instead of throwing an exception in these cases, we often return `null` for `get` and `false` for `delete`. -* Batching: The [`BatchResult`](https://github.com/GoogleCloudPlatform/gcloud-java/blob/dns-alpha-batch/gcloud-java-core/src/main/java/com/google/cloud/BatchResult.java) class provides a simple way for users to combine RPC calls for performance enhancement. API for services that support batching should implement a batch class that contains methods similar to the methods in the `Service.java` subclass, but instead return a subclass of `BatchResult`. Also provide an SPI-layer class to collect batch requests and submit the batch. +* Batching: The [`BatchResult`](https://github.com/GoogleCloudPlatform/gcloud-java/blob/dns-alpha-batch/gcloud-java-core/src/main/java/com/google/cloud/BatchResult.java) class provides a simple way for users to combine RPC calls for performance enhancement. APIs for services that support batching should implement a batch class that contains methods similar to the methods in the `Service.java` subclass. A batch operation's return type should be a subclass of `BatchResult`. Also provide an SPI-layer class to collect batch requests and submit the batch. A batch instance should be created by the service API, preferably via a method called `batch()`. The batch should be submitted using the batch instance itself, preferably using a method named `submit()`. + +* IAM Policies: If the Google Cloud service supports [IAM](https://cloud.google.com/iam/docs/), you should provide a subclass of [`IamPolicy`](https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/gcloud-java-core/src/main/java/com/google/cloud/IamPolicy.java) in your API. [`Policy`](https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/gcloud-java-resourcemanager/src/main/java/com/google/cloud/resourcemanager/Policy.java) can be used to set default access control lists on Google Cloud projects as a whole. However, if users want to set policies on specific resources within a project, they will need to use the subclass you provide in your API. Notes/reminders: -* API layer classes should be located in the package `com.google.cloud.servicename`, where "servicename" corresponds to the name of the Cloud service. +* API layer classes should be located in the package `com.google.cloud.servicename`, where "servicename" corresponds to the name of the Google Cloud service. * Override the `ServiceOptions.defaultRetryParams()` method in your service's options class to align with the Service Level Agreement (SLA) given by the underlying service. See #857 and #860 for context. +* Override the `ServiceOptions.projectIdRequired()` method to return false when the service does not require requests to be associated with a specific project. * See conventions about overriding the `equals` and `hashCode` methods in the discussion of #892. -* While not all fields for model objects need to be exposed to the user, `gcloud-java` services should get and set all relevant fields when making RPC calls to the Cloud service's API. For example, since the `parent` field of Cloud Resource Manager Project objects is in alpha (at the time this is written) and not available to most users, `gcloud-java-resourcemanager` gets and sets the parent when interacting with the Cloud Resource Manager, but does not expose the parent to users. As a result, the user won't accidentally unset the parent when updating a project. +* While not all fields for model objects need to be exposed to the user, `gcloud-java` services should get and set all relevant fields when making RPC calls to the Google Cloud service's API. For example, since the `parent` field of Cloud Resource Manager Project objects is in alpha (at the time this was written) and not available to most users, `gcloud-java-resourcemanager` gets and sets the parent when interacting with the Cloud Resource Manager, but does not expose the parent to users. As a result, the user won't accidentally unset the parent when updating a project. * Be aware of differences in "update" behavior and name update/replace methods accordingly in your API. See #321 for context. * Do not expose third party libraries in the API. This has been a design choice from the beginning of the project, and all existing `gcloud-java` services adhere to this convention. -* Member variable getters and builder setters should not use the prefixes "get" or "set". +* Member variable getters and builder setters should not use the JavaBean get/set prefix style. * Any service-generated IDs for model objects should be named `generatedId()`. #### SPI layer @@ -59,24 +63,24 @@ There are three types of test helpers: * When there is no local emulator but the service is simple enough to write an emulator, you should do so. The emulator should listen to a port for requests, process those requests, and send responses back, being as true to the actual service as possible. Dependencies in this mock should be as lightweight as possible. Be sure to document differences between your emulator and the actual service. Examples of this type of test helper are `LocalResourceManagerHelper` and `LocalDnsHelper`. -* When there is no local emulator and the service is too complex to write a solid emulator, the test helper should contain methods to get options and separate test data from other user data. `RemoteStorageHelper` is an example of this type of test helper, since there is no local emulator for Google Cloud Storage (at the time that this is written) and because the Google Cloud Storage API is complex. `RemoteStorageHelper` has methods to: +* When there is no local emulator and the service is too complex to write a solid emulator, the test helper should contain methods to get options and to help isolate test data from production data. `RemoteStorageHelper` is an example of this type of test helper, since there is no local emulator for Google Cloud Storage (at the time that this was written) and because the Google Cloud Storage API is complex. `RemoteStorageHelper` has methods to: * Get service options settings. * Create a test bucket with a sufficiently obscure name (to separate the bucket from any of the users other data). * Clean up data left over from tests in that test bucket. #### Tests -API-level functionality should be well-covered by unit tests. Coders and reviewers should examine test coverage to ensure that important code paths are not being left untested. As of now, `gcloud-java` relies on integration tests to test the SPI layer. Unit tests for the API layer should be located in the package `com.google.cloud.servicename`. Integration tests should be placed in a separate package, `com.google.cloud.servicename.it`, which enables us to catch method access bugs. Unit tests for the test helper should be placed in the package `com.google.cloud.servicename.testing`. All unit tests run for pull requests, but integration tests are only run upon merging the pull request. We only run integration tests upon merging pull requests to avoid decrypting and exposing credentials to anybody who can create a pull request from a fork. To separate integration tests from unit tests,prefix integration test file names with "IT" (e.g. `ITDnsTest`). +API-level functionality should be well-covered by unit tests. Coders and reviewers should examine test coverage to ensure that important code paths are not being left untested. As of now, `gcloud-java` relies on integration tests to test the SPI layer. Unit tests for the API layer should be located in the package `com.google.cloud.servicename`. Integration tests should be placed in a separate package, `com.google.cloud.servicename.it`, which enables us to catch method access bugs. Unit tests for the test helper should be placed in the package `com.google.cloud.servicename.testing`. All unit tests run for pull requests, but integration tests are only run upon merging the pull request. We only run integration tests upon merging pull requests to avoid decrypting and exposing credentials to anybody who can create a pull request from a fork. Prefix integration test file names with "IT" (e.g. `ITDnsTest`) to separate integration tests from unit tests. Simple service-related tests should be added to [GoogleCloudPlatform/gcloud-java-examples](https://github.com/GoogleCloudPlatform/gcloud-java-examples/tree/master/test-apps). To test releases and platform-specific bugs, it's valuable to deploy the apps in that repository on App Engine, Compute Engine, and from your own desktop. #### Example application -The example application should be a simple command line interface for the service. It should use common library use patterns so that users see good examples of how to use `gcloud-java` when viewing the source code. Be sure to keep the examples up to date if/when there are updates that make the API cleaner and more concise. See examples of applications under the `gcloud-java-examples` folder. The example application should be in the package `com.google.cloud.examples.servicename`. +The example application should be a simple command line interface for the service. It should use common library usage patterns so that users can have good examples of how to use the service. Be sure to keep the examples up to date if/when there are updates that make the API cleaner and more concise. See examples of applications under the `gcloud-java-examples` folder. The example application should be in the package `com.google.cloud.examples.servicename`. #### Documentation -* Include a summary of the service and code snippets on the main repository's README. These snippets should be simple and cover a few common usage patterns. The README snippets should also be added to `gcloud-java-examples` in the package `com.google.cloud.examples.servicename.snippets`. Placing snippet code in the repository ensures that the snippet code builds when Travis CI is run. For this purpose, README snippets and the snippet code in `gcloud-java-examples` should be kept in sync. As of yet, we do not have unit tests for snippets, so the snippets should be tested periodically, especially after any relevant library updates. +* Include a summary of the service and code snippets on the main repository's README. These snippets should be simple and cover a few common usage patterns. The README snippets should also be added to `gcloud-java-examples` in the package `com.google.cloud.examples.servicename.snippets`. Placing snippet code in the repository ensures that the snippet code builds when Travis CI is run. For this purpose, README snippets and the snippet code in `gcloud-java-examples` should be kept in sync. Issue #753 suggests autogenerating READMEs, which would be useful for keeping snippet code in sync. As of yet, we do not have unit tests for snippets, so the snippets should be tested periodically, especially after any relevant library updates. * Create a README in the service's folder. This README should mimic the structure of other services' READMEs. In particular, you should create a step-by-step "Getting Started" guide. See [`gcloud-java-datastore`'s README](https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/gcloud-java-datastore/README.md) for reference. All code in that step-by-step guide should also be included in the `gcloud-java-examples` snippets package. * The API and test helper packages should have `package-info.java` files. These files should contain descriptions of the packages as well as simple example code and/or links to code snippets. * Public methods, classes, and builders should contain meaningful Javadoc. When possible, copy docs from the service's `cloud.google.com` API documentation and provide `@see` links to relevant Google Cloud web pages. Document both unchecked and checked exceptions. @@ -89,20 +93,21 @@ Notes/reminders: ### Workflow -New services should be created in a branch based on `master`. The branch name should include the suffix "-alpha". For example, while developing `gcloud-java-pubsub`, all Pub/Sub related work should be done in `pubsub-alpha`. All code should be submitted through pull requests from a branch on a forked repository. Limiting pull request size is very helpful for reviewers. All code that is merged into the branch should be standalone and well-tested. Any todo comments in the code should have an associated Github issue number for tracking purposes. You should periodically pull updates from the master branch, especially if there are project-wide updates or if relevant changes have been made to the core utilities library, `gcloud-java-core`. +New services should be created in a branch based on `master`. The branch name should include the suffix "-alpha". For example, while developing `gcloud-java-pubsub`, all Pub/Sub related work should be done in `pubsub-alpha`. All code should be submitted through pull requests from a branch on a forked repository. Limiting pull request size is very helpful for reviewers. All code that is merged into the branch should be standalone and well-tested. Any todo comments in the code should have an associated Github issue number for tracking purposes. You should periodically pull updates from the master branch, especially if there are project-wide updates or if relevant changes have been made to the core utilities library, `gcloud-java-core`. This PR should only contain commits related to the merge to ease code review. Create at least two milestones (stable and future) for your service and an issue tag with the service name. Create issues for any to-do items and tag them appropriately. This keeps an up-to-date short-term to-do list and also allows for longer term roadmaps. Be sure you've configured the base folder's `pom.xml` correctly. * Add your module to the base directory's `pom.xml` file under the list of modules (in alphabetical order). * Add your module to the javadoc packaging settings. See PR #802 for an example. - [comment]: # (TODO: uncomment when gcs-nio branch is merged into master) * Add your example to the assembler plugin. PR #839 includes examples of using appassembler. + -When your service is complete, contact the service owners to get a review. The primary purpose of this review is to make sure that the gcloud-java service interacts with the Cloud service properly. Present the reviewers with a link to the Github repository, as well as your (updated) design document that details the API. +When your service is complete, contact the service owners to get a review. The primary purpose of this review is to make sure that the gcloud-java service interacts with the Google Cloud service properly. Present the reviewers with a link to the Github repository, as well as your (updated) design document that details the API. ### Closing remarks * Efforts should be made to maintain the current style of the repository and a consistent style between gcloud-java services. - * We anticipate that people will sometimes use multiple `gcloud-java` services, so we don't want differences in conventions from library to library. Look at existing `gcloud-java` services to see coding and naming conventions. - * Codacy is configured to report on pull requests about style issues. Whenever possible, those comments should be addressed. Coders and reviewers should also run a linter on pull requests, because the Codacy tool may not catch all style errors. -* When weighing which services to add, consider that a hand-crafted `gcloud-java` service API is especially useful if it can abstract away and/or make java-idiomatic significant parts of a service's autogenerated API. + * We anticipate that people will sometimes use multiple `gcloud-java` services, so we don't want differences in conventions from one service API to another. Look at existing `gcloud-java` services to see coding and naming conventions. + * Codacy is configured to report on pull requests about style issues. Whenever possible, those comments should be addressed. Coders and reviewers should also run a linter on pull requests, because the Codacy tool may not catch all style errors. There is a [Checkstyle configuration](https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/checkstyle.xml) provided in the repository. +* When weighing which services to add, consider that a hand-crafted `gcloud-java` service API is especially useful if it can simplify the usability of the autogenerated client.