doc.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. // Copyright 2014 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /*
  15. Package cloud is the root of the packages used to access Google Cloud
  16. Services. See https://godoc.org/cloud.google.com/go for a full list
  17. of sub-packages.
  18. # Client Options
  19. All clients in sub-packages are configurable via client options. These options are
  20. described here: https://godoc.org/google.golang.org/api/option.
  21. ## Endpoint Override
  22. Endpoint configuration is used to specify the URL to which requests are
  23. sent. It is used for services that support or require regional endpoints, as well
  24. as for other use cases such as [testing against fake
  25. servers](https://github.com/googleapis/google-cloud-go/blob/main/testing.md#testing-grpc-services-using-fakes).
  26. For example, the Vertex AI service recommends that you configure the endpoint to the
  27. location with the features you want that is closest to your physical location or the
  28. location of your users. There is no global endpoint for Vertex AI. See
  29. [Vertex AI - Locations](https://cloud.google.com/vertex-ai/docs/general/locations)
  30. for more details. The following example demonstrates configuring a Vertex AI client
  31. with a regional endpoint:
  32. ctx := context.Background()
  33. endpoint := "us-central1-aiplatform.googleapis.com:443"
  34. client, err := aiplatform.NewDatasetClient(ctx, option.WithEndpoint(endpoint))
  35. # Authentication and Authorization
  36. All the clients in sub-packages support authentication via Google Application Default
  37. Credentials (see https://cloud.google.com/docs/authentication/production), or
  38. by providing a JSON key file for a Service Account. See examples below.
  39. Google Application Default Credentials (ADC) is the recommended way to authorize
  40. and authenticate clients. For information on how to create and obtain
  41. Application Default Credentials, see
  42. https://cloud.google.com/docs/authentication/production. Here is an example
  43. of a client using ADC to authenticate:
  44. client, err := secretmanager.NewClient(context.Background())
  45. if err != nil {
  46. // TODO: handle error.
  47. }
  48. _ = client // Use the client.
  49. You can use a file with credentials to authenticate and authorize, such as a JSON
  50. key file associated with a Google service account. Service Account keys can be
  51. created and downloaded from
  52. https://console.cloud.google.com/iam-admin/serviceaccounts. This example uses
  53. the Secret Manger client, but the same steps apply to the other client libraries
  54. underneath this package. Example:
  55. client, err := secretmanager.NewClient(context.Background(),
  56. option.WithCredentialsFile("/path/to/service-account-key.json"))
  57. if err != nil {
  58. // TODO: handle error.
  59. }
  60. _ = client // Use the client.
  61. In some cases (for instance, you don't want to store secrets on disk), you can
  62. create credentials from in-memory JSON and use the WithCredentials option.
  63. The google package in this example is at golang.org/x/oauth2/google.
  64. This example uses the Secret Manager client, but the same steps apply to
  65. the other client libraries underneath this package. Note that scopes can be
  66. found at https://developers.google.com/identity/protocols/oauth2/scopes, and
  67. are also provided in all auto-generated libraries: for example,
  68. cloud.google.com/go/secretmanager/apiv1 provides DefaultAuthScopes. Example:
  69. ctx := context.Background()
  70. creds, err := google.CredentialsFromJSON(ctx, []byte("JSON creds"), secretmanager.DefaultAuthScopes()...)
  71. if err != nil {
  72. // TODO: handle error.
  73. }
  74. client, err := secretmanager.NewClient(ctx, option.WithCredentials(creds))
  75. if err != nil {
  76. // TODO: handle error.
  77. }
  78. _ = client // Use the client.
  79. # Timeouts and Cancellation
  80. By default, non-streaming methods, like Create or Get, will have a default
  81. deadline applied to the context provided at call time, unless a context deadline
  82. is already set. Streaming methods have no default deadline and will run
  83. indefinitely. To set timeouts or arrange for cancellation, use contexts.
  84. Transient errors will be retried when correctness allows.
  85. Here is an example of setting a timeout for an RPC using context.WithTimeout:
  86. ctx := context.Background()
  87. // Do not set a timeout on the context passed to NewClient: dialing happens
  88. // asynchronously, and the context is used to refresh credentials in the
  89. // background.
  90. client, err := secretmanager.NewClient(ctx)
  91. if err != nil {
  92. // TODO: handle error.
  93. }
  94. // Time out if it takes more than 10 seconds to create a dataset.
  95. tctx, cancel := context.WithTimeout(ctx, 10*time.Second)
  96. defer cancel() // Always call cancel.
  97. req := &secretmanagerpb.DeleteSecretRequest{Name: "projects/project-id/secrets/name"}
  98. if err := client.DeleteSecret(tctx, req); err != nil {
  99. // TODO: handle error.
  100. }
  101. Here is an example of setting a timeout for an RPC using gax.WithTimeout:
  102. ctx := context.Background()
  103. // Do not set a timeout on the context passed to NewClient: dialing happens
  104. // asynchronously, and the context is used to refresh credentials in the
  105. // background.
  106. client, err := secretmanager.NewClient(ctx)
  107. if err != nil {
  108. // TODO: handle error.
  109. }
  110. req := &secretmanagerpb.DeleteSecretRequest{Name: "projects/project-id/secrets/name"}
  111. // Time out if it takes more than 10 seconds to create a dataset.
  112. if err := client.DeleteSecret(tctx, req, gax.WithTimeout(10*time.Second)); err != nil {
  113. // TODO: handle error.
  114. }
  115. Here is an example of how to arrange for an RPC to be canceled, use context.WithCancel:
  116. ctx := context.Background()
  117. // Do not cancel the context passed to NewClient: dialing happens asynchronously,
  118. // and the context is used to refresh credentials in the background.
  119. client, err := secretmanager.NewClient(ctx)
  120. if err != nil {
  121. // TODO: handle error.
  122. }
  123. cctx, cancel := context.WithCancel(ctx)
  124. defer cancel() // Always call cancel.
  125. // TODO: Make the cancel function available to whatever might want to cancel the
  126. // call--perhaps a GUI button.
  127. req := &secretmanagerpb.DeleteSecretRequest{Name: "projects/proj/secrets/name"}
  128. if err := client.DeleteSecret(cctx, req); err != nil {
  129. // TODO: handle error.
  130. }
  131. Do not attempt to control the initial connection (dialing) of a service by setting a
  132. timeout on the context passed to NewClient. Dialing is non-blocking, so timeouts
  133. would be ineffective and would only interfere with credential refreshing, which uses
  134. the same context.
  135. # Connection Pooling
  136. Connection pooling differs in clients based on their transport. Cloud
  137. clients either rely on HTTP or gRPC transports to communicate
  138. with Google Cloud.
  139. Cloud clients that use HTTP (bigquery, compute, storage, and translate) rely on the
  140. underlying HTTP transport to cache connections for later re-use. These are cached to
  141. the default http.MaxIdleConns and http.MaxIdleConnsPerHost settings in
  142. http.DefaultTransport.
  143. For gRPC clients (all others in this repo), connection pooling is configurable. Users
  144. of cloud client libraries may specify option.WithGRPCConnectionPool(n) as a client
  145. option to NewClient calls. This configures the underlying gRPC connections to be
  146. pooled and addressed in a round robin fashion.
  147. # Using the Libraries with Docker
  148. Minimal docker images like Alpine lack CA certificates. This causes RPCs to appear to
  149. hang, because gRPC retries indefinitely. See https://github.com/googleapis/google-cloud-go/issues/928
  150. for more information.
  151. # Debugging
  152. To see gRPC logs, set the environment variable GRPC_GO_LOG_SEVERITY_LEVEL. See
  153. https://godoc.org/google.golang.org/grpc/grpclog for more information.
  154. For HTTP logging, set the GODEBUG environment variable to "http2debug=1" or "http2debug=2".
  155. # Inspecting errors
  156. Most of the errors returned by the generated clients are wrapped in an
  157. [github.com/googleapis/gax-go/v2/apierror.APIError] and can be further unwrapped
  158. into a [google.golang.org/grpc/status.Status] or
  159. [google.golang.org/api/googleapi.Error] depending
  160. on the transport used to make the call (gRPC or REST). Converting your errors to
  161. these types can be a useful way to get more information about what went wrong
  162. while debugging.
  163. [github.com/googleapis/gax-go/v2/apierror.APIError] gives access to specific
  164. details in the error. The transport-specific errors can still be unwrapped using
  165. the [github.com/googleapis/gax-go/v2/apierror.APIError].
  166. if err != nil {
  167. var ae *apierror.APIError
  168. if errors.As(err, &ae) {
  169. log.Println(ae.Reason())
  170. log.Println(ae.Details().Help.GetLinks())
  171. }
  172. }
  173. If the gRPC transport was used, the [google.golang.org/grpc/status.Status] can
  174. still be parsed using the [google.golang.org/grpc/status.FromError] function.
  175. if err != nil {
  176. if s, ok := status.FromError(err); ok {
  177. log.Println(s.Message())
  178. for _, d := range s.Proto().Details {
  179. log.Println(d)
  180. }
  181. }
  182. }
  183. If the REST transport was used, the [google.golang.org/api/googleapi.Error] can
  184. be parsed in a similar way, allowing access to details such as the HTTP response
  185. code.
  186. if err != nil {
  187. var gerr *googleapi.Error
  188. if errors.As(err, &gerr) {
  189. log.Println(gerr.Message)
  190. }
  191. }
  192. # Client Stability
  193. Clients in this repository are considered alpha or beta unless otherwise
  194. marked as stable in the README.md. Semver is not used to communicate stability
  195. of clients.
  196. Alpha and beta clients may change or go away without notice.
  197. Clients marked stable will maintain compatibility with future versions for as
  198. long as we can reasonably sustain. Incompatible changes might be made in some
  199. situations, including:
  200. - Security bugs may prompt backwards-incompatible changes.
  201. - Situations in which components are no longer feasible to maintain without
  202. making breaking changes, including removal.
  203. - Parts of the client surface may be outright unstable and subject to change.
  204. These parts of the surface will be labeled with the note, "It is EXPERIMENTAL
  205. and subject to change or removal without notice."
  206. */
  207. package cloud // import "cloud.google.com/go"