Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 

519 rindas
20 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 redis
  16. import (
  17. "math"
  18. "time"
  19. "cloud.google.com/go/internal/version"
  20. "cloud.google.com/go/longrunning"
  21. lroauto "cloud.google.com/go/longrunning/autogen"
  22. anypb "github.com/golang/protobuf/ptypes/any"
  23. gax "github.com/googleapis/gax-go"
  24. "golang.org/x/net/context"
  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", version.Go()}, keyval...)
  133. kv = append(kv, "gapic", version.Repo, "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. it.InternalFetch = func(pageSize int, pageToken string) ([]*redispb.Instance, string, error) {
  150. var resp *redispb.ListInstancesResponse
  151. req.PageToken = pageToken
  152. if pageSize > math.MaxInt32 {
  153. req.PageSize = math.MaxInt32
  154. } else {
  155. req.PageSize = int32(pageSize)
  156. }
  157. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  158. var err error
  159. resp, err = c.cloudRedisClient.ListInstances(ctx, req, settings.GRPC...)
  160. return err
  161. }, opts...)
  162. if err != nil {
  163. return nil, "", err
  164. }
  165. return resp.Instances, resp.NextPageToken, nil
  166. }
  167. fetch := func(pageSize int, pageToken string) (string, error) {
  168. items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
  169. if err != nil {
  170. return "", err
  171. }
  172. it.items = append(it.items, items...)
  173. return nextPageToken, nil
  174. }
  175. it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
  176. return it
  177. }
  178. // GetInstance gets the details of a specific Redis instance.
  179. func (c *CloudRedisClient) GetInstance(ctx context.Context, req *redispb.GetInstanceRequest, opts ...gax.CallOption) (*redispb.Instance, error) {
  180. ctx = insertMetadata(ctx, c.xGoogMetadata)
  181. opts = append(c.CallOptions.GetInstance[0:len(c.CallOptions.GetInstance):len(c.CallOptions.GetInstance)], opts...)
  182. var resp *redispb.Instance
  183. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  184. var err error
  185. resp, err = c.cloudRedisClient.GetInstance(ctx, req, settings.GRPC...)
  186. return err
  187. }, opts...)
  188. if err != nil {
  189. return nil, err
  190. }
  191. return resp, nil
  192. }
  193. // CreateInstance creates a Redis instance based on the specified tier and memory size.
  194. //
  195. // By default, the instance is peered to the project's
  196. // default network (at /compute/docs/networks-and-firewalls#networks).
  197. //
  198. // The creation is executed asynchronously and callers may check the returned
  199. // operation to track its progress. Once the operation is completed the Redis
  200. // instance will be fully functional. Completed longrunning.Operation will
  201. // contain the new instance object in the response field.
  202. //
  203. // The returned operation is automatically deleted after a few hours, so there
  204. // is no need to call DeleteOperation.
  205. func (c *CloudRedisClient) CreateInstance(ctx context.Context, req *redispb.CreateInstanceRequest, opts ...gax.CallOption) (*CreateInstanceOperation, error) {
  206. ctx = insertMetadata(ctx, c.xGoogMetadata)
  207. opts = append(c.CallOptions.CreateInstance[0:len(c.CallOptions.CreateInstance):len(c.CallOptions.CreateInstance)], opts...)
  208. var resp *longrunningpb.Operation
  209. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  210. var err error
  211. resp, err = c.cloudRedisClient.CreateInstance(ctx, req, settings.GRPC...)
  212. return err
  213. }, opts...)
  214. if err != nil {
  215. return nil, err
  216. }
  217. return &CreateInstanceOperation{
  218. lro: longrunning.InternalNewOperation(c.LROClient, resp),
  219. }, nil
  220. }
  221. // UpdateInstance updates the metadata and configuration of a specific Redis instance.
  222. //
  223. // Completed longrunning.Operation will contain the new instance object
  224. // in the response field. The returned operation is automatically deleted
  225. // after a few hours, so there is no need to call DeleteOperation.
  226. func (c *CloudRedisClient) UpdateInstance(ctx context.Context, req *redispb.UpdateInstanceRequest, opts ...gax.CallOption) (*UpdateInstanceOperation, error) {
  227. ctx = insertMetadata(ctx, c.xGoogMetadata)
  228. opts = append(c.CallOptions.UpdateInstance[0:len(c.CallOptions.UpdateInstance):len(c.CallOptions.UpdateInstance)], opts...)
  229. var resp *longrunningpb.Operation
  230. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  231. var err error
  232. resp, err = c.cloudRedisClient.UpdateInstance(ctx, req, settings.GRPC...)
  233. return err
  234. }, opts...)
  235. if err != nil {
  236. return nil, err
  237. }
  238. return &UpdateInstanceOperation{
  239. lro: longrunning.InternalNewOperation(c.LROClient, resp),
  240. }, nil
  241. }
  242. // DeleteInstance deletes a specific Redis instance. Instance stops serving and data is
  243. // deleted.
  244. func (c *CloudRedisClient) DeleteInstance(ctx context.Context, req *redispb.DeleteInstanceRequest, opts ...gax.CallOption) (*DeleteInstanceOperation, error) {
  245. ctx = insertMetadata(ctx, c.xGoogMetadata)
  246. opts = append(c.CallOptions.DeleteInstance[0:len(c.CallOptions.DeleteInstance):len(c.CallOptions.DeleteInstance)], opts...)
  247. var resp *longrunningpb.Operation
  248. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  249. var err error
  250. resp, err = c.cloudRedisClient.DeleteInstance(ctx, req, settings.GRPC...)
  251. return err
  252. }, opts...)
  253. if err != nil {
  254. return nil, err
  255. }
  256. return &DeleteInstanceOperation{
  257. lro: longrunning.InternalNewOperation(c.LROClient, resp),
  258. }, nil
  259. }
  260. // InstanceIterator manages a stream of *redispb.Instance.
  261. type InstanceIterator struct {
  262. items []*redispb.Instance
  263. pageInfo *iterator.PageInfo
  264. nextFunc func() error
  265. // InternalFetch is for use by the Google Cloud Libraries only.
  266. // It is not part of the stable interface of this package.
  267. //
  268. // InternalFetch returns results from a single call to the underlying RPC.
  269. // The number of results is no greater than pageSize.
  270. // If there are no more results, nextPageToken is empty and err is nil.
  271. InternalFetch func(pageSize int, pageToken string) (results []*redispb.Instance, nextPageToken string, err error)
  272. }
  273. // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
  274. func (it *InstanceIterator) PageInfo() *iterator.PageInfo {
  275. return it.pageInfo
  276. }
  277. // Next returns the next result. Its second return value is iterator.Done if there are no more
  278. // results. Once Next returns Done, all subsequent calls will return Done.
  279. func (it *InstanceIterator) Next() (*redispb.Instance, error) {
  280. var item *redispb.Instance
  281. if err := it.nextFunc(); err != nil {
  282. return item, err
  283. }
  284. item = it.items[0]
  285. it.items = it.items[1:]
  286. return item, nil
  287. }
  288. func (it *InstanceIterator) bufLen() int {
  289. return len(it.items)
  290. }
  291. func (it *InstanceIterator) takeBuf() interface{} {
  292. b := it.items
  293. it.items = nil
  294. return b
  295. }
  296. // CreateInstanceOperation manages a long-running operation from CreateInstance.
  297. type CreateInstanceOperation struct {
  298. lro *longrunning.Operation
  299. }
  300. // CreateInstanceOperation returns a new CreateInstanceOperation from a given name.
  301. // The name must be that of a previously created CreateInstanceOperation, possibly from a different process.
  302. func (c *CloudRedisClient) CreateInstanceOperation(name string) *CreateInstanceOperation {
  303. return &CreateInstanceOperation{
  304. lro: longrunning.InternalNewOperation(c.LROClient, &longrunningpb.Operation{Name: name}),
  305. }
  306. }
  307. // Wait blocks until the long-running operation is completed, returning the response and any errors encountered.
  308. //
  309. // See documentation of Poll for error-handling information.
  310. func (op *CreateInstanceOperation) Wait(ctx context.Context, opts ...gax.CallOption) (*redispb.Instance, error) {
  311. var resp redispb.Instance
  312. if err := op.lro.WaitWithInterval(ctx, &resp, 360000*time.Millisecond, opts...); err != nil {
  313. return nil, err
  314. }
  315. return &resp, nil
  316. }
  317. // Poll fetches the latest state of the long-running operation.
  318. //
  319. // Poll also fetches the latest metadata, which can be retrieved by Metadata.
  320. //
  321. // If Poll fails, the error is returned and op is unmodified. If Poll succeeds and
  322. // the operation has completed with failure, the error is returned and op.Done will return true.
  323. // If Poll succeeds and the operation has completed successfully,
  324. // op.Done will return true, and the response of the operation is returned.
  325. // If Poll succeeds and the operation has not completed, the returned response and error are both nil.
  326. func (op *CreateInstanceOperation) Poll(ctx context.Context, opts ...gax.CallOption) (*redispb.Instance, error) {
  327. var resp redispb.Instance
  328. if err := op.lro.Poll(ctx, &resp, opts...); err != nil {
  329. return nil, err
  330. }
  331. if !op.Done() {
  332. return nil, nil
  333. }
  334. return &resp, nil
  335. }
  336. // Metadata returns metadata associated with the long-running operation.
  337. // Metadata itself does not contact the server, but Poll does.
  338. // To get the latest metadata, call this method after a successful call to Poll.
  339. // If the metadata is not available, the returned metadata and error are both nil.
  340. func (op *CreateInstanceOperation) Metadata() (*anypb.Any, error) {
  341. var meta anypb.Any
  342. if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata {
  343. return nil, nil
  344. } else if err != nil {
  345. return nil, err
  346. }
  347. return &meta, nil
  348. }
  349. // Done reports whether the long-running operation has completed.
  350. func (op *CreateInstanceOperation) Done() bool {
  351. return op.lro.Done()
  352. }
  353. // Name returns the name of the long-running operation.
  354. // The name is assigned by the server and is unique within the service from which the operation is created.
  355. func (op *CreateInstanceOperation) Name() string {
  356. return op.lro.Name()
  357. }
  358. // DeleteInstanceOperation manages a long-running operation from DeleteInstance.
  359. type DeleteInstanceOperation struct {
  360. lro *longrunning.Operation
  361. }
  362. // DeleteInstanceOperation returns a new DeleteInstanceOperation from a given name.
  363. // The name must be that of a previously created DeleteInstanceOperation, possibly from a different process.
  364. func (c *CloudRedisClient) DeleteInstanceOperation(name string) *DeleteInstanceOperation {
  365. return &DeleteInstanceOperation{
  366. lro: longrunning.InternalNewOperation(c.LROClient, &longrunningpb.Operation{Name: name}),
  367. }
  368. }
  369. // Wait blocks until the long-running operation is completed, returning any error encountered.
  370. //
  371. // See documentation of Poll for error-handling information.
  372. func (op *DeleteInstanceOperation) Wait(ctx context.Context, opts ...gax.CallOption) error {
  373. return op.lro.WaitWithInterval(ctx, nil, 360000*time.Millisecond, opts...)
  374. }
  375. // Poll fetches the latest state of the long-running operation.
  376. //
  377. // Poll also fetches the latest metadata, which can be retrieved by Metadata.
  378. //
  379. // If Poll fails, the error is returned and op is unmodified. If Poll succeeds and
  380. // the operation has completed with failure, the error is returned and op.Done will return true.
  381. // If Poll succeeds and the operation has completed successfully, op.Done will return true.
  382. func (op *DeleteInstanceOperation) Poll(ctx context.Context, opts ...gax.CallOption) error {
  383. return op.lro.Poll(ctx, nil, opts...)
  384. }
  385. // Metadata returns metadata associated with the long-running operation.
  386. // Metadata itself does not contact the server, but Poll does.
  387. // To get the latest metadata, call this method after a successful call to Poll.
  388. // If the metadata is not available, the returned metadata and error are both nil.
  389. func (op *DeleteInstanceOperation) Metadata() (*anypb.Any, error) {
  390. var meta anypb.Any
  391. if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata {
  392. return nil, nil
  393. } else if err != nil {
  394. return nil, err
  395. }
  396. return &meta, nil
  397. }
  398. // Done reports whether the long-running operation has completed.
  399. func (op *DeleteInstanceOperation) Done() bool {
  400. return op.lro.Done()
  401. }
  402. // Name returns the name of the long-running operation.
  403. // The name is assigned by the server and is unique within the service from which the operation is created.
  404. func (op *DeleteInstanceOperation) Name() string {
  405. return op.lro.Name()
  406. }
  407. // UpdateInstanceOperation manages a long-running operation from UpdateInstance.
  408. type UpdateInstanceOperation struct {
  409. lro *longrunning.Operation
  410. }
  411. // UpdateInstanceOperation returns a new UpdateInstanceOperation from a given name.
  412. // The name must be that of a previously created UpdateInstanceOperation, possibly from a different process.
  413. func (c *CloudRedisClient) UpdateInstanceOperation(name string) *UpdateInstanceOperation {
  414. return &UpdateInstanceOperation{
  415. lro: longrunning.InternalNewOperation(c.LROClient, &longrunningpb.Operation{Name: name}),
  416. }
  417. }
  418. // Wait blocks until the long-running operation is completed, returning the response and any errors encountered.
  419. //
  420. // See documentation of Poll for error-handling information.
  421. func (op *UpdateInstanceOperation) Wait(ctx context.Context, opts ...gax.CallOption) (*redispb.Instance, error) {
  422. var resp redispb.Instance
  423. if err := op.lro.WaitWithInterval(ctx, &resp, 360000*time.Millisecond, opts...); err != nil {
  424. return nil, err
  425. }
  426. return &resp, nil
  427. }
  428. // Poll fetches the latest state of the long-running operation.
  429. //
  430. // Poll also fetches the latest metadata, which can be retrieved by Metadata.
  431. //
  432. // If Poll fails, the error is returned and op is unmodified. If Poll succeeds and
  433. // the operation has completed with failure, the error is returned and op.Done will return true.
  434. // If Poll succeeds and the operation has completed successfully,
  435. // op.Done will return true, and the response of the operation is returned.
  436. // If Poll succeeds and the operation has not completed, the returned response and error are both nil.
  437. func (op *UpdateInstanceOperation) Poll(ctx context.Context, opts ...gax.CallOption) (*redispb.Instance, error) {
  438. var resp redispb.Instance
  439. if err := op.lro.Poll(ctx, &resp, opts...); err != nil {
  440. return nil, err
  441. }
  442. if !op.Done() {
  443. return nil, nil
  444. }
  445. return &resp, nil
  446. }
  447. // Metadata returns metadata associated with the long-running operation.
  448. // Metadata itself does not contact the server, but Poll does.
  449. // To get the latest metadata, call this method after a successful call to Poll.
  450. // If the metadata is not available, the returned metadata and error are both nil.
  451. func (op *UpdateInstanceOperation) Metadata() (*anypb.Any, error) {
  452. var meta anypb.Any
  453. if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata {
  454. return nil, nil
  455. } else if err != nil {
  456. return nil, err
  457. }
  458. return &meta, nil
  459. }
  460. // Done reports whether the long-running operation has completed.
  461. func (op *UpdateInstanceOperation) Done() bool {
  462. return op.lro.Done()
  463. }
  464. // Name returns the name of the long-running operation.
  465. // The name is assigned by the server and is unique within the service from which the operation is created.
  466. func (op *UpdateInstanceOperation) Name() string {
  467. return op.lro.Name()
  468. }