1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package iam
18
19 import (
20 "bytes"
21 "context"
22 "fmt"
23 "io"
24 "math"
25 "net/http"
26 "net/url"
27
28 iampb "cloud.google.com/go/iam/apiv1/iampb"
29 gax "github.com/googleapis/gax-go/v2"
30 "google.golang.org/api/googleapi"
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 newIamPolicyClientHook clientHook
40
41
42 type IamPolicyCallOptions struct {
43 SetIamPolicy []gax.CallOption
44 GetIamPolicy []gax.CallOption
45 TestIamPermissions []gax.CallOption
46 }
47
48 func defaultIamPolicyGRPCClientOptions() []option.ClientOption {
49 return []option.ClientOption{
50 internaloption.WithDefaultEndpoint("iam-meta-api.googleapis.com:443"),
51 internaloption.WithDefaultEndpointTemplate("iam-meta-api.UNIVERSE_DOMAIN:443"),
52 internaloption.WithDefaultMTLSEndpoint("iam-meta-api.mtls.googleapis.com:443"),
53 internaloption.WithDefaultUniverseDomain("googleapis.com"),
54 internaloption.WithDefaultAudience("https://iam-meta-api.googleapis.com/"),
55 internaloption.WithDefaultScopes(DefaultAuthScopes()...),
56 internaloption.EnableJwtWithScope(),
57 option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
58 grpc.MaxCallRecvMsgSize(math.MaxInt32))),
59 }
60 }
61
62 func defaultIamPolicyCallOptions() *IamPolicyCallOptions {
63 return &IamPolicyCallOptions{
64 SetIamPolicy: []gax.CallOption{},
65 GetIamPolicy: []gax.CallOption{},
66 TestIamPermissions: []gax.CallOption{},
67 }
68 }
69
70 func defaultIamPolicyRESTCallOptions() *IamPolicyCallOptions {
71 return &IamPolicyCallOptions{
72 SetIamPolicy: []gax.CallOption{},
73 GetIamPolicy: []gax.CallOption{},
74 TestIamPermissions: []gax.CallOption{},
75 }
76 }
77
78
79 type internalIamPolicyClient interface {
80 Close() error
81 setGoogleClientInfo(...string)
82 Connection() *grpc.ClientConn
83 SetIamPolicy(context.Context, *iampb.SetIamPolicyRequest, ...gax.CallOption) (*iampb.Policy, error)
84 GetIamPolicy(context.Context, *iampb.GetIamPolicyRequest, ...gax.CallOption) (*iampb.Policy, error)
85 TestIamPermissions(context.Context, *iampb.TestIamPermissionsRequest, ...gax.CallOption) (*iampb.TestIamPermissionsResponse, error)
86 }
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112 type IamPolicyClient struct {
113
114 internalClient internalIamPolicyClient
115
116
117 CallOptions *IamPolicyCallOptions
118 }
119
120
121
122
123
124 func (c *IamPolicyClient) Close() error {
125 return c.internalClient.Close()
126 }
127
128
129
130
131 func (c *IamPolicyClient) setGoogleClientInfo(keyval ...string) {
132 c.internalClient.setGoogleClientInfo(keyval...)
133 }
134
135
136
137
138
139 func (c *IamPolicyClient) Connection() *grpc.ClientConn {
140 return c.internalClient.Connection()
141 }
142
143
144
145
146
147 func (c *IamPolicyClient) SetIamPolicy(ctx context.Context, req *iampb.SetIamPolicyRequest, opts ...gax.CallOption) (*iampb.Policy, error) {
148 return c.internalClient.SetIamPolicy(ctx, req, opts...)
149 }
150
151
152
153
154 func (c *IamPolicyClient) GetIamPolicy(ctx context.Context, req *iampb.GetIamPolicyRequest, opts ...gax.CallOption) (*iampb.Policy, error) {
155 return c.internalClient.GetIamPolicy(ctx, req, opts...)
156 }
157
158
159
160
161
162
163
164
165 func (c *IamPolicyClient) TestIamPermissions(ctx context.Context, req *iampb.TestIamPermissionsRequest, opts ...gax.CallOption) (*iampb.TestIamPermissionsResponse, error) {
166 return c.internalClient.TestIamPermissions(ctx, req, opts...)
167 }
168
169
170
171
172 type iamPolicyGRPCClient struct {
173
174 connPool gtransport.ConnPool
175
176
177 CallOptions **IamPolicyCallOptions
178
179
180 iamPolicyClient iampb.IAMPolicyClient
181
182
183 xGoogHeaders []string
184 }
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210 func NewIamPolicyClient(ctx context.Context, opts ...option.ClientOption) (*IamPolicyClient, error) {
211 clientOpts := defaultIamPolicyGRPCClientOptions()
212 if newIamPolicyClientHook != nil {
213 hookOpts, err := newIamPolicyClientHook(ctx, clientHookParams{})
214 if err != nil {
215 return nil, err
216 }
217 clientOpts = append(clientOpts, hookOpts...)
218 }
219
220 connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
221 if err != nil {
222 return nil, err
223 }
224 client := IamPolicyClient{CallOptions: defaultIamPolicyCallOptions()}
225
226 c := &iamPolicyGRPCClient{
227 connPool: connPool,
228 iamPolicyClient: iampb.NewIAMPolicyClient(connPool),
229 CallOptions: &client.CallOptions,
230 }
231 c.setGoogleClientInfo()
232
233 client.internalClient = c
234
235 return &client, nil
236 }
237
238
239
240
241
242 func (c *iamPolicyGRPCClient) Connection() *grpc.ClientConn {
243 return c.connPool.Conn()
244 }
245
246
247
248
249 func (c *iamPolicyGRPCClient) setGoogleClientInfo(keyval ...string) {
250 kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
251 kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "grpc", grpc.Version)
252 c.xGoogHeaders = []string{"x-goog-api-client", gax.XGoogHeader(kv...)}
253 }
254
255
256
257 func (c *iamPolicyGRPCClient) Close() error {
258 return c.connPool.Close()
259 }
260
261
262 type iamPolicyRESTClient struct {
263
264 endpoint string
265
266
267 httpClient *http.Client
268
269
270 xGoogHeaders []string
271
272
273 CallOptions **IamPolicyCallOptions
274 }
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299 func NewIamPolicyRESTClient(ctx context.Context, opts ...option.ClientOption) (*IamPolicyClient, error) {
300 clientOpts := append(defaultIamPolicyRESTClientOptions(), opts...)
301 httpClient, endpoint, err := httptransport.NewClient(ctx, clientOpts...)
302 if err != nil {
303 return nil, err
304 }
305
306 callOpts := defaultIamPolicyRESTCallOptions()
307 c := &iamPolicyRESTClient{
308 endpoint: endpoint,
309 httpClient: httpClient,
310 CallOptions: &callOpts,
311 }
312 c.setGoogleClientInfo()
313
314 return &IamPolicyClient{internalClient: c, CallOptions: callOpts}, nil
315 }
316
317 func defaultIamPolicyRESTClientOptions() []option.ClientOption {
318 return []option.ClientOption{
319 internaloption.WithDefaultEndpoint("https://iam-meta-api.googleapis.com"),
320 internaloption.WithDefaultEndpointTemplate("https://iam-meta-api.UNIVERSE_DOMAIN"),
321 internaloption.WithDefaultMTLSEndpoint("https://iam-meta-api.mtls.googleapis.com"),
322 internaloption.WithDefaultUniverseDomain("googleapis.com"),
323 internaloption.WithDefaultAudience("https://iam-meta-api.googleapis.com/"),
324 internaloption.WithDefaultScopes(DefaultAuthScopes()...),
325 }
326 }
327
328
329
330
331 func (c *iamPolicyRESTClient) setGoogleClientInfo(keyval ...string) {
332 kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
333 kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "rest", "UNKNOWN")
334 c.xGoogHeaders = []string{"x-goog-api-client", gax.XGoogHeader(kv...)}
335 }
336
337
338
339 func (c *iamPolicyRESTClient) Close() error {
340
341 c.httpClient = nil
342 return nil
343 }
344
345
346
347
348 func (c *iamPolicyRESTClient) Connection() *grpc.ClientConn {
349 return nil
350 }
351 func (c *iamPolicyGRPCClient) SetIamPolicy(ctx context.Context, req *iampb.SetIamPolicyRequest, opts ...gax.CallOption) (*iampb.Policy, error) {
352 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "resource", url.QueryEscape(req.GetResource()))}
353
354 hds = append(c.xGoogHeaders, hds...)
355 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
356 opts = append((*c.CallOptions).SetIamPolicy[0:len((*c.CallOptions).SetIamPolicy):len((*c.CallOptions).SetIamPolicy)], opts...)
357 var resp *iampb.Policy
358 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
359 var err error
360 resp, err = c.iamPolicyClient.SetIamPolicy(ctx, req, settings.GRPC...)
361 return err
362 }, opts...)
363 if err != nil {
364 return nil, err
365 }
366 return resp, nil
367 }
368
369 func (c *iamPolicyGRPCClient) GetIamPolicy(ctx context.Context, req *iampb.GetIamPolicyRequest, opts ...gax.CallOption) (*iampb.Policy, error) {
370 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "resource", url.QueryEscape(req.GetResource()))}
371
372 hds = append(c.xGoogHeaders, hds...)
373 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
374 opts = append((*c.CallOptions).GetIamPolicy[0:len((*c.CallOptions).GetIamPolicy):len((*c.CallOptions).GetIamPolicy)], opts...)
375 var resp *iampb.Policy
376 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
377 var err error
378 resp, err = c.iamPolicyClient.GetIamPolicy(ctx, req, settings.GRPC...)
379 return err
380 }, opts...)
381 if err != nil {
382 return nil, err
383 }
384 return resp, nil
385 }
386
387 func (c *iamPolicyGRPCClient) TestIamPermissions(ctx context.Context, req *iampb.TestIamPermissionsRequest, opts ...gax.CallOption) (*iampb.TestIamPermissionsResponse, error) {
388 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "resource", url.QueryEscape(req.GetResource()))}
389
390 hds = append(c.xGoogHeaders, hds...)
391 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
392 opts = append((*c.CallOptions).TestIamPermissions[0:len((*c.CallOptions).TestIamPermissions):len((*c.CallOptions).TestIamPermissions)], opts...)
393 var resp *iampb.TestIamPermissionsResponse
394 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
395 var err error
396 resp, err = c.iamPolicyClient.TestIamPermissions(ctx, req, settings.GRPC...)
397 return err
398 }, opts...)
399 if err != nil {
400 return nil, err
401 }
402 return resp, nil
403 }
404
405
406
407
408
409 func (c *iamPolicyRESTClient) SetIamPolicy(ctx context.Context, req *iampb.SetIamPolicyRequest, opts ...gax.CallOption) (*iampb.Policy, error) {
410 m := protojson.MarshalOptions{AllowPartial: true, UseEnumNumbers: true}
411 jsonReq, err := m.Marshal(req)
412 if err != nil {
413 return nil, err
414 }
415
416 baseUrl, err := url.Parse(c.endpoint)
417 if err != nil {
418 return nil, err
419 }
420 baseUrl.Path += fmt.Sprintf("/v1/%v:setIamPolicy", req.GetResource())
421
422 params := url.Values{}
423 params.Add("$alt", "json;enum-encoding=int")
424
425 baseUrl.RawQuery = params.Encode()
426
427
428 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "resource", url.QueryEscape(req.GetResource()))}
429
430 hds = append(c.xGoogHeaders, hds...)
431 hds = append(hds, "Content-Type", "application/json")
432 headers := gax.BuildHeaders(ctx, hds...)
433 opts = append((*c.CallOptions).SetIamPolicy[0:len((*c.CallOptions).SetIamPolicy):len((*c.CallOptions).SetIamPolicy)], opts...)
434 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
435 resp := &iampb.Policy{}
436 e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
437 if settings.Path != "" {
438 baseUrl.Path = settings.Path
439 }
440 httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq))
441 if err != nil {
442 return err
443 }
444 httpReq = httpReq.WithContext(ctx)
445 httpReq.Header = headers
446
447 httpRsp, err := c.httpClient.Do(httpReq)
448 if err != nil {
449 return err
450 }
451 defer httpRsp.Body.Close()
452
453 if err = googleapi.CheckResponse(httpRsp); err != nil {
454 return err
455 }
456
457 buf, err := io.ReadAll(httpRsp.Body)
458 if err != nil {
459 return err
460 }
461
462 if err := unm.Unmarshal(buf, resp); err != nil {
463 return err
464 }
465
466 return nil
467 }, opts...)
468 if e != nil {
469 return nil, e
470 }
471 return resp, nil
472 }
473
474
475
476
477 func (c *iamPolicyRESTClient) GetIamPolicy(ctx context.Context, req *iampb.GetIamPolicyRequest, opts ...gax.CallOption) (*iampb.Policy, error) {
478 m := protojson.MarshalOptions{AllowPartial: true, UseEnumNumbers: true}
479 jsonReq, err := m.Marshal(req)
480 if err != nil {
481 return nil, err
482 }
483
484 baseUrl, err := url.Parse(c.endpoint)
485 if err != nil {
486 return nil, err
487 }
488 baseUrl.Path += fmt.Sprintf("/v1/%v:getIamPolicy", req.GetResource())
489
490 params := url.Values{}
491 params.Add("$alt", "json;enum-encoding=int")
492
493 baseUrl.RawQuery = params.Encode()
494
495
496 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "resource", url.QueryEscape(req.GetResource()))}
497
498 hds = append(c.xGoogHeaders, hds...)
499 hds = append(hds, "Content-Type", "application/json")
500 headers := gax.BuildHeaders(ctx, hds...)
501 opts = append((*c.CallOptions).GetIamPolicy[0:len((*c.CallOptions).GetIamPolicy):len((*c.CallOptions).GetIamPolicy)], opts...)
502 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
503 resp := &iampb.Policy{}
504 e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
505 if settings.Path != "" {
506 baseUrl.Path = settings.Path
507 }
508 httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq))
509 if err != nil {
510 return err
511 }
512 httpReq = httpReq.WithContext(ctx)
513 httpReq.Header = headers
514
515 httpRsp, err := c.httpClient.Do(httpReq)
516 if err != nil {
517 return err
518 }
519 defer httpRsp.Body.Close()
520
521 if err = googleapi.CheckResponse(httpRsp); err != nil {
522 return err
523 }
524
525 buf, err := io.ReadAll(httpRsp.Body)
526 if err != nil {
527 return err
528 }
529
530 if err := unm.Unmarshal(buf, resp); err != nil {
531 return err
532 }
533
534 return nil
535 }, opts...)
536 if e != nil {
537 return nil, e
538 }
539 return resp, nil
540 }
541
542
543
544
545
546
547
548
549 func (c *iamPolicyRESTClient) TestIamPermissions(ctx context.Context, req *iampb.TestIamPermissionsRequest, opts ...gax.CallOption) (*iampb.TestIamPermissionsResponse, error) {
550 m := protojson.MarshalOptions{AllowPartial: true, UseEnumNumbers: true}
551 jsonReq, err := m.Marshal(req)
552 if err != nil {
553 return nil, err
554 }
555
556 baseUrl, err := url.Parse(c.endpoint)
557 if err != nil {
558 return nil, err
559 }
560 baseUrl.Path += fmt.Sprintf("/v1/%v:testIamPermissions", req.GetResource())
561
562 params := url.Values{}
563 params.Add("$alt", "json;enum-encoding=int")
564
565 baseUrl.RawQuery = params.Encode()
566
567
568 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "resource", url.QueryEscape(req.GetResource()))}
569
570 hds = append(c.xGoogHeaders, hds...)
571 hds = append(hds, "Content-Type", "application/json")
572 headers := gax.BuildHeaders(ctx, hds...)
573 opts = append((*c.CallOptions).TestIamPermissions[0:len((*c.CallOptions).TestIamPermissions):len((*c.CallOptions).TestIamPermissions)], opts...)
574 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
575 resp := &iampb.TestIamPermissionsResponse{}
576 e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
577 if settings.Path != "" {
578 baseUrl.Path = settings.Path
579 }
580 httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq))
581 if err != nil {
582 return err
583 }
584 httpReq = httpReq.WithContext(ctx)
585 httpReq.Header = headers
586
587 httpRsp, err := c.httpClient.Do(httpReq)
588 if err != nil {
589 return err
590 }
591 defer httpRsp.Body.Close()
592
593 if err = googleapi.CheckResponse(httpRsp); err != nil {
594 return err
595 }
596
597 buf, err := io.ReadAll(httpRsp.Body)
598 if err != nil {
599 return err
600 }
601
602 if err := unm.Unmarshal(buf, resp); err != nil {
603 return err
604 }
605
606 return nil
607 }, opts...)
608 if e != nil {
609 return nil, e
610 }
611 return resp, nil
612 }
613
View as plain text