You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

474 lines
19 KiB

  1. // Copyright 2018 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. // AUTO-GENERATED CODE. DO NOT EDIT.
  15. package admin
  16. import (
  17. "math"
  18. "time"
  19. "cloud.google.com/go/internal/version"
  20. gax "github.com/googleapis/gax-go"
  21. "golang.org/x/net/context"
  22. "google.golang.org/api/iterator"
  23. "google.golang.org/api/option"
  24. "google.golang.org/api/transport"
  25. adminpb "google.golang.org/genproto/googleapis/iam/admin/v1"
  26. iampb "google.golang.org/genproto/googleapis/iam/v1"
  27. "google.golang.org/grpc"
  28. "google.golang.org/grpc/codes"
  29. "google.golang.org/grpc/metadata"
  30. )
  31. // IamCallOptions contains the retry settings for each method of IamClient.
  32. type IamCallOptions struct {
  33. ListServiceAccounts []gax.CallOption
  34. GetServiceAccount []gax.CallOption
  35. CreateServiceAccount []gax.CallOption
  36. UpdateServiceAccount []gax.CallOption
  37. DeleteServiceAccount []gax.CallOption
  38. ListServiceAccountKeys []gax.CallOption
  39. GetServiceAccountKey []gax.CallOption
  40. CreateServiceAccountKey []gax.CallOption
  41. DeleteServiceAccountKey []gax.CallOption
  42. SignBlob []gax.CallOption
  43. GetIamPolicy []gax.CallOption
  44. SetIamPolicy []gax.CallOption
  45. TestIamPermissions []gax.CallOption
  46. QueryGrantableRoles []gax.CallOption
  47. SignJwt []gax.CallOption
  48. }
  49. func defaultIamClientOptions() []option.ClientOption {
  50. return []option.ClientOption{
  51. option.WithEndpoint("iam.googleapis.com:443"),
  52. option.WithScopes(DefaultAuthScopes()...),
  53. }
  54. }
  55. func defaultIamCallOptions() *IamCallOptions {
  56. retry := map[[2]string][]gax.CallOption{
  57. {"default", "idempotent"}: {
  58. gax.WithRetry(func() gax.Retryer {
  59. return gax.OnCodes([]codes.Code{
  60. codes.DeadlineExceeded,
  61. codes.Unavailable,
  62. }, gax.Backoff{
  63. Initial: 100 * time.Millisecond,
  64. Max: 60000 * time.Millisecond,
  65. Multiplier: 1.3,
  66. })
  67. }),
  68. },
  69. }
  70. return &IamCallOptions{
  71. ListServiceAccounts: retry[[2]string{"default", "idempotent"}],
  72. GetServiceAccount: retry[[2]string{"default", "idempotent"}],
  73. CreateServiceAccount: retry[[2]string{"default", "non_idempotent"}],
  74. UpdateServiceAccount: retry[[2]string{"default", "idempotent"}],
  75. DeleteServiceAccount: retry[[2]string{"default", "idempotent"}],
  76. ListServiceAccountKeys: retry[[2]string{"default", "idempotent"}],
  77. GetServiceAccountKey: retry[[2]string{"default", "idempotent"}],
  78. CreateServiceAccountKey: retry[[2]string{"default", "non_idempotent"}],
  79. DeleteServiceAccountKey: retry[[2]string{"default", "idempotent"}],
  80. SignBlob: retry[[2]string{"default", "non_idempotent"}],
  81. GetIamPolicy: retry[[2]string{"default", "non_idempotent"}],
  82. SetIamPolicy: retry[[2]string{"default", "non_idempotent"}],
  83. TestIamPermissions: retry[[2]string{"default", "non_idempotent"}],
  84. QueryGrantableRoles: retry[[2]string{"default", "non_idempotent"}],
  85. SignJwt: retry[[2]string{"default", "non_idempotent"}],
  86. }
  87. }
  88. // IamClient is a client for interacting with Google Identity and Access Management (IAM) API.
  89. //
  90. // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
  91. type IamClient struct {
  92. // The connection to the service.
  93. conn *grpc.ClientConn
  94. // The gRPC API client.
  95. iamClient adminpb.IAMClient
  96. // The call options for this service.
  97. CallOptions *IamCallOptions
  98. // The x-goog-* metadata to be sent with each request.
  99. xGoogMetadata metadata.MD
  100. }
  101. // NewIamClient creates a new iam client.
  102. //
  103. // Creates and manages service account objects.
  104. //
  105. // Service account is an account that belongs to your project instead
  106. // of to an individual end user. It is used to authenticate calls
  107. // to a Google API.
  108. //
  109. // To create a service account, specify the project_id and account_id
  110. // for the account. The account_id is unique within the project, and used
  111. // to generate the service account email address and a stable
  112. // unique_id.
  113. //
  114. // All other methods can identify accounts using the format
  115. // projects/{PROJECT_ID}/serviceAccounts/{SERVICE_ACCOUNT_EMAIL}.
  116. // Using - as a wildcard for the project will infer the project from
  117. // the account. The account value can be the email address or the
  118. // unique_id of the service account.
  119. func NewIamClient(ctx context.Context, opts ...option.ClientOption) (*IamClient, error) {
  120. conn, err := transport.DialGRPC(ctx, append(defaultIamClientOptions(), opts...)...)
  121. if err != nil {
  122. return nil, err
  123. }
  124. c := &IamClient{
  125. conn: conn,
  126. CallOptions: defaultIamCallOptions(),
  127. iamClient: adminpb.NewIAMClient(conn),
  128. }
  129. c.setGoogleClientInfo()
  130. return c, nil
  131. }
  132. // Connection returns the client's connection to the API service.
  133. func (c *IamClient) Connection() *grpc.ClientConn {
  134. return c.conn
  135. }
  136. // Close closes the connection to the API service. The user should invoke this when
  137. // the client is no longer required.
  138. func (c *IamClient) Close() error {
  139. return c.conn.Close()
  140. }
  141. // setGoogleClientInfo sets the name and version of the application in
  142. // the `x-goog-api-client` header passed on each request. Intended for
  143. // use by Google-written clients.
  144. func (c *IamClient) setGoogleClientInfo(keyval ...string) {
  145. kv := append([]string{"gl-go", version.Go()}, keyval...)
  146. kv = append(kv, "gapic", version.Repo, "gax", gax.Version, "grpc", grpc.Version)
  147. c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
  148. }
  149. // ListServiceAccounts lists [ServiceAccounts][google.iam.admin.v1.ServiceAccount] for a project.
  150. func (c *IamClient) ListServiceAccounts(ctx context.Context, req *adminpb.ListServiceAccountsRequest, opts ...gax.CallOption) *ServiceAccountIterator {
  151. ctx = insertMetadata(ctx, c.xGoogMetadata)
  152. opts = append(c.CallOptions.ListServiceAccounts[0:len(c.CallOptions.ListServiceAccounts):len(c.CallOptions.ListServiceAccounts)], opts...)
  153. it := &ServiceAccountIterator{}
  154. it.InternalFetch = func(pageSize int, pageToken string) ([]*adminpb.ServiceAccount, string, error) {
  155. var resp *adminpb.ListServiceAccountsResponse
  156. req.PageToken = pageToken
  157. if pageSize > math.MaxInt32 {
  158. req.PageSize = math.MaxInt32
  159. } else {
  160. req.PageSize = int32(pageSize)
  161. }
  162. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  163. var err error
  164. resp, err = c.iamClient.ListServiceAccounts(ctx, req, settings.GRPC...)
  165. return err
  166. }, opts...)
  167. if err != nil {
  168. return nil, "", err
  169. }
  170. return resp.Accounts, resp.NextPageToken, nil
  171. }
  172. fetch := func(pageSize int, pageToken string) (string, error) {
  173. items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
  174. if err != nil {
  175. return "", err
  176. }
  177. it.items = append(it.items, items...)
  178. return nextPageToken, nil
  179. }
  180. it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
  181. return it
  182. }
  183. // GetServiceAccount gets a [ServiceAccount][google.iam.admin.v1.ServiceAccount].
  184. func (c *IamClient) GetServiceAccount(ctx context.Context, req *adminpb.GetServiceAccountRequest, opts ...gax.CallOption) (*adminpb.ServiceAccount, error) {
  185. ctx = insertMetadata(ctx, c.xGoogMetadata)
  186. opts = append(c.CallOptions.GetServiceAccount[0:len(c.CallOptions.GetServiceAccount):len(c.CallOptions.GetServiceAccount)], opts...)
  187. var resp *adminpb.ServiceAccount
  188. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  189. var err error
  190. resp, err = c.iamClient.GetServiceAccount(ctx, req, settings.GRPC...)
  191. return err
  192. }, opts...)
  193. if err != nil {
  194. return nil, err
  195. }
  196. return resp, nil
  197. }
  198. // CreateServiceAccount creates a [ServiceAccount][google.iam.admin.v1.ServiceAccount]
  199. // and returns it.
  200. func (c *IamClient) CreateServiceAccount(ctx context.Context, req *adminpb.CreateServiceAccountRequest, opts ...gax.CallOption) (*adminpb.ServiceAccount, error) {
  201. ctx = insertMetadata(ctx, c.xGoogMetadata)
  202. opts = append(c.CallOptions.CreateServiceAccount[0:len(c.CallOptions.CreateServiceAccount):len(c.CallOptions.CreateServiceAccount)], opts...)
  203. var resp *adminpb.ServiceAccount
  204. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  205. var err error
  206. resp, err = c.iamClient.CreateServiceAccount(ctx, req, settings.GRPC...)
  207. return err
  208. }, opts...)
  209. if err != nil {
  210. return nil, err
  211. }
  212. return resp, nil
  213. }
  214. // UpdateServiceAccount updates a [ServiceAccount][google.iam.admin.v1.ServiceAccount].
  215. //
  216. // Currently, only the following fields are updatable:
  217. // display_name .
  218. // The etag is mandatory.
  219. func (c *IamClient) UpdateServiceAccount(ctx context.Context, req *adminpb.ServiceAccount, opts ...gax.CallOption) (*adminpb.ServiceAccount, error) {
  220. ctx = insertMetadata(ctx, c.xGoogMetadata)
  221. opts = append(c.CallOptions.UpdateServiceAccount[0:len(c.CallOptions.UpdateServiceAccount):len(c.CallOptions.UpdateServiceAccount)], opts...)
  222. var resp *adminpb.ServiceAccount
  223. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  224. var err error
  225. resp, err = c.iamClient.UpdateServiceAccount(ctx, req, settings.GRPC...)
  226. return err
  227. }, opts...)
  228. if err != nil {
  229. return nil, err
  230. }
  231. return resp, nil
  232. }
  233. // DeleteServiceAccount deletes a [ServiceAccount][google.iam.admin.v1.ServiceAccount].
  234. func (c *IamClient) DeleteServiceAccount(ctx context.Context, req *adminpb.DeleteServiceAccountRequest, opts ...gax.CallOption) error {
  235. ctx = insertMetadata(ctx, c.xGoogMetadata)
  236. opts = append(c.CallOptions.DeleteServiceAccount[0:len(c.CallOptions.DeleteServiceAccount):len(c.CallOptions.DeleteServiceAccount)], opts...)
  237. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  238. var err error
  239. _, err = c.iamClient.DeleteServiceAccount(ctx, req, settings.GRPC...)
  240. return err
  241. }, opts...)
  242. return err
  243. }
  244. // ListServiceAccountKeys lists [ServiceAccountKeys][google.iam.admin.v1.ServiceAccountKey].
  245. func (c *IamClient) ListServiceAccountKeys(ctx context.Context, req *adminpb.ListServiceAccountKeysRequest, opts ...gax.CallOption) (*adminpb.ListServiceAccountKeysResponse, error) {
  246. ctx = insertMetadata(ctx, c.xGoogMetadata)
  247. opts = append(c.CallOptions.ListServiceAccountKeys[0:len(c.CallOptions.ListServiceAccountKeys):len(c.CallOptions.ListServiceAccountKeys)], opts...)
  248. var resp *adminpb.ListServiceAccountKeysResponse
  249. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  250. var err error
  251. resp, err = c.iamClient.ListServiceAccountKeys(ctx, req, settings.GRPC...)
  252. return err
  253. }, opts...)
  254. if err != nil {
  255. return nil, err
  256. }
  257. return resp, nil
  258. }
  259. // GetServiceAccountKey gets the [ServiceAccountKey][google.iam.admin.v1.ServiceAccountKey]
  260. // by key id.
  261. func (c *IamClient) GetServiceAccountKey(ctx context.Context, req *adminpb.GetServiceAccountKeyRequest, opts ...gax.CallOption) (*adminpb.ServiceAccountKey, error) {
  262. ctx = insertMetadata(ctx, c.xGoogMetadata)
  263. opts = append(c.CallOptions.GetServiceAccountKey[0:len(c.CallOptions.GetServiceAccountKey):len(c.CallOptions.GetServiceAccountKey)], opts...)
  264. var resp *adminpb.ServiceAccountKey
  265. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  266. var err error
  267. resp, err = c.iamClient.GetServiceAccountKey(ctx, req, settings.GRPC...)
  268. return err
  269. }, opts...)
  270. if err != nil {
  271. return nil, err
  272. }
  273. return resp, nil
  274. }
  275. // CreateServiceAccountKey creates a [ServiceAccountKey][google.iam.admin.v1.ServiceAccountKey]
  276. // and returns it.
  277. func (c *IamClient) CreateServiceAccountKey(ctx context.Context, req *adminpb.CreateServiceAccountKeyRequest, opts ...gax.CallOption) (*adminpb.ServiceAccountKey, error) {
  278. ctx = insertMetadata(ctx, c.xGoogMetadata)
  279. opts = append(c.CallOptions.CreateServiceAccountKey[0:len(c.CallOptions.CreateServiceAccountKey):len(c.CallOptions.CreateServiceAccountKey)], opts...)
  280. var resp *adminpb.ServiceAccountKey
  281. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  282. var err error
  283. resp, err = c.iamClient.CreateServiceAccountKey(ctx, req, settings.GRPC...)
  284. return err
  285. }, opts...)
  286. if err != nil {
  287. return nil, err
  288. }
  289. return resp, nil
  290. }
  291. // DeleteServiceAccountKey deletes a [ServiceAccountKey][google.iam.admin.v1.ServiceAccountKey].
  292. func (c *IamClient) DeleteServiceAccountKey(ctx context.Context, req *adminpb.DeleteServiceAccountKeyRequest, opts ...gax.CallOption) error {
  293. ctx = insertMetadata(ctx, c.xGoogMetadata)
  294. opts = append(c.CallOptions.DeleteServiceAccountKey[0:len(c.CallOptions.DeleteServiceAccountKey):len(c.CallOptions.DeleteServiceAccountKey)], opts...)
  295. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  296. var err error
  297. _, err = c.iamClient.DeleteServiceAccountKey(ctx, req, settings.GRPC...)
  298. return err
  299. }, opts...)
  300. return err
  301. }
  302. // SignBlob signs a blob using a service account's system-managed private key.
  303. func (c *IamClient) SignBlob(ctx context.Context, req *adminpb.SignBlobRequest, opts ...gax.CallOption) (*adminpb.SignBlobResponse, error) {
  304. ctx = insertMetadata(ctx, c.xGoogMetadata)
  305. opts = append(c.CallOptions.SignBlob[0:len(c.CallOptions.SignBlob):len(c.CallOptions.SignBlob)], opts...)
  306. var resp *adminpb.SignBlobResponse
  307. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  308. var err error
  309. resp, err = c.iamClient.SignBlob(ctx, req, settings.GRPC...)
  310. return err
  311. }, opts...)
  312. if err != nil {
  313. return nil, err
  314. }
  315. return resp, nil
  316. }
  317. // getIamPolicy returns the IAM access control policy for a
  318. // [ServiceAccount][google.iam.admin.v1.ServiceAccount].
  319. func (c *IamClient) getIamPolicy(ctx context.Context, req *iampb.GetIamPolicyRequest, opts ...gax.CallOption) (*iampb.Policy, error) {
  320. ctx = insertMetadata(ctx, c.xGoogMetadata)
  321. opts = append(c.CallOptions.GetIamPolicy[0:len(c.CallOptions.GetIamPolicy):len(c.CallOptions.GetIamPolicy)], opts...)
  322. var resp *iampb.Policy
  323. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  324. var err error
  325. resp, err = c.iamClient.GetIamPolicy(ctx, req, settings.GRPC...)
  326. return err
  327. }, opts...)
  328. if err != nil {
  329. return nil, err
  330. }
  331. return resp, nil
  332. }
  333. // setIamPolicy sets the IAM access control policy for a
  334. // [ServiceAccount][google.iam.admin.v1.ServiceAccount].
  335. func (c *IamClient) setIamPolicy(ctx context.Context, req *iampb.SetIamPolicyRequest, opts ...gax.CallOption) (*iampb.Policy, error) {
  336. ctx = insertMetadata(ctx, c.xGoogMetadata)
  337. opts = append(c.CallOptions.SetIamPolicy[0:len(c.CallOptions.SetIamPolicy):len(c.CallOptions.SetIamPolicy)], opts...)
  338. var resp *iampb.Policy
  339. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  340. var err error
  341. resp, err = c.iamClient.SetIamPolicy(ctx, req, settings.GRPC...)
  342. return err
  343. }, opts...)
  344. if err != nil {
  345. return nil, err
  346. }
  347. return resp, nil
  348. }
  349. // TestIamPermissions tests the specified permissions against the IAM access control policy
  350. // for a [ServiceAccount][google.iam.admin.v1.ServiceAccount].
  351. func (c *IamClient) TestIamPermissions(ctx context.Context, req *iampb.TestIamPermissionsRequest, opts ...gax.CallOption) (*iampb.TestIamPermissionsResponse, error) {
  352. ctx = insertMetadata(ctx, c.xGoogMetadata)
  353. opts = append(c.CallOptions.TestIamPermissions[0:len(c.CallOptions.TestIamPermissions):len(c.CallOptions.TestIamPermissions)], opts...)
  354. var resp *iampb.TestIamPermissionsResponse
  355. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  356. var err error
  357. resp, err = c.iamClient.TestIamPermissions(ctx, req, settings.GRPC...)
  358. return err
  359. }, opts...)
  360. if err != nil {
  361. return nil, err
  362. }
  363. return resp, nil
  364. }
  365. // QueryGrantableRoles queries roles that can be granted on a particular resource.
  366. // A role is grantable if it can be used as the role in a binding for a policy
  367. // for that resource.
  368. func (c *IamClient) QueryGrantableRoles(ctx context.Context, req *adminpb.QueryGrantableRolesRequest, opts ...gax.CallOption) (*adminpb.QueryGrantableRolesResponse, error) {
  369. ctx = insertMetadata(ctx, c.xGoogMetadata)
  370. opts = append(c.CallOptions.QueryGrantableRoles[0:len(c.CallOptions.QueryGrantableRoles):len(c.CallOptions.QueryGrantableRoles)], opts...)
  371. var resp *adminpb.QueryGrantableRolesResponse
  372. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  373. var err error
  374. resp, err = c.iamClient.QueryGrantableRoles(ctx, req, settings.GRPC...)
  375. return err
  376. }, opts...)
  377. if err != nil {
  378. return nil, err
  379. }
  380. return resp, nil
  381. }
  382. // SignJwt signs a JWT using a service account's system-managed private key.
  383. //
  384. // If no expiry time (exp) is provided in the SignJwtRequest, IAM sets an
  385. // an expiry time of one hour by default. If you request an expiry time of
  386. // more than one hour, the request will fail.
  387. func (c *IamClient) SignJwt(ctx context.Context, req *adminpb.SignJwtRequest, opts ...gax.CallOption) (*adminpb.SignJwtResponse, error) {
  388. ctx = insertMetadata(ctx, c.xGoogMetadata)
  389. opts = append(c.CallOptions.SignJwt[0:len(c.CallOptions.SignJwt):len(c.CallOptions.SignJwt)], opts...)
  390. var resp *adminpb.SignJwtResponse
  391. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  392. var err error
  393. resp, err = c.iamClient.SignJwt(ctx, req, settings.GRPC...)
  394. return err
  395. }, opts...)
  396. if err != nil {
  397. return nil, err
  398. }
  399. return resp, nil
  400. }
  401. // ServiceAccountIterator manages a stream of *adminpb.ServiceAccount.
  402. type ServiceAccountIterator struct {
  403. items []*adminpb.ServiceAccount
  404. pageInfo *iterator.PageInfo
  405. nextFunc func() error
  406. // InternalFetch is for use by the Google Cloud Libraries only.
  407. // It is not part of the stable interface of this package.
  408. //
  409. // InternalFetch returns results from a single call to the underlying RPC.
  410. // The number of results is no greater than pageSize.
  411. // If there are no more results, nextPageToken is empty and err is nil.
  412. InternalFetch func(pageSize int, pageToken string) (results []*adminpb.ServiceAccount, nextPageToken string, err error)
  413. }
  414. // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
  415. func (it *ServiceAccountIterator) PageInfo() *iterator.PageInfo {
  416. return it.pageInfo
  417. }
  418. // Next returns the next result. Its second return value is iterator.Done if there are no more
  419. // results. Once Next returns Done, all subsequent calls will return Done.
  420. func (it *ServiceAccountIterator) Next() (*adminpb.ServiceAccount, error) {
  421. var item *adminpb.ServiceAccount
  422. if err := it.nextFunc(); err != nil {
  423. return item, err
  424. }
  425. item = it.items[0]
  426. it.items = it.items[1:]
  427. return item, nil
  428. }
  429. func (it *ServiceAccountIterator) bufLen() int {
  430. return len(it.items)
  431. }
  432. func (it *ServiceAccountIterator) takeBuf() interface{} {
  433. b := it.items
  434. it.items = nil
  435. return b
  436. }