google-cloud-bigtable 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (88) hide show
  1. checksums.yaml +7 -0
  2. data/.yardopts +10 -0
  3. data/LICENSE +201 -0
  4. data/README.md +65 -0
  5. data/lib/google/bigtable/admin/v2/bigtable_instance_admin_pb.rb +139 -0
  6. data/lib/google/bigtable/admin/v2/bigtable_instance_admin_services_pb.rb +85 -0
  7. data/lib/google/bigtable/admin/v2/bigtable_table_admin_pb.rb +137 -0
  8. data/lib/google/bigtable/admin/v2/bigtable_table_admin_services_pb.rb +117 -0
  9. data/lib/google/bigtable/admin/v2/common_pb.rb +24 -0
  10. data/lib/google/bigtable/admin/v2/instance_pb.rb +72 -0
  11. data/lib/google/bigtable/admin/v2/table_pb.rb +88 -0
  12. data/lib/google/bigtable/v2/bigtable_pb.rb +109 -0
  13. data/lib/google/bigtable/v2/bigtable_services_pb.rb +67 -0
  14. data/lib/google/bigtable/v2/data_pb.rb +155 -0
  15. data/lib/google/cloud/bigtable/admin/credentials.rb +26 -0
  16. data/lib/google/cloud/bigtable/admin/v2/bigtable_instance_admin_client.rb +1417 -0
  17. data/lib/google/cloud/bigtable/admin/v2/bigtable_instance_admin_client_config.json +123 -0
  18. data/lib/google/cloud/bigtable/admin/v2/bigtable_table_admin_client.rb +1079 -0
  19. data/lib/google/cloud/bigtable/admin/v2/bigtable_table_admin_client_config.json +109 -0
  20. data/lib/google/cloud/bigtable/admin/v2/credentials.rb +50 -0
  21. data/lib/google/cloud/bigtable/admin/v2/doc/google/bigtable/admin/v2/bigtable_instance_admin.rb +279 -0
  22. data/lib/google/cloud/bigtable/admin/v2/doc/google/bigtable/admin/v2/bigtable_table_admin.rb +353 -0
  23. data/lib/google/cloud/bigtable/admin/v2/doc/google/bigtable/admin/v2/instance.rb +194 -0
  24. data/lib/google/cloud/bigtable/admin/v2/doc/google/bigtable/admin/v2/table.rb +209 -0
  25. data/lib/google/cloud/bigtable/admin/v2/doc/google/iam/v1/iam_policy.rb +62 -0
  26. data/lib/google/cloud/bigtable/admin/v2/doc/google/iam/v1/policy.rb +127 -0
  27. data/lib/google/cloud/bigtable/admin/v2/doc/google/longrunning/operations.rb +92 -0
  28. data/lib/google/cloud/bigtable/admin/v2/doc/google/protobuf/any.rb +124 -0
  29. data/lib/google/cloud/bigtable/admin/v2/doc/google/protobuf/duration.rb +90 -0
  30. data/lib/google/cloud/bigtable/admin/v2/doc/google/protobuf/empty.rb +28 -0
  31. data/lib/google/cloud/bigtable/admin/v2/doc/google/protobuf/field_mask.rb +223 -0
  32. data/lib/google/cloud/bigtable/admin/v2/doc/google/protobuf/timestamp.rb +106 -0
  33. data/lib/google/cloud/bigtable/admin/v2/doc/google/rpc/status.rb +83 -0
  34. data/lib/google/cloud/bigtable/admin/v2.rb +200 -0
  35. data/lib/google/cloud/bigtable/admin.rb +196 -0
  36. data/lib/google/cloud/bigtable/app_profile/job.rb +102 -0
  37. data/lib/google/cloud/bigtable/app_profile/list.rb +159 -0
  38. data/lib/google/cloud/bigtable/app_profile.rb +373 -0
  39. data/lib/google/cloud/bigtable/chunk_processor.rb +253 -0
  40. data/lib/google/cloud/bigtable/cluster/job.rb +92 -0
  41. data/lib/google/cloud/bigtable/cluster/list.rb +169 -0
  42. data/lib/google/cloud/bigtable/cluster.rb +264 -0
  43. data/lib/google/cloud/bigtable/column_family.rb +280 -0
  44. data/lib/google/cloud/bigtable/column_range.rb +186 -0
  45. data/lib/google/cloud/bigtable/convert.rb +75 -0
  46. data/lib/google/cloud/bigtable/credentials.rb +24 -0
  47. data/lib/google/cloud/bigtable/errors.rb +35 -0
  48. data/lib/google/cloud/bigtable/gc_rule.rb +215 -0
  49. data/lib/google/cloud/bigtable/instance/cluster_map.rb +70 -0
  50. data/lib/google/cloud/bigtable/instance/job.rb +97 -0
  51. data/lib/google/cloud/bigtable/instance/list.rb +159 -0
  52. data/lib/google/cloud/bigtable/instance.rb +921 -0
  53. data/lib/google/cloud/bigtable/longrunning_job.rb +105 -0
  54. data/lib/google/cloud/bigtable/mutation_entry.rb +244 -0
  55. data/lib/google/cloud/bigtable/mutation_operations.rb +338 -0
  56. data/lib/google/cloud/bigtable/policy.rb +163 -0
  57. data/lib/google/cloud/bigtable/project.rb +580 -0
  58. data/lib/google/cloud/bigtable/read_modify_write_rule.rb +129 -0
  59. data/lib/google/cloud/bigtable/read_operations.rb +345 -0
  60. data/lib/google/cloud/bigtable/row.rb +125 -0
  61. data/lib/google/cloud/bigtable/row_filter/chain_filter.rb +539 -0
  62. data/lib/google/cloud/bigtable/row_filter/condition_filter.rb +108 -0
  63. data/lib/google/cloud/bigtable/row_filter/interleave_filter.rb +570 -0
  64. data/lib/google/cloud/bigtable/row_filter/simple_filter.rb +273 -0
  65. data/lib/google/cloud/bigtable/row_filter.rb +593 -0
  66. data/lib/google/cloud/bigtable/row_range.rb +174 -0
  67. data/lib/google/cloud/bigtable/rows_mutator.rb +120 -0
  68. data/lib/google/cloud/bigtable/rows_reader.rb +196 -0
  69. data/lib/google/cloud/bigtable/sample_row_key.rb +82 -0
  70. data/lib/google/cloud/bigtable/service.rb +817 -0
  71. data/lib/google/cloud/bigtable/table/cluster_state.rb +93 -0
  72. data/lib/google/cloud/bigtable/table/column_family_map.rb +68 -0
  73. data/lib/google/cloud/bigtable/table/list.rb +147 -0
  74. data/lib/google/cloud/bigtable/table.rb +676 -0
  75. data/lib/google/cloud/bigtable/v2/bigtable_client.rb +579 -0
  76. data/lib/google/cloud/bigtable/v2/bigtable_client_config.json +65 -0
  77. data/lib/google/cloud/bigtable/v2/credentials.rb +45 -0
  78. data/lib/google/cloud/bigtable/v2/doc/google/bigtable/v2/bigtable.rb +286 -0
  79. data/lib/google/cloud/bigtable/v2/doc/google/bigtable/v2/data.rb +492 -0
  80. data/lib/google/cloud/bigtable/v2/doc/google/protobuf/any.rb +124 -0
  81. data/lib/google/cloud/bigtable/v2/doc/google/protobuf/wrappers.rb +89 -0
  82. data/lib/google/cloud/bigtable/v2/doc/google/rpc/status.rb +83 -0
  83. data/lib/google/cloud/bigtable/v2.rb +132 -0
  84. data/lib/google/cloud/bigtable/value_range.rb +175 -0
  85. data/lib/google/cloud/bigtable/version.rb +22 -0
  86. data/lib/google/cloud/bigtable.rb +223 -0
  87. data/lib/google-cloud-bigtable.rb +167 -0
  88. metadata +283 -0
