| 1 |
// Copyright 2025 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 |
|
| 17 |
package asset |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"fmt" |
| 22 |
"log/slog" |
| 23 |
"math" |
| 24 |
"net/http" |
| 25 |
"net/url" |
| 26 |
"time" |
| 27 |
|
| 28 |
assetpb "cloud.google.com/go/asset/apiv1p5beta1/assetpb" |
| 29 |
gax "github.com/googleapis/gax-go/v2" |
| 30 |
"google.golang.org/api/iterator" |
| 31 |
"google.golang.org/api/option" |
| 32 |
"google.golang.org/api/option/internaloption" |
| 33 |
gtransport "google.golang.org/api/transport/grpc" |
| 34 |
httptransport "google.golang.org/api/transport/http" |
| 35 |
"google.golang.org/grpc" |
| 36 |
"google.golang.org/grpc/codes" |
| 37 |
"google.golang.org/protobuf/encoding/protojson" |
| 38 |
"google.golang.org/protobuf/proto" |
| 39 |
) |
| 40 |
|
| 41 |
var newClientHook clientHook |
| 42 |
|
| 43 |
// CallOptions contains the retry settings for each method of Client. |
| 44 |
type CallOptions struct { |
| 45 |
ListAssets []gax.CallOption |
| 46 |
} |
| 47 |
|
| 48 |
func defaultGRPCClientOptions() []option.ClientOption { |
| 49 |
return []option.ClientOption{ |
| 50 |
internaloption.WithDefaultEndpoint("cloudasset.googleapis.com:443"), |
| 51 |
internaloption.WithDefaultEndpointTemplate("cloudasset.UNIVERSE_DOMAIN:443"), |
| 52 |
internaloption.WithDefaultMTLSEndpoint("cloudasset.mtls.googleapis.com:443"), |
| 53 |
internaloption.WithDefaultUniverseDomain("googleapis.com"), |
| 54 |
internaloption.WithDefaultAudience("https://cloudasset.googleapis.com/"), |
| 55 |
internaloption.WithDefaultScopes(DefaultAuthScopes()...), |
| 56 |
internaloption.EnableJwtWithScope(), |
| 57 |
internaloption.EnableNewAuthLibrary(), |
| 58 |
option.WithGRPCDialOption(grpc.WithDefaultCallOptions( |
| 59 |
grpc.MaxCallRecvMsgSize(math.MaxInt32))), |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
func defaultCallOptions() *CallOptions { |
| 64 |
return &CallOptions{ |
| 65 |
ListAssets: []gax.CallOption{ |
| 66 |
gax.WithTimeout(60000 * time.Millisecond), |
| 67 |
gax.WithRetry(func() gax.Retryer { |
| 68 |
return gax.OnCodes([]codes.Code{ |
| 69 |
codes.DeadlineExceeded, |
| 70 |
codes.Unavailable, |
| 71 |
}, gax.Backoff{ |
| 72 |
Initial: 100 * time.Millisecond, |
| 73 |
Max: 60000 * time.Millisecond, |
| 74 |
Multiplier: 1.30, |
| 75 |
}) |
| 76 |
}), |
| 77 |
}, |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
func defaultRESTCallOptions() *CallOptions { |
| 82 |
return &CallOptions{ |
| 83 |
ListAssets: []gax.CallOption{ |
| 84 |
gax.WithTimeout(60000 * time.Millisecond), |
| 85 |
gax.WithRetry(func() gax.Retryer { |
| 86 |
return gax.OnHTTPCodes(gax.Backoff{ |
| 87 |
Initial: 100 * time.Millisecond, |
| 88 |
Max: 60000 * time.Millisecond, |
| 89 |
Multiplier: 1.30, |
| 90 |
}, |
| 91 |
http.StatusGatewayTimeout, |
| 92 |
http.StatusServiceUnavailable) |
| 93 |
}), |
| 94 |
}, |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
// internalClient is an interface that defines the methods available from Cloud Asset API. |
| 99 |
type internalClient interface { |
| 100 |
Close() error |
| 101 |
setGoogleClientInfo(...string) |
| 102 |
Connection() *grpc.ClientConn |
| 103 |
ListAssets(context.Context, *assetpb.ListAssetsRequest, ...gax.CallOption) *AssetIterator |
| 104 |
} |
| 105 |
|
| 106 |
// Client is a client for interacting with Cloud Asset API. |
| 107 |
// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls. |
| 108 |
// |
| 109 |
// Asset service definition. |
| 110 |
type Client struct { |
| 111 |
// The internal transport-dependent client. |
| 112 |
internalClient internalClient |
| 113 |
|
| 114 |
// The call options for this service. |
| 115 |
CallOptions *CallOptions |
| 116 |
} |
| 117 |
|
| 118 |
// Wrapper methods routed to the internal client. |
| 119 |
|
| 120 |
// Close closes the connection to the API service. The user should invoke this when |
| 121 |
// the client is no longer required. |
| 122 |
func (c *Client) Close() error { |
| 123 |
return c.internalClient.Close() |
| 124 |
} |
| 125 |
|
| 126 |
// setGoogleClientInfo sets the name and version of the application in |
| 127 |
// the `x-goog-api-client` header passed on each request. Intended for |
| 128 |
// use by Google-written clients. |
| 129 |
func (c *Client) setGoogleClientInfo(keyval ...string) { |
| 130 |
c.internalClient.setGoogleClientInfo(keyval...) |
| 131 |
} |
| 132 |
|
| 133 |
// Connection returns a connection to the API service. |
| 134 |
// |
| 135 |
// Deprecated: Connections are now pooled so this method does not always |
| 136 |
// return the same resource. |
| 137 |
func (c *Client) Connection() *grpc.ClientConn { |
| 138 |
return c.internalClient.Connection() |
| 139 |
} |
| 140 |
|
| 141 |
// ListAssets lists assets with time and resource types and returns paged results in |
| 142 |
// response. |
| 143 |
func (c *Client) ListAssets(ctx context.Context, req *assetpb.ListAssetsRequest, opts ...gax.CallOption) *AssetIterator { |
| 144 |
return c.internalClient.ListAssets(ctx, req, opts...) |
| 145 |
} |
| 146 |
|
| 147 |
// gRPCClient is a client for interacting with Cloud Asset API over gRPC transport. |
| 148 |
// |
| 149 |
// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls. |
| 150 |
type gRPCClient struct { |
| 151 |
// Connection pool of gRPC connections to the service. |
| 152 |
connPool gtransport.ConnPool |
| 153 |
|
| 154 |
// Points back to the CallOptions field of the containing Client |
| 155 |
CallOptions **CallOptions |
| 156 |
|
| 157 |
// The gRPC API client. |
| 158 |
client assetpb.AssetServiceClient |
| 159 |
|
| 160 |
// The x-goog-* metadata to be sent with each request. |
| 161 |
xGoogHeaders []string |
| 162 |
|
| 163 |
logger *slog.Logger |
| 164 |
} |
| 165 |
|
| 166 |
// NewClient creates a new asset service client based on gRPC. |
| 167 |
// The returned client must be Closed when it is done being used to clean up its underlying connections. |
| 168 |
// |
| 169 |
// Asset service definition. |
| 170 |
func NewClient(ctx context.Context, opts ...option.ClientOption) (*Client, error) { |
| 171 |
clientOpts := defaultGRPCClientOptions() |
| 172 |
if newClientHook != nil { |
| 173 |
hookOpts, err := newClientHook(ctx, clientHookParams{}) |
| 174 |
if err != nil { |
| 175 |
return nil, err |
| 176 |
} |
| 177 |
clientOpts = append(clientOpts, hookOpts...) |
| 178 |
} |
| 179 |
|
| 180 |
connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...) |
| 181 |
if err != nil { |
| 182 |
return nil, err |
| 183 |
} |
| 184 |
client := Client{CallOptions: defaultCallOptions()} |
| 185 |
|
| 186 |
c := &gRPCClient{ |
| 187 |
connPool: connPool, |
| 188 |
client: assetpb.NewAssetServiceClient(connPool), |
| 189 |
CallOptions: &client.CallOptions, |
| 190 |
logger: internaloption.GetLogger(opts), |
| 191 |
} |
| 192 |
c.setGoogleClientInfo() |
| 193 |
|
| 194 |
client.internalClient = c |
| 195 |
|
| 196 |
return &client, nil |
| 197 |
} |
| 198 |
|
| 199 |
// Connection returns a connection to the API service. |
| 200 |
// |
| 201 |
// Deprecated: Connections are now pooled so this method does not always |
| 202 |
// return the same resource. |
| 203 |
func (c *gRPCClient) Connection() *grpc.ClientConn { |
| 204 |
return c.connPool.Conn() |
| 205 |
} |
| 206 |
|
| 207 |
// setGoogleClientInfo sets the name and version of the application in |
| 208 |
// the `x-goog-api-client` header passed on each request. Intended for |
| 209 |
// use by Google-written clients. |
| 210 |
func (c *gRPCClient) setGoogleClientInfo(keyval ...string) { |
| 211 |
kv := append([]string{"gl-go", gax.GoVersion}, keyval...) |
| 212 |
kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "grpc", grpc.Version, "pb", protoVersion) |
| 213 |
c.xGoogHeaders = []string{ |
| 214 |
"x-goog-api-client", gax.XGoogHeader(kv...), |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
// Close closes the connection to the API service. The user should invoke this when |
| 219 |
// the client is no longer required. |
| 220 |
func (c *gRPCClient) Close() error { |
| 221 |
return c.connPool.Close() |
| 222 |
} |
| 223 |
|
| 224 |
// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls. |
| 225 |
type restClient struct { |
| 226 |
// The http endpoint to connect to. |
| 227 |
endpoint string |
| 228 |
|
| 229 |
// The http client. |
| 230 |
httpClient *http.Client |
| 231 |
|
| 232 |
// The x-goog-* headers to be sent with each request. |
| 233 |
xGoogHeaders []string |
| 234 |
|
| 235 |
// Points back to the CallOptions field of the containing Client |
| 236 |
CallOptions **CallOptions |
| 237 |
|
| 238 |
logger *slog.Logger |
| 239 |
} |
| 240 |
|
| 241 |
// NewRESTClient creates a new asset service rest client. |
| 242 |
// |
| 243 |
// Asset service definition. |
| 244 |
func NewRESTClient(ctx context.Context, opts ...option.ClientOption) (*Client, error) { |
| 245 |
clientOpts := append(defaultRESTClientOptions(), opts...) |
| 246 |
httpClient, endpoint, err := httptransport.NewClient(ctx, clientOpts...) |
| 247 |
if err != nil { |
| 248 |
return nil, err |
| 249 |
} |
| 250 |
|
| 251 |
callOpts := defaultRESTCallOptions() |
| 252 |
c := &restClient{ |
| 253 |
endpoint: endpoint, |
| 254 |
httpClient: httpClient, |
| 255 |
CallOptions: &callOpts, |
| 256 |
logger: internaloption.GetLogger(opts), |
| 257 |
} |
| 258 |
c.setGoogleClientInfo() |
| 259 |
|
| 260 |
return &Client{internalClient: c, CallOptions: callOpts}, nil |
| 261 |
} |
| 262 |
|
| 263 |
func defaultRESTClientOptions() []option.ClientOption { |
| 264 |
return []option.ClientOption{ |
| 265 |
internaloption.WithDefaultEndpoint("https://cloudasset.googleapis.com"), |
| 266 |
internaloption.WithDefaultEndpointTemplate("https://cloudasset.UNIVERSE_DOMAIN"), |
| 267 |
internaloption.WithDefaultMTLSEndpoint("https://cloudasset.mtls.googleapis.com"), |
| 268 |
internaloption.WithDefaultUniverseDomain("googleapis.com"), |
| 269 |
internaloption.WithDefaultAudience("https://cloudasset.googleapis.com/"), |
| 270 |
internaloption.WithDefaultScopes(DefaultAuthScopes()...), |
| 271 |
internaloption.EnableNewAuthLibrary(), |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
// setGoogleClientInfo sets the name and version of the application in |
| 276 |
// the `x-goog-api-client` header passed on each request. Intended for |
| 277 |
// use by Google-written clients. |
| 278 |
func (c *restClient) setGoogleClientInfo(keyval ...string) { |
| 279 |
kv := append([]string{"gl-go", gax.GoVersion}, keyval...) |
| 280 |
kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "rest", "UNKNOWN", "pb", protoVersion) |
| 281 |
c.xGoogHeaders = []string{ |
| 282 |
"x-goog-api-client", gax.XGoogHeader(kv...), |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
// Close closes the connection to the API service. The user should invoke this when |
| 287 |
// the client is no longer required. |
| 288 |
func (c *restClient) Close() error { |
| 289 |
// Replace httpClient with nil to force cleanup. |
| 290 |
c.httpClient = nil |
| 291 |
return nil |
| 292 |
} |
| 293 |
|
| 294 |
// Connection returns a connection to the API service. |
| 295 |
// |
| 296 |
// Deprecated: This method always returns nil. |
| 297 |
func (c *restClient) Connection() *grpc.ClientConn { |
| 298 |
return nil |
| 299 |
} |
| 300 |
func (c *gRPCClient) ListAssets(ctx context.Context, req *assetpb.ListAssetsRequest, opts ...gax.CallOption) *AssetIterator { |
| 301 |
hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))} |
| 302 |
|
| 303 |
hds = append(c.xGoogHeaders, hds...) |
| 304 |
ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...) |
| 305 |
opts = append((*c.CallOptions).ListAssets[0:len((*c.CallOptions).ListAssets):len((*c.CallOptions).ListAssets)], opts...) |
| 306 |
it := &AssetIterator{} |
| 307 |
req = proto.Clone(req).(*assetpb.ListAssetsRequest) |
| 308 |
it.InternalFetch = func(pageSize int, pageToken string) ([]*assetpb.Asset, string, error) { |
| 309 |
resp := &assetpb.ListAssetsResponse{} |
| 310 |
if pageToken != "" { |
| 311 |
req.PageToken = pageToken |
| 312 |
} |
| 313 |
if pageSize > math.MaxInt32 { |
| 314 |
req.PageSize = math.MaxInt32 |
| 315 |
} else if pageSize != 0 { |
| 316 |
req.PageSize = int32(pageSize) |
| 317 |
} |
| 318 |
err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { |
| 319 |
var err error |
| 320 |
resp, err = executeRPC(ctx, c.client.ListAssets, req, settings.GRPC, c.logger, "ListAssets") |
| 321 |
return err |
| 322 |
}, opts...) |
| 323 |
if err != nil { |
| 324 |
return nil, "", err |
| 325 |
} |
| 326 |
|
| 327 |
it.Response = resp |
| 328 |
return resp.GetAssets(), resp.GetNextPageToken(), nil |
| 329 |
} |
| 330 |
fetch := func(pageSize int, pageToken string) (string, error) { |
| 331 |
items, nextPageToken, err := it.InternalFetch(pageSize, pageToken) |
| 332 |
if err != nil { |
| 333 |
return "", err |
| 334 |
} |
| 335 |
it.items = append(it.items, items...) |
| 336 |
return nextPageToken, nil |
| 337 |
} |
| 338 |
|
| 339 |
it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf) |
| 340 |
it.pageInfo.MaxSize = int(req.GetPageSize()) |
| 341 |
it.pageInfo.Token = req.GetPageToken() |
| 342 |
|
| 343 |
return it |
| 344 |
} |
| 345 |
|
| 346 |
// ListAssets lists assets with time and resource types and returns paged results in |
| 347 |
// response. |
| 348 |
func (c *restClient) ListAssets(ctx context.Context, req *assetpb.ListAssetsRequest, opts ...gax.CallOption) *AssetIterator { |
| 349 |
it := &AssetIterator{} |
| 350 |
req = proto.Clone(req).(*assetpb.ListAssetsRequest) |
| 351 |
unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true} |
| 352 |
it.InternalFetch = func(pageSize int, pageToken string) ([]*assetpb.Asset, string, error) { |
| 353 |
resp := &assetpb.ListAssetsResponse{} |
| 354 |
if pageToken != "" { |
| 355 |
req.PageToken = pageToken |
| 356 |
} |
| 357 |
if pageSize > math.MaxInt32 { |
| 358 |
req.PageSize = math.MaxInt32 |
| 359 |
} else if pageSize != 0 { |
| 360 |
req.PageSize = int32(pageSize) |
| 361 |
} |
| 362 |
baseUrl, err := url.Parse(c.endpoint) |
| 363 |
if err != nil { |
| 364 |
return nil, "", err |
| 365 |
} |
| 366 |
baseUrl.Path += fmt.Sprintf("/v1p5beta1/%v/assets", req.GetParent()) |
| 367 |
|
| 368 |
params := url.Values{} |
| 369 |
params.Add("$alt", "json;enum-encoding=int") |
| 370 |
if items := req.GetAssetTypes(); len(items) > 0 { |
| 371 |
for _, item := range items { |
| 372 |
params.Add("assetTypes", fmt.Sprintf("%v", item)) |
| 373 |
} |
| 374 |
} |
| 375 |
if req.GetContentType() != 0 { |
| 376 |
params.Add("contentType", fmt.Sprintf("%v", req.GetContentType())) |
| 377 |
} |
| 378 |
if req.GetPageSize() != 0 { |
| 379 |
params.Add("pageSize", fmt.Sprintf("%v", req.GetPageSize())) |
| 380 |
} |
| 381 |
if req.GetPageToken() != "" { |
| 382 |
params.Add("pageToken", fmt.Sprintf("%v", req.GetPageToken())) |
| 383 |
} |
| 384 |
if req.GetReadTime() != nil { |
| 385 |
field, err := protojson.Marshal(req.GetReadTime()) |
| 386 |
if err != nil { |
| 387 |
return nil, "", err |
| 388 |
} |
| 389 |
params.Add("readTime", string(field[1:len(field)-1])) |
| 390 |
} |
| 391 |
|
| 392 |
baseUrl.RawQuery = params.Encode() |
| 393 |
|
| 394 |
// Build HTTP headers from client and context metadata. |
| 395 |
hds := append(c.xGoogHeaders, "Content-Type", "application/json") |
| 396 |
headers := gax.BuildHeaders(ctx, hds...) |
| 397 |
e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { |
| 398 |
if settings.Path != "" { |
| 399 |
baseUrl.Path = settings.Path |
| 400 |
} |
| 401 |
httpReq, err := http.NewRequest("GET", baseUrl.String(), nil) |
| 402 |
if err != nil { |
| 403 |
return err |
| 404 |
} |
| 405 |
httpReq.Header = headers |
| 406 |
|
| 407 |
buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, nil, "ListAssets") |
| 408 |
if err != nil { |
| 409 |
return err |
| 410 |
} |
| 411 |
if err := unm.Unmarshal(buf, resp); err != nil { |
| 412 |
return err |
| 413 |
} |
| 414 |
|
| 415 |
return nil |
| 416 |
}, opts...) |
| 417 |
if e != nil { |
| 418 |
return nil, "", e |
| 419 |
} |
| 420 |
it.Response = resp |
| 421 |
return resp.GetAssets(), resp.GetNextPageToken(), nil |
| 422 |
} |
| 423 |
|
| 424 |
fetch := func(pageSize int, pageToken string) (string, error) { |
| 425 |
items, nextPageToken, err := it.InternalFetch(pageSize, pageToken) |
| 426 |
if err != nil { |
| 427 |
return "", err |
| 428 |
} |
| 429 |
it.items = append(it.items, items...) |
| 430 |
return nextPageToken, nil |
| 431 |
} |
| 432 |
|
| 433 |
it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf) |
| 434 |
it.pageInfo.MaxSize = int(req.GetPageSize()) |
| 435 |
it.pageInfo.Token = req.GetPageToken() |
| 436 |
|
| 437 |
return it |
| 438 |
} |