Debian Codegraph @

policytroubleshooter/iam/apiv3/policy_troubleshooter_client.go

raw
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 iam
18
19 import (
20 "bytes"
21 "context"
22 "fmt"
23 "log/slog"
24 "math"
25 "net/http"
26 "net/url"
27 "time"
28
29 iampb "cloud.google.com/go/policytroubleshooter/iam/apiv3/iampb"
30 gax "github.com/googleapis/gax-go/v2"
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 )
39
40 var newPolicyTroubleshooterClientHook clientHook
41
42 // PolicyTroubleshooterCallOptions contains the retry settings for each method of PolicyTroubleshooterClient.
43 type PolicyTroubleshooterCallOptions struct {
44 TroubleshootIamPolicy []gax.CallOption
45 }
46
47 func defaultPolicyTroubleshooterGRPCClientOptions() []option.ClientOption {
48 return []option.ClientOption{
49 internaloption.WithDefaultEndpoint("policytroubleshooter.googleapis.com:443"),
50 internaloption.WithDefaultEndpointTemplate("policytroubleshooter.UNIVERSE_DOMAIN:443"),
51 internaloption.WithDefaultMTLSEndpoint("policytroubleshooter.mtls.googleapis.com:443"),
52 internaloption.WithDefaultUniverseDomain("googleapis.com"),
53 internaloption.WithDefaultAudience("https://policytroubleshooter.googleapis.com/"),
54 internaloption.WithDefaultScopes(DefaultAuthScopes()...),
55 internaloption.EnableJwtWithScope(),
56 internaloption.EnableNewAuthLibrary(),
57 option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
58 grpc.MaxCallRecvMsgSize(math.MaxInt32))),
59 }
60 }
61
62 func defaultPolicyTroubleshooterCallOptions() *PolicyTroubleshooterCallOptions {
63 return &PolicyTroubleshooterCallOptions{
64 TroubleshootIamPolicy: []gax.CallOption{
65 gax.WithTimeout(60000 * time.Millisecond),
66 gax.WithRetry(func() gax.Retryer {
67 return gax.OnCodes([]codes.Code{
68 codes.Unavailable,
69 }, gax.Backoff{
70 Initial: 1000 * time.Millisecond,
71 Max: 10000 * time.Millisecond,
72 Multiplier: 1.30,
73 })
74 }),
75 },
76 }
77 }
78
79 func defaultPolicyTroubleshooterRESTCallOptions() *PolicyTroubleshooterCallOptions {
80 return &PolicyTroubleshooterCallOptions{
81 TroubleshootIamPolicy: []gax.CallOption{
82 gax.WithTimeout(60000 * time.Millisecond),
83 gax.WithRetry(func() gax.Retryer {
84 return gax.OnHTTPCodes(gax.Backoff{
85 Initial: 1000 * time.Millisecond,
86 Max: 10000 * time.Millisecond,
87 Multiplier: 1.30,
88 },
89 http.StatusServiceUnavailable)
90 }),
91 },
92 }
93 }
94
95 // internalPolicyTroubleshooterClient is an interface that defines the methods available from Policy Troubleshooter API.
96 type internalPolicyTroubleshooterClient interface {
97 Close() error
98 setGoogleClientInfo(...string)
99 Connection() *grpc.ClientConn
100 TroubleshootIamPolicy(context.Context, *iampb.TroubleshootIamPolicyRequest, ...gax.CallOption) (*iampb.TroubleshootIamPolicyResponse, error)
101 }
102
103 // PolicyTroubleshooterClient is a client for interacting with Policy Troubleshooter API.
104 // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
105 //
106 // IAM Policy Troubleshooter service.
107 //
108 // This service helps you troubleshoot access issues for Google Cloud resources.
109 type PolicyTroubleshooterClient struct {
110 // The internal transport-dependent client.
111 internalClient internalPolicyTroubleshooterClient
112
113 // The call options for this service.
114 CallOptions *PolicyTroubleshooterCallOptions
115 }
116
117 // Wrapper methods routed to the internal client.
118
119 // Close closes the connection to the API service. The user should invoke this when
120 // the client is no longer required.
121 func (c *PolicyTroubleshooterClient) Close() error {
122 return c.internalClient.Close()
123 }
124
125 // setGoogleClientInfo sets the name and version of the application in
126 // the `x-goog-api-client` header passed on each request. Intended for
127 // use by Google-written clients.
128 func (c *PolicyTroubleshooterClient) setGoogleClientInfo(keyval ...string) {
129 c.internalClient.setGoogleClientInfo(keyval...)
130 }
131
132 // Connection returns a connection to the API service.
133 //
134 // Deprecated: Connections are now pooled so this method does not always
135 // return the same resource.
136 func (c *PolicyTroubleshooterClient) Connection() *grpc.ClientConn {
137 return c.internalClient.Connection()
138 }
139
140 // TroubleshootIamPolicy checks whether a principal has a specific permission for a specific
141 // resource, and explains why the principal does or doesn’t have that
142 // permission.
143 func (c *PolicyTroubleshooterClient) TroubleshootIamPolicy(ctx context.Context, req *iampb.TroubleshootIamPolicyRequest, opts ...gax.CallOption) (*iampb.TroubleshootIamPolicyResponse, error) {
144 return c.internalClient.TroubleshootIamPolicy(ctx, req, opts...)
145 }
146
147 // policyTroubleshooterGRPCClient is a client for interacting with Policy Troubleshooter 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 policyTroubleshooterGRPCClient 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 PolicyTroubleshooterClient
155 CallOptions **PolicyTroubleshooterCallOptions
156
157 // The gRPC API client.
158 policyTroubleshooterClient iampb.PolicyTroubleshooterClient
159
160 // The x-goog-* metadata to be sent with each request.
161 xGoogHeaders []string
162
163 logger *slog.Logger
164 }
165
166 // NewPolicyTroubleshooterClient creates a new policy troubleshooter 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 // IAM Policy Troubleshooter service.
170 //
171 // This service helps you troubleshoot access issues for Google Cloud resources.
172 func NewPolicyTroubleshooterClient(ctx context.Context, opts ...option.ClientOption) (*PolicyTroubleshooterClient, error) {
173 clientOpts := defaultPolicyTroubleshooterGRPCClientOptions()
174 if newPolicyTroubleshooterClientHook != nil {
175 hookOpts, err := newPolicyTroubleshooterClientHook(ctx, clientHookParams{})
176 if err != nil {
177 return nil, err
178 }
179 clientOpts = append(clientOpts, hookOpts...)
180 }
181
182 connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
183 if err != nil {
184 return nil, err
185 }
186 client := PolicyTroubleshooterClient{CallOptions: defaultPolicyTroubleshooterCallOptions()}
187
188 c := &policyTroubleshooterGRPCClient{
189 connPool: connPool,
190 policyTroubleshooterClient: iampb.NewPolicyTroubleshooterClient(connPool),
191 CallOptions: &client.CallOptions,
192 logger: internaloption.GetLogger(opts),
193 }
194 c.setGoogleClientInfo()
195
196 client.internalClient = c
197
198 return &client, nil
199 }
200
201 // Connection returns a connection to the API service.
202 //
203 // Deprecated: Connections are now pooled so this method does not always
204 // return the same resource.
205 func (c *policyTroubleshooterGRPCClient) Connection() *grpc.ClientConn {
206 return c.connPool.Conn()
207 }
208
209 // setGoogleClientInfo sets the name and version of the application in
210 // the `x-goog-api-client` header passed on each request. Intended for
211 // use by Google-written clients.
212 func (c *policyTroubleshooterGRPCClient) setGoogleClientInfo(keyval ...string) {
213 kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
214 kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "grpc", grpc.Version, "pb", protoVersion)
215 c.xGoogHeaders = []string{
216 "x-goog-api-client", gax.XGoogHeader(kv...),
217 }
218 }
219
220 // Close closes the connection to the API service. The user should invoke this when
221 // the client is no longer required.
222 func (c *policyTroubleshooterGRPCClient) Close() error {
223 return c.connPool.Close()
224 }
225
226 // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
227 type policyTroubleshooterRESTClient struct {
228 // The http endpoint to connect to.
229 endpoint string
230
231 // The http client.
232 httpClient *http.Client
233
234 // The x-goog-* headers to be sent with each request.
235 xGoogHeaders []string
236
237 // Points back to the CallOptions field of the containing PolicyTroubleshooterClient
238 CallOptions **PolicyTroubleshooterCallOptions
239
240 logger *slog.Logger
241 }
242
243 // NewPolicyTroubleshooterRESTClient creates a new policy troubleshooter rest client.
244 //
245 // IAM Policy Troubleshooter service.
246 //
247 // This service helps you troubleshoot access issues for Google Cloud resources.
248 func NewPolicyTroubleshooterRESTClient(ctx context.Context, opts ...option.ClientOption) (*PolicyTroubleshooterClient, error) {
249 clientOpts := append(defaultPolicyTroubleshooterRESTClientOptions(), opts...)
250 httpClient, endpoint, err := httptransport.NewClient(ctx, clientOpts...)
251 if err != nil {
252 return nil, err
253 }
254
255 callOpts := defaultPolicyTroubleshooterRESTCallOptions()
256 c := &policyTroubleshooterRESTClient{
257 endpoint: endpoint,
258 httpClient: httpClient,
259 CallOptions: &callOpts,
260 logger: internaloption.GetLogger(opts),
261 }
262 c.setGoogleClientInfo()
263
264 return &PolicyTroubleshooterClient{internalClient: c, CallOptions: callOpts}, nil
265 }
266
267 func defaultPolicyTroubleshooterRESTClientOptions() []option.ClientOption {
268 return []option.ClientOption{
269 internaloption.WithDefaultEndpoint("https://policytroubleshooter.googleapis.com"),
270 internaloption.WithDefaultEndpointTemplate("https://policytroubleshooter.UNIVERSE_DOMAIN"),
271 internaloption.WithDefaultMTLSEndpoint("https://policytroubleshooter.mtls.googleapis.com"),
272 internaloption.WithDefaultUniverseDomain("googleapis.com"),
273 internaloption.WithDefaultAudience("https://policytroubleshooter.googleapis.com/"),
274 internaloption.WithDefaultScopes(DefaultAuthScopes()...),
275 internaloption.EnableNewAuthLibrary(),
276 }
277 }
278
279 // setGoogleClientInfo sets the name and version of the application in
280 // the `x-goog-api-client` header passed on each request. Intended for
281 // use by Google-written clients.
282 func (c *policyTroubleshooterRESTClient) setGoogleClientInfo(keyval ...string) {
283 kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
284 kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "rest", "UNKNOWN", "pb", protoVersion)
285 c.xGoogHeaders = []string{
286 "x-goog-api-client", gax.XGoogHeader(kv...),
287 }
288 }
289
290 // Close closes the connection to the API service. The user should invoke this when
291 // the client is no longer required.
292 func (c *policyTroubleshooterRESTClient) Close() error {
293 // Replace httpClient with nil to force cleanup.
294 c.httpClient = nil
295 return nil
296 }
297
298 // Connection returns a connection to the API service.
299 //
300 // Deprecated: This method always returns nil.
301 func (c *policyTroubleshooterRESTClient) Connection() *grpc.ClientConn {
302 return nil
303 }
304 func (c *policyTroubleshooterGRPCClient) TroubleshootIamPolicy(ctx context.Context, req *iampb.TroubleshootIamPolicyRequest, opts ...gax.CallOption) (*iampb.TroubleshootIamPolicyResponse, error) {
305 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, c.xGoogHeaders...)
306 opts = append((*c.CallOptions).TroubleshootIamPolicy[0:len((*c.CallOptions).TroubleshootIamPolicy):len((*c.CallOptions).TroubleshootIamPolicy)], opts...)
307 var resp *iampb.TroubleshootIamPolicyResponse
308 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
309 var err error
310 resp, err = executeRPC(ctx, c.policyTroubleshooterClient.TroubleshootIamPolicy, req, settings.GRPC, c.logger, "TroubleshootIamPolicy")
311 return err
312 }, opts...)
313 if err != nil {
314 return nil, err
315 }
316 return resp, nil
317 }
318
319 // TroubleshootIamPolicy checks whether a principal has a specific permission for a specific
320 // resource, and explains why the principal does or doesn’t have that
321 // permission.
322 func (c *policyTroubleshooterRESTClient) TroubleshootIamPolicy(ctx context.Context, req *iampb.TroubleshootIamPolicyRequest, opts ...gax.CallOption) (*iampb.TroubleshootIamPolicyResponse, error) {
323 m := protojson.MarshalOptions{AllowPartial: true, UseEnumNumbers: true}
324 jsonReq, err := m.Marshal(req)
325 if err != nil {
326 return nil, err
327 }
328
329 baseUrl, err := url.Parse(c.endpoint)
330 if err != nil {
331 return nil, err
332 }
333 baseUrl.Path += fmt.Sprintf("/v3/iam:troubleshoot")
334
335 params := url.Values{}
336 params.Add("$alt", "json;enum-encoding=int")
337
338 baseUrl.RawQuery = params.Encode()
339
340 // Build HTTP headers from client and context metadata.
341 hds := append(c.xGoogHeaders, "Content-Type", "application/json")
342 headers := gax.BuildHeaders(ctx, hds...)
343 opts = append((*c.CallOptions).TroubleshootIamPolicy[0:len((*c.CallOptions).TroubleshootIamPolicy):len((*c.CallOptions).TroubleshootIamPolicy)], opts...)
344 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
345 resp := &iampb.TroubleshootIamPolicyResponse{}
346 e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
347 if settings.Path != "" {
348 baseUrl.Path = settings.Path
349 }
350 httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq))
351 if err != nil {
352 return err
353 }
354 httpReq = httpReq.WithContext(ctx)
355 httpReq.Header = headers
356
357 buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, jsonReq, "TroubleshootIamPolicy")
358 if err != nil {
359 return err
360 }
361
362 if err := unm.Unmarshal(buf, resp); err != nil {
363 return err
364 }
365
366 return nil
367 }, opts...)
368 if e != nil {
369 return nil, e
370 }
371 return resp, nil
372 }