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.
 
 
 

268 lines
9.0 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 talent
  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. talentpb "google.golang.org/genproto/googleapis/cloud/talent/v4beta1"
  26. "google.golang.org/grpc"
  27. "google.golang.org/grpc/codes"
  28. "google.golang.org/grpc/metadata"
  29. )
  30. // TenantCallOptions contains the retry settings for each method of TenantClient.
  31. type TenantCallOptions struct {
  32. CreateTenant []gax.CallOption
  33. GetTenant []gax.CallOption
  34. UpdateTenant []gax.CallOption
  35. DeleteTenant []gax.CallOption
  36. ListTenants []gax.CallOption
  37. }
  38. func defaultTenantClientOptions() []option.ClientOption {
  39. return []option.ClientOption{
  40. option.WithEndpoint("jobs.googleapis.com:443"),
  41. option.WithScopes(DefaultAuthScopes()...),
  42. }
  43. }
  44. func defaultTenantCallOptions() *TenantCallOptions {
  45. retry := map[[2]string][]gax.CallOption{
  46. {"default", "idempotent"}: {
  47. gax.WithRetry(func() gax.Retryer {
  48. return gax.OnCodes([]codes.Code{
  49. codes.DeadlineExceeded,
  50. codes.Unavailable,
  51. }, gax.Backoff{
  52. Initial: 100 * time.Millisecond,
  53. Max: 60000 * time.Millisecond,
  54. Multiplier: 1.3,
  55. })
  56. }),
  57. },
  58. }
  59. return &TenantCallOptions{
  60. CreateTenant: retry[[2]string{"default", "non_idempotent"}],
  61. GetTenant: retry[[2]string{"default", "idempotent"}],
  62. UpdateTenant: retry[[2]string{"default", "non_idempotent"}],
  63. DeleteTenant: retry[[2]string{"default", "idempotent"}],
  64. ListTenants: retry[[2]string{"default", "idempotent"}],
  65. }
  66. }
  67. // TenantClient is a client for interacting with Cloud Talent Solution API.
  68. //
  69. // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
  70. type TenantClient struct {
  71. // The connection to the service.
  72. conn *grpc.ClientConn
  73. // The gRPC API client.
  74. tenantClient talentpb.TenantServiceClient
  75. // The call options for this service.
  76. CallOptions *TenantCallOptions
  77. // The x-goog-* metadata to be sent with each request.
  78. xGoogMetadata metadata.MD
  79. }
  80. // NewTenantClient creates a new tenant service client.
  81. //
  82. // A service that handles tenant management, including CRUD and enumeration.
  83. func NewTenantClient(ctx context.Context, opts ...option.ClientOption) (*TenantClient, error) {
  84. conn, err := transport.DialGRPC(ctx, append(defaultTenantClientOptions(), opts...)...)
  85. if err != nil {
  86. return nil, err
  87. }
  88. c := &TenantClient{
  89. conn: conn,
  90. CallOptions: defaultTenantCallOptions(),
  91. tenantClient: talentpb.NewTenantServiceClient(conn),
  92. }
  93. c.setGoogleClientInfo()
  94. return c, nil
  95. }
  96. // Connection returns the client's connection to the API service.
  97. func (c *TenantClient) Connection() *grpc.ClientConn {
  98. return c.conn
  99. }
  100. // Close closes the connection to the API service. The user should invoke this when
  101. // the client is no longer required.
  102. func (c *TenantClient) Close() error {
  103. return c.conn.Close()
  104. }
  105. // setGoogleClientInfo sets the name and version of the application in
  106. // the `x-goog-api-client` header passed on each request. Intended for
  107. // use by Google-written clients.
  108. func (c *TenantClient) setGoogleClientInfo(keyval ...string) {
  109. kv := append([]string{"gl-go", versionGo()}, keyval...)
  110. kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version)
  111. c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
  112. }
  113. // CreateTenant creates a new tenant entity.
  114. func (c *TenantClient) CreateTenant(ctx context.Context, req *talentpb.CreateTenantRequest, opts ...gax.CallOption) (*talentpb.Tenant, error) {
  115. ctx = insertMetadata(ctx, c.xGoogMetadata)
  116. opts = append(c.CallOptions.CreateTenant[0:len(c.CallOptions.CreateTenant):len(c.CallOptions.CreateTenant)], opts...)
  117. var resp *talentpb.Tenant
  118. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  119. var err error
  120. resp, err = c.tenantClient.CreateTenant(ctx, req, settings.GRPC...)
  121. return err
  122. }, opts...)
  123. if err != nil {
  124. return nil, err
  125. }
  126. return resp, nil
  127. }
  128. // GetTenant retrieves specified tenant.
  129. func (c *TenantClient) GetTenant(ctx context.Context, req *talentpb.GetTenantRequest, opts ...gax.CallOption) (*talentpb.Tenant, error) {
  130. ctx = insertMetadata(ctx, c.xGoogMetadata)
  131. opts = append(c.CallOptions.GetTenant[0:len(c.CallOptions.GetTenant):len(c.CallOptions.GetTenant)], opts...)
  132. var resp *talentpb.Tenant
  133. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  134. var err error
  135. resp, err = c.tenantClient.GetTenant(ctx, req, settings.GRPC...)
  136. return err
  137. }, opts...)
  138. if err != nil {
  139. return nil, err
  140. }
  141. return resp, nil
  142. }
  143. // UpdateTenant updates specified tenant.
  144. func (c *TenantClient) UpdateTenant(ctx context.Context, req *talentpb.UpdateTenantRequest, opts ...gax.CallOption) (*talentpb.Tenant, error) {
  145. ctx = insertMetadata(ctx, c.xGoogMetadata)
  146. opts = append(c.CallOptions.UpdateTenant[0:len(c.CallOptions.UpdateTenant):len(c.CallOptions.UpdateTenant)], opts...)
  147. var resp *talentpb.Tenant
  148. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  149. var err error
  150. resp, err = c.tenantClient.UpdateTenant(ctx, req, settings.GRPC...)
  151. return err
  152. }, opts...)
  153. if err != nil {
  154. return nil, err
  155. }
  156. return resp, nil
  157. }
  158. // DeleteTenant deletes specified tenant.
  159. func (c *TenantClient) DeleteTenant(ctx context.Context, req *talentpb.DeleteTenantRequest, opts ...gax.CallOption) error {
  160. ctx = insertMetadata(ctx, c.xGoogMetadata)
  161. opts = append(c.CallOptions.DeleteTenant[0:len(c.CallOptions.DeleteTenant):len(c.CallOptions.DeleteTenant)], opts...)
  162. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  163. var err error
  164. _, err = c.tenantClient.DeleteTenant(ctx, req, settings.GRPC...)
  165. return err
  166. }, opts...)
  167. return err
  168. }
  169. // ListTenants lists all tenants associated with the project.
  170. func (c *TenantClient) ListTenants(ctx context.Context, req *talentpb.ListTenantsRequest, opts ...gax.CallOption) *TenantIterator {
  171. ctx = insertMetadata(ctx, c.xGoogMetadata)
  172. opts = append(c.CallOptions.ListTenants[0:len(c.CallOptions.ListTenants):len(c.CallOptions.ListTenants)], opts...)
  173. it := &TenantIterator{}
  174. req = proto.Clone(req).(*talentpb.ListTenantsRequest)
  175. it.InternalFetch = func(pageSize int, pageToken string) ([]*talentpb.Tenant, string, error) {
  176. var resp *talentpb.ListTenantsResponse
  177. req.PageToken = pageToken
  178. if pageSize > math.MaxInt32 {
  179. req.PageSize = math.MaxInt32
  180. } else {
  181. req.PageSize = int32(pageSize)
  182. }
  183. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  184. var err error
  185. resp, err = c.tenantClient.ListTenants(ctx, req, settings.GRPC...)
  186. return err
  187. }, opts...)
  188. if err != nil {
  189. return nil, "", err
  190. }
  191. return resp.Tenants, resp.NextPageToken, nil
  192. }
  193. fetch := func(pageSize int, pageToken string) (string, error) {
  194. items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
  195. if err != nil {
  196. return "", err
  197. }
  198. it.items = append(it.items, items...)
  199. return nextPageToken, nil
  200. }
  201. it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
  202. it.pageInfo.MaxSize = int(req.PageSize)
  203. return it
  204. }
  205. // TenantIterator manages a stream of *talentpb.Tenant.
  206. type TenantIterator struct {
  207. items []*talentpb.Tenant
  208. pageInfo *iterator.PageInfo
  209. nextFunc func() error
  210. // InternalFetch is for use by the Google Cloud Libraries only.
  211. // It is not part of the stable interface of this package.
  212. //
  213. // InternalFetch returns results from a single call to the underlying RPC.
  214. // The number of results is no greater than pageSize.
  215. // If there are no more results, nextPageToken is empty and err is nil.
  216. InternalFetch func(pageSize int, pageToken string) (results []*talentpb.Tenant, nextPageToken string, err error)
  217. }
  218. // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
  219. func (it *TenantIterator) PageInfo() *iterator.PageInfo {
  220. return it.pageInfo
  221. }
  222. // Next returns the next result. Its second return value is iterator.Done if there are no more
  223. // results. Once Next returns Done, all subsequent calls will return Done.
  224. func (it *TenantIterator) Next() (*talentpb.Tenant, error) {
  225. var item *talentpb.Tenant
  226. if err := it.nextFunc(); err != nil {
  227. return item, err
  228. }
  229. item = it.items[0]
  230. it.items = it.items[1:]
  231. return item, nil
  232. }
  233. func (it *TenantIterator) bufLen() int {
  234. return len(it.items)
  235. }
  236. func (it *TenantIterator) takeBuf() interface{} {
  237. b := it.items
  238. it.items = nil
  239. return b
  240. }