Class: Azure::Table::TableService
- Inherits:
-
Service::StorageService
- Object
- Core::SignedService
- Service::StorageService
- Azure::Table::TableService
- Defined in:
- lib/azure/table/table_service.rb
Instance Method Summary collapse
-
#create_table(table_name, options = {}) ⇒ nil
Public: Creates new table in the storage account.
-
#delete_entity(table_name, partition_key, row_key, options = {}) ⇒ Object
Public: Deletes an existing entity in the table.
-
#delete_table(table_name, options = {}) ⇒ Object
Public: Deletes the specified table and any data it contains.
- #entities_uri(table_name, partition_key = nil, row_key = nil, query = {}) ⇒ Object
-
#execute_batch(batch, options = {}) ⇒ Object
Public: Executes a batch of operations.
-
#get_entity(table_name, partition_key, row_key, options = {}) ⇒ Object
Public: Gets an existing entity in the table.
-
#get_table(table_name, options = {}) ⇒ Object
Public: Gets the table.
-
#get_table_acl(table_name, options = {}) ⇒ Object
Public: Gets the access control list (ACL) for the table.
-
#initialize(options = {}) ⇒ TableService
constructor
A new instance of TableService.
-
#insert_entity(table_name, entity_values, options = {}) ⇒ Object
Public: Inserts new entity to the table.
-
#insert_or_merge_entity(table_name, entity_values, options = {}) ⇒ Object
Public: Inserts or updates an existing entity within a table by merging new property values into the entity.
-
#insert_or_replace_entity(table_name, entity_values, options = {}) ⇒ Object
Public: Inserts or updates a new entity into a table.
-
#merge_entity(table_name, entity_values, options = {}) ⇒ Object
Public: Updates an existing entity by updating the entity’s properties.
-
#query_entities(table_name, options = {}) ⇒ Object
Public: Queries entities for the given table name.
-
#query_tables(options = {}) ⇒ Object
Public: Gets a list of all tables on the account.
-
#set_table_acl(table_name, options = {}) ⇒ Object
Public: Sets the access control list (ACL) for the table.
- #table_uri(name, query = {}) ⇒ Object
-
#update_entity(table_name, entity_values, options = {}) ⇒ Object
Public: Updates an existing entity in a table.
Methods inherited from Service::StorageService
#add_metadata_to_headers, #get_service_properties, #service_properties_headers, #service_properties_uri, #set_service_properties
Constructor Details
#initialize(options = {}) ⇒ TableService
Returns a new instance of TableService.
25 26 27 28 29 30 |
# File 'lib/azure/table/table_service.rb', line 25 def initialize( = {}) client_config = [:client] || Azure signer = [:signer] || Auth::SharedKey.new(client_config.storage_account_name, client_config.storage_access_key) super(signer, client_config.storage_account_name, ) @host = client.storage_table_host end |
Instance Method Details
#create_table(table_name, options = {}) ⇒ nil
Public: Creates new table in the storage account
Attributes
-
table_name
- String. The table name -
options
- Hash. Optional parameters.
Options
Accepted key/value pairs in options parameter are:
-
:timeout
- Integer. A timeout in seconds.
48 49 50 51 52 53 54 55 |
# File 'lib/azure/table/table_service.rb', line 48 def create_table(table_name, ={}) query = { } query['timeout'] = [:timeout].to_s if [:timeout] body = Azure::Table::Serialization.hash_to_entry_xml({"TableName" => table_name}).to_xml call(:post, collection_uri(query), body) nil end |
#delete_entity(table_name, partition_key, row_key, options = {}) ⇒ Object
Public: Deletes an existing entity in the table.
Attributes
-
table_name
- String. The table name -
partition_key
- String. The partition key -
row_key
- String. The row key -
options
- Hash. Optional parameters.
Options
Accepted key/value pairs in options parameter are:
-
:if_match
- String. A matching condition which is required for update (optional, Default=“*”) -
:timeout
- Integer. A timeout in seconds.
See msdn.microsoft.com/en-us/library/azure/dd135727
Returns nil on success
414 415 416 417 418 419 420 421 422 423 |
# File 'lib/azure/table/table_service.rb', line 414 def delete_entity(table_name, partition_key, row_key, ={}) if_match = "*" if_match = [:if_match] if [:if_match] query = { } query["timeout"] = [:timeout].to_s if [:timeout] call(:delete, entities_uri(table_name, partition_key, row_key, query), nil, { "If-Match"=> if_match }) nil end |
#delete_table(table_name, options = {}) ⇒ Object
Public: Deletes the specified table and any data it contains.
Attributes
-
table_name
- String. The table name -
options
- Hash. Optional parameters.
Options
Accepted key/value pairs in options parameter are:
-
:timeout
- Integer. A timeout in seconds.
See msdn.microsoft.com/en-us/library/azure/dd179387
Returns nil on success
72 73 74 75 76 77 78 |
# File 'lib/azure/table/table_service.rb', line 72 def delete_table(table_name, ={}) query = { } query["timeout"] = [:timeout].to_s if [:timeout] call(:delete, table_uri(table_name, query)) nil end |
#entities_uri(table_name, partition_key = nil, row_key = nil, query = {}) ⇒ Object
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 |
# File 'lib/azure/table/table_service.rb', line 510 def entities_uri(table_name, partition_key=nil, row_key=nil, query={}) return table_name if table_name.kind_of? ::URI path = if partition_key && row_key "%s(PartitionKey='%s',RowKey='%s')" % [ table_name.encode("UTF-8"), encodeODataUriValue(partition_key.encode("UTF-8")), encodeODataUriValue(row_key.encode("UTF-8")) ] else "%s()" % table_name.encode("UTF-8") end uri = generate_uri(path) qs = [] if query query.each do | key, val | key = key.encode("UTF-8") val = val.encode("UTF-8") if key[0] == "$" qs.push "#{key}#{::URI.encode_www_form(""=>val)}" else qs.push ::URI.encode_www_form(key=>val) end end end uri.query = qs.join '&' if qs.length > 0 uri end |
#execute_batch(batch, options = {}) ⇒ Object
Public: Executes a batch of operations.
Attributes
-
batch
- The Azure::Table::Batch instance to execute. -
options
- Hash. Optional parameters.
Options
Accepted key/value pairs in options parameter are:
-
:timeout
- Integer. A timeout in seconds.
See msdn.microsoft.com/en-us/library/azure/dd894038
Returns an array of results, one for each operation in the batch
440 441 442 443 444 445 446 447 448 449 450 451 452 453 |
# File 'lib/azure/table/table_service.rb', line 440 def execute_batch(batch, ={}) headers = { 'Content-Type' => "multipart/mixed; boundary=#{batch.batch_id}", 'Accept' => 'application/atom+xml,application/xml', 'Accept-Charset'=> 'UTF-8' } query = { } query["timeout"] = [:timeout].to_s if [:timeout] body = batch.to_body response = call(:post, generate_uri('/$batch', query), body, headers) batch.parse_response(response) end |
#get_entity(table_name, partition_key, row_key, options = {}) ⇒ Object
Public: Gets an existing entity in the table.
Attributes
-
table_name
- String. The table name -
partition_key
- String. The partition key -
row_key
- String. The row key -
options
- Hash. Optional parameters.
Options
Accepted key/value pairs in options parameter are:
-
:timeout
- Integer. A timeout in seconds.
Returns an Azure::Table::Entity instance on success
470 471 472 473 474 475 |
# File 'lib/azure/table/table_service.rb', line 470 def get_entity(table_name, partition_key, row_key, ={}) [:partition_key] = partition_key [:row_key] = row_key results = query_entities(table_name, ) results.length > 0 ? results[0] : nil end |
#get_table(table_name, options = {}) ⇒ Object
Public: Gets the table.
Attributes
-
table_name
- String. The table name -
options
- Hash. Optional parameters.
Options
Accepted key/value pairs in options parameter are:
-
:timeout
- Integer. A timeout in seconds.
Returns the last updated time for the table
93 94 95 96 97 98 99 100 |
# File 'lib/azure/table/table_service.rb', line 93 def get_table(table_name, ={}) query = { } query["timeout"] = [:timeout].to_s if [:timeout] response = call(:get, table_uri(table_name, query)) results = Azure::Table::Serialization.hash_from_entry_xml(response.body) results[:updated] end |
#get_table_acl(table_name, options = {}) ⇒ Object
Public: Gets the access control list (ACL) for the table.
Attributes
-
table_name
- String. The table name -
options
- Hash. Optional parameters.
Options
Accepted key/value pairs in options parameter are:
-
:timeout
- Integer. A timeout in seconds.
See msdn.microsoft.com/en-us/library/azure/jj159100
Returns a list of Azure::Entity::SignedIdentifier instances
148 149 150 151 152 153 154 155 156 157 |
# File 'lib/azure/table/table_service.rb', line 148 def get_table_acl(table_name, ={}) query = { 'comp' => 'acl'} query['timeout'] = [:timeout].to_s if [:timeout] response = call(:get, generate_uri(table_name, query), nil, {'x-ms-version' => '2012-02-12'}) signed_identifiers = [] signed_identifiers = Azure::Table::Serialization.signed_identifiers_from_xml response.body unless response.body == nil or response.body.length < 1 signed_identifiers end |
#insert_entity(table_name, entity_values, options = {}) ⇒ Object
Public: Inserts new entity to the table.
Attributes
-
table_name
- String. The table name -
entity_values
- Hash. A hash of the name/value pairs for the entity. -
options
- Hash. Optional parameters.
Options
Accepted key/value pairs in options parameter are:
-
:timeout
- Integer. A timeout in seconds.
See msdn.microsoft.com/en-us/library/azure/dd179433
Returns a Azure::Entity::Table::Entity
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/azure/table/table_service.rb', line 204 def insert_entity(table_name, entity_values, ={}) body = Azure::Table::Serialization.hash_to_entry_xml(entity_values).to_xml query = { } query['timeout'] = [:timeout].to_s if [:timeout] response = call(:post, entities_uri(table_name, nil, nil, query), body) result = Azure::Table::Serialization.hash_from_entry_xml(response.body) Entity.new do |entity| entity.table = table_name entity.updated = result[:updated] entity.etag = response.headers['etag'] || result[:etag] entity.properties = result[:properties] end end |
#insert_or_merge_entity(table_name, entity_values, options = {}) ⇒ Object
Public: Inserts or updates an existing entity within a table by merging new property values into the entity.
Attributes
-
table_name
- String. The table name -
entity_values
- Hash. A hash of the name/value pairs for the entity. -
options
- Hash. Optional parameters.
Options
Accepted key/value pairs in options parameter are:
-
:timeout
- Integer. A timeout in seconds.
See msdn.microsoft.com/en-us/library/azure/hh452241
Returns the ETag for the entity on success
370 371 372 373 |
# File 'lib/azure/table/table_service.rb', line 370 def insert_or_merge_entity(table_name, entity_values, ={}) [:create_if_not_exists] = true merge_entity(table_name, entity_values, ) end |
#insert_or_replace_entity(table_name, entity_values, options = {}) ⇒ Object
Public: Inserts or updates a new entity into a table.
Attributes
-
table_name
- String. The table name -
entity_values
- Hash. A hash of the name/value pairs for the entity. -
options
- Hash. Optional parameters.
Options
Accepted key/value pairs in options parameter are:
-
:timeout
- Integer. A timeout in seconds.
See msdn.microsoft.com/en-us/library/azure/hh452242
Returns the ETag for the entity on success
391 392 393 394 |
# File 'lib/azure/table/table_service.rb', line 391 def insert_or_replace_entity(table_name, entity_values, ={}) [:create_if_not_exists] = true update_entity(table_name, entity_values, ) end |
#merge_entity(table_name, entity_values, options = {}) ⇒ Object
Public: Updates an existing entity by updating the entity’s properties. This operation does not replace the existing entity, as the update_entity operation does.
Attributes
-
table_name
- String. The table name -
entity_values
- Hash. A hash of the name/value pairs for the entity. -
options
- Hash. Optional parameters.
Options
Accepted key/value pairs in options parameter are:
-
:if_match
- String. A matching condition which is required for update (optional, Default=“*”) -
:create_if_not_exists
- Boolean. If true, and partition_key and row_key do not reference and existing entity, that entity will be inserted. If false, the operation will fail. (optional, Default=false) -
:timeout
- Integer. A timeout in seconds.
See msdn.microsoft.com/en-us/library/azure/dd179392
Returns the ETag for the entity on success
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
# File 'lib/azure/table/table_service.rb', line 336 def merge_entity(table_name, entity_values, ={}) if_match = "*" if_match = [:if_match] if [:if_match] query = { } query["timeout"] = [:timeout].to_s if [:timeout] uri = entities_uri(table_name, entity_values["PartitionKey"], entity_values["RowKey"], query) headers = { "X-HTTP-Method"=> "MERGE" } headers["If-Match"] = if_match || "*" unless [:create_if_not_exists] body = Azure::Table::Serialization.hash_to_entry_xml(entity_values).to_xml response = call(:post, uri, body, headers) response.headers["etag"] end |
#query_entities(table_name, options = {}) ⇒ Object
Public: Queries entities for the given table name
Attributes
-
table_name
- String. The table name -
options
- Hash. Optional parameters.
Options
Accepted key/value pairs in options parameter are:
-
:partition_key
- String. The partition key (optional) -
:row_key
- String. The row key (optional) -
:select
- Array. An array of property names to return (optional) -
:filter
- String. A filter expression (optional) -
:top
- Integer. A limit for the number of results returned (optional) -
:continuation_token
- Hash. The continuation token. -
:timeout
- Integer. A timeout in seconds.
See msdn.microsoft.com/en-us/library/azure/dd179421
Returns an array with an extra continuation_token property on success
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/azure/table/table_service.rb', line 243 def query_entities(table_name, ={}) query ={} query["$select"] = [:select].join ',' if [:select] query["$filter"] = [:filter] if [:filter] query["$top"] = [:top].to_s if [:top] unless [:partition_key] and [:row_key] query["NextPartitionKey"] = [:continuation_token][:next_partition_key] if [:continuation_token] and [:continuation_token][:next_partition_key] query["NextRowKey"] = [:continuation_token][:next_row_key] if [:continuation_token] and [:continuation_token][:next_row_key] query["timeout"] = [:timeout].to_s if [:timeout] uri = entities_uri(table_name, [:partition_key], [:row_key], query) response = call(:get, uri, nil, { "DataServiceVersion" => "2.0;NetFx"}) entities = Azure::Service::EnumerationResults.new results = ([:partition_key] and [:row_key]) ? [Azure::Table::Serialization.hash_from_entry_xml(response.body)] : Azure::Table::Serialization.entries_from_feed_xml(response.body) results.each do |result| entity = Entity.new do |e| e.table = table_name e.updated = result[:updated] e.etag = response.headers["etag"] || result[:etag] e.properties = result[:properties] end entities.push entity end if results entities.continuation_token = nil entities.continuation_token = { :next_partition_key=> response.headers["x-ms-continuation-NextPartitionKey"], :next_row_key => response.headers["x-ms-continuation-NextRowKey"] } if response.headers["x-ms-continuation-NextPartitionKey"] entities end |
#query_tables(options = {}) ⇒ Object
Public: Gets a list of all tables on the account.
Attributes
-
options
- Hash. Optional parameters.
Options
Accepted key/value pairs in options parameter are:
-
:next_table_token
- String. A token used to enumerate the next page of results, when the list of tables is larger than a single operation can return at once. (optional) -
:timeout
- Integer. A timeout in seconds.
See msdn.microsoft.com/en-us/library/azure/dd179405
Returns an array with an extra continuation_token property on success
118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/azure/table/table_service.rb', line 118 def query_tables(={}) query = { } query["NextTable"] = [:next_table_token] if [:next_table_token] query["timeout"] = [:timeout].to_s if [:timeout] uri = collection_uri(query) response = call(:get, uri) entries = Azure::Table::Serialization.entries_from_feed_xml(response.body) || [] values = Azure::Service::EnumerationResults.new(entries) values.continuation_token = response.headers["x-ms-continuation-NextTableName"] values end |
#set_table_acl(table_name, options = {}) ⇒ Object
Public: Sets the access control list (ACL) for the table.
Attributes
-
table_name
- String. The table name -
options
- Hash. Optional parameters.
Options
Accepted key/value pairs in options parameter are:
-
:signed_identifiers
- Array. A list of Azure::Entity::SignedIdentifier instances -
:timeout
- Integer. A timeout in seconds.
See msdn.microsoft.com/en-us/library/azure/jj159102
Returns nil on success
175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/azure/table/table_service.rb', line 175 def set_table_acl(table_name, ={}) query = { 'comp' => 'acl'} query['timeout'] = [:timeout].to_s if [:timeout] uri = generate_uri(table_name, query) body = nil body = Azure::Table::Serialization.signed_identifiers_to_xml [:signed_identifiers] if [:signed_identifiers] && [:signed_identifiers].length > 0 call(:put, uri, body, {'x-ms-version' => '2012-02-12'}) nil end |
#table_uri(name, query = {}) ⇒ Object
493 494 495 496 |
# File 'lib/azure/table/table_service.rb', line 493 def table_uri(name, query={}) return name if name.kind_of? ::URI generate_uri("Tables('#{name}')", query) end |
#update_entity(table_name, entity_values, options = {}) ⇒ Object
Public: Updates an existing entity in a table. The Update Entity operation replaces the entire entity and can be used to remove properties.
Attributes
-
table_name
- String. The table name -
entity_values
- Hash. A hash of the name/value pairs for the entity. -
options
- Hash. Optional parameters.
Options
Accepted key/value pairs in options parameter are:
-
:if_match
- String. A matching condition which is required for update (optional, Default=“*”) -
:create_if_not_exists
- Boolean. If true, and partition_key and row_key do not reference and existing entity, that entity will be inserted. If false, the operation will fail. (optional, Default=false) -
:timeout
- Integer. A timeout in seconds.
See msdn.microsoft.com/en-us/library/azure/dd179427
Returns the ETag for the entity on success
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
# File 'lib/azure/table/table_service.rb', line 298 def update_entity(table_name, entity_values, ={}) if_match = "*" if_match = [:if_match] if [:if_match] query = { } query["timeout"] = [:timeout].to_s if [:timeout] uri = entities_uri(table_name, entity_values["PartitionKey"], entity_values["RowKey"], query) headers = {} headers["If-Match"] = if_match || "*" unless [:create_if_not_exists] body = Azure::Table::Serialization.hash_to_entry_xml(entity_values).to_xml response = call(:put, uri, body, headers) response.headers["etag"] end |