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.
 
 
 

250 lines
9.0 KiB

  1. // Copyright 2019 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. // Code generated by gapic-generator. 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
  124. // [google.longrunning.Operation][google.longrunning.Operation] API allowing
  125. // you to keep track of the export.
  126. func (c *Client) ExportAssets(ctx context.Context, req *assetpb.ExportAssetsRequest, opts ...gax.CallOption) (*ExportAssetsOperation, error) {
  127. ctx = insertMetadata(ctx, c.xGoogMetadata)
  128. opts = append(c.CallOptions.ExportAssets[0:len(c.CallOptions.ExportAssets):len(c.CallOptions.ExportAssets)], opts...)
  129. var resp *longrunningpb.Operation
  130. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  131. var err error
  132. resp, err = c.client.ExportAssets(ctx, req, settings.GRPC...)
  133. return err
  134. }, opts...)
  135. if err != nil {
  136. return nil, err
  137. }
  138. return &ExportAssetsOperation{
  139. lro: longrunning.InternalNewOperation(c.LROClient, resp),
  140. }, nil
  141. }
  142. // BatchGetAssetsHistory batch gets the update history of assets that overlap a time window.
  143. // For RESOURCE content, this API outputs history with asset in both
  144. // non-delete or deleted status.
  145. // For IAM_POLICY content, this API outputs history when the asset and its
  146. // attached IAM POLICY both exist. This can create gaps in the output history.
  147. func (c *Client) BatchGetAssetsHistory(ctx context.Context, req *assetpb.BatchGetAssetsHistoryRequest, opts ...gax.CallOption) (*assetpb.BatchGetAssetsHistoryResponse, error) {
  148. ctx = insertMetadata(ctx, c.xGoogMetadata)
  149. opts = append(c.CallOptions.BatchGetAssetsHistory[0:len(c.CallOptions.BatchGetAssetsHistory):len(c.CallOptions.BatchGetAssetsHistory)], opts...)
  150. var resp *assetpb.BatchGetAssetsHistoryResponse
  151. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  152. var err error
  153. resp, err = c.client.BatchGetAssetsHistory(ctx, req, settings.GRPC...)
  154. return err
  155. }, opts...)
  156. if err != nil {
  157. return nil, err
  158. }
  159. return resp, nil
  160. }
  161. // ExportAssetsOperation manages a long-running operation from ExportAssets.
  162. type ExportAssetsOperation struct {
  163. lro *longrunning.Operation
  164. }
  165. // ExportAssetsOperation returns a new ExportAssetsOperation from a given name.
  166. // The name must be that of a previously created ExportAssetsOperation, possibly from a different process.
  167. func (c *Client) ExportAssetsOperation(name string) *ExportAssetsOperation {
  168. return &ExportAssetsOperation{
  169. lro: longrunning.InternalNewOperation(c.LROClient, &longrunningpb.Operation{Name: name}),
  170. }
  171. }
  172. // Wait blocks until the long-running operation is completed, returning the response and any errors encountered.
  173. //
  174. // See documentation of Poll for error-handling information.
  175. func (op *ExportAssetsOperation) Wait(ctx context.Context, opts ...gax.CallOption) (*assetpb.ExportAssetsResponse, error) {
  176. var resp assetpb.ExportAssetsResponse
  177. if err := op.lro.WaitWithInterval(ctx, &resp, 5000*time.Millisecond, opts...); err != nil {
  178. return nil, err
  179. }
  180. return &resp, nil
  181. }
  182. // Poll fetches the latest state of the long-running operation.
  183. //
  184. // Poll also fetches the latest metadata, which can be retrieved by Metadata.
  185. //
  186. // If Poll fails, the error is returned and op is unmodified. If Poll succeeds and
  187. // the operation has completed with failure, the error is returned and op.Done will return true.
  188. // If Poll succeeds and the operation has completed successfully,
  189. // op.Done will return true, and the response of the operation is returned.
  190. // If Poll succeeds and the operation has not completed, the returned response and error are both nil.
  191. func (op *ExportAssetsOperation) Poll(ctx context.Context, opts ...gax.CallOption) (*assetpb.ExportAssetsResponse, error) {
  192. var resp assetpb.ExportAssetsResponse
  193. if err := op.lro.Poll(ctx, &resp, opts...); err != nil {
  194. return nil, err
  195. }
  196. if !op.Done() {
  197. return nil, nil
  198. }
  199. return &resp, nil
  200. }
  201. // Metadata returns metadata associated with the long-running operation.
  202. // Metadata itself does not contact the server, but Poll does.
  203. // To get the latest metadata, call this method after a successful call to Poll.
  204. // If the metadata is not available, the returned metadata and error are both nil.
  205. func (op *ExportAssetsOperation) Metadata() (*assetpb.ExportAssetsRequest, error) {
  206. var meta assetpb.ExportAssetsRequest
  207. if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata {
  208. return nil, nil
  209. } else if err != nil {
  210. return nil, err
  211. }
  212. return &meta, nil
  213. }
  214. // Done reports whether the long-running operation has completed.
  215. func (op *ExportAssetsOperation) Done() bool {
  216. return op.lro.Done()
  217. }
  218. // Name returns the name of the long-running operation.
  219. // The name is assigned by the server and is unique within the service from which the operation is created.
  220. func (op *ExportAssetsOperation) Name() string {
  221. return op.lro.Name()
  222. }