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.
 
 
 

707 lines
28 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 instance
  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. gax "github.com/googleapis/gax-go/v2"
  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", versionGo()}, keyval...)
  158. kv = append(kv, "gapic", versionClient, "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. req = proto.Clone(req).(*instancepb.ListInstanceConfigsRequest)
  167. it.InternalFetch = func(pageSize int, pageToken string) ([]*instancepb.InstanceConfig, string, error) {
  168. var resp *instancepb.ListInstanceConfigsResponse
  169. req.PageToken = pageToken
  170. if pageSize > math.MaxInt32 {
  171. req.PageSize = math.MaxInt32
  172. } else {
  173. req.PageSize = int32(pageSize)
  174. }
  175. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  176. var err error
  177. resp, err = c.instanceAdminClient.ListInstanceConfigs(ctx, req, settings.GRPC...)
  178. return err
  179. }, opts...)
  180. if err != nil {
  181. return nil, "", err
  182. }
  183. return resp.InstanceConfigs, resp.NextPageToken, nil
  184. }
  185. fetch := func(pageSize int, pageToken string) (string, error) {
  186. items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
  187. if err != nil {
  188. return "", err
  189. }
  190. it.items = append(it.items, items...)
  191. return nextPageToken, nil
  192. }
  193. it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
  194. it.pageInfo.MaxSize = int(req.PageSize)
  195. return it
  196. }
  197. // GetInstanceConfig gets information about a particular instance configuration.
  198. func (c *InstanceAdminClient) GetInstanceConfig(ctx context.Context, req *instancepb.GetInstanceConfigRequest, opts ...gax.CallOption) (*instancepb.InstanceConfig, error) {
  199. ctx = insertMetadata(ctx, c.xGoogMetadata)
  200. opts = append(c.CallOptions.GetInstanceConfig[0:len(c.CallOptions.GetInstanceConfig):len(c.CallOptions.GetInstanceConfig)], opts...)
  201. var resp *instancepb.InstanceConfig
  202. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  203. var err error
  204. resp, err = c.instanceAdminClient.GetInstanceConfig(ctx, req, settings.GRPC...)
  205. return err
  206. }, opts...)
  207. if err != nil {
  208. return nil, err
  209. }
  210. return resp, nil
  211. }
  212. // ListInstances lists all instances in the given project.
  213. func (c *InstanceAdminClient) ListInstances(ctx context.Context, req *instancepb.ListInstancesRequest, opts ...gax.CallOption) *InstanceIterator {
  214. ctx = insertMetadata(ctx, c.xGoogMetadata)
  215. opts = append(c.CallOptions.ListInstances[0:len(c.CallOptions.ListInstances):len(c.CallOptions.ListInstances)], opts...)
  216. it := &InstanceIterator{}
  217. req = proto.Clone(req).(*instancepb.ListInstancesRequest)
  218. it.InternalFetch = func(pageSize int, pageToken string) ([]*instancepb.Instance, string, error) {
  219. var resp *instancepb.ListInstancesResponse
  220. req.PageToken = pageToken
  221. if pageSize > math.MaxInt32 {
  222. req.PageSize = math.MaxInt32
  223. } else {
  224. req.PageSize = int32(pageSize)
  225. }
  226. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  227. var err error
  228. resp, err = c.instanceAdminClient.ListInstances(ctx, req, settings.GRPC...)
  229. return err
  230. }, opts...)
  231. if err != nil {
  232. return nil, "", err
  233. }
  234. return resp.Instances, resp.NextPageToken, nil
  235. }
  236. fetch := func(pageSize int, pageToken string) (string, error) {
  237. items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
  238. if err != nil {
  239. return "", err
  240. }
  241. it.items = append(it.items, items...)
  242. return nextPageToken, nil
  243. }
  244. it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
  245. it.pageInfo.MaxSize = int(req.PageSize)
  246. return it
  247. }
  248. // GetInstance gets information about a particular instance.
  249. func (c *InstanceAdminClient) GetInstance(ctx context.Context, req *instancepb.GetInstanceRequest, opts ...gax.CallOption) (*instancepb.Instance, error) {
  250. ctx = insertMetadata(ctx, c.xGoogMetadata)
  251. opts = append(c.CallOptions.GetInstance[0:len(c.CallOptions.GetInstance):len(c.CallOptions.GetInstance)], opts...)
  252. var resp *instancepb.Instance
  253. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  254. var err error
  255. resp, err = c.instanceAdminClient.GetInstance(ctx, req, settings.GRPC...)
  256. return err
  257. }, opts...)
  258. if err != nil {
  259. return nil, err
  260. }
  261. return resp, nil
  262. }
  263. // CreateInstance creates an instance and begins preparing it to begin serving. The
  264. // returned [long-running operation][google.longrunning.Operation]
  265. // can be used to track the progress of preparing the new
  266. // instance. The instance name is assigned by the caller. If the
  267. // named instance already exists, CreateInstance returns
  268. // ALREADY_EXISTS.
  269. //
  270. // Immediately upon completion of this request:
  271. //
  272. // The instance is readable via the API, with all requested attributes
  273. // but no allocated resources. Its state is CREATING.
  274. //
  275. // Until completion of the returned operation:
  276. //
  277. // Cancelling the operation renders the instance immediately unreadable
  278. // via the API.
  279. //
  280. // The instance can be deleted.
  281. //
  282. // All other attempts to modify the instance are rejected.
  283. //
  284. // Upon completion of the returned operation:
  285. //
  286. // Billing for all successfully-allocated resources begins (some types
  287. // may have lower than the requested levels).
  288. //
  289. // Databases can be created in the instance.
  290. //
  291. // The instance's allocated resource levels are readable via the API.
  292. //
  293. // The instance's state becomes READY.
  294. //
  295. // The returned [long-running operation][google.longrunning.Operation] will
  296. // have a name of the format <instance_name>/operations/<operation_id> and
  297. // can be used to track creation of the instance. The
  298. // [metadata][google.longrunning.Operation.metadata] field type is
  299. // [CreateInstanceMetadata][google.spanner.admin.instance.v1.CreateInstanceMetadata].
  300. // The [response][google.longrunning.Operation.response] field type is
  301. // [Instance][google.spanner.admin.instance.v1.Instance], if successful.
  302. func (c *InstanceAdminClient) CreateInstance(ctx context.Context, req *instancepb.CreateInstanceRequest, opts ...gax.CallOption) (*CreateInstanceOperation, error) {
  303. ctx = insertMetadata(ctx, c.xGoogMetadata)
  304. opts = append(c.CallOptions.CreateInstance[0:len(c.CallOptions.CreateInstance):len(c.CallOptions.CreateInstance)], opts...)
  305. var resp *longrunningpb.Operation
  306. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  307. var err error
  308. resp, err = c.instanceAdminClient.CreateInstance(ctx, req, settings.GRPC...)
  309. return err
  310. }, opts...)
  311. if err != nil {
  312. return nil, err
  313. }
  314. return &CreateInstanceOperation{
  315. lro: longrunning.InternalNewOperation(c.LROClient, resp),
  316. }, nil
  317. }
  318. // UpdateInstance updates an instance, and begins allocating or releasing resources
  319. // as requested. The returned [long-running
  320. // operation][google.longrunning.Operation] can be used to track the
  321. // progress of updating the instance. If the named instance does not
  322. // exist, returns NOT_FOUND.
  323. //
  324. // Immediately upon completion of this request:
  325. //
  326. // For resource types for which a decrease in the instance's allocation
  327. // has been requested, billing is based on the newly-requested level.
  328. //
  329. // Until completion of the returned operation:
  330. //
  331. // Cancelling the operation sets its metadata's
  332. // [cancel_time][google.spanner.admin.instance.v1.UpdateInstanceMetadata.cancel_time],
  333. // and begins restoring resources to their pre-request values. The
  334. // operation is guaranteed to succeed at undoing all resource changes,
  335. // after which point it terminates with a CANCELLED status.
  336. //
  337. // All other attempts to modify the instance are rejected.
  338. //
  339. // Reading the instance via the API continues to give the pre-request
  340. // resource levels.
  341. //
  342. // Upon completion of the returned operation:
  343. //
  344. // Billing begins for all successfully-allocated resources (some types
  345. // may have lower than the requested levels).
  346. //
  347. // All newly-reserved resources are available for serving the instance's
  348. // tables.
  349. //
  350. // The instance's new resource levels are readable via the API.
  351. //
  352. // The returned [long-running operation][google.longrunning.Operation] will
  353. // have a name of the format <instance_name>/operations/<operation_id> and
  354. // can be used to track the instance modification. The
  355. // [metadata][google.longrunning.Operation.metadata] field type is
  356. // [UpdateInstanceMetadata][google.spanner.admin.instance.v1.UpdateInstanceMetadata].
  357. // The [response][google.longrunning.Operation.response] field type is
  358. // [Instance][google.spanner.admin.instance.v1.Instance], if successful.
  359. //
  360. // Authorization requires spanner.instances.update permission on
  361. // resource [name][google.spanner.admin.instance.v1.Instance.name].
  362. func (c *InstanceAdminClient) UpdateInstance(ctx context.Context, req *instancepb.UpdateInstanceRequest, opts ...gax.CallOption) (*UpdateInstanceOperation, error) {
  363. ctx = insertMetadata(ctx, c.xGoogMetadata)
  364. opts = append(c.CallOptions.UpdateInstance[0:len(c.CallOptions.UpdateInstance):len(c.CallOptions.UpdateInstance)], opts...)
  365. var resp *longrunningpb.Operation
  366. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  367. var err error
  368. resp, err = c.instanceAdminClient.UpdateInstance(ctx, req, settings.GRPC...)
  369. return err
  370. }, opts...)
  371. if err != nil {
  372. return nil, err
  373. }
  374. return &UpdateInstanceOperation{
  375. lro: longrunning.InternalNewOperation(c.LROClient, resp),
  376. }, nil
  377. }
  378. // DeleteInstance deletes an instance.
  379. //
  380. // Immediately upon completion of the request:
  381. //
  382. // Billing ceases for all of the instance's reserved resources.
  383. //
  384. // Soon afterward:
  385. //
  386. // The instance and all of its databases immediately and
  387. // irrevocably disappear from the API. All data in the databases
  388. // is permanently deleted.
  389. func (c *InstanceAdminClient) DeleteInstance(ctx context.Context, req *instancepb.DeleteInstanceRequest, opts ...gax.CallOption) error {
  390. ctx = insertMetadata(ctx, c.xGoogMetadata)
  391. opts = append(c.CallOptions.DeleteInstance[0:len(c.CallOptions.DeleteInstance):len(c.CallOptions.DeleteInstance)], opts...)
  392. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  393. var err error
  394. _, err = c.instanceAdminClient.DeleteInstance(ctx, req, settings.GRPC...)
  395. return err
  396. }, opts...)
  397. return err
  398. }
  399. // SetIamPolicy sets the access control policy on an instance resource. Replaces any
  400. // existing policy.
  401. //
  402. // Authorization requires spanner.instances.setIamPolicy on
  403. // [resource][google.iam.v1.SetIamPolicyRequest.resource].
  404. func (c *InstanceAdminClient) SetIamPolicy(ctx context.Context, req *iampb.SetIamPolicyRequest, opts ...gax.CallOption) (*iampb.Policy, error) {
  405. ctx = insertMetadata(ctx, c.xGoogMetadata)
  406. opts = append(c.CallOptions.SetIamPolicy[0:len(c.CallOptions.SetIamPolicy):len(c.CallOptions.SetIamPolicy)], opts...)
  407. var resp *iampb.Policy
  408. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  409. var err error
  410. resp, err = c.instanceAdminClient.SetIamPolicy(ctx, req, settings.GRPC...)
  411. return err
  412. }, opts...)
  413. if err != nil {
  414. return nil, err
  415. }
  416. return resp, nil
  417. }
  418. // GetIamPolicy gets the access control policy for an instance resource. Returns an empty
  419. // policy if an instance exists but does not have a policy set.
  420. //
  421. // Authorization requires spanner.instances.getIamPolicy on
  422. // [resource][google.iam.v1.GetIamPolicyRequest.resource].
  423. func (c *InstanceAdminClient) GetIamPolicy(ctx context.Context, req *iampb.GetIamPolicyRequest, opts ...gax.CallOption) (*iampb.Policy, error) {
  424. ctx = insertMetadata(ctx, c.xGoogMetadata)
  425. opts = append(c.CallOptions.GetIamPolicy[0:len(c.CallOptions.GetIamPolicy):len(c.CallOptions.GetIamPolicy)], opts...)
  426. var resp *iampb.Policy
  427. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  428. var err error
  429. resp, err = c.instanceAdminClient.GetIamPolicy(ctx, req, settings.GRPC...)
  430. return err
  431. }, opts...)
  432. if err != nil {
  433. return nil, err
  434. }
  435. return resp, nil
  436. }
  437. // TestIamPermissions returns permissions that the caller has on the specified instance resource.
  438. //
  439. // Attempting this RPC on a non-existent Cloud Spanner instance resource will
  440. // result in a NOT_FOUND error if the user has spanner.instances.list
  441. // permission on the containing Google Cloud Project. Otherwise returns an
  442. // empty set of permissions.
  443. func (c *InstanceAdminClient) TestIamPermissions(ctx context.Context, req *iampb.TestIamPermissionsRequest, opts ...gax.CallOption) (*iampb.TestIamPermissionsResponse, error) {
  444. ctx = insertMetadata(ctx, c.xGoogMetadata)
  445. opts = append(c.CallOptions.TestIamPermissions[0:len(c.CallOptions.TestIamPermissions):len(c.CallOptions.TestIamPermissions)], opts...)
  446. var resp *iampb.TestIamPermissionsResponse
  447. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  448. var err error
  449. resp, err = c.instanceAdminClient.TestIamPermissions(ctx, req, settings.GRPC...)
  450. return err
  451. }, opts...)
  452. if err != nil {
  453. return nil, err
  454. }
  455. return resp, nil
  456. }
  457. // InstanceConfigIterator manages a stream of *instancepb.InstanceConfig.
  458. type InstanceConfigIterator struct {
  459. items []*instancepb.InstanceConfig
  460. pageInfo *iterator.PageInfo
  461. nextFunc func() error
  462. // InternalFetch is for use by the Google Cloud Libraries only.
  463. // It is not part of the stable interface of this package.
  464. //
  465. // InternalFetch returns results from a single call to the underlying RPC.
  466. // The number of results is no greater than pageSize.
  467. // If there are no more results, nextPageToken is empty and err is nil.
  468. InternalFetch func(pageSize int, pageToken string) (results []*instancepb.InstanceConfig, nextPageToken string, err error)
  469. }
  470. // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
  471. func (it *InstanceConfigIterator) PageInfo() *iterator.PageInfo {
  472. return it.pageInfo
  473. }
  474. // Next returns the next result. Its second return value is iterator.Done if there are no more
  475. // results. Once Next returns Done, all subsequent calls will return Done.
  476. func (it *InstanceConfigIterator) Next() (*instancepb.InstanceConfig, error) {
  477. var item *instancepb.InstanceConfig
  478. if err := it.nextFunc(); err != nil {
  479. return item, err
  480. }
  481. item = it.items[0]
  482. it.items = it.items[1:]
  483. return item, nil
  484. }
  485. func (it *InstanceConfigIterator) bufLen() int {
  486. return len(it.items)
  487. }
  488. func (it *InstanceConfigIterator) takeBuf() interface{} {
  489. b := it.items
  490. it.items = nil
  491. return b
  492. }
  493. // InstanceIterator manages a stream of *instancepb.Instance.
  494. type InstanceIterator struct {
  495. items []*instancepb.Instance
  496. pageInfo *iterator.PageInfo
  497. nextFunc func() error
  498. // InternalFetch is for use by the Google Cloud Libraries only.
  499. // It is not part of the stable interface of this package.
  500. //
  501. // InternalFetch returns results from a single call to the underlying RPC.
  502. // The number of results is no greater than pageSize.
  503. // If there are no more results, nextPageToken is empty and err is nil.
  504. InternalFetch func(pageSize int, pageToken string) (results []*instancepb.Instance, nextPageToken string, err error)
  505. }
  506. // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
  507. func (it *InstanceIterator) PageInfo() *iterator.PageInfo {
  508. return it.pageInfo
  509. }
  510. // Next returns the next result. Its second return value is iterator.Done if there are no more
  511. // results. Once Next returns Done, all subsequent calls will return Done.
  512. func (it *InstanceIterator) Next() (*instancepb.Instance, error) {
  513. var item *instancepb.Instance
  514. if err := it.nextFunc(); err != nil {
  515. return item, err
  516. }
  517. item = it.items[0]
  518. it.items = it.items[1:]
  519. return item, nil
  520. }
  521. func (it *InstanceIterator) bufLen() int {
  522. return len(it.items)
  523. }
  524. func (it *InstanceIterator) takeBuf() interface{} {
  525. b := it.items
  526. it.items = nil
  527. return b
  528. }
  529. // CreateInstanceOperation manages a long-running operation from CreateInstance.
  530. type CreateInstanceOperation struct {
  531. lro *longrunning.Operation
  532. }
  533. // CreateInstanceOperation returns a new CreateInstanceOperation from a given name.
  534. // The name must be that of a previously created CreateInstanceOperation, possibly from a different process.
  535. func (c *InstanceAdminClient) CreateInstanceOperation(name string) *CreateInstanceOperation {
  536. return &CreateInstanceOperation{
  537. lro: longrunning.InternalNewOperation(c.LROClient, &longrunningpb.Operation{Name: name}),
  538. }
  539. }
  540. // Wait blocks until the long-running operation is completed, returning the response and any errors encountered.
  541. //
  542. // See documentation of Poll for error-handling information.
  543. func (op *CreateInstanceOperation) Wait(ctx context.Context, opts ...gax.CallOption) (*instancepb.Instance, error) {
  544. var resp instancepb.Instance
  545. if err := op.lro.WaitWithInterval(ctx, &resp, 45000*time.Millisecond, opts...); err != nil {
  546. return nil, err
  547. }
  548. return &resp, nil
  549. }
  550. // Poll fetches the latest state of the long-running operation.
  551. //
  552. // Poll also fetches the latest metadata, which can be retrieved by Metadata.
  553. //
  554. // If Poll fails, the error is returned and op is unmodified. If Poll succeeds and
  555. // the operation has completed with failure, the error is returned and op.Done will return true.
  556. // If Poll succeeds and the operation has completed successfully,
  557. // op.Done will return true, and the response of the operation is returned.
  558. // If Poll succeeds and the operation has not completed, the returned response and error are both nil.
  559. func (op *CreateInstanceOperation) Poll(ctx context.Context, opts ...gax.CallOption) (*instancepb.Instance, error) {
  560. var resp instancepb.Instance
  561. if err := op.lro.Poll(ctx, &resp, opts...); err != nil {
  562. return nil, err
  563. }
  564. if !op.Done() {
  565. return nil, nil
  566. }
  567. return &resp, nil
  568. }
  569. // Metadata returns metadata associated with the long-running operation.
  570. // Metadata itself does not contact the server, but Poll does.
  571. // To get the latest metadata, call this method after a successful call to Poll.
  572. // If the metadata is not available, the returned metadata and error are both nil.
  573. func (op *CreateInstanceOperation) Metadata() (*instancepb.CreateInstanceMetadata, error) {
  574. var meta instancepb.CreateInstanceMetadata
  575. if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata {
  576. return nil, nil
  577. } else if err != nil {
  578. return nil, err
  579. }
  580. return &meta, nil
  581. }
  582. // Done reports whether the long-running operation has completed.
  583. func (op *CreateInstanceOperation) Done() bool {
  584. return op.lro.Done()
  585. }
  586. // Name returns the name of the long-running operation.
  587. // The name is assigned by the server and is unique within the service from which the operation is created.
  588. func (op *CreateInstanceOperation) Name() string {
  589. return op.lro.Name()
  590. }
  591. // UpdateInstanceOperation manages a long-running operation from UpdateInstance.
  592. type UpdateInstanceOperation struct {
  593. lro *longrunning.Operation
  594. }
  595. // UpdateInstanceOperation returns a new UpdateInstanceOperation from a given name.
  596. // The name must be that of a previously created UpdateInstanceOperation, possibly from a different process.
  597. func (c *InstanceAdminClient) UpdateInstanceOperation(name string) *UpdateInstanceOperation {
  598. return &UpdateInstanceOperation{
  599. lro: longrunning.InternalNewOperation(c.LROClient, &longrunningpb.Operation{Name: name}),
  600. }
  601. }
  602. // Wait blocks until the long-running operation is completed, returning the response and any errors encountered.
  603. //
  604. // See documentation of Poll for error-handling information.
  605. func (op *UpdateInstanceOperation) Wait(ctx context.Context, opts ...gax.CallOption) (*instancepb.Instance, error) {
  606. var resp instancepb.Instance
  607. if err := op.lro.WaitWithInterval(ctx, &resp, 45000*time.Millisecond, opts...); err != nil {
  608. return nil, err
  609. }
  610. return &resp, nil
  611. }
  612. // Poll fetches the latest state of the long-running operation.
  613. //
  614. // Poll also fetches the latest metadata, which can be retrieved by Metadata.
  615. //
  616. // If Poll fails, the error is returned and op is unmodified. If Poll succeeds and
  617. // the operation has completed with failure, the error is returned and op.Done will return true.
  618. // If Poll succeeds and the operation has completed successfully,
  619. // op.Done will return true, and the response of the operation is returned.
  620. // If Poll succeeds and the operation has not completed, the returned response and error are both nil.
  621. func (op *UpdateInstanceOperation) Poll(ctx context.Context, opts ...gax.CallOption) (*instancepb.Instance, error) {
  622. var resp instancepb.Instance
  623. if err := op.lro.Poll(ctx, &resp, opts...); err != nil {
  624. return nil, err
  625. }
  626. if !op.Done() {
  627. return nil, nil
  628. }
  629. return &resp, nil
  630. }
  631. // Metadata returns metadata associated with the long-running operation.
  632. // Metadata itself does not contact the server, but Poll does.
  633. // To get the latest metadata, call this method after a successful call to Poll.
  634. // If the metadata is not available, the returned metadata and error are both nil.
  635. func (op *UpdateInstanceOperation) Metadata() (*instancepb.UpdateInstanceMetadata, error) {
  636. var meta instancepb.UpdateInstanceMetadata
  637. if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata {
  638. return nil, nil
  639. } else if err != nil {
  640. return nil, err
  641. }
  642. return &meta, nil
  643. }
  644. // Done reports whether the long-running operation has completed.
  645. func (op *UpdateInstanceOperation) Done() bool {
  646. return op.lro.Done()
  647. }
  648. // Name returns the name of the long-running operation.
  649. // The name is assigned by the server and is unique within the service from which the operation is created.
  650. func (op *UpdateInstanceOperation) Name() string {
  651. return op.lro.Name()
  652. }