@@ -0,0 +1,163 @@
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
+
15
+
16
+ module Google
17
+ module Cloud
18
+ module Bigtable
19
+ # # Policy
20
+ #
21
+ # Represents a Cloud IAM Policy for the Bigtable instance resources.
22
+ #
23
+ # A common pattern for updating a resource's metadata, such as its Policy,
24
+ # is to read the current data from the service, update the data locally,
25
+ # and then send the modified data for writing. This pattern may result in
26
+ # a conflict if two or more processes attempt the sequence simultaneously.
27
+ # IAM solves this problem with the {Google::Cloud::Bigtable::Policy#etag}
28
+ # property, which is used to verify whether the policy has changed since
29
+ # the last request. When you make a request to with an `etag` value, Cloud
30
+ # IAM compares the `etag` value in the request with the existing `etag`
31
+ # value associated with the policy. It writes the policy only if the
32
+ # `etag` values match.
33
+ #
34
+ # @see https://cloud.google.com/bigtable/docs/access-control Permissions and roles
35
+ #
36
+ # @attr [String] etag Used to verify whether the policy has changed since
37
+ # the last request. The policy will be written only if the `etag` values
38
+ # match.
39
+ # @attr [Hash{String => Array<String>}] roles The bindings that associate
40
+ # roles with an array of members. See [Understanding
41
+ # Roles](https://cloud.google.com/iam/docs/understanding-roles) for a
42
+ # listing of primitive and curated roles.
43
+ #
44
+ # @example
45
+ # require "google/cloud/bigtable"
46
+ #
47
+ # bigtable = Google::Cloud::Bigtable.new
48
+ # instance = bigtable.instance "my-instance"
49
+ #
50
+ # policy = instance.policy
51
+ # policy.remove("roles/owner", "user:owner@example.com")
52
+ # policy.add("roles/owner", "user:newowner@example.com")
53
+ # policy.roles["roles/viewer"] = ["allUsers"]
54
+ #
55
+ class Policy
56
+ attr_reader :etag, :roles
57
+
58
+ # Creates a Policy instance.
59
+ # @param etag [String]
60
+ # @param roles [Array<String>]
61
+ def initialize etag, roles = nil
62
+ @etag = etag
63
+ @roles = roles
64
+ end
65
+
66
+ # Convenience method for adding a member to a binding on this policy.
67
+ # See [Understanding
68
+ # Roles](https://cloud.google.com/iam/docs/understanding-roles) for a
69
+ # listing of primitive and curated roles. See
70
+ # [Binding](https://cloud.google.com/bigtable/docs/access-control)
71
+ # for a listing of values and patterns for members.
72
+ #
73
+ # @param [String] role_name A Cloud IAM role, such as
74
+ # `"roles/bigtable.admin"`.
75
+ # @param [String] member A Cloud IAM identity, such as
76
+ # `"user:owner@example.com"`.
77
+ #
78
+ # @example
79
+ # require "google/cloud/bigtable"
80
+ #
81
+ # bigtable = Google::Cloud::Bigtable.new
82
+ # instance = bigtable.instance "my-instance"
83
+ #
84
+ # policy = instance.policy
85
+ # policy.add("roles/owner", "user:newowner@example.com")
86
+ #
87
+ def add role_name, member
88
+ role(role_name) << member
89
+ end
90
+
91
+ # Convenience method for removing a member from a binding on this
92
+ # policy. See [Understanding
93
+ # Roles](https://cloud.google.com/iam/docs/understanding-roles) for a
94
+ # listing of primitive and curated roles.See
95
+ # [Binding](https://cloud.google.com/bigtable/docs/access-control)
96
+ # for a listing of values and patterns for members.
97
+ #
98
+ # @param [String] role_name A Cloud IAM role, such as
99
+ # `"roles/Bigtable.admin"`.
100
+ # @param [String] member A Cloud IAM identity, such as
101
+ # `"user:owner@example.com"`.
102
+ #
103
+ # @example
104
+ # require "google/cloud/Bigtable"
105
+ #
106
+ # bigtable = Google::Cloud::Bigtable.new
107
+ # instance = bigtable.instance "my-instance"
108
+ #
109
+ # policy = instance.policy
110
+ # policy.remove("roles/owner", "user:newowner@example.com")
111
+ #
112
+ def remove role_name, member
113
+ role(role_name).delete member
114
+ end
115
+
116
+ # Convenience method returning the array of members bound to a role in
117
+ # this policy, or an empty array if no value is present for the role in
118
+ # {#roles}. See [Understanding
119
+ # Roles](https://cloud.google.com/iam/docs/understanding-roles) for a
120
+ # listing of primitive and curated roles. See
121
+ # [Binding](https://cloud.google.com/bigtable/docs/access-control)
122
+ # for a listing of values and patterns for members.
123
+ #
124
+ # @return [Array<String>] The members strings, or an empty array.
125
+ #
126
+ # @example
127
+ # require "google/cloud/Bigtable"
128
+ #
129
+ # bigtable = Google::Cloud::Bigtable.new
130
+ # instance = bigtable.instance "my-instance"
131
+ #
132
+ # policy = instance.policy
133
+ # policy.role("roles/viewer") << "user:viewer@example.com"
134
+ #
135
+ def role role_name
136
+ roles[role_name] ||= []
137
+ end
138
+
139
+ # @private
140
+ # Convert the Policy to a Google::Iam::V1::Policy object.
141
+ def to_grpc
142
+ bindings = roles.keys.map do |role_name|
143
+ next if roles[role_name].empty?
144
+ Google::Iam::V1::Binding.new(
145
+ role: role_name,
146
+ members: roles[role_name]
147
+ )
148
+ end
149
+ Google::Iam::V1::Policy.new(etag: etag, bindings: bindings)
150
+ end
151
+
152
+ # @private
153
+ # New Policy from a Google::Iam::V1::Policy object.
154
+ def self.from_grpc grpc
155
+ roles = grpc.bindings.each_with_object({}) do |binding, memo|
156
+ memo[binding.role] = binding.members.to_a
157
+ end
158
+ new(grpc.etag, roles)
159
+ end
160
+ end
161
+ end
162
+ end
163
+ end
@@ -0,0 +1,580 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2018 Google LLC
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # https://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+
18
+ require "google/cloud/bigtable/errors"
19
+ require "google/cloud/bigtable/longrunning_job"
20
+ require "google/cloud/bigtable/convert"
21
+ require "google/cloud/bigtable/service"
22
+ require "google/cloud/bigtable/instance"
23
+ require "google/cloud/bigtable/cluster"
24
+ require "google/cloud/bigtable/table"
25
+
26
+ module Google
27
+ module Cloud
28
+ module Bigtable
29
+ # # Project
30
+ #
31
+ # Projects are top-level containers in Google Cloud Platform. They store
32
+ # information about billing and authorized users, and they contain
33
+ # Cloud Bigtable data. Each project has a friendly name and a unique ID.
34
+ #
35
+ # Google::Cloud::Bigtable::Project is the main object for interacting with
36
+ # Cloud Bigtable.
37
+ #
38
+ # {Google::Cloud::Bigtable::Cluster}, {Google::Cloud::Bigtable::Instance}
39
+ # objects are created, accessed, and managed by Google::Cloud::Bigtable::Project.
40
+ #
41
+ # See {Google::Cloud::Bigtable.new} and {Google::Cloud#bigtable}.
42
+ #
43
+ # @example Obtaining an instance and the clusters from a project.
44
+ # require "google/cloud"
45
+ #
46
+ # bigtable = Google::Cloud::Bigtable.new
47
+ #
48
+ # instance = bigtable.instance("my-instance")
49
+ # clusters = bigtable.clusters # All clusters in the project
50
+ #
51
+ class Project
52
+ # @private
53
+ # The Service object
54
+ attr_accessor :service
55
+
56
+ # @private
57
+ # Creates a new Bigtable Project instance.
58
+ # @param service [Google::Cloud::Bigtable::Service]
59
+
60
+ def initialize service
61
+ @service = service
62
+ end
63
+
64
+ # The identifier for the Cloud Bigtable project.
65
+ #
66
+ # @return [String] Project id.
67
+ #
68
+ # @example
69
+ # require "google/cloud"
70
+ #
71
+ # bigtable = Google::Cloud::Bigtable.new(
72
+ # project_id: "my-project",
73
+ # credentials: "/path/to/keyfile.json"
74
+ # )
75
+ #
76
+ # bigtable.project_id #=> "my-project"
77
+
78
+ def project_id
79
+ ensure_service!
80
+ service.project_id
81
+ end
82
+
83
+ # Retrieves the list of Bigtable instances for the project.
84
+ #
85
+ # @param token [String] The `token` value returned by the last call to
86
+ # `instances`; indicates that this is a continuation of a call,
87
+ # and that the system should return the next page of data.
88
+ # @return [Array<Google::Cloud::Bigtable::Instance>] The list of instances.
89
+ # (See {Google::Cloud::Bigtable::Instance::List})
90
+ #
91
+ # @example
92
+ # require "google/cloud/bigtable"
93
+ #
94
+ # bigtable = Google::Cloud::Bigtable.new
95
+ #
96
+ # instances = bigtable.instances
97
+ # instances.all do |instance|
98
+ # puts instance.instance_id
99
+ # end
100
+
101
+ def instances token: nil
102
+ ensure_service!
103
+ grpc = service.list_instances(token: token)
104
+ Instance::List.from_grpc(grpc, service)
105
+ end
106
+
107
+ # Get existing Bigtable instance.
108
+ #
109
+ # @param instance_id [String] Existing instance id.
110
+ # @return [Google::Cloud::Bigtable::Instance, nil]
111
+ #
112
+ # @example
113
+ # require "google/cloud/bigtable"
114
+ #
115
+ # bigtable = Google::Cloud::Bigtable.new
116
+ #
117
+ # instance = bigtable.instance("my-instance")
118
+ #
119
+ # if instance
120
+ # puts instance.instance_id
121
+ # end
122
+
123
+ def instance instance_id
124
+ ensure_service!
125
+ grpc = service.get_instance(instance_id)
126
+ Instance.from_grpc(grpc, service)
127
+ rescue Google::Cloud::NotFoundError
128
+ nil
129
+ end
130
+
131
+ # Create a Bigtable instance.
132
+ #
133
+ # @see https://cloud.google.com/compute/docs/regions-zones for cluster zone locations
134
+ #
135
+ # @param instance_id [String] The unique identifier for the instance,
136
+ # which cannot be changed after the instance is created. Values are of
137
+ # the form `[a-z][-a-z0-9]*[a-z0-9]` and must be between 6 and 30
138
+ # characters in length. Required.
139
+ # @param display_name [String] The descriptive name for this instance as it
140
+ # appears in UIs. Must be unique per project and between 4 and 30
141
+ # characters in length.
142
+ # @param type [Symbol] The type of the instance.
143
+ # Valid values are `:DEVELOPMENT` or `:PRODUCTION`.
144
+ # Default `:PRODUCTION` instance will created if left blank.
145
+ # @param labels [Hash{String=>String}] labels Cloud Labels are a flexible and lightweight
146
+ # mechanism for organizing cloud resources into groups that reflect a
147
+ # customer's organizational needs and deployment strategies. Cloud
148
+ # Labels can be used to filter collections of resources. They can be
149
+ # used to control how resource metrics are aggregated. And they can be
150
+ # used as arguments to policy management rules (e.g. route, firewall,
151
+ # load balancing, etc.).
152
+ #
153
+ # * Label keys must be between 1 and 63 characters long and must
154
+ # conform to the following regular expression:
155
+ # `[a-z]([-a-z0-9]*[a-z0-9])?`.
156
+ # * Label values must be between 0 and 63 characters long and must
157
+ # conform to the regular expression `([a-z]([-a-z0-9]*[a-z0-9])?)?`.
158
+ # * No more than 64 labels can be associated with a given resource.
159
+ # @param clusters [Hash{String => Google::Cloud::Bigtable::Cluster}]
160
+ # (See {Google::Cloud::Bigtable::Instance::ClusterMap})
161
+ # If passed as an empty use code block to add clusters.
162
+ # Minimum one cluster must be specified.
163
+ # @yield [clusters] A block for adding clusters.
164
+ # @yieldparam [Hash{String => Google::Cloud::Bigtable::Cluster}]
165
+ # Cluster map of cluster name and cluster object.
166
+ # (See {Google::Cloud::Bigtable::Instance::ClusterMap})
167
+ # @return [Google::Cloud::Bigtable::Instance::Job]
168
+ # The job representing the long-running, asynchronous processing of
169
+ # an instance create operation.
170
+ #
171
+ # @example Create development instance.
172
+ # require "google/cloud/bigtable"
173
+ #
174
+ # bigtable = Google::Cloud::Bigtable.new
175
+ #
176
+ # job = bigtable.create_instance(
177
+ # "my-instance",
178
+ # display_name: "Instance for user data",
179
+ # type: :DEVELOPMENT,
180
+ # labels: { "env" => "dev"}
181
+ # ) do |clusters|
182
+ # clusters.add("test-cluster", "us-east1-b", nodes: 1)
183
+ # end
184
+ #
185
+ # job.done? #=> false
186
+ #
187
+ # # Reload job until completion.
188
+ # job.wait_until_done
189
+ # job.done? #=> true
190
+ #
191
+ # if job.error?
192
+ # status = job.error
193
+ # else
194
+ # instance = job.instance
195
+ # end
196
+ #
197
+ # @example Create production instance.
198
+ # require "google/cloud/bigtable"
199
+ #
200
+ # bigtable = Google::Cloud::Bigtable.new
201
+ #
202
+ # job = bigtable.create_instance(
203
+ # "my-instance",
204
+ # display_name: "Instance for user data",
205
+ # labels: { "env" => "dev"}
206
+ # ) do |clusters|
207
+ # clusters.add("test-cluster", "us-east1-b", nodes: 3, storage_type: :SSD)
208
+ # end
209
+ #
210
+ # job.done? #=> false
211
+ #
212
+ # # To block until the operation completes.
213
+ # job.wait_until_done
214
+ # job.done? #=> true
215
+ #
216
+ # if job.error?
217
+ # status = job.error
218
+ # else
219
+ # instance = job.instance
220
+ # end
221
+
222
+ def create_instance \
223
+ instance_id,
224
+ display_name: nil,
225
+ type: nil,
226
+ labels: nil,
227
+ clusters: nil
228
+ labels = Hash[labels.map { |k, v| [String(k), String(v)] }] if labels
229
+
230
+ instance_attrs = {
231
+ display_name: display_name,
232
+ type: type,
233
+ labels: labels
234
+ }.delete_if { |_, v| v.nil? }
235
+ instance = Google::Bigtable::Admin::V2::Instance.new(instance_attrs)
236
+ clusters ||= Instance::ClusterMap.new
237
+ yield clusters if block_given?
238
+
239
+ clusters.each_value do |cluster|
240
+ unless cluster.location == "".freeze
241
+ cluster.location = service.location_path(cluster.location)
242
+ end
243
+ end
244
+
245
+ grpc = service.create_instance(
246
+ instance_id,
247
+ instance,
248
+ clusters.to_h
249
+ )
250
+ Instance::Job.from_grpc(grpc, service)
251
+ end
252
+
253
+ # List all clusters in project.
254
+ #
255
+ # @param token [String] The `token` value returned by the last call to
256
+ # `clusters`; indicates that this is a continuation of a call,
257
+ # and that the system should return the next page of data.
258
+ # @return [Array<Google::Cloud::Bigtable::Cluster>]
259
+ # (See {Google::Cloud::Bigtable::Cluster::List})
260
+ # @example
261
+ # require "google/cloud/bigtable"
262
+ #
263
+ # bigtable = Google::Cloud::Bigtable.new
264
+ #
265
+ # bigtable.clusters.all do |cluster|
266
+ # puts cluster.cluster_id
267
+ # puts cluster.ready?
268
+ # end
269
+
270
+ def clusters token: nil
271
+ ensure_service!
272
+ grpc = service.list_clusters("-", token: token)
273
+ Cluster::List.from_grpc(grpc, service, instance_id: "-")
274
+ end
275
+
276
+ # List all tables for given instance.
277
+ #
278
+ # @param instance_id [String] Existing instance Id.
279
+ # @return [Array<Google::Cloud::Bigtable::Table>]
280
+ # (See {Google::Cloud::Bigtable::Table::List})
281
+ #
282
+ # @example Get tables
283
+ # require "google/cloud/bigtable"
284
+ #
285
+ # bigtable = Google::Cloud::Bigtable.new
286
+ #
287
+ # bigtable.tables("my-instance").all do |table|
288
+ # puts table.name
289
+ # puts table.column_families
290
+ # end
291
+
292
+ def tables instance_id
293
+ ensure_service!
294
+ grpc = service.list_tables(instance_id)
295
+ Table::List.from_grpc(grpc, service)
296
+ end
297
+
298
+ # Get table information.
299
+ #
300
+ #
301
+ # @param instance_id [String] Existing instance Id.
302
+ # @param table_id [String] Existing table Id.
303
+ # @param view [Symbol] Optional. Table view type. Default `:SCHEMA_VIEW`
304
+ # Valid view types are.
305
+ # * `:NAME_ONLY` - Only populates `name`
306
+ # * `:SCHEMA_VIEW` - Only populates `name` and fields related to the table's schema
307
+ # * `:REPLICATION_VIEW` - Only populates `name` and fields related to the table's replication state.
308
+ # * `:FULL` - Populates all fields
309
+ # @param perform_lookup [Boolean]
310
+ # Get table object without verifying that the table resource exists.
311
+ # Calls made on this object will raise errors if the table does not exist.
312
+ # Default value is `false`. Optional.
313
+ # It helps to reduce admin apis calls.
314
+ # @param app_profile_id [String] The unique identifier for the app profile. Optional.
315
+ # It is used only in data operations.
316
+ # This value specifies routing for replication. If not specified, the
317
+ # "default" application profile will be used.
318
+ # @return [Google::Cloud::Bigtable::Table, nil]
319
+ #
320
+ # @example Get table with schema only view
321
+ # require "google/cloud/bigtable"
322
+ #
323
+ # bigtable = Google::Cloud::Bigtable.new
324
+ #
325
+ # table = bigtable.table("my-instance", "my-table", perform_lookup: true, view: :SCHEMA_VIEW)
326
+ # if table
327
+ # p table.name
328
+ # p table.column_families
329
+ # end
330
+ #
331
+ # @example Get table object without calling get table admin api.
332
+ # require "google/cloud/bigtable"
333
+ #
334
+ # bigtable = Google::Cloud::Bigtable.new
335
+ #
336
+ # table = bigtable.table("my-instance", "my-table")
337
+ #
338
+ # @example Get table with all fields. Clusters states, column families
339
+ # require "google/cloud/bigtable"
340
+ #
341
+ # bigtable = Google::Cloud::Bigtable.new
342
+ #
343
+ # table = bigtable.table("my-instance", "my-table", view: :FULL, perform_lookup: true)
344
+ # iftruee
345
+ # puts table.name
346
+ # p table.column_families
347
+ # p table.cluster_states
348
+ # end
349
+ #
350
+ # @example Mutate rows
351
+ # require "google/cloud/bigtable"
352
+ #
353
+ # bigtable = Google::Cloud::Bigtable.new
354
+ #
355
+ # table = bigtable.table("my-instance", "my-table")
356
+ #
357
+ # entry = table.new_mutation_entry("user-1")
358
+ # entry.set_cell(
359
+ # "cf-1",
360
+ # "field-1",
361
+ # "XYZ"
362
+ # timestamp: Time.now.to_i * 1000 # Time stamp in milli seconds.
363
+ # ).delete_from_column("cf2", "field02")
364
+ #
365
+ # table.mutate_row(entry)
366
+ # @example Read rows using app profile routing
367
+ # require "google/cloud/bigtable"
368
+ #
369
+ # bigtable = Google::Cloud::Bigtable.new
370
+ #
371
+ # table = bigtable.table("my-instance", "my-table", app_profile_id: "my-app-profile")
372
+ #
373
+ # table.read_rows(limit: 5).each do |row|
374
+ # row
375
+ # end
376
+
377
+ def table \
378
+ instance_id,
379
+ table_id,
380
+ view: nil,
381
+ perform_lookup: nil,
382
+ app_profile_id: nil
383
+ ensure_service!
384
+
385
+ table = if perform_lookup
386
+ grpc = service.get_table(instance_id, table_id, view: view)
387
+ Table.from_grpc(grpc, service, view: view)
388
+ else
389
+ Table.from_path(
390
+ service.table_path(instance_id, table_id),
391
+ service
392
+ )
393
+ end
394
+
395
+ table.app_profile_id = app_profile_id
396
+ table
397
+ rescue Google::Cloud::NotFoundError
398
+ nil
399
+ end
400
+
401
+ # Creates a new table in the specified instance.
402
+ # The table can be created with a full set of initial column families,
403
+ # specified in the request.
404
+ #
405
+ # @param instance_id [String]
406
+ # The unique Id of the instance in which to create the table.
407
+ # @param table_id [String]
408
+ # The name by which the new table should be referred to within the parent
409
+ # instance, e.g., +foobar+
410
+ # @param column_families [Hash{String => Google::Cloud::Bigtable::ColumnFamily}]
411
+ # (See {Google::Cloud::Bigtable::Table::ColumnFamilyMap})
412
+ # If passed as an empty use code block to add column families.
413
+ # @param granularity [Symbol]
414
+ # The granularity at which timestamps are stored in this table.
415
+ # Timestamps not matching the granularity will be rejected.
416
+ # Valid values are `:MILLIS`.
417
+ # If unspecified, the value will be set to `:MILLIS`
418
+ # @param initial_splits [Array<String>]
419
+ # The optional list of row keys that will be used to initially split the
420
+ # table into several tablets (tablets are similar to HBase regions).
421
+ # Given two split keys, +s1+ and +s2+, three tablets will be created,
422
+ # spanning the key ranges: +[, s1), [s1, s2), [s2, )+.
423
+ #
424
+ # Example:
425
+ #
426
+ # * Row keys := ["a", "apple", "custom", "customer_1", "customer_2",+
427
+ # +"other", "zz"]
428
+ # * initial_split_keys := +["apple", "customer_1", "customer_2", "other"]+
429
+ # * Key assignment:
430
+ # * Tablet 1 : +[, apple) => {"a"}.+
431
+ # * Tablet 2 : +[apple, customer_1) => {"apple", "custom"}.+
432
+ # * Tablet 3 : +[customer_1, customer_2) => {"customer_1"}.+
433
+ # * Tablet 4 : +[customer_2, other) => {"customer_2"}.+
434
+ # * Tablet 5 : +[other, ) => {"other", "zz"}.+
435
+ # A hash of the same form as `Google::Bigtable::Admin::V2::CreateTableRequest::Split`
436
+ # can also be provided.
437
+ # @yield [column_families] A block for adding column_families.
438
+ # @yieldparam [Hash{String => Google::Cloud::Bigtable::ColumnFamily}]
439
+ # Map of family name and column family object.
440
+ # (See {Google::Cloud::Bigtable::Instance::ColumnFamilyMap})
441
+ # GC Rules for column family see {Google::Cloud::Bigtable::GcRule})
442
+ #
443
+ # @return [Google::Cloud::Bigtable::Table]
444
+ #
445
+ # @example Create table without column family
446
+ # require "google/cloud/bigtable"
447
+ #
448
+ # bigtable = Google::Cloud::Bigtable.new
449
+ #
450
+ # table = bigtable.create_table("my-instance", "my-table")
451
+ # puts table.name
452
+ #
453
+ # @example Create table with column families and initial splits.
454
+ # require "google/cloud/bigtable"
455
+ #
456
+ # bigtable = Google::Cloud::Bigtable.new
457
+ #
458
+ # initial_splits = ["user-00001", "user-100000", "others"]
459
+ # table = bigtable.create_table("my-instance", "my-table", initial_splits: initial_splits) do |column_families|
460
+ # column_families.add('cf1', Google::Cloud::Bigtable::GcRule.max_versions(5))
461
+ # column_families.add('cf2', Google::Cloud::Bigtable::GcRule.max_age(600))
462
+ #
463
+ # gc_rule = Google::Cloud::Bigtable::GcRule.union(
464
+ # Google::Cloud::Bigtable::GcRule.max_age(1800),
465
+ # Google::Cloud::Bigtable::GcRule.max_versions(3)
466
+ # )
467
+ # column_families.add('cf3', gc_rule)
468
+ # end
469
+ #
470
+ # p table
471
+
472
+ def create_table \
473
+ instance_id,
474
+ table_id,
475
+ column_families: nil,
476
+ granularity: nil,
477
+ initial_splits: nil,
478
+ &block
479
+ ensure_service!
480
+ Table.create(
481
+ service,
482
+ instance_id,
483
+ table_id,
484
+ column_families: column_families,
485
+ granularity: granularity,
486
+ initial_splits: initial_splits,
487
+ &block
488
+ )
489
+ end
490
+
491
+ # Permanently deletes a specified table and all of its data.
492
+ #
493
+ # @param instance_id [String]
494
+ # The unique Id of the instance in which table is exists.
495
+ # @param table_id [String]
496
+ # The unique name of the table to be deleted.
497
+ # e.g., +foobar+
498
+ #
499
+ # @example Create table with column families and initial splits.
500
+ # require "google/cloud/bigtable"
501
+ #
502
+ # bigtable = Google::Cloud::Bigtable.new
503
+ #
504
+ # bigtable.delete_table("my-instance", "my-table")
505
+ #
506
+ def delete_table instance_id, table_id
507
+ service.delete_table(instance_id, table_id)
508
+ true
509
+ end
510
+
511
+ # Performs a series of column family modifications on the specified table.
512
+ # Either all or none of the modifications will occur before this method
513
+ # returns, but data requests received prior to that point may see a table
514
+ # where only some modifications have taken effect.
515
+ #
516
+ # @param instance_id [String]
517
+ # The unique Id of the instance in which table is exists.
518
+ # @param table_id [String]
519
+ # The unique Id of the table whose families should be modified.
520
+ # @param modifications [Array<Google::Bigtable::Admin::V2::ModifyColumnFamiliesRequest::Modification> | Google::Bigtable::Admin::V2::ModifyColumnFamiliesRequest::Modification]
521
+ # Modifications to be atomically applied to the specified table's families.
522
+ # Entries are applied in order, meaning that earlier modifications can be
523
+ # masked by later ones (in the case of repeated updates to the same family,
524
+ # for example).
525
+ # @return [Google::Cloud::Bigtable::Table] Table with updated column families.
526
+ #
527
+ # @example
528
+ # require "google/cloud/bigtable"
529
+ #
530
+ # bigtable = Google::Cloud::Bigtable.new
531
+ #
532
+ # modifications = []
533
+ # modifications << Google::Cloud::Bigtable::ColumnFamily.create_modification(
534
+ # "cf1", Google::Cloud::Bigtable::GcRule.max_age(600))
535
+ # )
536
+ #
537
+ # modifications << Google::Cloud::Bigtable::ColumnFamily.update_modification(
538
+ # "cf2", Google::Cloud::Bigtable::GcRule.max_versions(5)
539
+ # )
540
+ #
541
+ # gc_rule_1 = Google::Cloud::Bigtable::GcRule.max_versions(3)
542
+ # gc_rule_2 = Google::Cloud::Bigtable::GcRule.max_age(600)
543
+ # modifications << Google::Cloud::Bigtable::ColumnFamily.update_modification(
544
+ # "cf3", Google::Cloud::Bigtable::GcRule.union(gc_rule_1, gc_rule_2)
545
+ # )
546
+ #
547
+ # max_age_gc_rule = Google::Cloud::Bigtable::GcRule.max_age(300)
548
+ # modifications << Google::Cloud::Bigtable::ColumnFamily.update_modification(
549
+ # "cf4", Google::Cloud::Bigtable::GcRule.union(max_version_gc_rule)
550
+ # )
551
+ #
552
+ # modifications << Google::Cloud::Bigtable::ColumnFamily.drop_modification("cf5")
553
+ #
554
+ # table = bigtable.modify_column_families("my-instance", "my-table", modifications)
555
+ #
556
+ # p table.column_families
557
+
558
+ def modify_column_families instance_id, table_id, modifications
559
+ ensure_service!
560
+ Table.modify_column_families(
561
+ service,
562
+ instance_id,
563
+ table_id,
564
+ modifications
565
+ )
566
+ end
567
+
568
+ protected
569
+
570
+ # @private
571
+ #
572
+ # Raise an error unless an active connection to the service is
573
+ # available.
574
+ def ensure_service!
575
+ raise "Must have active connection to service" unless service
576
+ end
577
+ end
578
+ end
579
+ end
580
+ end