Class: Google::Cloud::Spanner::Service
- Inherits:
-
Object
- Object
- Google::Cloud::Spanner::Service
- Defined in:
- lib/google/cloud/spanner/service.rb
Overview
methods.
Instance Attribute Summary collapse
-
#client_config ⇒ Object
Returns the value of attribute client_config.
-
#credentials ⇒ Object
Returns the value of attribute credentials.
-
#host ⇒ Object
Returns the value of attribute host.
-
#mocked_databases ⇒ Object
Returns the value of attribute mocked_databases.
-
#mocked_instances ⇒ Object
Returns the value of attribute mocked_instances.
-
#mocked_service ⇒ Object
Returns the value of attribute mocked_service.
-
#project ⇒ Object
Returns the value of attribute project.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
Instance Method Summary collapse
- #begin_transaction(session_name) ⇒ Object
- #chan_creds ⇒ Object
- #channel ⇒ Object
- #commit(session_name, mutations = [], transaction_id: nil) ⇒ Object
- #create_database(instance_id, database_id, statements: []) ⇒ Object
- #create_instance(instance_id, name: nil, config: nil, nodes: nil, labels: nil) ⇒ Object
- #create_session(database_name) ⇒ Object
- #create_snapshot(session_name, strong: nil, timestamp: nil, staleness: nil) ⇒ Object
- #databases ⇒ Object
- #delete_instance(name) ⇒ Object
- #delete_session(session_name) ⇒ Object
- #drop_database(instance_id, database_id) ⇒ Object
- #get_database(instance_id, database_id) ⇒ Object
- #get_database_ddl(instance_id, database_id) ⇒ Object
- #get_database_policy(instance_id, database_id) ⇒ Object
- #get_instance(name) ⇒ Object
- #get_instance_config(name) ⇒ Object
- #get_instance_policy(name) ⇒ Object
- #get_session(session_name) ⇒ Object
-
#initialize(project, credentials, host: nil, timeout: nil, client_config: nil) ⇒ Service
constructor
Creates a new Service instance.
- #insecure? ⇒ Boolean
- #inspect ⇒ Object
- #instances ⇒ Object
- #list_databases(instance_id, token: nil, max: nil) ⇒ Object
- #list_instance_configs(token: nil, max: nil) ⇒ Object
- #list_instances(token: nil, max: nil) ⇒ Object
- #rollback(session_name, transaction_id) ⇒ Object
- #service ⇒ Object
- #set_database_policy(instance_id, database_id, new_policy) ⇒ Object
- #set_instance_policy(name, new_policy) ⇒ Object
- #streaming_execute_sql(session_name, sql, transaction: nil, params: nil, types: nil, resume_token: nil) ⇒ Object
- #streaming_read_table(session_name, table_name, columns, keys: nil, index: nil, transaction: nil, limit: nil, resume_token: nil) ⇒ Object
- #test_database_permissions(instance_id, database_id, permissions) ⇒ Object
- #test_instance_permissions(name, permissions) ⇒ Object
- #update_database_ddl(instance_id, database_id, statements: [], operation_id: nil) ⇒ Object
- #update_instance(instance_obj) ⇒ Object
Constructor Details
#initialize(project, credentials, host: nil, timeout: nil, client_config: nil) ⇒ Service
Creates a new Service instance.
35 36 37 38 39 40 41 42 |
# File 'lib/google/cloud/spanner/service.rb', line 35 def initialize project, credentials, host: nil, timeout: nil, client_config: nil @project = project @credentials = credentials @host = host || V1::SpannerClient::SERVICE_ADDRESS @timeout = timeout @client_config = client_config || {} end |
Instance Attribute Details
#client_config ⇒ Object
Returns the value of attribute client_config.
31 32 33 |
# File 'lib/google/cloud/spanner/service.rb', line 31 def client_config @client_config end |
#credentials ⇒ Object
Returns the value of attribute credentials.
31 32 33 |
# File 'lib/google/cloud/spanner/service.rb', line 31 def credentials @credentials end |
#host ⇒ Object
Returns the value of attribute host.
31 32 33 |
# File 'lib/google/cloud/spanner/service.rb', line 31 def host @host end |
#mocked_databases ⇒ Object
Returns the value of attribute mocked_databases.
93 94 95 |
# File 'lib/google/cloud/spanner/service.rb', line 93 def mocked_databases @mocked_databases end |
#mocked_instances ⇒ Object
Returns the value of attribute mocked_instances.
80 81 82 |
# File 'lib/google/cloud/spanner/service.rb', line 80 def mocked_instances @mocked_instances end |
#mocked_service ⇒ Object
Returns the value of attribute mocked_service.
67 68 69 |
# File 'lib/google/cloud/spanner/service.rb', line 67 def mocked_service @mocked_service end |
#project ⇒ Object
Returns the value of attribute project.
31 32 33 |
# File 'lib/google/cloud/spanner/service.rb', line 31 def project @project end |
#timeout ⇒ Object
Returns the value of attribute timeout.
31 32 33 |
# File 'lib/google/cloud/spanner/service.rb', line 31 def timeout @timeout end |
Instance Method Details
#begin_transaction(session_name) ⇒ Object
330 331 332 333 334 335 336 337 |
# File 'lib/google/cloud/spanner/service.rb', line 330 def begin_transaction session_name tx_opts = Google::Spanner::V1::TransactionOptions.new(read_write: Google::Spanner::V1::TransactionOptions::ReadWrite.new) opts = session_name execute do service.begin_transaction session_name, tx_opts, options: opts end end |
#chan_creds ⇒ Object
49 50 51 52 53 54 |
# File 'lib/google/cloud/spanner/service.rb', line 49 def chan_creds return credentials if insecure? require "grpc" GRPC::Core::ChannelCredentials.new.compose \ GRPC::Core::CallCredentials.new credentials.client.updater_proc end |
#channel ⇒ Object
44 45 46 47 |
# File 'lib/google/cloud/spanner/service.rb', line 44 def channel require "grpc" GRPC::Core::Channel.new host, nil, chan_creds end |
#commit(session_name, mutations = [], transaction_id: nil) ⇒ Object
308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
# File 'lib/google/cloud/spanner/service.rb', line 308 def commit session_name, mutations = [], transaction_id: nil tx_opts = nil if transaction_id.nil? tx_opts = Google::Spanner::V1::TransactionOptions.new(read_write: Google::Spanner::V1::TransactionOptions::ReadWrite.new) end opts = session_name execute do service.commit \ session_name, mutations, transaction_id: transaction_id, single_use_transaction: tx_opts, options: opts end end |
#create_database(instance_id, database_id, statements: []) ⇒ Object
203 204 205 206 207 208 209 210 |
# File 'lib/google/cloud/spanner/service.rb', line 203 def create_database instance_id, database_id, statements: [] execute do databases.create_database \ instance_path(instance_id), "CREATE DATABASE `#{database_id}`", extra_statements: Array(statements) end end |
#create_instance(instance_id, name: nil, config: nil, nodes: nil, labels: nil) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/google/cloud/spanner/service.rb', line 118 def create_instance instance_id, name: nil, config: nil, nodes: nil, labels: nil labels = Hash[labels.map { |k, v| [String(k), String(v)] }] if labels create_obj = Google::Spanner::Admin::Instance::V1::Instance.new({ display_name: name, config: instance_config_path(config), node_count: nodes, labels: labels }.delete_if { |_, v| v.nil? }) execute do instances.create_instance project_path, instance_id, create_obj end end |
#create_session(database_name) ⇒ Object
261 262 263 264 265 266 |
# File 'lib/google/cloud/spanner/service.rb', line 261 def create_session database_name opts = database_name execute do service.create_session database_name, options: opts end end |
#create_snapshot(session_name, strong: nil, timestamp: nil, staleness: nil) ⇒ Object
339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
# File 'lib/google/cloud/spanner/service.rb', line 339 def create_snapshot session_name, strong: nil, timestamp: nil, staleness: nil tx_opts = Google::Spanner::V1::TransactionOptions.new(read_only: Google::Spanner::V1::TransactionOptions::ReadOnly.new({ strong: strong, read_timestamp: Convert.(), exact_staleness: Convert.number_to_duration(staleness), return_read_timestamp: true }.delete_if { |_, v| v.nil? })) opts = session_name execute do service.begin_transaction session_name, tx_opts, options: opts end end |
#databases ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/google/cloud/spanner/service.rb', line 82 def databases return mocked_databases if mocked_databases @databases ||= \ Admin::Database::V1::DatabaseAdminClient.new( service_path: host, channel: channel, timeout: timeout, client_config: client_config, lib_name: "gccl", lib_version: Google::Cloud::Spanner::VERSION) end |
#delete_instance(name) ⇒ Object
141 142 143 144 145 |
# File 'lib/google/cloud/spanner/service.rb', line 141 def delete_instance name execute do instances.delete_instance instance_path(name) end end |
#delete_session(session_name) ⇒ Object
268 269 270 271 272 273 |
# File 'lib/google/cloud/spanner/service.rb', line 268 def delete_session session_name opts = session_name execute do service.delete_session session_name, options: opts end end |
#drop_database(instance_id, database_id) ⇒ Object
212 213 214 215 216 |
# File 'lib/google/cloud/spanner/service.rb', line 212 def drop_database instance_id, database_id execute do databases.drop_database database_path(instance_id, database_id) end end |
#get_database(instance_id, database_id) ⇒ Object
197 198 199 200 201 |
# File 'lib/google/cloud/spanner/service.rb', line 197 def get_database instance_id, database_id execute do databases.get_database database_path(instance_id, database_id) end end |
#get_database_ddl(instance_id, database_id) ⇒ Object
218 219 220 221 222 |
# File 'lib/google/cloud/spanner/service.rb', line 218 def get_database_ddl instance_id, database_id execute do databases.get_database_ddl database_path(instance_id, database_id) end end |
#get_database_policy(instance_id, database_id) ⇒ Object
234 235 236 237 238 |
# File 'lib/google/cloud/spanner/service.rb', line 234 def get_database_policy instance_id, database_id execute do databases.get_iam_policy database_path(instance_id, database_id) end end |
#get_instance(name) ⇒ Object
112 113 114 115 116 |
# File 'lib/google/cloud/spanner/service.rb', line 112 def get_instance name execute do instances.get_instance instance_path(name) end end |
#get_instance_config(name) ⇒ Object
178 179 180 181 182 |
# File 'lib/google/cloud/spanner/service.rb', line 178 def get_instance_config name execute do instances.get_instance_config instance_config_path(name) end end |
#get_instance_policy(name) ⇒ Object
147 148 149 150 151 |
# File 'lib/google/cloud/spanner/service.rb', line 147 def get_instance_policy name execute do instances.get_iam_policy instance_path(name) end end |
#get_session(session_name) ⇒ Object
254 255 256 257 258 259 |
# File 'lib/google/cloud/spanner/service.rb', line 254 def get_session session_name opts = session_name execute do service.get_session session_name, options: opts end end |
#insecure? ⇒ Boolean
95 96 97 |
# File 'lib/google/cloud/spanner/service.rb', line 95 def insecure? credentials == :this_channel_is_insecure end |
#inspect ⇒ Object
354 355 356 |
# File 'lib/google/cloud/spanner/service.rb', line 354 def inspect "#{self.class}(#{@project})" end |
#instances ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/google/cloud/spanner/service.rb', line 69 def instances return mocked_instances if mocked_instances @instances ||= \ Admin::Instance::V1::InstanceAdminClient.new( service_path: host, channel: channel, timeout: timeout, client_config: client_config, lib_name: "gccl", lib_version: Google::Cloud::Spanner::VERSION) end |
#list_databases(instance_id, token: nil, max: nil) ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/google/cloud/spanner/service.rb', line 184 def list_databases instance_id, token: nil, max: nil = nil = Google::Gax::CallOptions.new page_token: token if token execute do paged_enum = databases.list_databases instance_path(instance_id), page_size: max, options: paged_enum.page.response end end |
#list_instance_configs(token: nil, max: nil) ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/google/cloud/spanner/service.rb', line 165 def list_instance_configs token: nil, max: nil = nil = Google::Gax::CallOptions.new page_token: token if token execute do paged_enum = instances.list_instance_configs project_path, page_size: max, options: paged_enum.page.response end end |
#list_instances(token: nil, max: nil) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/google/cloud/spanner/service.rb', line 99 def list_instances token: nil, max: nil = nil = Google::Gax::CallOptions.new page_token: token if token execute do paged_enum = instances.list_instances project_path, page_size: max, options: paged_enum.page.response end end |
#rollback(session_name, transaction_id) ⇒ Object
323 324 325 326 327 328 |
# File 'lib/google/cloud/spanner/service.rb', line 323 def rollback session_name, transaction_id opts = session_name execute do service.rollback session_name, transaction_id, options: opts end end |
#service ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/google/cloud/spanner/service.rb', line 56 def service return mocked_service if mocked_service @service ||= \ V1::SpannerClient.new( service_path: host, channel: channel, timeout: timeout, client_config: client_config, lib_name: "gccl", lib_version: Google::Cloud::Spanner::VERSION) end |
#set_database_policy(instance_id, database_id, new_policy) ⇒ Object
240 241 242 243 244 245 |
# File 'lib/google/cloud/spanner/service.rb', line 240 def set_database_policy instance_id, database_id, new_policy execute do databases.set_iam_policy \ database_path(instance_id, database_id), new_policy end end |
#set_instance_policy(name, new_policy) ⇒ Object
153 154 155 156 157 |
# File 'lib/google/cloud/spanner/service.rb', line 153 def set_instance_policy name, new_policy execute do instances.set_iam_policy instance_path(name), new_policy end end |
#streaming_execute_sql(session_name, sql, transaction: nil, params: nil, types: nil, resume_token: nil) ⇒ Object
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
# File 'lib/google/cloud/spanner/service.rb', line 275 def streaming_execute_sql session_name, sql, transaction: nil, params: nil, types: nil, resume_token: nil input_params = nil input_param_types = nil unless params.nil? input_param_pairs = Convert.to_query_params params, types input_params = Google::Protobuf::Struct.new( fields: Hash[input_param_pairs.map { |k, v| [k, v.first] }]) input_param_types = Hash[ input_param_pairs.map { |k, v| [k, v.last] }] end opts = session_name execute do service.execute_streaming_sql \ session_name, sql, transaction: transaction, params: input_params, param_types: input_param_types, resume_token: resume_token, options: opts end end |
#streaming_read_table(session_name, table_name, columns, keys: nil, index: nil, transaction: nil, limit: nil, resume_token: nil) ⇒ Object
295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/google/cloud/spanner/service.rb', line 295 def streaming_read_table session_name, table_name, columns, keys: nil, index: nil, transaction: nil, limit: nil, resume_token: nil columns.map!(&:to_s) opts = session_name execute do service.streaming_read \ session_name, table_name, columns, key_set(keys), transaction: transaction, index: index, limit: limit, resume_token: resume_token, options: opts end end |
#test_database_permissions(instance_id, database_id, permissions) ⇒ Object
247 248 249 250 251 252 |
# File 'lib/google/cloud/spanner/service.rb', line 247 def instance_id, database_id, execute do databases. \ database_path(instance_id, database_id), end end |
#test_instance_permissions(name, permissions) ⇒ Object
159 160 161 162 163 |
# File 'lib/google/cloud/spanner/service.rb', line 159 def name, execute do instances. instance_path(name), end end |
#update_database_ddl(instance_id, database_id, statements: [], operation_id: nil) ⇒ Object
224 225 226 227 228 229 230 231 232 |
# File 'lib/google/cloud/spanner/service.rb', line 224 def update_database_ddl instance_id, database_id, statements: [], operation_id: nil execute do databases.update_database_ddl \ database_path(instance_id, database_id), Array(statements), operation_id: operation_id end end |
#update_instance(instance_obj) ⇒ Object
132 133 134 135 136 137 138 139 |
# File 'lib/google/cloud/spanner/service.rb', line 132 def update_instance instance_obj mask = Google::Protobuf::FieldMask.new( paths: %w(display_name node_count labels)) execute do instances.update_instance instance_obj, mask end end |