選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 

476 行
19 KiB

  1. // Copyright 2019 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. // Code generated by gapic-generator. DO NOT EDIT.
  15. package admin
  16. import (
  17. "context"
  18. "math"
  19. "time"
  20. "github.com/golang/protobuf/proto"
  21. gax "github.com/googleapis/gax-go/v2"
  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", versionGo()}, keyval...)
  146. kv = append(kv, "gapic", versionClient, "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. req = proto.Clone(req).(*adminpb.ListServiceAccountsRequest)
  155. it.InternalFetch = func(pageSize int, pageToken string) ([]*adminpb.ServiceAccount, string, error) {
  156. var resp *adminpb.ListServiceAccountsResponse
  157. req.PageToken = pageToken
  158. if pageSize > math.MaxInt32 {
  159. req.PageSize = math.MaxInt32
  160. } else {
  161. req.PageSize = int32(pageSize)
  162. }
  163. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  164. var err error
  165. resp, err = c.iamClient.ListServiceAccounts(ctx, req, settings.GRPC...)
  166. return err
  167. }, opts...)
  168. if err != nil {
  169. return nil, "", err
  170. }
  171. return resp.Accounts, resp.NextPageToken, nil
  172. }
  173. fetch := func(pageSize int, pageToken string) (string, error) {
  174. items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
  175. if err != nil {
  176. return "", err
  177. }
  178. it.items = append(it.items, items...)
  179. return nextPageToken, nil
  180. }
  181. it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
  182. it.pageInfo.MaxSize = int(req.PageSize)
  183. return it
  184. }
  185. // GetServiceAccount gets a [ServiceAccount][google.iam.admin.v1.ServiceAccount].
  186. func (c *IamClient) GetServiceAccount(ctx context.Context, req *adminpb.GetServiceAccountRequest, opts ...gax.CallOption) (*adminpb.ServiceAccount, error) {
  187. ctx = insertMetadata(ctx, c.xGoogMetadata)
  188. opts = append(c.CallOptions.GetServiceAccount[0:len(c.CallOptions.GetServiceAccount):len(c.CallOptions.GetServiceAccount)], opts...)
  189. var resp *adminpb.ServiceAccount
  190. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  191. var err error
  192. resp, err = c.iamClient.GetServiceAccount(ctx, req, settings.GRPC...)
  193. return err
  194. }, opts...)
  195. if err != nil {
  196. return nil, err
  197. }
  198. return resp, nil
  199. }
  200. // CreateServiceAccount creates a [ServiceAccount][google.iam.admin.v1.ServiceAccount]
  201. // and returns it.
  202. func (c *IamClient) CreateServiceAccount(ctx context.Context, req *adminpb.CreateServiceAccountRequest, opts ...gax.CallOption) (*adminpb.ServiceAccount, error) {
  203. ctx = insertMetadata(ctx, c.xGoogMetadata)
  204. opts = append(c.CallOptions.CreateServiceAccount[0:len(c.CallOptions.CreateServiceAccount):len(c.CallOptions.CreateServiceAccount)], opts...)
  205. var resp *adminpb.ServiceAccount
  206. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  207. var err error
  208. resp, err = c.iamClient.CreateServiceAccount(ctx, req, settings.GRPC...)
  209. return err
  210. }, opts...)
  211. if err != nil {
  212. return nil, err
  213. }
  214. return resp, nil
  215. }
  216. // UpdateServiceAccount updates a [ServiceAccount][google.iam.admin.v1.ServiceAccount].
  217. //
  218. // Currently, only the following fields are updatable:
  219. // display_name .
  220. // The etag is mandatory.
  221. func (c *IamClient) UpdateServiceAccount(ctx context.Context, req *adminpb.ServiceAccount, opts ...gax.CallOption) (*adminpb.ServiceAccount, error) {
  222. ctx = insertMetadata(ctx, c.xGoogMetadata)
  223. opts = append(c.CallOptions.UpdateServiceAccount[0:len(c.CallOptions.UpdateServiceAccount):len(c.CallOptions.UpdateServiceAccount)], opts...)
  224. var resp *adminpb.ServiceAccount
  225. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  226. var err error
  227. resp, err = c.iamClient.UpdateServiceAccount(ctx, req, settings.GRPC...)
  228. return err
  229. }, opts...)
  230. if err != nil {
  231. return nil, err
  232. }
  233. return resp, nil
  234. }
  235. // DeleteServiceAccount deletes a [ServiceAccount][google.iam.admin.v1.ServiceAccount].
  236. func (c *IamClient) DeleteServiceAccount(ctx context.Context, req *adminpb.DeleteServiceAccountRequest, opts ...gax.CallOption) error {
  237. ctx = insertMetadata(ctx, c.xGoogMetadata)
  238. opts = append(c.CallOptions.DeleteServiceAccount[0:len(c.CallOptions.DeleteServiceAccount):len(c.CallOptions.DeleteServiceAccount)], opts...)
  239. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  240. var err error
  241. _, err = c.iamClient.DeleteServiceAccount(ctx, req, settings.GRPC...)
  242. return err
  243. }, opts...)
  244. return err
  245. }
  246. // ListServiceAccountKeys lists [ServiceAccountKeys][google.iam.admin.v1.ServiceAccountKey].
  247. func (c *IamClient) ListServiceAccountKeys(ctx context.Context, req *adminpb.ListServiceAccountKeysRequest, opts ...gax.CallOption) (*adminpb.ListServiceAccountKeysResponse, error) {
  248. ctx = insertMetadata(ctx, c.xGoogMetadata)
  249. opts = append(c.CallOptions.ListServiceAccountKeys[0:len(c.CallOptions.ListServiceAccountKeys):len(c.CallOptions.ListServiceAccountKeys)], opts...)
  250. var resp *adminpb.ListServiceAccountKeysResponse
  251. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  252. var err error
  253. resp, err = c.iamClient.ListServiceAccountKeys(ctx, req, settings.GRPC...)
  254. return err
  255. }, opts...)
  256. if err != nil {
  257. return nil, err
  258. }
  259. return resp, nil
  260. }
  261. // GetServiceAccountKey gets the [ServiceAccountKey][google.iam.admin.v1.ServiceAccountKey]
  262. // by key id.
  263. func (c *IamClient) GetServiceAccountKey(ctx context.Context, req *adminpb.GetServiceAccountKeyRequest, opts ...gax.CallOption) (*adminpb.ServiceAccountKey, error) {
  264. ctx = insertMetadata(ctx, c.xGoogMetadata)
  265. opts = append(c.CallOptions.GetServiceAccountKey[0:len(c.CallOptions.GetServiceAccountKey):len(c.CallOptions.GetServiceAccountKey)], opts...)
  266. var resp *adminpb.ServiceAccountKey
  267. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  268. var err error
  269. resp, err = c.iamClient.GetServiceAccountKey(ctx, req, settings.GRPC...)
  270. return err
  271. }, opts...)
  272. if err != nil {
  273. return nil, err
  274. }
  275. return resp, nil
  276. }
  277. // CreateServiceAccountKey creates a [ServiceAccountKey][google.iam.admin.v1.ServiceAccountKey]
  278. // and returns it.
  279. func (c *IamClient) CreateServiceAccountKey(ctx context.Context, req *adminpb.CreateServiceAccountKeyRequest, opts ...gax.CallOption) (*adminpb.ServiceAccountKey, error) {
  280. ctx = insertMetadata(ctx, c.xGoogMetadata)
  281. opts = append(c.CallOptions.CreateServiceAccountKey[0:len(c.CallOptions.CreateServiceAccountKey):len(c.CallOptions.CreateServiceAccountKey)], opts...)
  282. var resp *adminpb.ServiceAccountKey
  283. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  284. var err error
  285. resp, err = c.iamClient.CreateServiceAccountKey(ctx, req, settings.GRPC...)
  286. return err
  287. }, opts...)
  288. if err != nil {
  289. return nil, err
  290. }
  291. return resp, nil
  292. }
  293. // DeleteServiceAccountKey deletes a [ServiceAccountKey][google.iam.admin.v1.ServiceAccountKey].
  294. func (c *IamClient) DeleteServiceAccountKey(ctx context.Context, req *adminpb.DeleteServiceAccountKeyRequest, opts ...gax.CallOption) error {
  295. ctx = insertMetadata(ctx, c.xGoogMetadata)
  296. opts = append(c.CallOptions.DeleteServiceAccountKey[0:len(c.CallOptions.DeleteServiceAccountKey):len(c.CallOptions.DeleteServiceAccountKey)], opts...)
  297. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  298. var err error
  299. _, err = c.iamClient.DeleteServiceAccountKey(ctx, req, settings.GRPC...)
  300. return err
  301. }, opts...)
  302. return err
  303. }
  304. // SignBlob signs a blob using a service account's system-managed private key.
  305. func (c *IamClient) SignBlob(ctx context.Context, req *adminpb.SignBlobRequest, opts ...gax.CallOption) (*adminpb.SignBlobResponse, error) {
  306. ctx = insertMetadata(ctx, c.xGoogMetadata)
  307. opts = append(c.CallOptions.SignBlob[0:len(c.CallOptions.SignBlob):len(c.CallOptions.SignBlob)], opts...)
  308. var resp *adminpb.SignBlobResponse
  309. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  310. var err error
  311. resp, err = c.iamClient.SignBlob(ctx, req, settings.GRPC...)
  312. return err
  313. }, opts...)
  314. if err != nil {
  315. return nil, err
  316. }
  317. return resp, nil
  318. }
  319. // getIamPolicy returns the IAM access control policy for a
  320. // [ServiceAccount][google.iam.admin.v1.ServiceAccount].
  321. func (c *IamClient) getIamPolicy(ctx context.Context, req *iampb.GetIamPolicyRequest, opts ...gax.CallOption) (*iampb.Policy, error) {
  322. ctx = insertMetadata(ctx, c.xGoogMetadata)
  323. opts = append(c.CallOptions.GetIamPolicy[0:len(c.CallOptions.GetIamPolicy):len(c.CallOptions.GetIamPolicy)], opts...)
  324. var resp *iampb.Policy
  325. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  326. var err error
  327. resp, err = c.iamClient.GetIamPolicy(ctx, req, settings.GRPC...)
  328. return err
  329. }, opts...)
  330. if err != nil {
  331. return nil, err
  332. }
  333. return resp, nil
  334. }
  335. // setIamPolicy sets the IAM access control policy for a
  336. // [ServiceAccount][google.iam.admin.v1.ServiceAccount].
  337. func (c *IamClient) setIamPolicy(ctx context.Context, req *iampb.SetIamPolicyRequest, opts ...gax.CallOption) (*iampb.Policy, error) {
  338. ctx = insertMetadata(ctx, c.xGoogMetadata)
  339. opts = append(c.CallOptions.SetIamPolicy[0:len(c.CallOptions.SetIamPolicy):len(c.CallOptions.SetIamPolicy)], opts...)
  340. var resp *iampb.Policy
  341. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  342. var err error
  343. resp, err = c.iamClient.SetIamPolicy(ctx, req, settings.GRPC...)
  344. return err
  345. }, opts...)
  346. if err != nil {
  347. return nil, err
  348. }
  349. return resp, nil
  350. }
  351. // TestIamPermissions tests the specified permissions against the IAM access control policy
  352. // for a [ServiceAccount][google.iam.admin.v1.ServiceAccount].
  353. func (c *IamClient) TestIamPermissions(ctx context.Context, req *iampb.TestIamPermissionsRequest, opts ...gax.CallOption) (*iampb.TestIamPermissionsResponse, error) {
  354. ctx = insertMetadata(ctx, c.xGoogMetadata)
  355. opts = append(c.CallOptions.TestIamPermissions[0:len(c.CallOptions.TestIamPermissions):len(c.CallOptions.TestIamPermissions)], opts...)
  356. var resp *iampb.TestIamPermissionsResponse
  357. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  358. var err error
  359. resp, err = c.iamClient.TestIamPermissions(ctx, req, settings.GRPC...)
  360. return err
  361. }, opts...)
  362. if err != nil {
  363. return nil, err
  364. }
  365. return resp, nil
  366. }
  367. // QueryGrantableRoles queries roles that can be granted on a particular resource.
  368. // A role is grantable if it can be used as the role in a binding for a policy
  369. // for that resource.
  370. func (c *IamClient) QueryGrantableRoles(ctx context.Context, req *adminpb.QueryGrantableRolesRequest, opts ...gax.CallOption) (*adminpb.QueryGrantableRolesResponse, error) {
  371. ctx = insertMetadata(ctx, c.xGoogMetadata)
  372. opts = append(c.CallOptions.QueryGrantableRoles[0:len(c.CallOptions.QueryGrantableRoles):len(c.CallOptions.QueryGrantableRoles)], opts...)
  373. var resp *adminpb.QueryGrantableRolesResponse
  374. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  375. var err error
  376. resp, err = c.iamClient.QueryGrantableRoles(ctx, req, settings.GRPC...)
  377. return err
  378. }, opts...)
  379. if err != nil {
  380. return nil, err
  381. }
  382. return resp, nil
  383. }
  384. // SignJwt signs a JWT using a service account's system-managed private key.
  385. //
  386. // If no expiry time (exp) is provided in the SignJwtRequest, IAM sets an
  387. // an expiry time of one hour by default. If you request an expiry time of
  388. // more than one hour, the request will fail.
  389. func (c *IamClient) SignJwt(ctx context.Context, req *adminpb.SignJwtRequest, opts ...gax.CallOption) (*adminpb.SignJwtResponse, error) {
  390. ctx = insertMetadata(ctx, c.xGoogMetadata)
  391. opts = append(c.CallOptions.SignJwt[0:len(c.CallOptions.SignJwt):len(c.CallOptions.SignJwt)], opts...)
  392. var resp *adminpb.SignJwtResponse
  393. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  394. var err error
  395. resp, err = c.iamClient.SignJwt(ctx, req, settings.GRPC...)
  396. return err
  397. }, opts...)
  398. if err != nil {
  399. return nil, err
  400. }
  401. return resp, nil
  402. }
  403. // ServiceAccountIterator manages a stream of *adminpb.ServiceAccount.
  404. type ServiceAccountIterator struct {
  405. items []*adminpb.ServiceAccount
  406. pageInfo *iterator.PageInfo
  407. nextFunc func() error
  408. // InternalFetch is for use by the Google Cloud Libraries only.
  409. // It is not part of the stable interface of this package.
  410. //
  411. // InternalFetch returns results from a single call to the underlying RPC.
  412. // The number of results is no greater than pageSize.
  413. // If there are no more results, nextPageToken is empty and err is nil.
  414. InternalFetch func(pageSize int, pageToken string) (results []*adminpb.ServiceAccount, nextPageToken string, err error)
  415. }
  416. // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
  417. func (it *ServiceAccountIterator) PageInfo() *iterator.PageInfo {
  418. return it.pageInfo
  419. }
  420. // Next returns the next result. Its second return value is iterator.Done if there are no more
  421. // results. Once Next returns Done, all subsequent calls will return Done.
  422. func (it *ServiceAccountIterator) Next() (*adminpb.ServiceAccount, error) {
  423. var item *adminpb.ServiceAccount
  424. if err := it.nextFunc(); err != nil {
  425. return item, err
  426. }
  427. item = it.items[0]
  428. it.items = it.items[1:]
  429. return item, nil
  430. }
  431. func (it *ServiceAccountIterator) bufLen() int {
  432. return len(it.items)
  433. }
  434. func (it *ServiceAccountIterator) takeBuf() interface{} {
  435. b := it.items
  436. it.items = nil
  437. return b
  438. }