您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

703 行
28 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 instance
  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. gax "github.com/googleapis/gax-go"
  23. "golang.org/x/net/context"
  24. "google.golang.org/api/iterator"
  25. "google.golang.org/api/option"
  26. "google.golang.org/api/transport"
  27. iampb "google.golang.org/genproto/googleapis/iam/v1"
  28. longrunningpb "google.golang.org/genproto/googleapis/longrunning"
  29. instancepb "google.golang.org/genproto/googleapis/spanner/admin/instance/v1"
  30. "google.golang.org/grpc"
  31. "google.golang.org/grpc/codes"
  32. "google.golang.org/grpc/metadata"
  33. )
  34. // InstanceAdminCallOptions contains the retry settings for each method of InstanceAdminClient.
  35. type InstanceAdminCallOptions struct {
  36. ListInstanceConfigs []gax.CallOption
  37. GetInstanceConfig []gax.CallOption
  38. ListInstances []gax.CallOption
  39. GetInstance []gax.CallOption
  40. CreateInstance []gax.CallOption
  41. UpdateInstance []gax.CallOption
  42. DeleteInstance []gax.CallOption
  43. SetIamPolicy []gax.CallOption
  44. GetIamPolicy []gax.CallOption
  45. TestIamPermissions []gax.CallOption
  46. }
  47. func defaultInstanceAdminClientOptions() []option.ClientOption {
  48. return []option.ClientOption{
  49. option.WithEndpoint("spanner.googleapis.com:443"),
  50. option.WithScopes(DefaultAuthScopes()...),
  51. }
  52. }
  53. func defaultInstanceAdminCallOptions() *InstanceAdminCallOptions {
  54. retry := map[[2]string][]gax.CallOption{
  55. {"default", "idempotent"}: {
  56. gax.WithRetry(func() gax.Retryer {
  57. return gax.OnCodes([]codes.Code{
  58. codes.DeadlineExceeded,
  59. codes.Unavailable,
  60. }, gax.Backoff{
  61. Initial: 1000 * time.Millisecond,
  62. Max: 32000 * time.Millisecond,
  63. Multiplier: 1.3,
  64. })
  65. }),
  66. },
  67. }
  68. return &InstanceAdminCallOptions{
  69. ListInstanceConfigs: retry[[2]string{"default", "idempotent"}],
  70. GetInstanceConfig: retry[[2]string{"default", "idempotent"}],
  71. ListInstances: retry[[2]string{"default", "idempotent"}],
  72. GetInstance: retry[[2]string{"default", "idempotent"}],
  73. CreateInstance: retry[[2]string{"default", "non_idempotent"}],
  74. UpdateInstance: retry[[2]string{"default", "non_idempotent"}],
  75. DeleteInstance: retry[[2]string{"default", "idempotent"}],
  76. SetIamPolicy: retry[[2]string{"default", "non_idempotent"}],
  77. GetIamPolicy: retry[[2]string{"default", "idempotent"}],
  78. TestIamPermissions: retry[[2]string{"default", "non_idempotent"}],
  79. }
  80. }
  81. // InstanceAdminClient is a client for interacting with Cloud Spanner Instance Admin API.
  82. //
  83. // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
  84. type InstanceAdminClient struct {
  85. // The connection to the service.
  86. conn *grpc.ClientConn
  87. // The gRPC API client.
  88. instanceAdminClient instancepb.InstanceAdminClient
  89. // LROClient is used internally to handle longrunning operations.
  90. // It is exposed so that its CallOptions can be modified if required.
  91. // Users should not Close this client.
  92. LROClient *lroauto.OperationsClient
  93. // The call options for this service.
  94. CallOptions *InstanceAdminCallOptions
  95. // The x-goog-* metadata to be sent with each request.
  96. xGoogMetadata metadata.MD
  97. }
  98. // NewInstanceAdminClient creates a new instance admin client.
  99. //
  100. // Cloud Spanner Instance Admin API
  101. //
  102. // The Cloud Spanner Instance Admin API can be used to create, delete,
  103. // modify and list instances. Instances are dedicated Cloud Spanner serving
  104. // and storage resources to be used by Cloud Spanner databases.
  105. //
  106. // Each instance has a "configuration", which dictates where the
  107. // serving resources for the Cloud Spanner instance are located (e.g.,
  108. // US-central, Europe). Configurations are created by Google based on
  109. // resource availability.
  110. //
  111. // Cloud Spanner billing is based on the instances that exist and their
  112. // sizes. After an instance exists, there are no additional
  113. // per-database or per-operation charges for use of the instance
  114. // (though there may be additional network bandwidth charges).
  115. // Instances offer isolation: problems with databases in one instance
  116. // will not affect other instances. However, within an instance
  117. // databases can affect each other. For example, if one database in an
  118. // instance receives a lot of requests and consumes most of the
  119. // instance resources, fewer resources are available for other
  120. // databases in that instance, and their performance may suffer.
  121. func NewInstanceAdminClient(ctx context.Context, opts ...option.ClientOption) (*InstanceAdminClient, error) {
  122. conn, err := transport.DialGRPC(ctx, append(defaultInstanceAdminClientOptions(), opts...)...)
  123. if err != nil {
  124. return nil, err
  125. }
  126. c := &InstanceAdminClient{
  127. conn: conn,
  128. CallOptions: defaultInstanceAdminCallOptions(),
  129. instanceAdminClient: instancepb.NewInstanceAdminClient(conn),
  130. }
  131. c.setGoogleClientInfo()
  132. c.LROClient, err = lroauto.NewOperationsClient(ctx, option.WithGRPCConn(conn))
  133. if err != nil {
  134. // This error "should not happen", since we are just reusing old connection
  135. // and never actually need to dial.
  136. // If this does happen, we could leak conn. However, we cannot close conn:
  137. // If the user invoked the function with option.WithGRPCConn,
  138. // we would close a connection that's still in use.
  139. // TODO(pongad): investigate error conditions.
  140. return nil, err
  141. }
  142. return c, nil
  143. }
  144. // Connection returns the client's connection to the API service.
  145. func (c *InstanceAdminClient) Connection() *grpc.ClientConn {
  146. return c.conn
  147. }
  148. // Close closes the connection to the API service. The user should invoke this when
  149. // the client is no longer required.
  150. func (c *InstanceAdminClient) Close() error {
  151. return c.conn.Close()
  152. }
  153. // setGoogleClientInfo sets the name and version of the application in
  154. // the `x-goog-api-client` header passed on each request. Intended for
  155. // use by Google-written clients.
  156. func (c *InstanceAdminClient) setGoogleClientInfo(keyval ...string) {
  157. kv := append([]string{"gl-go", version.Go()}, keyval...)
  158. kv = append(kv, "gapic", version.Repo, "gax", gax.Version, "grpc", grpc.Version)
  159. c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
  160. }
  161. // ListInstanceConfigs lists the supported instance configurations for a given project.
  162. func (c *InstanceAdminClient) ListInstanceConfigs(ctx context.Context, req *instancepb.ListInstanceConfigsRequest, opts ...gax.CallOption) *InstanceConfigIterator {
  163. ctx = insertMetadata(ctx, c.xGoogMetadata)
  164. opts = append(c.CallOptions.ListInstanceConfigs[0:len(c.CallOptions.ListInstanceConfigs):len(c.CallOptions.ListInstanceConfigs)], opts...)
  165. it := &InstanceConfigIterator{}
  166. it.InternalFetch = func(pageSize int, pageToken string) ([]*instancepb.InstanceConfig, string, error) {
  167. var resp *instancepb.ListInstanceConfigsResponse
  168. req.PageToken = pageToken
  169. if pageSize > math.MaxInt32 {
  170. req.PageSize = math.MaxInt32
  171. } else {
  172. req.PageSize = int32(pageSize)
  173. }
  174. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  175. var err error
  176. resp, err = c.instanceAdminClient.ListInstanceConfigs(ctx, req, settings.GRPC...)
  177. return err
  178. }, opts...)
  179. if err != nil {
  180. return nil, "", err
  181. }
  182. return resp.InstanceConfigs, resp.NextPageToken, nil
  183. }
  184. fetch := func(pageSize int, pageToken string) (string, error) {
  185. items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
  186. if err != nil {
  187. return "", err
  188. }
  189. it.items = append(it.items, items...)
  190. return nextPageToken, nil
  191. }
  192. it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
  193. return it
  194. }
  195. // GetInstanceConfig gets information about a particular instance configuration.
  196. func (c *InstanceAdminClient) GetInstanceConfig(ctx context.Context, req *instancepb.GetInstanceConfigRequest, opts ...gax.CallOption) (*instancepb.InstanceConfig, error) {
  197. ctx = insertMetadata(ctx, c.xGoogMetadata)
  198. opts = append(c.CallOptions.GetInstanceConfig[0:len(c.CallOptions.GetInstanceConfig):len(c.CallOptions.GetInstanceConfig)], opts...)
  199. var resp *instancepb.InstanceConfig
  200. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  201. var err error
  202. resp, err = c.instanceAdminClient.GetInstanceConfig(ctx, req, settings.GRPC...)
  203. return err
  204. }, opts...)
  205. if err != nil {
  206. return nil, err
  207. }
  208. return resp, nil
  209. }
  210. // ListInstances lists all instances in the given project.
  211. func (c *InstanceAdminClient) ListInstances(ctx context.Context, req *instancepb.ListInstancesRequest, opts ...gax.CallOption) *InstanceIterator {
  212. ctx = insertMetadata(ctx, c.xGoogMetadata)
  213. opts = append(c.CallOptions.ListInstances[0:len(c.CallOptions.ListInstances):len(c.CallOptions.ListInstances)], opts...)
  214. it := &InstanceIterator{}
  215. it.InternalFetch = func(pageSize int, pageToken string) ([]*instancepb.Instance, string, error) {
  216. var resp *instancepb.ListInstancesResponse
  217. req.PageToken = pageToken
  218. if pageSize > math.MaxInt32 {
  219. req.PageSize = math.MaxInt32
  220. } else {
  221. req.PageSize = int32(pageSize)
  222. }
  223. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  224. var err error
  225. resp, err = c.instanceAdminClient.ListInstances(ctx, req, settings.GRPC...)
  226. return err
  227. }, opts...)
  228. if err != nil {
  229. return nil, "", err
  230. }
  231. return resp.Instances, resp.NextPageToken, nil
  232. }
  233. fetch := func(pageSize int, pageToken string) (string, error) {
  234. items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
  235. if err != nil {
  236. return "", err
  237. }
  238. it.items = append(it.items, items...)
  239. return nextPageToken, nil
  240. }
  241. it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
  242. return it
  243. }
  244. // GetInstance gets information about a particular instance.
  245. func (c *InstanceAdminClient) GetInstance(ctx context.Context, req *instancepb.GetInstanceRequest, opts ...gax.CallOption) (*instancepb.Instance, error) {
  246. ctx = insertMetadata(ctx, c.xGoogMetadata)
  247. opts = append(c.CallOptions.GetInstance[0:len(c.CallOptions.GetInstance):len(c.CallOptions.GetInstance)], opts...)
  248. var resp *instancepb.Instance
  249. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  250. var err error
  251. resp, err = c.instanceAdminClient.GetInstance(ctx, req, settings.GRPC...)
  252. return err
  253. }, opts...)
  254. if err != nil {
  255. return nil, err
  256. }
  257. return resp, nil
  258. }
  259. // CreateInstance creates an instance and begins preparing it to begin serving. The
  260. // returned [long-running operation][google.longrunning.Operation]
  261. // can be used to track the progress of preparing the new
  262. // instance. The instance name is assigned by the caller. If the
  263. // named instance already exists, CreateInstance returns
  264. // ALREADY_EXISTS.
  265. //
  266. // Immediately upon completion of this request:
  267. //
  268. // The instance is readable via the API, with all requested attributes
  269. // but no allocated resources. Its state is CREATING.
  270. //
  271. // Until completion of the returned operation:
  272. //
  273. // Cancelling the operation renders the instance immediately unreadable
  274. // via the API.
  275. //
  276. // The instance can be deleted.
  277. //
  278. // All other attempts to modify the instance are rejected.
  279. //
  280. // Upon completion of the returned operation:
  281. //
  282. // Billing for all successfully-allocated resources begins (some types
  283. // may have lower than the requested levels).
  284. //
  285. // Databases can be created in the instance.
  286. //
  287. // The instance's allocated resource levels are readable via the API.
  288. //
  289. // The instance's state becomes READY.
  290. //
  291. // The returned [long-running operation][google.longrunning.Operation] will
  292. // have a name of the format <instance_name>/operations/<operation_id> and
  293. // can be used to track creation of the instance. The
  294. // [metadata][google.longrunning.Operation.metadata] field type is
  295. // [CreateInstanceMetadata][google.spanner.admin.instance.v1.CreateInstanceMetadata].
  296. // The [response][google.longrunning.Operation.response] field type is
  297. // [Instance][google.spanner.admin.instance.v1.Instance], if successful.
  298. func (c *InstanceAdminClient) CreateInstance(ctx context.Context, req *instancepb.CreateInstanceRequest, opts ...gax.CallOption) (*CreateInstanceOperation, error) {
  299. ctx = insertMetadata(ctx, c.xGoogMetadata)
  300. opts = append(c.CallOptions.CreateInstance[0:len(c.CallOptions.CreateInstance):len(c.CallOptions.CreateInstance)], opts...)
  301. var resp *longrunningpb.Operation
  302. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  303. var err error
  304. resp, err = c.instanceAdminClient.CreateInstance(ctx, req, settings.GRPC...)
  305. return err
  306. }, opts...)
  307. if err != nil {
  308. return nil, err
  309. }
  310. return &CreateInstanceOperation{
  311. lro: longrunning.InternalNewOperation(c.LROClient, resp),
  312. }, nil
  313. }
  314. // UpdateInstance updates an instance, and begins allocating or releasing resources
  315. // as requested. The returned [long-running
  316. // operation][google.longrunning.Operation] can be used to track the
  317. // progress of updating the instance. If the named instance does not
  318. // exist, returns NOT_FOUND.
  319. //
  320. // Immediately upon completion of this request:
  321. //
  322. // For resource types for which a decrease in the instance's allocation
  323. // has been requested, billing is based on the newly-requested level.
  324. //
  325. // Until completion of the returned operation:
  326. //
  327. // Cancelling the operation sets its metadata's
  328. // [cancel_time][google.spanner.admin.instance.v1.UpdateInstanceMetadata.cancel_time], and begins
  329. // restoring resources to their pre-request values. The operation
  330. // is guaranteed to succeed at undoing all resource changes,
  331. // after which point it terminates with a CANCELLED status.
  332. //
  333. // All other attempts to modify the instance are rejected.
  334. //
  335. // Reading the instance via the API continues to give the pre-request
  336. // resource levels.
  337. //
  338. // Upon completion of the returned operation:
  339. //
  340. // Billing begins for all successfully-allocated resources (some types
  341. // may have lower than the requested levels).
  342. //
  343. // All newly-reserved resources are available for serving the instance's
  344. // tables.
  345. //
  346. // The instance's new resource levels are readable via the API.
  347. //
  348. // The returned [long-running operation][google.longrunning.Operation] will
  349. // have a name of the format <instance_name>/operations/<operation_id> and
  350. // can be used to track the instance modification. The
  351. // [metadata][google.longrunning.Operation.metadata] field type is
  352. // [UpdateInstanceMetadata][google.spanner.admin.instance.v1.UpdateInstanceMetadata].
  353. // The [response][google.longrunning.Operation.response] field type is
  354. // [Instance][google.spanner.admin.instance.v1.Instance], if successful.
  355. //
  356. // Authorization requires spanner.instances.update permission on
  357. // resource [name][google.spanner.admin.instance.v1.Instance.name].
  358. func (c *InstanceAdminClient) UpdateInstance(ctx context.Context, req *instancepb.UpdateInstanceRequest, opts ...gax.CallOption) (*UpdateInstanceOperation, error) {
  359. ctx = insertMetadata(ctx, c.xGoogMetadata)
  360. opts = append(c.CallOptions.UpdateInstance[0:len(c.CallOptions.UpdateInstance):len(c.CallOptions.UpdateInstance)], opts...)
  361. var resp *longrunningpb.Operation
  362. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  363. var err error
  364. resp, err = c.instanceAdminClient.UpdateInstance(ctx, req, settings.GRPC...)
  365. return err
  366. }, opts...)
  367. if err != nil {
  368. return nil, err
  369. }
  370. return &UpdateInstanceOperation{
  371. lro: longrunning.InternalNewOperation(c.LROClient, resp),
  372. }, nil
  373. }
  374. // DeleteInstance deletes an instance.
  375. //
  376. // Immediately upon completion of the request:
  377. //
  378. // Billing ceases for all of the instance's reserved resources.
  379. //
  380. // Soon afterward:
  381. //
  382. // The instance and all of its databases immediately and
  383. // irrevocably disappear from the API. All data in the databases
  384. // is permanently deleted.
  385. func (c *InstanceAdminClient) DeleteInstance(ctx context.Context, req *instancepb.DeleteInstanceRequest, opts ...gax.CallOption) error {
  386. ctx = insertMetadata(ctx, c.xGoogMetadata)
  387. opts = append(c.CallOptions.DeleteInstance[0:len(c.CallOptions.DeleteInstance):len(c.CallOptions.DeleteInstance)], opts...)
  388. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  389. var err error
  390. _, err = c.instanceAdminClient.DeleteInstance(ctx, req, settings.GRPC...)
  391. return err
  392. }, opts...)
  393. return err
  394. }
  395. // SetIamPolicy sets the access control policy on an instance resource. Replaces any
  396. // existing policy.
  397. //
  398. // Authorization requires spanner.instances.setIamPolicy on
  399. // [resource][google.iam.v1.SetIamPolicyRequest.resource].
  400. func (c *InstanceAdminClient) SetIamPolicy(ctx context.Context, req *iampb.SetIamPolicyRequest, opts ...gax.CallOption) (*iampb.Policy, error) {
  401. ctx = insertMetadata(ctx, c.xGoogMetadata)
  402. opts = append(c.CallOptions.SetIamPolicy[0:len(c.CallOptions.SetIamPolicy):len(c.CallOptions.SetIamPolicy)], opts...)
  403. var resp *iampb.Policy
  404. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  405. var err error
  406. resp, err = c.instanceAdminClient.SetIamPolicy(ctx, req, settings.GRPC...)
  407. return err
  408. }, opts...)
  409. if err != nil {
  410. return nil, err
  411. }
  412. return resp, nil
  413. }
  414. // GetIamPolicy gets the access control policy for an instance resource. Returns an empty
  415. // policy if an instance exists but does not have a policy set.
  416. //
  417. // Authorization requires spanner.instances.getIamPolicy on
  418. // [resource][google.iam.v1.GetIamPolicyRequest.resource].
  419. func (c *InstanceAdminClient) GetIamPolicy(ctx context.Context, req *iampb.GetIamPolicyRequest, opts ...gax.CallOption) (*iampb.Policy, error) {
  420. ctx = insertMetadata(ctx, c.xGoogMetadata)
  421. opts = append(c.CallOptions.GetIamPolicy[0:len(c.CallOptions.GetIamPolicy):len(c.CallOptions.GetIamPolicy)], opts...)
  422. var resp *iampb.Policy
  423. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  424. var err error
  425. resp, err = c.instanceAdminClient.GetIamPolicy(ctx, req, settings.GRPC...)
  426. return err
  427. }, opts...)
  428. if err != nil {
  429. return nil, err
  430. }
  431. return resp, nil
  432. }
  433. // TestIamPermissions returns permissions that the caller has on the specified instance resource.
  434. //
  435. // Attempting this RPC on a non-existent Cloud Spanner instance resource will
  436. // result in a NOT_FOUND error if the user has spanner.instances.list
  437. // permission on the containing Google Cloud Project. Otherwise returns an
  438. // empty set of permissions.
  439. func (c *InstanceAdminClient) TestIamPermissions(ctx context.Context, req *iampb.TestIamPermissionsRequest, opts ...gax.CallOption) (*iampb.TestIamPermissionsResponse, error) {
  440. ctx = insertMetadata(ctx, c.xGoogMetadata)
  441. opts = append(c.CallOptions.TestIamPermissions[0:len(c.CallOptions.TestIamPermissions):len(c.CallOptions.TestIamPermissions)], opts...)
  442. var resp *iampb.TestIamPermissionsResponse
  443. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  444. var err error
  445. resp, err = c.instanceAdminClient.TestIamPermissions(ctx, req, settings.GRPC...)
  446. return err
  447. }, opts...)
  448. if err != nil {
  449. return nil, err
  450. }
  451. return resp, nil
  452. }
  453. // InstanceConfigIterator manages a stream of *instancepb.InstanceConfig.
  454. type InstanceConfigIterator struct {
  455. items []*instancepb.InstanceConfig
  456. pageInfo *iterator.PageInfo
  457. nextFunc func() error
  458. // InternalFetch is for use by the Google Cloud Libraries only.
  459. // It is not part of the stable interface of this package.
  460. //
  461. // InternalFetch returns results from a single call to the underlying RPC.
  462. // The number of results is no greater than pageSize.
  463. // If there are no more results, nextPageToken is empty and err is nil.
  464. InternalFetch func(pageSize int, pageToken string) (results []*instancepb.InstanceConfig, nextPageToken string, err error)
  465. }
  466. // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
  467. func (it *InstanceConfigIterator) PageInfo() *iterator.PageInfo {
  468. return it.pageInfo
  469. }
  470. // Next returns the next result. Its second return value is iterator.Done if there are no more
  471. // results. Once Next returns Done, all subsequent calls will return Done.
  472. func (it *InstanceConfigIterator) Next() (*instancepb.InstanceConfig, error) {
  473. var item *instancepb.InstanceConfig
  474. if err := it.nextFunc(); err != nil {
  475. return item, err
  476. }
  477. item = it.items[0]
  478. it.items = it.items[1:]
  479. return item, nil
  480. }
  481. func (it *InstanceConfigIterator) bufLen() int {
  482. return len(it.items)
  483. }
  484. func (it *InstanceConfigIterator) takeBuf() interface{} {
  485. b := it.items
  486. it.items = nil
  487. return b
  488. }
  489. // InstanceIterator manages a stream of *instancepb.Instance.
  490. type InstanceIterator struct {
  491. items []*instancepb.Instance
  492. pageInfo *iterator.PageInfo
  493. nextFunc func() error
  494. // InternalFetch is for use by the Google Cloud Libraries only.
  495. // It is not part of the stable interface of this package.
  496. //
  497. // InternalFetch returns results from a single call to the underlying RPC.
  498. // The number of results is no greater than pageSize.
  499. // If there are no more results, nextPageToken is empty and err is nil.
  500. InternalFetch func(pageSize int, pageToken string) (results []*instancepb.Instance, nextPageToken string, err error)
  501. }
  502. // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
  503. func (it *InstanceIterator) PageInfo() *iterator.PageInfo {
  504. return it.pageInfo
  505. }
  506. // Next returns the next result. Its second return value is iterator.Done if there are no more
  507. // results. Once Next returns Done, all subsequent calls will return Done.
  508. func (it *InstanceIterator) Next() (*instancepb.Instance, error) {
  509. var item *instancepb.Instance
  510. if err := it.nextFunc(); err != nil {
  511. return item, err
  512. }
  513. item = it.items[0]
  514. it.items = it.items[1:]
  515. return item, nil
  516. }
  517. func (it *InstanceIterator) bufLen() int {
  518. return len(it.items)
  519. }
  520. func (it *InstanceIterator) takeBuf() interface{} {
  521. b := it.items
  522. it.items = nil
  523. return b
  524. }
  525. // CreateInstanceOperation manages a long-running operation from CreateInstance.
  526. type CreateInstanceOperation struct {
  527. lro *longrunning.Operation
  528. }
  529. // CreateInstanceOperation returns a new CreateInstanceOperation from a given name.
  530. // The name must be that of a previously created CreateInstanceOperation, possibly from a different process.
  531. func (c *InstanceAdminClient) CreateInstanceOperation(name string) *CreateInstanceOperation {
  532. return &CreateInstanceOperation{
  533. lro: longrunning.InternalNewOperation(c.LROClient, &longrunningpb.Operation{Name: name}),
  534. }
  535. }
  536. // Wait blocks until the long-running operation is completed, returning the response and any errors encountered.
  537. //
  538. // See documentation of Poll for error-handling information.
  539. func (op *CreateInstanceOperation) Wait(ctx context.Context, opts ...gax.CallOption) (*instancepb.Instance, error) {
  540. var resp instancepb.Instance
  541. if err := op.lro.WaitWithInterval(ctx, &resp, 45000*time.Millisecond, opts...); err != nil {
  542. return nil, err
  543. }
  544. return &resp, nil
  545. }
  546. // Poll fetches the latest state of the long-running operation.
  547. //
  548. // Poll also fetches the latest metadata, which can be retrieved by Metadata.
  549. //
  550. // If Poll fails, the error is returned and op is unmodified. If Poll succeeds and
  551. // the operation has completed with failure, the error is returned and op.Done will return true.
  552. // If Poll succeeds and the operation has completed successfully,
  553. // op.Done will return true, and the response of the operation is returned.
  554. // If Poll succeeds and the operation has not completed, the returned response and error are both nil.
  555. func (op *CreateInstanceOperation) Poll(ctx context.Context, opts ...gax.CallOption) (*instancepb.Instance, error) {
  556. var resp instancepb.Instance
  557. if err := op.lro.Poll(ctx, &resp, opts...); err != nil {
  558. return nil, err
  559. }
  560. if !op.Done() {
  561. return nil, nil
  562. }
  563. return &resp, nil
  564. }
  565. // Metadata returns metadata associated with the long-running operation.
  566. // Metadata itself does not contact the server, but Poll does.
  567. // To get the latest metadata, call this method after a successful call to Poll.
  568. // If the metadata is not available, the returned metadata and error are both nil.
  569. func (op *CreateInstanceOperation) Metadata() (*instancepb.CreateInstanceMetadata, error) {
  570. var meta instancepb.CreateInstanceMetadata
  571. if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata {
  572. return nil, nil
  573. } else if err != nil {
  574. return nil, err
  575. }
  576. return &meta, nil
  577. }
  578. // Done reports whether the long-running operation has completed.
  579. func (op *CreateInstanceOperation) Done() bool {
  580. return op.lro.Done()
  581. }
  582. // Name returns the name of the long-running operation.
  583. // The name is assigned by the server and is unique within the service from which the operation is created.
  584. func (op *CreateInstanceOperation) Name() string {
  585. return op.lro.Name()
  586. }
  587. // UpdateInstanceOperation manages a long-running operation from UpdateInstance.
  588. type UpdateInstanceOperation struct {
  589. lro *longrunning.Operation
  590. }
  591. // UpdateInstanceOperation returns a new UpdateInstanceOperation from a given name.
  592. // The name must be that of a previously created UpdateInstanceOperation, possibly from a different process.
  593. func (c *InstanceAdminClient) UpdateInstanceOperation(name string) *UpdateInstanceOperation {
  594. return &UpdateInstanceOperation{
  595. lro: longrunning.InternalNewOperation(c.LROClient, &longrunningpb.Operation{Name: name}),
  596. }
  597. }
  598. // Wait blocks until the long-running operation is completed, returning the response and any errors encountered.
  599. //
  600. // See documentation of Poll for error-handling information.
  601. func (op *UpdateInstanceOperation) Wait(ctx context.Context, opts ...gax.CallOption) (*instancepb.Instance, error) {
  602. var resp instancepb.Instance
  603. if err := op.lro.WaitWithInterval(ctx, &resp, 45000*time.Millisecond, opts...); err != nil {
  604. return nil, err
  605. }
  606. return &resp, nil
  607. }
  608. // Poll fetches the latest state of the long-running operation.
  609. //
  610. // Poll also fetches the latest metadata, which can be retrieved by Metadata.
  611. //
  612. // If Poll fails, the error is returned and op is unmodified. If Poll succeeds and
  613. // the operation has completed with failure, the error is returned and op.Done will return true.
  614. // If Poll succeeds and the operation has completed successfully,
  615. // op.Done will return true, and the response of the operation is returned.
  616. // If Poll succeeds and the operation has not completed, the returned response and error are both nil.
  617. func (op *UpdateInstanceOperation) Poll(ctx context.Context, opts ...gax.CallOption) (*instancepb.Instance, error) {
  618. var resp instancepb.Instance
  619. if err := op.lro.Poll(ctx, &resp, opts...); err != nil {
  620. return nil, err
  621. }
  622. if !op.Done() {
  623. return nil, nil
  624. }
  625. return &resp, nil
  626. }
  627. // Metadata returns metadata associated with the long-running operation.
  628. // Metadata itself does not contact the server, but Poll does.
  629. // To get the latest metadata, call this method after a successful call to Poll.
  630. // If the metadata is not available, the returned metadata and error are both nil.
  631. func (op *UpdateInstanceOperation) Metadata() (*instancepb.UpdateInstanceMetadata, error) {
  632. var meta instancepb.UpdateInstanceMetadata
  633. if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata {
  634. return nil, nil
  635. } else if err != nil {
  636. return nil, err
  637. }
  638. return &meta, nil
  639. }
  640. // Done reports whether the long-running operation has completed.
  641. func (op *UpdateInstanceOperation) Done() bool {
  642. return op.lro.Done()
  643. }
  644. // Name returns the name of the long-running operation.
  645. // The name is assigned by the server and is unique within the service from which the operation is created.
  646. func (op *UpdateInstanceOperation) Name() string {
  647. return op.lro.Name()
  648. }