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.
 
 
 

303 lines
10 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 dataproc
  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. dataprocpb "google.golang.org/genproto/googleapis/cloud/dataproc/v1"
  26. "google.golang.org/grpc"
  27. "google.golang.org/grpc/codes"
  28. "google.golang.org/grpc/metadata"
  29. )
  30. // JobControllerCallOptions contains the retry settings for each method of JobControllerClient.
  31. type JobControllerCallOptions struct {
  32. SubmitJob []gax.CallOption
  33. GetJob []gax.CallOption
  34. ListJobs []gax.CallOption
  35. UpdateJob []gax.CallOption
  36. CancelJob []gax.CallOption
  37. DeleteJob []gax.CallOption
  38. }
  39. func defaultJobControllerClientOptions() []option.ClientOption {
  40. return []option.ClientOption{
  41. option.WithEndpoint("dataproc.googleapis.com:443"),
  42. option.WithScopes(DefaultAuthScopes()...),
  43. }
  44. }
  45. func defaultJobControllerCallOptions() *JobControllerCallOptions {
  46. retry := map[[2]string][]gax.CallOption{
  47. {"default", "idempotent"}: {
  48. gax.WithRetry(func() gax.Retryer {
  49. return gax.OnCodes([]codes.Code{
  50. codes.DeadlineExceeded,
  51. codes.Internal,
  52. codes.Unavailable,
  53. }, gax.Backoff{
  54. Initial: 100 * time.Millisecond,
  55. Max: 60000 * time.Millisecond,
  56. Multiplier: 1.3,
  57. })
  58. }),
  59. },
  60. {"default", "non_idempotent"}: {
  61. gax.WithRetry(func() gax.Retryer {
  62. return gax.OnCodes([]codes.Code{
  63. codes.Unavailable,
  64. }, gax.Backoff{
  65. Initial: 100 * time.Millisecond,
  66. Max: 60000 * time.Millisecond,
  67. Multiplier: 1.3,
  68. })
  69. }),
  70. },
  71. }
  72. return &JobControllerCallOptions{
  73. SubmitJob: retry[[2]string{"default", "non_idempotent"}],
  74. GetJob: retry[[2]string{"default", "idempotent"}],
  75. ListJobs: retry[[2]string{"default", "idempotent"}],
  76. UpdateJob: retry[[2]string{"default", "non_idempotent"}],
  77. CancelJob: retry[[2]string{"default", "idempotent"}],
  78. DeleteJob: retry[[2]string{"default", "non_idempotent"}],
  79. }
  80. }
  81. // JobControllerClient is a client for interacting with Google Cloud Dataproc API.
  82. //
  83. // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
  84. type JobControllerClient struct {
  85. // The connection to the service.
  86. conn *grpc.ClientConn
  87. // The gRPC API client.
  88. jobControllerClient dataprocpb.JobControllerClient
  89. // The call options for this service.
  90. CallOptions *JobControllerCallOptions
  91. // The x-goog-* metadata to be sent with each request.
  92. xGoogMetadata metadata.MD
  93. }
  94. // NewJobControllerClient creates a new job controller client.
  95. //
  96. // The JobController provides methods to manage jobs.
  97. func NewJobControllerClient(ctx context.Context, opts ...option.ClientOption) (*JobControllerClient, error) {
  98. conn, err := transport.DialGRPC(ctx, append(defaultJobControllerClientOptions(), opts...)...)
  99. if err != nil {
  100. return nil, err
  101. }
  102. c := &JobControllerClient{
  103. conn: conn,
  104. CallOptions: defaultJobControllerCallOptions(),
  105. jobControllerClient: dataprocpb.NewJobControllerClient(conn),
  106. }
  107. c.setGoogleClientInfo()
  108. return c, nil
  109. }
  110. // Connection returns the client's connection to the API service.
  111. func (c *JobControllerClient) Connection() *grpc.ClientConn {
  112. return c.conn
  113. }
  114. // Close closes the connection to the API service. The user should invoke this when
  115. // the client is no longer required.
  116. func (c *JobControllerClient) Close() error {
  117. return c.conn.Close()
  118. }
  119. // setGoogleClientInfo sets the name and version of the application in
  120. // the `x-goog-api-client` header passed on each request. Intended for
  121. // use by Google-written clients.
  122. func (c *JobControllerClient) setGoogleClientInfo(keyval ...string) {
  123. kv := append([]string{"gl-go", versionGo()}, keyval...)
  124. kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version)
  125. c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
  126. }
  127. // SubmitJob submits a job to a cluster.
  128. func (c *JobControllerClient) SubmitJob(ctx context.Context, req *dataprocpb.SubmitJobRequest, opts ...gax.CallOption) (*dataprocpb.Job, error) {
  129. ctx = insertMetadata(ctx, c.xGoogMetadata)
  130. opts = append(c.CallOptions.SubmitJob[0:len(c.CallOptions.SubmitJob):len(c.CallOptions.SubmitJob)], opts...)
  131. var resp *dataprocpb.Job
  132. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  133. var err error
  134. resp, err = c.jobControllerClient.SubmitJob(ctx, req, settings.GRPC...)
  135. return err
  136. }, opts...)
  137. if err != nil {
  138. return nil, err
  139. }
  140. return resp, nil
  141. }
  142. // GetJob gets the resource representation for a job in a project.
  143. func (c *JobControllerClient) GetJob(ctx context.Context, req *dataprocpb.GetJobRequest, opts ...gax.CallOption) (*dataprocpb.Job, error) {
  144. ctx = insertMetadata(ctx, c.xGoogMetadata)
  145. opts = append(c.CallOptions.GetJob[0:len(c.CallOptions.GetJob):len(c.CallOptions.GetJob)], opts...)
  146. var resp *dataprocpb.Job
  147. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  148. var err error
  149. resp, err = c.jobControllerClient.GetJob(ctx, req, settings.GRPC...)
  150. return err
  151. }, opts...)
  152. if err != nil {
  153. return nil, err
  154. }
  155. return resp, nil
  156. }
  157. // ListJobs lists regions/{region}/jobs in a project.
  158. func (c *JobControllerClient) ListJobs(ctx context.Context, req *dataprocpb.ListJobsRequest, opts ...gax.CallOption) *JobIterator {
  159. ctx = insertMetadata(ctx, c.xGoogMetadata)
  160. opts = append(c.CallOptions.ListJobs[0:len(c.CallOptions.ListJobs):len(c.CallOptions.ListJobs)], opts...)
  161. it := &JobIterator{}
  162. req = proto.Clone(req).(*dataprocpb.ListJobsRequest)
  163. it.InternalFetch = func(pageSize int, pageToken string) ([]*dataprocpb.Job, string, error) {
  164. var resp *dataprocpb.ListJobsResponse
  165. req.PageToken = pageToken
  166. if pageSize > math.MaxInt32 {
  167. req.PageSize = math.MaxInt32
  168. } else {
  169. req.PageSize = int32(pageSize)
  170. }
  171. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  172. var err error
  173. resp, err = c.jobControllerClient.ListJobs(ctx, req, settings.GRPC...)
  174. return err
  175. }, opts...)
  176. if err != nil {
  177. return nil, "", err
  178. }
  179. return resp.Jobs, resp.NextPageToken, nil
  180. }
  181. fetch := func(pageSize int, pageToken string) (string, error) {
  182. items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
  183. if err != nil {
  184. return "", err
  185. }
  186. it.items = append(it.items, items...)
  187. return nextPageToken, nil
  188. }
  189. it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
  190. it.pageInfo.MaxSize = int(req.PageSize)
  191. return it
  192. }
  193. // UpdateJob updates a job in a project.
  194. func (c *JobControllerClient) UpdateJob(ctx context.Context, req *dataprocpb.UpdateJobRequest, opts ...gax.CallOption) (*dataprocpb.Job, error) {
  195. ctx = insertMetadata(ctx, c.xGoogMetadata)
  196. opts = append(c.CallOptions.UpdateJob[0:len(c.CallOptions.UpdateJob):len(c.CallOptions.UpdateJob)], opts...)
  197. var resp *dataprocpb.Job
  198. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  199. var err error
  200. resp, err = c.jobControllerClient.UpdateJob(ctx, req, settings.GRPC...)
  201. return err
  202. }, opts...)
  203. if err != nil {
  204. return nil, err
  205. }
  206. return resp, nil
  207. }
  208. // CancelJob starts a job cancellation request. To access the job resource
  209. // after cancellation, call
  210. // regions/{region}/jobs.list (at /dataproc/docs/reference/rest/v1/projects.regions.jobs/list)
  211. // or
  212. // regions/{region}/jobs.get (at /dataproc/docs/reference/rest/v1/projects.regions.jobs/get).
  213. func (c *JobControllerClient) CancelJob(ctx context.Context, req *dataprocpb.CancelJobRequest, opts ...gax.CallOption) (*dataprocpb.Job, error) {
  214. ctx = insertMetadata(ctx, c.xGoogMetadata)
  215. opts = append(c.CallOptions.CancelJob[0:len(c.CallOptions.CancelJob):len(c.CallOptions.CancelJob)], opts...)
  216. var resp *dataprocpb.Job
  217. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  218. var err error
  219. resp, err = c.jobControllerClient.CancelJob(ctx, req, settings.GRPC...)
  220. return err
  221. }, opts...)
  222. if err != nil {
  223. return nil, err
  224. }
  225. return resp, nil
  226. }
  227. // DeleteJob deletes the job from the project. If the job is active, the delete fails,
  228. // and the response returns FAILED_PRECONDITION.
  229. func (c *JobControllerClient) DeleteJob(ctx context.Context, req *dataprocpb.DeleteJobRequest, opts ...gax.CallOption) error {
  230. ctx = insertMetadata(ctx, c.xGoogMetadata)
  231. opts = append(c.CallOptions.DeleteJob[0:len(c.CallOptions.DeleteJob):len(c.CallOptions.DeleteJob)], opts...)
  232. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  233. var err error
  234. _, err = c.jobControllerClient.DeleteJob(ctx, req, settings.GRPC...)
  235. return err
  236. }, opts...)
  237. return err
  238. }
  239. // JobIterator manages a stream of *dataprocpb.Job.
  240. type JobIterator struct {
  241. items []*dataprocpb.Job
  242. pageInfo *iterator.PageInfo
  243. nextFunc func() error
  244. // InternalFetch is for use by the Google Cloud Libraries only.
  245. // It is not part of the stable interface of this package.
  246. //
  247. // InternalFetch returns results from a single call to the underlying RPC.
  248. // The number of results is no greater than pageSize.
  249. // If there are no more results, nextPageToken is empty and err is nil.
  250. InternalFetch func(pageSize int, pageToken string) (results []*dataprocpb.Job, nextPageToken string, err error)
  251. }
  252. // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
  253. func (it *JobIterator) PageInfo() *iterator.PageInfo {
  254. return it.pageInfo
  255. }
  256. // Next returns the next result. Its second return value is iterator.Done if there are no more
  257. // results. Once Next returns Done, all subsequent calls will return Done.
  258. func (it *JobIterator) Next() (*dataprocpb.Job, error) {
  259. var item *dataprocpb.Job
  260. if err := it.nextFunc(); err != nil {
  261. return item, err
  262. }
  263. item = it.items[0]
  264. it.items = it.items[1:]
  265. return item, nil
  266. }
  267. func (it *JobIterator) bufLen() int {
  268. return len(it.items)
  269. }
  270. func (it *JobIterator) takeBuf() interface{} {
  271. b := it.items
  272. it.items = nil
  273. return b
  274. }