You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

249 lines
9.0 KiB

  1. // Copyright 2018 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. // https://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. // AUTO-GENERATED CODE. DO NOT EDIT.
  15. package asset
  16. import (
  17. "context"
  18. "time"
  19. "cloud.google.com/go/longrunning"
  20. lroauto "cloud.google.com/go/longrunning/autogen"
  21. gax "github.com/googleapis/gax-go/v2"
  22. "google.golang.org/api/option"
  23. "google.golang.org/api/transport"
  24. assetpb "google.golang.org/genproto/googleapis/cloud/asset/v1beta1"
  25. longrunningpb "google.golang.org/genproto/googleapis/longrunning"
  26. "google.golang.org/grpc"
  27. "google.golang.org/grpc/codes"
  28. "google.golang.org/grpc/metadata"
  29. )
  30. // CallOptions contains the retry settings for each method of Client.
  31. type CallOptions struct {
  32. ExportAssets []gax.CallOption
  33. BatchGetAssetsHistory []gax.CallOption
  34. }
  35. func defaultClientOptions() []option.ClientOption {
  36. return []option.ClientOption{
  37. option.WithEndpoint("cloudasset.googleapis.com:443"),
  38. option.WithScopes(DefaultAuthScopes()...),
  39. }
  40. }
  41. func defaultCallOptions() *CallOptions {
  42. retry := map[[2]string][]gax.CallOption{
  43. {"default", "idempotent"}: {
  44. gax.WithRetry(func() gax.Retryer {
  45. return gax.OnCodes([]codes.Code{
  46. codes.DeadlineExceeded,
  47. codes.Unavailable,
  48. }, gax.Backoff{
  49. Initial: 100 * time.Millisecond,
  50. Max: 60000 * time.Millisecond,
  51. Multiplier: 1.3,
  52. })
  53. }),
  54. },
  55. }
  56. return &CallOptions{
  57. ExportAssets: retry[[2]string{"default", "non_idempotent"}],
  58. BatchGetAssetsHistory: retry[[2]string{"default", "idempotent"}],
  59. }
  60. }
  61. // Client is a client for interacting with Cloud Asset API.
  62. //
  63. // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
  64. type Client struct {
  65. // The connection to the service.
  66. conn *grpc.ClientConn
  67. // The gRPC API client.
  68. client assetpb.AssetServiceClient
  69. // LROClient is used internally to handle longrunning operations.
  70. // It is exposed so that its CallOptions can be modified if required.
  71. // Users should not Close this client.
  72. LROClient *lroauto.OperationsClient
  73. // The call options for this service.
  74. CallOptions *CallOptions
  75. // The x-goog-* metadata to be sent with each request.
  76. xGoogMetadata metadata.MD
  77. }
  78. // NewClient creates a new asset service client.
  79. //
  80. // Asset service definition.
  81. func NewClient(ctx context.Context, opts ...option.ClientOption) (*Client, error) {
  82. conn, err := transport.DialGRPC(ctx, append(defaultClientOptions(), opts...)...)
  83. if err != nil {
  84. return nil, err
  85. }
  86. c := &Client{
  87. conn: conn,
  88. CallOptions: defaultCallOptions(),
  89. client: assetpb.NewAssetServiceClient(conn),
  90. }
  91. c.setGoogleClientInfo()
  92. c.LROClient, err = lroauto.NewOperationsClient(ctx, option.WithGRPCConn(conn))
  93. if err != nil {
  94. // This error "should not happen", since we are just reusing old connection
  95. // and never actually need to dial.
  96. // If this does happen, we could leak conn. However, we cannot close conn:
  97. // If the user invoked the function with option.WithGRPCConn,
  98. // we would close a connection that's still in use.
  99. // TODO(pongad): investigate error conditions.
  100. return nil, err
  101. }
  102. return c, nil
  103. }
  104. // Connection returns the client's connection to the API service.
  105. func (c *Client) Connection() *grpc.ClientConn {
  106. return c.conn
  107. }
  108. // Close closes the connection to the API service. The user should invoke this when
  109. // the client is no longer required.
  110. func (c *Client) Close() error {
  111. return c.conn.Close()
  112. }
  113. // setGoogleClientInfo sets the name and version of the application in
  114. // the `x-goog-api-client` header passed on each request. Intended for
  115. // use by Google-written clients.
  116. func (c *Client) setGoogleClientInfo(keyval ...string) {
  117. kv := append([]string{"gl-go", versionGo()}, keyval...)
  118. kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version)
  119. c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
  120. }
  121. // ExportAssets exports assets with time and resource types to a given Cloud Storage
  122. // location. The output format is newline-delimited JSON.
  123. // This API implements the [google.longrunning.Operation][google.longrunning.Operation] API allowing you
  124. // to keep track of the export.
  125. func (c *Client) ExportAssets(ctx context.Context, req *assetpb.ExportAssetsRequest, opts ...gax.CallOption) (*ExportAssetsOperation, error) {
  126. ctx = insertMetadata(ctx, c.xGoogMetadata)
  127. opts = append(c.CallOptions.ExportAssets[0:len(c.CallOptions.ExportAssets):len(c.CallOptions.ExportAssets)], opts...)
  128. var resp *longrunningpb.Operation
  129. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  130. var err error
  131. resp, err = c.client.ExportAssets(ctx, req, settings.GRPC...)
  132. return err
  133. }, opts...)
  134. if err != nil {
  135. return nil, err
  136. }
  137. return &ExportAssetsOperation{
  138. lro: longrunning.InternalNewOperation(c.LROClient, resp),
  139. }, nil
  140. }
  141. // BatchGetAssetsHistory batch gets the update history of assets that overlap a time window.
  142. // For RESOURCE content, this API outputs history with asset in both
  143. // non-delete or deleted status.
  144. // For IAM_POLICY content, this API outputs history when the asset and its
  145. // attached IAM POLICY both exist. This can create gaps in the output history.
  146. func (c *Client) BatchGetAssetsHistory(ctx context.Context, req *assetpb.BatchGetAssetsHistoryRequest, opts ...gax.CallOption) (*assetpb.BatchGetAssetsHistoryResponse, error) {
  147. ctx = insertMetadata(ctx, c.xGoogMetadata)
  148. opts = append(c.CallOptions.BatchGetAssetsHistory[0:len(c.CallOptions.BatchGetAssetsHistory):len(c.CallOptions.BatchGetAssetsHistory)], opts...)
  149. var resp *assetpb.BatchGetAssetsHistoryResponse
  150. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  151. var err error
  152. resp, err = c.client.BatchGetAssetsHistory(ctx, req, settings.GRPC...)
  153. return err
  154. }, opts...)
  155. if err != nil {
  156. return nil, err
  157. }
  158. return resp, nil
  159. }
  160. // ExportAssetsOperation manages a long-running operation from ExportAssets.
  161. type ExportAssetsOperation struct {
  162. lro *longrunning.Operation
  163. }
  164. // ExportAssetsOperation returns a new ExportAssetsOperation from a given name.
  165. // The name must be that of a previously created ExportAssetsOperation, possibly from a different process.
  166. func (c *Client) ExportAssetsOperation(name string) *ExportAssetsOperation {
  167. return &ExportAssetsOperation{
  168. lro: longrunning.InternalNewOperation(c.LROClient, &longrunningpb.Operation{Name: name}),
  169. }
  170. }
  171. // Wait blocks until the long-running operation is completed, returning the response and any errors encountered.
  172. //
  173. // See documentation of Poll for error-handling information.
  174. func (op *ExportAssetsOperation) Wait(ctx context.Context, opts ...gax.CallOption) (*assetpb.ExportAssetsResponse, error) {
  175. var resp assetpb.ExportAssetsResponse
  176. if err := op.lro.WaitWithInterval(ctx, &resp, 5000*time.Millisecond, opts...); err != nil {
  177. return nil, err
  178. }
  179. return &resp, nil
  180. }
  181. // Poll fetches the latest state of the long-running operation.
  182. //
  183. // Poll also fetches the latest metadata, which can be retrieved by Metadata.
  184. //
  185. // If Poll fails, the error is returned and op is unmodified. If Poll succeeds and
  186. // the operation has completed with failure, the error is returned and op.Done will return true.
  187. // If Poll succeeds and the operation has completed successfully,
  188. // op.Done will return true, and the response of the operation is returned.
  189. // If Poll succeeds and the operation has not completed, the returned response and error are both nil.
  190. func (op *ExportAssetsOperation) Poll(ctx context.Context, opts ...gax.CallOption) (*assetpb.ExportAssetsResponse, error) {
  191. var resp assetpb.ExportAssetsResponse
  192. if err := op.lro.Poll(ctx, &resp, opts...); err != nil {
  193. return nil, err
  194. }
  195. if !op.Done() {
  196. return nil, nil
  197. }
  198. return &resp, nil
  199. }
  200. // Metadata returns metadata associated with the long-running operation.
  201. // Metadata itself does not contact the server, but Poll does.
  202. // To get the latest metadata, call this method after a successful call to Poll.
  203. // If the metadata is not available, the returned metadata and error are both nil.
  204. func (op *ExportAssetsOperation) Metadata() (*assetpb.ExportAssetsRequest, error) {
  205. var meta assetpb.ExportAssetsRequest
  206. if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata {
  207. return nil, nil
  208. } else if err != nil {
  209. return nil, err
  210. }
  211. return &meta, nil
  212. }
  213. // Done reports whether the long-running operation has completed.
  214. func (op *ExportAssetsOperation) Done() bool {
  215. return op.lro.Done()
  216. }
  217. // Name returns the name of the long-running operation.
  218. // The name is assigned by the server and is unique within the service from which the operation is created.
  219. func (op *ExportAssetsOperation) Name() string {
  220. return op.lro.Name()
  221. }