1// Copyright 2020 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
15// Code generated by protoc-gen-go_gapic. DO NOT EDIT.
16
17package asset
18
19import (
20	"context"
21	"fmt"
22	"math"
23	"net/url"
24	"time"
25
26	"cloud.google.com/go/longrunning"
27	lroauto "cloud.google.com/go/longrunning/autogen"
28	gax "github.com/googleapis/gax-go/v2"
29	"google.golang.org/api/option"
30	"google.golang.org/api/transport"
31	assetpb "google.golang.org/genproto/googleapis/cloud/asset/v1"
32	longrunningpb "google.golang.org/genproto/googleapis/longrunning"
33	"google.golang.org/grpc"
34	"google.golang.org/grpc/codes"
35	"google.golang.org/grpc/metadata"
36)
37
38// CallOptions contains the retry settings for each method of Client.
39type CallOptions struct {
40	ExportAssets          []gax.CallOption
41	BatchGetAssetsHistory []gax.CallOption
42	CreateFeed            []gax.CallOption
43	GetFeed               []gax.CallOption
44	ListFeeds             []gax.CallOption
45	UpdateFeed            []gax.CallOption
46	DeleteFeed            []gax.CallOption
47}
48
49func defaultClientOptions() []option.ClientOption {
50	return []option.ClientOption{
51		option.WithEndpoint("cloudasset.googleapis.com:443"),
52		option.WithGRPCDialOption(grpc.WithDisableServiceConfig()),
53		option.WithScopes(DefaultAuthScopes()...),
54		option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
55			grpc.MaxCallRecvMsgSize(math.MaxInt32))),
56	}
57}
58
59func defaultCallOptions() *CallOptions {
60	return &CallOptions{
61		ExportAssets: []gax.CallOption{},
62		BatchGetAssetsHistory: []gax.CallOption{
63			gax.WithRetry(func() gax.Retryer {
64				return gax.OnCodes([]codes.Code{
65					codes.DeadlineExceeded,
66					codes.Unavailable,
67				}, gax.Backoff{
68					Initial:    100 * time.Millisecond,
69					Max:        60000 * time.Millisecond,
70					Multiplier: 1.30,
71				})
72			}),
73		},
74		CreateFeed: []gax.CallOption{},
75		GetFeed:    []gax.CallOption{},
76		ListFeeds:  []gax.CallOption{},
77		UpdateFeed: []gax.CallOption{},
78		DeleteFeed: []gax.CallOption{},
79	}
80}
81
82// Client is a client for interacting with Cloud Asset API.
83//
84// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
85type Client struct {
86	// The connection to the service.
87	conn *grpc.ClientConn
88
89	// The gRPC API client.
90	client assetpb.AssetServiceClient
91
92	// LROClient is used internally to handle longrunning operations.
93	// It is exposed so that its CallOptions can be modified if required.
94	// Users should not Close this client.
95	LROClient *lroauto.OperationsClient
96
97	// The call options for this service.
98	CallOptions *CallOptions
99
100	// The x-goog-* metadata to be sent with each request.
101	xGoogMetadata metadata.MD
102}
103
104// NewClient creates a new asset service client.
105//
106// Asset service definition.
107func NewClient(ctx context.Context, opts ...option.ClientOption) (*Client, error) {
108	conn, err := transport.DialGRPC(ctx, append(defaultClientOptions(), opts...)...)
109	if err != nil {
110		return nil, err
111	}
112	c := &Client{
113		conn:        conn,
114		CallOptions: defaultCallOptions(),
115
116		client: assetpb.NewAssetServiceClient(conn),
117	}
118	c.setGoogleClientInfo()
119
120	c.LROClient, err = lroauto.NewOperationsClient(ctx, option.WithGRPCConn(conn))
121	if err != nil {
122		// This error "should not happen", since we are just reusing old connection
123		// and never actually need to dial.
124		// If this does happen, we could leak conn. However, we cannot close conn:
125		// If the user invoked the function with option.WithGRPCConn,
126		// we would close a connection that's still in use.
127		// TODO(pongad): investigate error conditions.
128		return nil, err
129	}
130	return c, nil
131}
132
133// Connection returns the client's connection to the API service.
134func (c *Client) Connection() *grpc.ClientConn {
135	return c.conn
136}
137
138// Close closes the connection to the API service. The user should invoke this when
139// the client is no longer required.
140func (c *Client) Close() error {
141	return c.conn.Close()
142}
143
144// setGoogleClientInfo sets the name and version of the application in
145// the `x-goog-api-client` header passed on each request. Intended for
146// use by Google-written clients.
147func (c *Client) setGoogleClientInfo(keyval ...string) {
148	kv := append([]string{"gl-go", versionGo()}, keyval...)
149	kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version)
150	c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
151}
152
153// ExportAssets exports assets with time and resource types to a given Cloud Storage
154// location. The output format is newline-delimited JSON.
155// This API implements the [google.longrunning.Operation][google.longrunning.Operation] API allowing you
156// to keep track of the export.
157func (c *Client) ExportAssets(ctx context.Context, req *assetpb.ExportAssetsRequest, opts ...gax.CallOption) (*ExportAssetsOperation, error) {
158	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent())))
159	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
160	opts = append(c.CallOptions.ExportAssets[0:len(c.CallOptions.ExportAssets):len(c.CallOptions.ExportAssets)], opts...)
161	var resp *longrunningpb.Operation
162	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
163		var err error
164		resp, err = c.client.ExportAssets(ctx, req, settings.GRPC...)
165		return err
166	}, opts...)
167	if err != nil {
168		return nil, err
169	}
170	return &ExportAssetsOperation{
171		lro: longrunning.InternalNewOperation(c.LROClient, resp),
172	}, nil
173}
174
175// BatchGetAssetsHistory batch gets the update history of assets that overlap a time window.
176// For RESOURCE content, this API outputs history with asset in both
177// non-delete or deleted status.
178// For IAM_POLICY content, this API outputs history when the asset and its
179// attached IAM POLICY both exist. This can create gaps in the output history.
180// If a specified asset does not exist, this API returns an INVALID_ARGUMENT
181// error.
182func (c *Client) BatchGetAssetsHistory(ctx context.Context, req *assetpb.BatchGetAssetsHistoryRequest, opts ...gax.CallOption) (*assetpb.BatchGetAssetsHistoryResponse, error) {
183	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent())))
184	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
185	opts = append(c.CallOptions.BatchGetAssetsHistory[0:len(c.CallOptions.BatchGetAssetsHistory):len(c.CallOptions.BatchGetAssetsHistory)], opts...)
186	var resp *assetpb.BatchGetAssetsHistoryResponse
187	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
188		var err error
189		resp, err = c.client.BatchGetAssetsHistory(ctx, req, settings.GRPC...)
190		return err
191	}, opts...)
192	if err != nil {
193		return nil, err
194	}
195	return resp, nil
196}
197
198// CreateFeed creates a feed in a parent project/folder/organization to listen to its
199// asset updates.
200func (c *Client) CreateFeed(ctx context.Context, req *assetpb.CreateFeedRequest, opts ...gax.CallOption) (*assetpb.Feed, error) {
201	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent())))
202	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
203	opts = append(c.CallOptions.CreateFeed[0:len(c.CallOptions.CreateFeed):len(c.CallOptions.CreateFeed)], opts...)
204	var resp *assetpb.Feed
205	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
206		var err error
207		resp, err = c.client.CreateFeed(ctx, req, settings.GRPC...)
208		return err
209	}, opts...)
210	if err != nil {
211		return nil, err
212	}
213	return resp, nil
214}
215
216// GetFeed gets details about an asset feed.
217func (c *Client) GetFeed(ctx context.Context, req *assetpb.GetFeedRequest, opts ...gax.CallOption) (*assetpb.Feed, error) {
218	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
219	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
220	opts = append(c.CallOptions.GetFeed[0:len(c.CallOptions.GetFeed):len(c.CallOptions.GetFeed)], opts...)
221	var resp *assetpb.Feed
222	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
223		var err error
224		resp, err = c.client.GetFeed(ctx, req, settings.GRPC...)
225		return err
226	}, opts...)
227	if err != nil {
228		return nil, err
229	}
230	return resp, nil
231}
232
233// ListFeeds lists all asset feeds in a parent project/folder/organization.
234func (c *Client) ListFeeds(ctx context.Context, req *assetpb.ListFeedsRequest, opts ...gax.CallOption) (*assetpb.ListFeedsResponse, error) {
235	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent())))
236	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
237	opts = append(c.CallOptions.ListFeeds[0:len(c.CallOptions.ListFeeds):len(c.CallOptions.ListFeeds)], opts...)
238	var resp *assetpb.ListFeedsResponse
239	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
240		var err error
241		resp, err = c.client.ListFeeds(ctx, req, settings.GRPC...)
242		return err
243	}, opts...)
244	if err != nil {
245		return nil, err
246	}
247	return resp, nil
248}
249
250// UpdateFeed updates an asset feed configuration.
251func (c *Client) UpdateFeed(ctx context.Context, req *assetpb.UpdateFeedRequest, opts ...gax.CallOption) (*assetpb.Feed, error) {
252	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "feed.name", url.QueryEscape(req.GetFeed().GetName())))
253	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
254	opts = append(c.CallOptions.UpdateFeed[0:len(c.CallOptions.UpdateFeed):len(c.CallOptions.UpdateFeed)], opts...)
255	var resp *assetpb.Feed
256	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
257		var err error
258		resp, err = c.client.UpdateFeed(ctx, req, settings.GRPC...)
259		return err
260	}, opts...)
261	if err != nil {
262		return nil, err
263	}
264	return resp, nil
265}
266
267// DeleteFeed deletes an asset feed.
268func (c *Client) DeleteFeed(ctx context.Context, req *assetpb.DeleteFeedRequest, opts ...gax.CallOption) error {
269	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
270	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
271	opts = append(c.CallOptions.DeleteFeed[0:len(c.CallOptions.DeleteFeed):len(c.CallOptions.DeleteFeed)], opts...)
272	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
273		var err error
274		_, err = c.client.DeleteFeed(ctx, req, settings.GRPC...)
275		return err
276	}, opts...)
277	return err
278}
279
280// ExportAssetsOperation manages a long-running operation from ExportAssets.
281type ExportAssetsOperation struct {
282	lro *longrunning.Operation
283}
284
285// ExportAssetsOperation returns a new ExportAssetsOperation from a given name.
286// The name must be that of a previously created ExportAssetsOperation, possibly from a different process.
287func (c *Client) ExportAssetsOperation(name string) *ExportAssetsOperation {
288	return &ExportAssetsOperation{
289		lro: longrunning.InternalNewOperation(c.LROClient, &longrunningpb.Operation{Name: name}),
290	}
291}
292
293// Wait blocks until the long-running operation is completed, returning the response and any errors encountered.
294//
295// See documentation of Poll for error-handling information.
296func (op *ExportAssetsOperation) Wait(ctx context.Context, opts ...gax.CallOption) (*assetpb.ExportAssetsResponse, error) {
297	var resp assetpb.ExportAssetsResponse
298	if err := op.lro.WaitWithInterval(ctx, &resp, time.Minute, opts...); err != nil {
299		return nil, err
300	}
301	return &resp, nil
302}
303
304// Poll fetches the latest state of the long-running operation.
305//
306// Poll also fetches the latest metadata, which can be retrieved by Metadata.
307//
308// If Poll fails, the error is returned and op is unmodified. If Poll succeeds and
309// the operation has completed with failure, the error is returned and op.Done will return true.
310// If Poll succeeds and the operation has completed successfully,
311// op.Done will return true, and the response of the operation is returned.
312// If Poll succeeds and the operation has not completed, the returned response and error are both nil.
313func (op *ExportAssetsOperation) Poll(ctx context.Context, opts ...gax.CallOption) (*assetpb.ExportAssetsResponse, error) {
314	var resp assetpb.ExportAssetsResponse
315	if err := op.lro.Poll(ctx, &resp, opts...); err != nil {
316		return nil, err
317	}
318	if !op.Done() {
319		return nil, nil
320	}
321	return &resp, nil
322}
323
324// Metadata returns metadata associated with the long-running operation.
325// Metadata itself does not contact the server, but Poll does.
326// To get the latest metadata, call this method after a successful call to Poll.
327// If the metadata is not available, the returned metadata and error are both nil.
328func (op *ExportAssetsOperation) Metadata() (*assetpb.ExportAssetsRequest, error) {
329	var meta assetpb.ExportAssetsRequest
330	if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata {
331		return nil, nil
332	} else if err != nil {
333		return nil, err
334	}
335	return &meta, nil
336}
337
338// Done reports whether the long-running operation has completed.
339func (op *ExportAssetsOperation) Done() bool {
340	return op.lro.Done()
341}
342
343// Name returns the name of the long-running operation.
344// The name is assigned by the server and is unique within the service from which the operation is created.
345func (op *ExportAssetsOperation) Name() string {
346	return op.lro.Name()
347}
348