...

Source file src/cloud.google.com/go/resourcemanager/apiv3/organizations_client.go

Documentation: cloud.google.com/go/resourcemanager/apiv3

     1  // Copyright 2024 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 resourcemanager
    18  
    19  import (
    20  	"bytes"
    21  	"context"
    22  	"fmt"
    23  	"io"
    24  	"math"
    25  	"net/http"
    26  	"net/url"
    27  	"time"
    28  
    29  	iampb "cloud.google.com/go/iam/apiv1/iampb"
    30  	longrunningpb "cloud.google.com/go/longrunning/autogen/longrunningpb"
    31  	resourcemanagerpb "cloud.google.com/go/resourcemanager/apiv3/resourcemanagerpb"
    32  	gax "github.com/googleapis/gax-go/v2"
    33  	"google.golang.org/api/googleapi"
    34  	"google.golang.org/api/iterator"
    35  	"google.golang.org/api/option"
    36  	"google.golang.org/api/option/internaloption"
    37  	gtransport "google.golang.org/api/transport/grpc"
    38  	httptransport "google.golang.org/api/transport/http"
    39  	"google.golang.org/grpc"
    40  	"google.golang.org/grpc/codes"
    41  	"google.golang.org/protobuf/encoding/protojson"
    42  	"google.golang.org/protobuf/proto"
    43  )
    44  
    45  var newOrganizationsClientHook clientHook
    46  
    47  // OrganizationsCallOptions contains the retry settings for each method of OrganizationsClient.
    48  type OrganizationsCallOptions struct {
    49  	GetOrganization     []gax.CallOption
    50  	SearchOrganizations []gax.CallOption
    51  	GetIamPolicy        []gax.CallOption
    52  	SetIamPolicy        []gax.CallOption
    53  	TestIamPermissions  []gax.CallOption
    54  	GetOperation        []gax.CallOption
    55  }
    56  
    57  func defaultOrganizationsGRPCClientOptions() []option.ClientOption {
    58  	return []option.ClientOption{
    59  		internaloption.WithDefaultEndpoint("cloudresourcemanager.googleapis.com:443"),
    60  		internaloption.WithDefaultEndpointTemplate("cloudresourcemanager.UNIVERSE_DOMAIN:443"),
    61  		internaloption.WithDefaultMTLSEndpoint("cloudresourcemanager.mtls.googleapis.com:443"),
    62  		internaloption.WithDefaultUniverseDomain("googleapis.com"),
    63  		internaloption.WithDefaultAudience("https://cloudresourcemanager.googleapis.com/"),
    64  		internaloption.WithDefaultScopes(DefaultAuthScopes()...),
    65  		internaloption.EnableJwtWithScope(),
    66  		option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
    67  			grpc.MaxCallRecvMsgSize(math.MaxInt32))),
    68  	}
    69  }
    70  
    71  func defaultOrganizationsCallOptions() *OrganizationsCallOptions {
    72  	return &OrganizationsCallOptions{
    73  		GetOrganization: []gax.CallOption{
    74  			gax.WithTimeout(60000 * time.Millisecond),
    75  			gax.WithRetry(func() gax.Retryer {
    76  				return gax.OnCodes([]codes.Code{
    77  					codes.Unavailable,
    78  				}, gax.Backoff{
    79  					Initial:    100 * time.Millisecond,
    80  					Max:        60000 * time.Millisecond,
    81  					Multiplier: 1.30,
    82  				})
    83  			}),
    84  		},
    85  		SearchOrganizations: []gax.CallOption{
    86  			gax.WithTimeout(60000 * time.Millisecond),
    87  		},
    88  		GetIamPolicy: []gax.CallOption{
    89  			gax.WithTimeout(60000 * time.Millisecond),
    90  			gax.WithRetry(func() gax.Retryer {
    91  				return gax.OnCodes([]codes.Code{
    92  					codes.Unavailable,
    93  				}, gax.Backoff{
    94  					Initial:    100 * time.Millisecond,
    95  					Max:        60000 * time.Millisecond,
    96  					Multiplier: 1.30,
    97  				})
    98  			}),
    99  		},
   100  		SetIamPolicy: []gax.CallOption{
   101  			gax.WithTimeout(60000 * time.Millisecond),
   102  		},
   103  		TestIamPermissions: []gax.CallOption{},
   104  		GetOperation:       []gax.CallOption{},
   105  	}
   106  }
   107  
   108  func defaultOrganizationsRESTCallOptions() *OrganizationsCallOptions {
   109  	return &OrganizationsCallOptions{
   110  		GetOrganization: []gax.CallOption{
   111  			gax.WithTimeout(60000 * time.Millisecond),
   112  			gax.WithRetry(func() gax.Retryer {
   113  				return gax.OnHTTPCodes(gax.Backoff{
   114  					Initial:    100 * time.Millisecond,
   115  					Max:        60000 * time.Millisecond,
   116  					Multiplier: 1.30,
   117  				},
   118  					http.StatusServiceUnavailable)
   119  			}),
   120  		},
   121  		SearchOrganizations: []gax.CallOption{
   122  			gax.WithTimeout(60000 * time.Millisecond),
   123  		},
   124  		GetIamPolicy: []gax.CallOption{
   125  			gax.WithTimeout(60000 * time.Millisecond),
   126  			gax.WithRetry(func() gax.Retryer {
   127  				return gax.OnHTTPCodes(gax.Backoff{
   128  					Initial:    100 * time.Millisecond,
   129  					Max:        60000 * time.Millisecond,
   130  					Multiplier: 1.30,
   131  				},
   132  					http.StatusServiceUnavailable)
   133  			}),
   134  		},
   135  		SetIamPolicy: []gax.CallOption{
   136  			gax.WithTimeout(60000 * time.Millisecond),
   137  		},
   138  		TestIamPermissions: []gax.CallOption{},
   139  		GetOperation:       []gax.CallOption{},
   140  	}
   141  }
   142  
   143  // internalOrganizationsClient is an interface that defines the methods available from Cloud Resource Manager API.
   144  type internalOrganizationsClient interface {
   145  	Close() error
   146  	setGoogleClientInfo(...string)
   147  	Connection() *grpc.ClientConn
   148  	GetOrganization(context.Context, *resourcemanagerpb.GetOrganizationRequest, ...gax.CallOption) (*resourcemanagerpb.Organization, error)
   149  	SearchOrganizations(context.Context, *resourcemanagerpb.SearchOrganizationsRequest, ...gax.CallOption) *OrganizationIterator
   150  	GetIamPolicy(context.Context, *iampb.GetIamPolicyRequest, ...gax.CallOption) (*iampb.Policy, error)
   151  	SetIamPolicy(context.Context, *iampb.SetIamPolicyRequest, ...gax.CallOption) (*iampb.Policy, error)
   152  	TestIamPermissions(context.Context, *iampb.TestIamPermissionsRequest, ...gax.CallOption) (*iampb.TestIamPermissionsResponse, error)
   153  	GetOperation(context.Context, *longrunningpb.GetOperationRequest, ...gax.CallOption) (*longrunningpb.Operation, error)
   154  }
   155  
   156  // OrganizationsClient is a client for interacting with Cloud Resource Manager API.
   157  // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
   158  //
   159  // Allows users to manage their organization resources.
   160  type OrganizationsClient struct {
   161  	// The internal transport-dependent client.
   162  	internalClient internalOrganizationsClient
   163  
   164  	// The call options for this service.
   165  	CallOptions *OrganizationsCallOptions
   166  }
   167  
   168  // Wrapper methods routed to the internal client.
   169  
   170  // Close closes the connection to the API service. The user should invoke this when
   171  // the client is no longer required.
   172  func (c *OrganizationsClient) Close() error {
   173  	return c.internalClient.Close()
   174  }
   175  
   176  // setGoogleClientInfo sets the name and version of the application in
   177  // the `x-goog-api-client` header passed on each request. Intended for
   178  // use by Google-written clients.
   179  func (c *OrganizationsClient) setGoogleClientInfo(keyval ...string) {
   180  	c.internalClient.setGoogleClientInfo(keyval...)
   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 *OrganizationsClient) Connection() *grpc.ClientConn {
   188  	return c.internalClient.Connection()
   189  }
   190  
   191  // GetOrganization fetches an organization resource identified by the specified resource name.
   192  func (c *OrganizationsClient) GetOrganization(ctx context.Context, req *resourcemanagerpb.GetOrganizationRequest, opts ...gax.CallOption) (*resourcemanagerpb.Organization, error) {
   193  	return c.internalClient.GetOrganization(ctx, req, opts...)
   194  }
   195  
   196  // SearchOrganizations searches organization resources that are visible to the user and satisfy
   197  // the specified filter. This method returns organizations in an unspecified
   198  // order. New organizations do not necessarily appear at the end of the
   199  // results, and may take a small amount of time to appear.
   200  //
   201  // Search will only return organizations on which the user has the permission
   202  // resourcemanager.organizations.get
   203  func (c *OrganizationsClient) SearchOrganizations(ctx context.Context, req *resourcemanagerpb.SearchOrganizationsRequest, opts ...gax.CallOption) *OrganizationIterator {
   204  	return c.internalClient.SearchOrganizations(ctx, req, opts...)
   205  }
   206  
   207  // GetIamPolicy gets the access control policy for an organization resource. The policy may
   208  // be empty if no such policy or resource exists. The resource field should
   209  // be the organization’s resource name, for example: “organizations/123”.
   210  //
   211  // Authorization requires the IAM permission
   212  // resourcemanager.organizations.getIamPolicy on the specified organization.
   213  func (c *OrganizationsClient) GetIamPolicy(ctx context.Context, req *iampb.GetIamPolicyRequest, opts ...gax.CallOption) (*iampb.Policy, error) {
   214  	return c.internalClient.GetIamPolicy(ctx, req, opts...)
   215  }
   216  
   217  // SetIamPolicy sets the access control policy on an organization resource. Replaces any
   218  // existing policy. The resource field should be the organization’s resource
   219  // name, for example: “organizations/123”.
   220  //
   221  // Authorization requires the IAM permission
   222  // resourcemanager.organizations.setIamPolicy on the specified organization.
   223  func (c *OrganizationsClient) SetIamPolicy(ctx context.Context, req *iampb.SetIamPolicyRequest, opts ...gax.CallOption) (*iampb.Policy, error) {
   224  	return c.internalClient.SetIamPolicy(ctx, req, opts...)
   225  }
   226  
   227  // TestIamPermissions returns the permissions that a caller has on the specified organization.
   228  // The resource field should be the organization’s resource name,
   229  // for example: “organizations/123”.
   230  //
   231  // There are no permissions required for making this API call.
   232  func (c *OrganizationsClient) TestIamPermissions(ctx context.Context, req *iampb.TestIamPermissionsRequest, opts ...gax.CallOption) (*iampb.TestIamPermissionsResponse, error) {
   233  	return c.internalClient.TestIamPermissions(ctx, req, opts...)
   234  }
   235  
   236  // GetOperation is a utility method from google.longrunning.Operations.
   237  func (c *OrganizationsClient) GetOperation(ctx context.Context, req *longrunningpb.GetOperationRequest, opts ...gax.CallOption) (*longrunningpb.Operation, error) {
   238  	return c.internalClient.GetOperation(ctx, req, opts...)
   239  }
   240  
   241  // organizationsGRPCClient is a client for interacting with Cloud Resource Manager API over gRPC transport.
   242  //
   243  // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
   244  type organizationsGRPCClient struct {
   245  	// Connection pool of gRPC connections to the service.
   246  	connPool gtransport.ConnPool
   247  
   248  	// Points back to the CallOptions field of the containing OrganizationsClient
   249  	CallOptions **OrganizationsCallOptions
   250  
   251  	// The gRPC API client.
   252  	organizationsClient resourcemanagerpb.OrganizationsClient
   253  
   254  	operationsClient longrunningpb.OperationsClient
   255  
   256  	// The x-goog-* metadata to be sent with each request.
   257  	xGoogHeaders []string
   258  }
   259  
   260  // NewOrganizationsClient creates a new organizations client based on gRPC.
   261  // The returned client must be Closed when it is done being used to clean up its underlying connections.
   262  //
   263  // Allows users to manage their organization resources.
   264  func NewOrganizationsClient(ctx context.Context, opts ...option.ClientOption) (*OrganizationsClient, error) {
   265  	clientOpts := defaultOrganizationsGRPCClientOptions()
   266  	if newOrganizationsClientHook != nil {
   267  		hookOpts, err := newOrganizationsClientHook(ctx, clientHookParams{})
   268  		if err != nil {
   269  			return nil, err
   270  		}
   271  		clientOpts = append(clientOpts, hookOpts...)
   272  	}
   273  
   274  	connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
   275  	if err != nil {
   276  		return nil, err
   277  	}
   278  	client := OrganizationsClient{CallOptions: defaultOrganizationsCallOptions()}
   279  
   280  	c := &organizationsGRPCClient{
   281  		connPool:            connPool,
   282  		organizationsClient: resourcemanagerpb.NewOrganizationsClient(connPool),
   283  		CallOptions:         &client.CallOptions,
   284  		operationsClient:    longrunningpb.NewOperationsClient(connPool),
   285  	}
   286  	c.setGoogleClientInfo()
   287  
   288  	client.internalClient = c
   289  
   290  	return &client, nil
   291  }
   292  
   293  // Connection returns a connection to the API service.
   294  //
   295  // Deprecated: Connections are now pooled so this method does not always
   296  // return the same resource.
   297  func (c *organizationsGRPCClient) Connection() *grpc.ClientConn {
   298  	return c.connPool.Conn()
   299  }
   300  
   301  // setGoogleClientInfo sets the name and version of the application in
   302  // the `x-goog-api-client` header passed on each request. Intended for
   303  // use by Google-written clients.
   304  func (c *organizationsGRPCClient) setGoogleClientInfo(keyval ...string) {
   305  	kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
   306  	kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "grpc", grpc.Version)
   307  	c.xGoogHeaders = []string{"x-goog-api-client", gax.XGoogHeader(kv...)}
   308  }
   309  
   310  // Close closes the connection to the API service. The user should invoke this when
   311  // the client is no longer required.
   312  func (c *organizationsGRPCClient) Close() error {
   313  	return c.connPool.Close()
   314  }
   315  
   316  // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
   317  type organizationsRESTClient struct {
   318  	// The http endpoint to connect to.
   319  	endpoint string
   320  
   321  	// The http client.
   322  	httpClient *http.Client
   323  
   324  	// The x-goog-* headers to be sent with each request.
   325  	xGoogHeaders []string
   326  
   327  	// Points back to the CallOptions field of the containing OrganizationsClient
   328  	CallOptions **OrganizationsCallOptions
   329  }
   330  
   331  // NewOrganizationsRESTClient creates a new organizations rest client.
   332  //
   333  // Allows users to manage their organization resources.
   334  func NewOrganizationsRESTClient(ctx context.Context, opts ...option.ClientOption) (*OrganizationsClient, error) {
   335  	clientOpts := append(defaultOrganizationsRESTClientOptions(), opts...)
   336  	httpClient, endpoint, err := httptransport.NewClient(ctx, clientOpts...)
   337  	if err != nil {
   338  		return nil, err
   339  	}
   340  
   341  	callOpts := defaultOrganizationsRESTCallOptions()
   342  	c := &organizationsRESTClient{
   343  		endpoint:    endpoint,
   344  		httpClient:  httpClient,
   345  		CallOptions: &callOpts,
   346  	}
   347  	c.setGoogleClientInfo()
   348  
   349  	return &OrganizationsClient{internalClient: c, CallOptions: callOpts}, nil
   350  }
   351  
   352  func defaultOrganizationsRESTClientOptions() []option.ClientOption {
   353  	return []option.ClientOption{
   354  		internaloption.WithDefaultEndpoint("https://cloudresourcemanager.googleapis.com"),
   355  		internaloption.WithDefaultEndpointTemplate("https://cloudresourcemanager.UNIVERSE_DOMAIN"),
   356  		internaloption.WithDefaultMTLSEndpoint("https://cloudresourcemanager.mtls.googleapis.com"),
   357  		internaloption.WithDefaultUniverseDomain("googleapis.com"),
   358  		internaloption.WithDefaultAudience("https://cloudresourcemanager.googleapis.com/"),
   359  		internaloption.WithDefaultScopes(DefaultAuthScopes()...),
   360  	}
   361  }
   362  
   363  // setGoogleClientInfo sets the name and version of the application in
   364  // the `x-goog-api-client` header passed on each request. Intended for
   365  // use by Google-written clients.
   366  func (c *organizationsRESTClient) setGoogleClientInfo(keyval ...string) {
   367  	kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
   368  	kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "rest", "UNKNOWN")
   369  	c.xGoogHeaders = []string{"x-goog-api-client", gax.XGoogHeader(kv...)}
   370  }
   371  
   372  // Close closes the connection to the API service. The user should invoke this when
   373  // the client is no longer required.
   374  func (c *organizationsRESTClient) Close() error {
   375  	// Replace httpClient with nil to force cleanup.
   376  	c.httpClient = nil
   377  	return nil
   378  }
   379  
   380  // Connection returns a connection to the API service.
   381  //
   382  // Deprecated: This method always returns nil.
   383  func (c *organizationsRESTClient) Connection() *grpc.ClientConn {
   384  	return nil
   385  }
   386  func (c *organizationsGRPCClient) GetOrganization(ctx context.Context, req *resourcemanagerpb.GetOrganizationRequest, opts ...gax.CallOption) (*resourcemanagerpb.Organization, error) {
   387  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
   388  
   389  	hds = append(c.xGoogHeaders, hds...)
   390  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   391  	opts = append((*c.CallOptions).GetOrganization[0:len((*c.CallOptions).GetOrganization):len((*c.CallOptions).GetOrganization)], opts...)
   392  	var resp *resourcemanagerpb.Organization
   393  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   394  		var err error
   395  		resp, err = c.organizationsClient.GetOrganization(ctx, req, settings.GRPC...)
   396  		return err
   397  	}, opts...)
   398  	if err != nil {
   399  		return nil, err
   400  	}
   401  	return resp, nil
   402  }
   403  
   404  func (c *organizationsGRPCClient) SearchOrganizations(ctx context.Context, req *resourcemanagerpb.SearchOrganizationsRequest, opts ...gax.CallOption) *OrganizationIterator {
   405  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, c.xGoogHeaders...)
   406  	opts = append((*c.CallOptions).SearchOrganizations[0:len((*c.CallOptions).SearchOrganizations):len((*c.CallOptions).SearchOrganizations)], opts...)
   407  	it := &OrganizationIterator{}
   408  	req = proto.Clone(req).(*resourcemanagerpb.SearchOrganizationsRequest)
   409  	it.InternalFetch = func(pageSize int, pageToken string) ([]*resourcemanagerpb.Organization, string, error) {
   410  		resp := &resourcemanagerpb.SearchOrganizationsResponse{}
   411  		if pageToken != "" {
   412  			req.PageToken = pageToken
   413  		}
   414  		if pageSize > math.MaxInt32 {
   415  			req.PageSize = math.MaxInt32
   416  		} else if pageSize != 0 {
   417  			req.PageSize = int32(pageSize)
   418  		}
   419  		err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   420  			var err error
   421  			resp, err = c.organizationsClient.SearchOrganizations(ctx, req, settings.GRPC...)
   422  			return err
   423  		}, opts...)
   424  		if err != nil {
   425  			return nil, "", err
   426  		}
   427  
   428  		it.Response = resp
   429  		return resp.GetOrganizations(), resp.GetNextPageToken(), nil
   430  	}
   431  	fetch := func(pageSize int, pageToken string) (string, error) {
   432  		items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
   433  		if err != nil {
   434  			return "", err
   435  		}
   436  		it.items = append(it.items, items...)
   437  		return nextPageToken, nil
   438  	}
   439  
   440  	it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
   441  	it.pageInfo.MaxSize = int(req.GetPageSize())
   442  	it.pageInfo.Token = req.GetPageToken()
   443  
   444  	return it
   445  }
   446  
   447  func (c *organizationsGRPCClient) GetIamPolicy(ctx context.Context, req *iampb.GetIamPolicyRequest, opts ...gax.CallOption) (*iampb.Policy, error) {
   448  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "resource", url.QueryEscape(req.GetResource()))}
   449  
   450  	hds = append(c.xGoogHeaders, hds...)
   451  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   452  	opts = append((*c.CallOptions).GetIamPolicy[0:len((*c.CallOptions).GetIamPolicy):len((*c.CallOptions).GetIamPolicy)], opts...)
   453  	var resp *iampb.Policy
   454  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   455  		var err error
   456  		resp, err = c.organizationsClient.GetIamPolicy(ctx, req, settings.GRPC...)
   457  		return err
   458  	}, opts...)
   459  	if err != nil {
   460  		return nil, err
   461  	}
   462  	return resp, nil
   463  }
   464  
   465  func (c *organizationsGRPCClient) SetIamPolicy(ctx context.Context, req *iampb.SetIamPolicyRequest, opts ...gax.CallOption) (*iampb.Policy, error) {
   466  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "resource", url.QueryEscape(req.GetResource()))}
   467  
   468  	hds = append(c.xGoogHeaders, hds...)
   469  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   470  	opts = append((*c.CallOptions).SetIamPolicy[0:len((*c.CallOptions).SetIamPolicy):len((*c.CallOptions).SetIamPolicy)], opts...)
   471  	var resp *iampb.Policy
   472  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   473  		var err error
   474  		resp, err = c.organizationsClient.SetIamPolicy(ctx, req, settings.GRPC...)
   475  		return err
   476  	}, opts...)
   477  	if err != nil {
   478  		return nil, err
   479  	}
   480  	return resp, nil
   481  }
   482  
   483  func (c *organizationsGRPCClient) TestIamPermissions(ctx context.Context, req *iampb.TestIamPermissionsRequest, opts ...gax.CallOption) (*iampb.TestIamPermissionsResponse, error) {
   484  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "resource", url.QueryEscape(req.GetResource()))}
   485  
   486  	hds = append(c.xGoogHeaders, hds...)
   487  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   488  	opts = append((*c.CallOptions).TestIamPermissions[0:len((*c.CallOptions).TestIamPermissions):len((*c.CallOptions).TestIamPermissions)], opts...)
   489  	var resp *iampb.TestIamPermissionsResponse
   490  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   491  		var err error
   492  		resp, err = c.organizationsClient.TestIamPermissions(ctx, req, settings.GRPC...)
   493  		return err
   494  	}, opts...)
   495  	if err != nil {
   496  		return nil, err
   497  	}
   498  	return resp, nil
   499  }
   500  
   501  func (c *organizationsGRPCClient) GetOperation(ctx context.Context, req *longrunningpb.GetOperationRequest, opts ...gax.CallOption) (*longrunningpb.Operation, error) {
   502  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
   503  
   504  	hds = append(c.xGoogHeaders, hds...)
   505  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   506  	opts = append((*c.CallOptions).GetOperation[0:len((*c.CallOptions).GetOperation):len((*c.CallOptions).GetOperation)], opts...)
   507  	var resp *longrunningpb.Operation
   508  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   509  		var err error
   510  		resp, err = c.operationsClient.GetOperation(ctx, req, settings.GRPC...)
   511  		return err
   512  	}, opts...)
   513  	if err != nil {
   514  		return nil, err
   515  	}
   516  	return resp, nil
   517  }
   518  
   519  // GetOrganization fetches an organization resource identified by the specified resource name.
   520  func (c *organizationsRESTClient) GetOrganization(ctx context.Context, req *resourcemanagerpb.GetOrganizationRequest, opts ...gax.CallOption) (*resourcemanagerpb.Organization, error) {
   521  	baseUrl, err := url.Parse(c.endpoint)
   522  	if err != nil {
   523  		return nil, err
   524  	}
   525  	baseUrl.Path += fmt.Sprintf("/v3/%v", req.GetName())
   526  
   527  	params := url.Values{}
   528  	params.Add("$alt", "json;enum-encoding=int")
   529  
   530  	baseUrl.RawQuery = params.Encode()
   531  
   532  	// Build HTTP headers from client and context metadata.
   533  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
   534  
   535  	hds = append(c.xGoogHeaders, hds...)
   536  	hds = append(hds, "Content-Type", "application/json")
   537  	headers := gax.BuildHeaders(ctx, hds...)
   538  	opts = append((*c.CallOptions).GetOrganization[0:len((*c.CallOptions).GetOrganization):len((*c.CallOptions).GetOrganization)], opts...)
   539  	unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
   540  	resp := &resourcemanagerpb.Organization{}
   541  	e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   542  		if settings.Path != "" {
   543  			baseUrl.Path = settings.Path
   544  		}
   545  		httpReq, err := http.NewRequest("GET", baseUrl.String(), nil)
   546  		if err != nil {
   547  			return err
   548  		}
   549  		httpReq = httpReq.WithContext(ctx)
   550  		httpReq.Header = headers
   551  
   552  		httpRsp, err := c.httpClient.Do(httpReq)
   553  		if err != nil {
   554  			return err
   555  		}
   556  		defer httpRsp.Body.Close()
   557  
   558  		if err = googleapi.CheckResponse(httpRsp); err != nil {
   559  			return err
   560  		}
   561  
   562  		buf, err := io.ReadAll(httpRsp.Body)
   563  		if err != nil {
   564  			return err
   565  		}
   566  
   567  		if err := unm.Unmarshal(buf, resp); err != nil {
   568  			return err
   569  		}
   570  
   571  		return nil
   572  	}, opts...)
   573  	if e != nil {
   574  		return nil, e
   575  	}
   576  	return resp, nil
   577  }
   578  
   579  // SearchOrganizations searches organization resources that are visible to the user and satisfy
   580  // the specified filter. This method returns organizations in an unspecified
   581  // order. New organizations do not necessarily appear at the end of the
   582  // results, and may take a small amount of time to appear.
   583  //
   584  // Search will only return organizations on which the user has the permission
   585  // resourcemanager.organizations.get
   586  func (c *organizationsRESTClient) SearchOrganizations(ctx context.Context, req *resourcemanagerpb.SearchOrganizationsRequest, opts ...gax.CallOption) *OrganizationIterator {
   587  	it := &OrganizationIterator{}
   588  	req = proto.Clone(req).(*resourcemanagerpb.SearchOrganizationsRequest)
   589  	unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
   590  	it.InternalFetch = func(pageSize int, pageToken string) ([]*resourcemanagerpb.Organization, string, error) {
   591  		resp := &resourcemanagerpb.SearchOrganizationsResponse{}
   592  		if pageToken != "" {
   593  			req.PageToken = pageToken
   594  		}
   595  		if pageSize > math.MaxInt32 {
   596  			req.PageSize = math.MaxInt32
   597  		} else if pageSize != 0 {
   598  			req.PageSize = int32(pageSize)
   599  		}
   600  		baseUrl, err := url.Parse(c.endpoint)
   601  		if err != nil {
   602  			return nil, "", err
   603  		}
   604  		baseUrl.Path += fmt.Sprintf("/v3/organizations:search")
   605  
   606  		params := url.Values{}
   607  		params.Add("$alt", "json;enum-encoding=int")
   608  		if req.GetPageSize() != 0 {
   609  			params.Add("pageSize", fmt.Sprintf("%v", req.GetPageSize()))
   610  		}
   611  		if req.GetPageToken() != "" {
   612  			params.Add("pageToken", fmt.Sprintf("%v", req.GetPageToken()))
   613  		}
   614  		if req.GetQuery() != "" {
   615  			params.Add("query", fmt.Sprintf("%v", req.GetQuery()))
   616  		}
   617  
   618  		baseUrl.RawQuery = params.Encode()
   619  
   620  		// Build HTTP headers from client and context metadata.
   621  		hds := append(c.xGoogHeaders, "Content-Type", "application/json")
   622  		headers := gax.BuildHeaders(ctx, hds...)
   623  		e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   624  			if settings.Path != "" {
   625  				baseUrl.Path = settings.Path
   626  			}
   627  			httpReq, err := http.NewRequest("GET", baseUrl.String(), nil)
   628  			if err != nil {
   629  				return err
   630  			}
   631  			httpReq.Header = headers
   632  
   633  			httpRsp, err := c.httpClient.Do(httpReq)
   634  			if err != nil {
   635  				return err
   636  			}
   637  			defer httpRsp.Body.Close()
   638  
   639  			if err = googleapi.CheckResponse(httpRsp); err != nil {
   640  				return err
   641  			}
   642  
   643  			buf, err := io.ReadAll(httpRsp.Body)
   644  			if err != nil {
   645  				return err
   646  			}
   647  
   648  			if err := unm.Unmarshal(buf, resp); err != nil {
   649  				return err
   650  			}
   651  
   652  			return nil
   653  		}, opts...)
   654  		if e != nil {
   655  			return nil, "", e
   656  		}
   657  		it.Response = resp
   658  		return resp.GetOrganizations(), resp.GetNextPageToken(), nil
   659  	}
   660  
   661  	fetch := func(pageSize int, pageToken string) (string, error) {
   662  		items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
   663  		if err != nil {
   664  			return "", err
   665  		}
   666  		it.items = append(it.items, items...)
   667  		return nextPageToken, nil
   668  	}
   669  
   670  	it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
   671  	it.pageInfo.MaxSize = int(req.GetPageSize())
   672  	it.pageInfo.Token = req.GetPageToken()
   673  
   674  	return it
   675  }
   676  
   677  // GetIamPolicy gets the access control policy for an organization resource. The policy may
   678  // be empty if no such policy or resource exists. The resource field should
   679  // be the organization’s resource name, for example: “organizations/123”.
   680  //
   681  // Authorization requires the IAM permission
   682  // resourcemanager.organizations.getIamPolicy on the specified organization.
   683  func (c *organizationsRESTClient) GetIamPolicy(ctx context.Context, req *iampb.GetIamPolicyRequest, opts ...gax.CallOption) (*iampb.Policy, error) {
   684  	m := protojson.MarshalOptions{AllowPartial: true, UseEnumNumbers: true}
   685  	jsonReq, err := m.Marshal(req)
   686  	if err != nil {
   687  		return nil, err
   688  	}
   689  
   690  	baseUrl, err := url.Parse(c.endpoint)
   691  	if err != nil {
   692  		return nil, err
   693  	}
   694  	baseUrl.Path += fmt.Sprintf("/v3/%v:getIamPolicy", req.GetResource())
   695  
   696  	params := url.Values{}
   697  	params.Add("$alt", "json;enum-encoding=int")
   698  
   699  	baseUrl.RawQuery = params.Encode()
   700  
   701  	// Build HTTP headers from client and context metadata.
   702  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "resource", url.QueryEscape(req.GetResource()))}
   703  
   704  	hds = append(c.xGoogHeaders, hds...)
   705  	hds = append(hds, "Content-Type", "application/json")
   706  	headers := gax.BuildHeaders(ctx, hds...)
   707  	opts = append((*c.CallOptions).GetIamPolicy[0:len((*c.CallOptions).GetIamPolicy):len((*c.CallOptions).GetIamPolicy)], opts...)
   708  	unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
   709  	resp := &iampb.Policy{}
   710  	e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   711  		if settings.Path != "" {
   712  			baseUrl.Path = settings.Path
   713  		}
   714  		httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq))
   715  		if err != nil {
   716  			return err
   717  		}
   718  		httpReq = httpReq.WithContext(ctx)
   719  		httpReq.Header = headers
   720  
   721  		httpRsp, err := c.httpClient.Do(httpReq)
   722  		if err != nil {
   723  			return err
   724  		}
   725  		defer httpRsp.Body.Close()
   726  
   727  		if err = googleapi.CheckResponse(httpRsp); err != nil {
   728  			return err
   729  		}
   730  
   731  		buf, err := io.ReadAll(httpRsp.Body)
   732  		if err != nil {
   733  			return err
   734  		}
   735  
   736  		if err := unm.Unmarshal(buf, resp); err != nil {
   737  			return err
   738  		}
   739  
   740  		return nil
   741  	}, opts...)
   742  	if e != nil {
   743  		return nil, e
   744  	}
   745  	return resp, nil
   746  }
   747  
   748  // SetIamPolicy sets the access control policy on an organization resource. Replaces any
   749  // existing policy. The resource field should be the organization’s resource
   750  // name, for example: “organizations/123”.
   751  //
   752  // Authorization requires the IAM permission
   753  // resourcemanager.organizations.setIamPolicy on the specified organization.
   754  func (c *organizationsRESTClient) SetIamPolicy(ctx context.Context, req *iampb.SetIamPolicyRequest, opts ...gax.CallOption) (*iampb.Policy, error) {
   755  	m := protojson.MarshalOptions{AllowPartial: true, UseEnumNumbers: true}
   756  	jsonReq, err := m.Marshal(req)
   757  	if err != nil {
   758  		return nil, err
   759  	}
   760  
   761  	baseUrl, err := url.Parse(c.endpoint)
   762  	if err != nil {
   763  		return nil, err
   764  	}
   765  	baseUrl.Path += fmt.Sprintf("/v3/%v:setIamPolicy", req.GetResource())
   766  
   767  	params := url.Values{}
   768  	params.Add("$alt", "json;enum-encoding=int")
   769  
   770  	baseUrl.RawQuery = params.Encode()
   771  
   772  	// Build HTTP headers from client and context metadata.
   773  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "resource", url.QueryEscape(req.GetResource()))}
   774  
   775  	hds = append(c.xGoogHeaders, hds...)
   776  	hds = append(hds, "Content-Type", "application/json")
   777  	headers := gax.BuildHeaders(ctx, hds...)
   778  	opts = append((*c.CallOptions).SetIamPolicy[0:len((*c.CallOptions).SetIamPolicy):len((*c.CallOptions).SetIamPolicy)], opts...)
   779  	unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
   780  	resp := &iampb.Policy{}
   781  	e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   782  		if settings.Path != "" {
   783  			baseUrl.Path = settings.Path
   784  		}
   785  		httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq))
   786  		if err != nil {
   787  			return err
   788  		}
   789  		httpReq = httpReq.WithContext(ctx)
   790  		httpReq.Header = headers
   791  
   792  		httpRsp, err := c.httpClient.Do(httpReq)
   793  		if err != nil {
   794  			return err
   795  		}
   796  		defer httpRsp.Body.Close()
   797  
   798  		if err = googleapi.CheckResponse(httpRsp); err != nil {
   799  			return err
   800  		}
   801  
   802  		buf, err := io.ReadAll(httpRsp.Body)
   803  		if err != nil {
   804  			return err
   805  		}
   806  
   807  		if err := unm.Unmarshal(buf, resp); err != nil {
   808  			return err
   809  		}
   810  
   811  		return nil
   812  	}, opts...)
   813  	if e != nil {
   814  		return nil, e
   815  	}
   816  	return resp, nil
   817  }
   818  
   819  // TestIamPermissions returns the permissions that a caller has on the specified organization.
   820  // The resource field should be the organization’s resource name,
   821  // for example: “organizations/123”.
   822  //
   823  // There are no permissions required for making this API call.
   824  func (c *organizationsRESTClient) TestIamPermissions(ctx context.Context, req *iampb.TestIamPermissionsRequest, opts ...gax.CallOption) (*iampb.TestIamPermissionsResponse, error) {
   825  	m := protojson.MarshalOptions{AllowPartial: true, UseEnumNumbers: true}
   826  	jsonReq, err := m.Marshal(req)
   827  	if err != nil {
   828  		return nil, err
   829  	}
   830  
   831  	baseUrl, err := url.Parse(c.endpoint)
   832  	if err != nil {
   833  		return nil, err
   834  	}
   835  	baseUrl.Path += fmt.Sprintf("/v3/%v:testIamPermissions", req.GetResource())
   836  
   837  	params := url.Values{}
   838  	params.Add("$alt", "json;enum-encoding=int")
   839  
   840  	baseUrl.RawQuery = params.Encode()
   841  
   842  	// Build HTTP headers from client and context metadata.
   843  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "resource", url.QueryEscape(req.GetResource()))}
   844  
   845  	hds = append(c.xGoogHeaders, hds...)
   846  	hds = append(hds, "Content-Type", "application/json")
   847  	headers := gax.BuildHeaders(ctx, hds...)
   848  	opts = append((*c.CallOptions).TestIamPermissions[0:len((*c.CallOptions).TestIamPermissions):len((*c.CallOptions).TestIamPermissions)], opts...)
   849  	unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
   850  	resp := &iampb.TestIamPermissionsResponse{}
   851  	e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   852  		if settings.Path != "" {
   853  			baseUrl.Path = settings.Path
   854  		}
   855  		httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq))
   856  		if err != nil {
   857  			return err
   858  		}
   859  		httpReq = httpReq.WithContext(ctx)
   860  		httpReq.Header = headers
   861  
   862  		httpRsp, err := c.httpClient.Do(httpReq)
   863  		if err != nil {
   864  			return err
   865  		}
   866  		defer httpRsp.Body.Close()
   867  
   868  		if err = googleapi.CheckResponse(httpRsp); err != nil {
   869  			return err
   870  		}
   871  
   872  		buf, err := io.ReadAll(httpRsp.Body)
   873  		if err != nil {
   874  			return err
   875  		}
   876  
   877  		if err := unm.Unmarshal(buf, resp); err != nil {
   878  			return err
   879  		}
   880  
   881  		return nil
   882  	}, opts...)
   883  	if e != nil {
   884  		return nil, e
   885  	}
   886  	return resp, nil
   887  }
   888  
   889  // GetOperation is a utility method from google.longrunning.Operations.
   890  func (c *organizationsRESTClient) GetOperation(ctx context.Context, req *longrunningpb.GetOperationRequest, opts ...gax.CallOption) (*longrunningpb.Operation, error) {
   891  	baseUrl, err := url.Parse(c.endpoint)
   892  	if err != nil {
   893  		return nil, err
   894  	}
   895  	baseUrl.Path += fmt.Sprintf("/v3/%v", req.GetName())
   896  
   897  	params := url.Values{}
   898  	params.Add("$alt", "json;enum-encoding=int")
   899  
   900  	baseUrl.RawQuery = params.Encode()
   901  
   902  	// Build HTTP headers from client and context metadata.
   903  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
   904  
   905  	hds = append(c.xGoogHeaders, hds...)
   906  	hds = append(hds, "Content-Type", "application/json")
   907  	headers := gax.BuildHeaders(ctx, hds...)
   908  	opts = append((*c.CallOptions).GetOperation[0:len((*c.CallOptions).GetOperation):len((*c.CallOptions).GetOperation)], opts...)
   909  	unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
   910  	resp := &longrunningpb.Operation{}
   911  	e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   912  		if settings.Path != "" {
   913  			baseUrl.Path = settings.Path
   914  		}
   915  		httpReq, err := http.NewRequest("GET", baseUrl.String(), nil)
   916  		if err != nil {
   917  			return err
   918  		}
   919  		httpReq = httpReq.WithContext(ctx)
   920  		httpReq.Header = headers
   921  
   922  		httpRsp, err := c.httpClient.Do(httpReq)
   923  		if err != nil {
   924  			return err
   925  		}
   926  		defer httpRsp.Body.Close()
   927  
   928  		if err = googleapi.CheckResponse(httpRsp); err != nil {
   929  			return err
   930  		}
   931  
   932  		buf, err := io.ReadAll(httpRsp.Body)
   933  		if err != nil {
   934  			return err
   935  		}
   936  
   937  		if err := unm.Unmarshal(buf, resp); err != nil {
   938  			return err
   939  		}
   940  
   941  		return nil
   942  	}, opts...)
   943  	if e != nil {
   944  		return nil, e
   945  	}
   946  	return resp, nil
   947  }
   948  

View as plain text