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.
 
 
 

521 lines
20 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 redis
  16. import (
  17. "context"
  18. "math"
  19. "time"
  20. "cloud.google.com/go/longrunning"
  21. lroauto "cloud.google.com/go/longrunning/autogen"
  22. "github.com/golang/protobuf/proto"
  23. anypb "github.com/golang/protobuf/ptypes/any"
  24. gax "github.com/googleapis/gax-go/v2"
  25. "google.golang.org/api/iterator"
  26. "google.golang.org/api/option"
  27. "google.golang.org/api/transport"
  28. redispb "google.golang.org/genproto/googleapis/cloud/redis/v1beta1"
  29. longrunningpb "google.golang.org/genproto/googleapis/longrunning"
  30. "google.golang.org/grpc"
  31. "google.golang.org/grpc/metadata"
  32. )
  33. // CloudRedisCallOptions contains the retry settings for each method of CloudRedisClient.
  34. type CloudRedisCallOptions struct {
  35. ListInstances []gax.CallOption
  36. GetInstance []gax.CallOption
  37. CreateInstance []gax.CallOption
  38. UpdateInstance []gax.CallOption
  39. DeleteInstance []gax.CallOption
  40. }
  41. func defaultCloudRedisClientOptions() []option.ClientOption {
  42. return []option.ClientOption{
  43. option.WithEndpoint("redis.googleapis.com:443"),
  44. option.WithScopes(DefaultAuthScopes()...),
  45. }
  46. }
  47. func defaultCloudRedisCallOptions() *CloudRedisCallOptions {
  48. retry := map[[2]string][]gax.CallOption{}
  49. return &CloudRedisCallOptions{
  50. ListInstances: retry[[2]string{"default", "non_idempotent"}],
  51. GetInstance: retry[[2]string{"default", "non_idempotent"}],
  52. CreateInstance: retry[[2]string{"default", "non_idempotent"}],
  53. UpdateInstance: retry[[2]string{"default", "non_idempotent"}],
  54. DeleteInstance: retry[[2]string{"default", "non_idempotent"}],
  55. }
  56. }
  57. // CloudRedisClient is a client for interacting with Google Cloud Memorystore for Redis API.
  58. //
  59. // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
  60. type CloudRedisClient struct {
  61. // The connection to the service.
  62. conn *grpc.ClientConn
  63. // The gRPC API client.
  64. cloudRedisClient redispb.CloudRedisClient
  65. // LROClient is used internally to handle longrunning operations.
  66. // It is exposed so that its CallOptions can be modified if required.
  67. // Users should not Close this client.
  68. LROClient *lroauto.OperationsClient
  69. // The call options for this service.
  70. CallOptions *CloudRedisCallOptions
  71. // The x-goog-* metadata to be sent with each request.
  72. xGoogMetadata metadata.MD
  73. }
  74. // NewCloudRedisClient creates a new cloud redis client.
  75. //
  76. // Configures and manages Cloud Memorystore for Redis instances
  77. //
  78. // Google Cloud Memorystore for Redis v1beta1
  79. //
  80. // The redis.googleapis.com service implements the Google Cloud Memorystore
  81. // for Redis API and defines the following resource model for managing Redis
  82. // instances:
  83. //
  84. // The service works with a collection of cloud projects, named: /projects/*
  85. //
  86. // Each project has a collection of available locations, named: /locations/*
  87. //
  88. // Each location has a collection of Redis instances, named: /instances/*
  89. //
  90. // As such, Redis instances are resources of the form:
  91. // /projects/{project_id}/locations/{location_id}/instances/{instance_id}
  92. //
  93. // Note that location_id must be refering to a GCP region; for example:
  94. //
  95. // projects/redpepper-1290/locations/us-central1/instances/my-redis
  96. func NewCloudRedisClient(ctx context.Context, opts ...option.ClientOption) (*CloudRedisClient, error) {
  97. conn, err := transport.DialGRPC(ctx, append(defaultCloudRedisClientOptions(), opts...)...)
  98. if err != nil {
  99. return nil, err
  100. }
  101. c := &CloudRedisClient{
  102. conn: conn,
  103. CallOptions: defaultCloudRedisCallOptions(),
  104. cloudRedisClient: redispb.NewCloudRedisClient(conn),
  105. }
  106. c.setGoogleClientInfo()
  107. c.LROClient, err = lroauto.NewOperationsClient(ctx, option.WithGRPCConn(conn))
  108. if err != nil {
  109. // This error "should not happen", since we are just reusing old connection
  110. // and never actually need to dial.
  111. // If this does happen, we could leak conn. However, we cannot close conn:
  112. // If the user invoked the function with option.WithGRPCConn,
  113. // we would close a connection that's still in use.
  114. // TODO(pongad): investigate error conditions.
  115. return nil, err
  116. }
  117. return c, nil
  118. }
  119. // Connection returns the client's connection to the API service.
  120. func (c *CloudRedisClient) Connection() *grpc.ClientConn {
  121. return c.conn
  122. }
  123. // Close closes the connection to the API service. The user should invoke this when
  124. // the client is no longer required.
  125. func (c *CloudRedisClient) Close() error {
  126. return c.conn.Close()
  127. }
  128. // setGoogleClientInfo sets the name and version of the application in
  129. // the `x-goog-api-client` header passed on each request. Intended for
  130. // use by Google-written clients.
  131. func (c *CloudRedisClient) setGoogleClientInfo(keyval ...string) {
  132. kv := append([]string{"gl-go", versionGo()}, keyval...)
  133. kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version)
  134. c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
  135. }
  136. // ListInstances lists all Redis instances owned by a project in either the specified
  137. // location (region) or all locations.
  138. //
  139. // The location should have the following format:
  140. //
  141. // projects/{project_id}/locations/{location_id}
  142. //
  143. // If location_id is specified as - (wildcard), then all regions
  144. // available to the project are queried, and the results are aggregated.
  145. func (c *CloudRedisClient) ListInstances(ctx context.Context, req *redispb.ListInstancesRequest, opts ...gax.CallOption) *InstanceIterator {
  146. ctx = insertMetadata(ctx, c.xGoogMetadata)
  147. opts = append(c.CallOptions.ListInstances[0:len(c.CallOptions.ListInstances):len(c.CallOptions.ListInstances)], opts...)
  148. it := &InstanceIterator{}
  149. req = proto.Clone(req).(*redispb.ListInstancesRequest)
  150. it.InternalFetch = func(pageSize int, pageToken string) ([]*redispb.Instance, string, error) {
  151. var resp *redispb.ListInstancesResponse
  152. req.PageToken = pageToken
  153. if pageSize > math.MaxInt32 {
  154. req.PageSize = math.MaxInt32
  155. } else {
  156. req.PageSize = int32(pageSize)
  157. }
  158. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  159. var err error
  160. resp, err = c.cloudRedisClient.ListInstances(ctx, req, settings.GRPC...)
  161. return err
  162. }, opts...)
  163. if err != nil {
  164. return nil, "", err
  165. }
  166. return resp.Instances, resp.NextPageToken, nil
  167. }
  168. fetch := func(pageSize int, pageToken string) (string, error) {
  169. items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
  170. if err != nil {
  171. return "", err
  172. }
  173. it.items = append(it.items, items...)
  174. return nextPageToken, nil
  175. }
  176. it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
  177. it.pageInfo.MaxSize = int(req.PageSize)
  178. return it
  179. }
  180. // GetInstance gets the details of a specific Redis instance.
  181. func (c *CloudRedisClient) GetInstance(ctx context.Context, req *redispb.GetInstanceRequest, opts ...gax.CallOption) (*redispb.Instance, error) {
  182. ctx = insertMetadata(ctx, c.xGoogMetadata)
  183. opts = append(c.CallOptions.GetInstance[0:len(c.CallOptions.GetInstance):len(c.CallOptions.GetInstance)], opts...)
  184. var resp *redispb.Instance
  185. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  186. var err error
  187. resp, err = c.cloudRedisClient.GetInstance(ctx, req, settings.GRPC...)
  188. return err
  189. }, opts...)
  190. if err != nil {
  191. return nil, err
  192. }
  193. return resp, nil
  194. }
  195. // CreateInstance creates a Redis instance based on the specified tier and memory size.
  196. //
  197. // By default, the instance is peered to the project's
  198. // default network (at /compute/docs/networks-and-firewalls#networks).
  199. //
  200. // The creation is executed asynchronously and callers may check the returned
  201. // operation to track its progress. Once the operation is completed the Redis
  202. // instance will be fully functional. Completed longrunning.Operation will
  203. // contain the new instance object in the response field.
  204. //
  205. // The returned operation is automatically deleted after a few hours, so there
  206. // is no need to call DeleteOperation.
  207. func (c *CloudRedisClient) CreateInstance(ctx context.Context, req *redispb.CreateInstanceRequest, opts ...gax.CallOption) (*CreateInstanceOperation, error) {
  208. ctx = insertMetadata(ctx, c.xGoogMetadata)
  209. opts = append(c.CallOptions.CreateInstance[0:len(c.CallOptions.CreateInstance):len(c.CallOptions.CreateInstance)], opts...)
  210. var resp *longrunningpb.Operation
  211. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  212. var err error
  213. resp, err = c.cloudRedisClient.CreateInstance(ctx, req, settings.GRPC...)
  214. return err
  215. }, opts...)
  216. if err != nil {
  217. return nil, err
  218. }
  219. return &CreateInstanceOperation{
  220. lro: longrunning.InternalNewOperation(c.LROClient, resp),
  221. }, nil
  222. }
  223. // UpdateInstance updates the metadata and configuration of a specific Redis instance.
  224. //
  225. // Completed longrunning.Operation will contain the new instance object
  226. // in the response field. The returned operation is automatically deleted
  227. // after a few hours, so there is no need to call DeleteOperation.
  228. func (c *CloudRedisClient) UpdateInstance(ctx context.Context, req *redispb.UpdateInstanceRequest, opts ...gax.CallOption) (*UpdateInstanceOperation, error) {
  229. ctx = insertMetadata(ctx, c.xGoogMetadata)
  230. opts = append(c.CallOptions.UpdateInstance[0:len(c.CallOptions.UpdateInstance):len(c.CallOptions.UpdateInstance)], opts...)
  231. var resp *longrunningpb.Operation
  232. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  233. var err error
  234. resp, err = c.cloudRedisClient.UpdateInstance(ctx, req, settings.GRPC...)
  235. return err
  236. }, opts...)
  237. if err != nil {
  238. return nil, err
  239. }
  240. return &UpdateInstanceOperation{
  241. lro: longrunning.InternalNewOperation(c.LROClient, resp),
  242. }, nil
  243. }
  244. // DeleteInstance deletes a specific Redis instance. Instance stops serving and data is
  245. // deleted.
  246. func (c *CloudRedisClient) DeleteInstance(ctx context.Context, req *redispb.DeleteInstanceRequest, opts ...gax.CallOption) (*DeleteInstanceOperation, error) {
  247. ctx = insertMetadata(ctx, c.xGoogMetadata)
  248. opts = append(c.CallOptions.DeleteInstance[0:len(c.CallOptions.DeleteInstance):len(c.CallOptions.DeleteInstance)], opts...)
  249. var resp *longrunningpb.Operation
  250. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  251. var err error
  252. resp, err = c.cloudRedisClient.DeleteInstance(ctx, req, settings.GRPC...)
  253. return err
  254. }, opts...)
  255. if err != nil {
  256. return nil, err
  257. }
  258. return &DeleteInstanceOperation{
  259. lro: longrunning.InternalNewOperation(c.LROClient, resp),
  260. }, nil
  261. }
  262. // InstanceIterator manages a stream of *redispb.Instance.
  263. type InstanceIterator struct {
  264. items []*redispb.Instance
  265. pageInfo *iterator.PageInfo
  266. nextFunc func() error
  267. // InternalFetch is for use by the Google Cloud Libraries only.
  268. // It is not part of the stable interface of this package.
  269. //
  270. // InternalFetch returns results from a single call to the underlying RPC.
  271. // The number of results is no greater than pageSize.
  272. // If there are no more results, nextPageToken is empty and err is nil.
  273. InternalFetch func(pageSize int, pageToken string) (results []*redispb.Instance, nextPageToken string, err error)
  274. }
  275. // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
  276. func (it *InstanceIterator) PageInfo() *iterator.PageInfo {
  277. return it.pageInfo
  278. }
  279. // Next returns the next result. Its second return value is iterator.Done if there are no more
  280. // results. Once Next returns Done, all subsequent calls will return Done.
  281. func (it *InstanceIterator) Next() (*redispb.Instance, error) {
  282. var item *redispb.Instance
  283. if err := it.nextFunc(); err != nil {
  284. return item, err
  285. }
  286. item = it.items[0]
  287. it.items = it.items[1:]
  288. return item, nil
  289. }
  290. func (it *InstanceIterator) bufLen() int {
  291. return len(it.items)
  292. }
  293. func (it *InstanceIterator) takeBuf() interface{} {
  294. b := it.items
  295. it.items = nil
  296. return b
  297. }
  298. // CreateInstanceOperation manages a long-running operation from CreateInstance.
  299. type CreateInstanceOperation struct {
  300. lro *longrunning.Operation
  301. }
  302. // CreateInstanceOperation returns a new CreateInstanceOperation from a given name.
  303. // The name must be that of a previously created CreateInstanceOperation, possibly from a different process.
  304. func (c *CloudRedisClient) CreateInstanceOperation(name string) *CreateInstanceOperation {
  305. return &CreateInstanceOperation{
  306. lro: longrunning.InternalNewOperation(c.LROClient, &longrunningpb.Operation{Name: name}),
  307. }
  308. }
  309. // Wait blocks until the long-running operation is completed, returning the response and any errors encountered.
  310. //
  311. // See documentation of Poll for error-handling information.
  312. func (op *CreateInstanceOperation) Wait(ctx context.Context, opts ...gax.CallOption) (*redispb.Instance, error) {
  313. var resp redispb.Instance
  314. if err := op.lro.WaitWithInterval(ctx, &resp, 360000*time.Millisecond, opts...); err != nil {
  315. return nil, err
  316. }
  317. return &resp, nil
  318. }
  319. // Poll fetches the latest state of the long-running operation.
  320. //
  321. // Poll also fetches the latest metadata, which can be retrieved by Metadata.
  322. //
  323. // If Poll fails, the error is returned and op is unmodified. If Poll succeeds and
  324. // the operation has completed with failure, the error is returned and op.Done will return true.
  325. // If Poll succeeds and the operation has completed successfully,
  326. // op.Done will return true, and the response of the operation is returned.
  327. // If Poll succeeds and the operation has not completed, the returned response and error are both nil.
  328. func (op *CreateInstanceOperation) Poll(ctx context.Context, opts ...gax.CallOption) (*redispb.Instance, error) {
  329. var resp redispb.Instance
  330. if err := op.lro.Poll(ctx, &resp, opts...); err != nil {
  331. return nil, err
  332. }
  333. if !op.Done() {
  334. return nil, nil
  335. }
  336. return &resp, nil
  337. }
  338. // Metadata returns metadata associated with the long-running operation.
  339. // Metadata itself does not contact the server, but Poll does.
  340. // To get the latest metadata, call this method after a successful call to Poll.
  341. // If the metadata is not available, the returned metadata and error are both nil.
  342. func (op *CreateInstanceOperation) Metadata() (*anypb.Any, error) {
  343. var meta anypb.Any
  344. if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata {
  345. return nil, nil
  346. } else if err != nil {
  347. return nil, err
  348. }
  349. return &meta, nil
  350. }
  351. // Done reports whether the long-running operation has completed.
  352. func (op *CreateInstanceOperation) Done() bool {
  353. return op.lro.Done()
  354. }
  355. // Name returns the name of the long-running operation.
  356. // The name is assigned by the server and is unique within the service from which the operation is created.
  357. func (op *CreateInstanceOperation) Name() string {
  358. return op.lro.Name()
  359. }
  360. // DeleteInstanceOperation manages a long-running operation from DeleteInstance.
  361. type DeleteInstanceOperation struct {
  362. lro *longrunning.Operation
  363. }
  364. // DeleteInstanceOperation returns a new DeleteInstanceOperation from a given name.
  365. // The name must be that of a previously created DeleteInstanceOperation, possibly from a different process.
  366. func (c *CloudRedisClient) DeleteInstanceOperation(name string) *DeleteInstanceOperation {
  367. return &DeleteInstanceOperation{
  368. lro: longrunning.InternalNewOperation(c.LROClient, &longrunningpb.Operation{Name: name}),
  369. }
  370. }
  371. // Wait blocks until the long-running operation is completed, returning any error encountered.
  372. //
  373. // See documentation of Poll for error-handling information.
  374. func (op *DeleteInstanceOperation) Wait(ctx context.Context, opts ...gax.CallOption) error {
  375. return op.lro.WaitWithInterval(ctx, nil, 360000*time.Millisecond, opts...)
  376. }
  377. // Poll fetches the latest state of the long-running operation.
  378. //
  379. // Poll also fetches the latest metadata, which can be retrieved by Metadata.
  380. //
  381. // If Poll fails, the error is returned and op is unmodified. If Poll succeeds and
  382. // the operation has completed with failure, the error is returned and op.Done will return true.
  383. // If Poll succeeds and the operation has completed successfully, op.Done will return true.
  384. func (op *DeleteInstanceOperation) Poll(ctx context.Context, opts ...gax.CallOption) error {
  385. return op.lro.Poll(ctx, nil, opts...)
  386. }
  387. // Metadata returns metadata associated with the long-running operation.
  388. // Metadata itself does not contact the server, but Poll does.
  389. // To get the latest metadata, call this method after a successful call to Poll.
  390. // If the metadata is not available, the returned metadata and error are both nil.
  391. func (op *DeleteInstanceOperation) Metadata() (*anypb.Any, error) {
  392. var meta anypb.Any
  393. if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata {
  394. return nil, nil
  395. } else if err != nil {
  396. return nil, err
  397. }
  398. return &meta, nil
  399. }
  400. // Done reports whether the long-running operation has completed.
  401. func (op *DeleteInstanceOperation) Done() bool {
  402. return op.lro.Done()
  403. }
  404. // Name returns the name of the long-running operation.
  405. // The name is assigned by the server and is unique within the service from which the operation is created.
  406. func (op *DeleteInstanceOperation) Name() string {
  407. return op.lro.Name()
  408. }
  409. // UpdateInstanceOperation manages a long-running operation from UpdateInstance.
  410. type UpdateInstanceOperation struct {
  411. lro *longrunning.Operation
  412. }
  413. // UpdateInstanceOperation returns a new UpdateInstanceOperation from a given name.
  414. // The name must be that of a previously created UpdateInstanceOperation, possibly from a different process.
  415. func (c *CloudRedisClient) UpdateInstanceOperation(name string) *UpdateInstanceOperation {
  416. return &UpdateInstanceOperation{
  417. lro: longrunning.InternalNewOperation(c.LROClient, &longrunningpb.Operation{Name: name}),
  418. }
  419. }
  420. // Wait blocks until the long-running operation is completed, returning the response and any errors encountered.
  421. //
  422. // See documentation of Poll for error-handling information.
  423. func (op *UpdateInstanceOperation) Wait(ctx context.Context, opts ...gax.CallOption) (*redispb.Instance, error) {
  424. var resp redispb.Instance
  425. if err := op.lro.WaitWithInterval(ctx, &resp, 360000*time.Millisecond, opts...); err != nil {
  426. return nil, err
  427. }
  428. return &resp, nil
  429. }
  430. // Poll fetches the latest state of the long-running operation.
  431. //
  432. // Poll also fetches the latest metadata, which can be retrieved by Metadata.
  433. //
  434. // If Poll fails, the error is returned and op is unmodified. If Poll succeeds and
  435. // the operation has completed with failure, the error is returned and op.Done will return true.
  436. // If Poll succeeds and the operation has completed successfully,
  437. // op.Done will return true, and the response of the operation is returned.
  438. // If Poll succeeds and the operation has not completed, the returned response and error are both nil.
  439. func (op *UpdateInstanceOperation) Poll(ctx context.Context, opts ...gax.CallOption) (*redispb.Instance, error) {
  440. var resp redispb.Instance
  441. if err := op.lro.Poll(ctx, &resp, opts...); err != nil {
  442. return nil, err
  443. }
  444. if !op.Done() {
  445. return nil, nil
  446. }
  447. return &resp, nil
  448. }
  449. // Metadata returns metadata associated with the long-running operation.
  450. // Metadata itself does not contact the server, but Poll does.
  451. // To get the latest metadata, call this method after a successful call to Poll.
  452. // If the metadata is not available, the returned metadata and error are both nil.
  453. func (op *UpdateInstanceOperation) Metadata() (*anypb.Any, error) {
  454. var meta anypb.Any
  455. if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata {
  456. return nil, nil
  457. } else if err != nil {
  458. return nil, err
  459. }
  460. return &meta, nil
  461. }
  462. // Done reports whether the long-running operation has completed.
  463. func (op *UpdateInstanceOperation) Done() bool {
  464. return op.lro.Done()
  465. }
  466. // Name returns the name of the long-running operation.
  467. // The name is assigned by the server and is unique within the service from which the operation is created.
  468. func (op *UpdateInstanceOperation) Name() string {
  469. return op.lro.Name()
  470. }