Debian Codegraph @

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