azure-storage 0.10.0.preview

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. checksums.yaml +7 -0
  2. data/lib/azure/storage.rb +58 -0
  3. data/lib/azure/storage/autoload.rb +71 -0
  4. data/lib/azure/storage/blob/append.rb +154 -0
  5. data/lib/azure/storage/blob/blob.rb +821 -0
  6. data/lib/azure/storage/blob/blob_service.rb +510 -0
  7. data/lib/azure/storage/blob/block.rb +264 -0
  8. data/lib/azure/storage/blob/container.rb +552 -0
  9. data/lib/azure/storage/blob/page.rb +380 -0
  10. data/lib/azure/storage/blob/serialization.rb +297 -0
  11. data/lib/azure/storage/client.rb +185 -0
  12. data/lib/azure/storage/configurable.rb +137 -0
  13. data/lib/azure/storage/core.rb +33 -0
  14. data/lib/azure/storage/core/auth/shared_access_signature.rb +27 -0
  15. data/lib/azure/storage/core/auth/shared_access_signature_generator.rb +194 -0
  16. data/lib/azure/storage/core/auth/shared_access_signature_signer.rb +49 -0
  17. data/lib/azure/storage/core/auth/shared_key.rb +125 -0
  18. data/lib/azure/storage/core/auth/shared_key_lite.rb +55 -0
  19. data/lib/azure/storage/core/auth/signer.rb +60 -0
  20. data/lib/azure/storage/core/autoload.rb +35 -0
  21. data/lib/azure/storage/core/client_options.rb +334 -0
  22. data/lib/azure/storage/core/client_options_error.rb +39 -0
  23. data/lib/azure/storage/core/constants.rb +1077 -0
  24. data/lib/azure/storage/core/error.rb +47 -0
  25. data/lib/azure/storage/core/filtered_service.rb +54 -0
  26. data/lib/azure/storage/core/http/debug_filter.rb +45 -0
  27. data/lib/azure/storage/core/http/http_error.rb +95 -0
  28. data/lib/azure/storage/core/http/http_filter.rb +62 -0
  29. data/lib/azure/storage/core/http/http_request.rb +182 -0
  30. data/lib/azure/storage/core/http/http_response.rb +105 -0
  31. data/lib/azure/storage/core/http/retry_policy.rb +83 -0
  32. data/lib/azure/storage/core/http/signer_filter.rb +42 -0
  33. data/lib/azure/storage/core/http_client.rb +63 -0
  34. data/lib/azure/storage/core/service.rb +55 -0
  35. data/lib/azure/storage/core/signed_service.rb +54 -0
  36. data/lib/azure/storage/core/sr.rb +83 -0
  37. data/lib/azure/storage/core/utility.rb +254 -0
  38. data/lib/azure/storage/queue/message.rb +39 -0
  39. data/lib/azure/storage/queue/queue.rb +37 -0
  40. data/lib/azure/storage/queue/queue_service.rb +580 -0
  41. data/lib/azure/storage/queue/serialization.rb +113 -0
  42. data/lib/azure/storage/service/access_policy.rb +35 -0
  43. data/lib/azure/storage/service/cors.rb +36 -0
  44. data/lib/azure/storage/service/cors_rule.rb +46 -0
  45. data/lib/azure/storage/service/enumeration_results.rb +30 -0
  46. data/lib/azure/storage/service/logging.rb +45 -0
  47. data/lib/azure/storage/service/metrics.rb +43 -0
  48. data/lib/azure/storage/service/retention_policy.rb +35 -0
  49. data/lib/azure/storage/service/serialization.rb +308 -0
  50. data/lib/azure/storage/service/signed_identifier.rb +39 -0
  51. data/lib/azure/storage/service/storage_service.rb +131 -0
  52. data/lib/azure/storage/service/storage_service_properties.rb +46 -0
  53. data/lib/azure/storage/table/auth/shared_key.rb +68 -0
  54. data/lib/azure/storage/table/auth/shared_key_lite.rb +53 -0
  55. data/lib/azure/storage/table/batch.rb +339 -0
  56. data/lib/azure/storage/table/batch_response.rb +127 -0
  57. data/lib/azure/storage/table/edmtype.rb +136 -0
  58. data/lib/azure/storage/table/entity.rb +40 -0
  59. data/lib/azure/storage/table/guid.rb +33 -0
  60. data/lib/azure/storage/table/query.rb +121 -0
  61. data/lib/azure/storage/table/serialization.rb +117 -0
  62. data/lib/azure/storage/table/table_service.rb +571 -0
  63. data/lib/azure/storage/version.rb +46 -0
  64. metadata +329 -0
@@ -0,0 +1,254 @@
1
+ #-------------------------------------------------------------------------
2
+ # # Copyright (c) Microsoft and contributors. All rights reserved.
3
+ #
4
+ # The MIT License(MIT)
5
+
6
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ # of this software and associated documentation files(the "Software"), to deal
8
+ # in the Software without restriction, including without limitation the rights
9
+ # to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
10
+ # copies of the Software, and to permit persons to whom the Software is
11
+ # furnished to do so, subject to the following conditions :
12
+
13
+ # The above copyright notice and this permission notice shall be included in
14
+ # all copies or substantial portions of the Software.
15
+
16
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
19
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ # THE SOFTWARE.
23
+ #--------------------------------------------------------------------------
24
+
25
+ require 'ipaddr'
26
+ require 'azure/storage/core/error'
27
+
28
+ if RUBY_VERSION.to_f < 2.0
29
+ begin
30
+ require 'Win32/Console/ANSI' if RUBY_PLATFORM =~ /win32|mingw32/
31
+ rescue LoadError
32
+ puts 'WARNING: Output will look weird on Windows unless'\
33
+ ' you install the "win32console" gem.'
34
+ end
35
+ end
36
+
37
+ module Azure
38
+ module Error
39
+ # Azure Error
40
+ class Error < Azure::Core::Error
41
+ attr_reader :description
42
+ attr_reader :status_code
43
+ attr_reader :type
44
+
45
+ def initialize(type, status, description)
46
+ @type = type
47
+ @status_code = status
48
+ @description = description
49
+ super("#{type} (#{status_code}): #{description}")
50
+ end
51
+ end
52
+ end
53
+
54
+ module Core
55
+ module Utility
56
+ def random_string(str = 'azure', no_of_char = 5)
57
+ str + (0...no_of_char).map { ('a'..'z').to_a[rand(26)] }.join
58
+ end
59
+
60
+ def xml_content(xml, key, default = '')
61
+ content = default
62
+ node = xml.at_css(key)
63
+ content = node.text if node
64
+ content
65
+ end
66
+
67
+ def locate_file(name)
68
+ if File.exist? name
69
+ name
70
+ elsif File.exist?(File.join(ENV['HOME'], name))
71
+ File.join(ENV['HOME'], name)
72
+ else
73
+ Azure::Loggerx.error_with_exit "Unable to find #{name} file "
74
+ end
75
+ end
76
+
77
+ def export_der(cert, key, pass = nil, name = nil)
78
+ pkcs12 = OpenSSL::PKCS12.create(pass, name, key, cert)
79
+ Base64.encode64(pkcs12.to_der)
80
+ rescue Exception => e
81
+ puts e.message
82
+ abort
83
+ end
84
+
85
+ def export_fingerprint(certificate)
86
+ Digest::SHA1.hexdigest(certificate.to_der)
87
+ end
88
+
89
+ def enable_winrm?(winrm_transport)
90
+ (!winrm_transport.nil? && (winrm_transport.select { |x| x.downcase == 'http' || x.downcase == 'https' }.size > 0))
91
+ end
92
+
93
+ def get_certificate(private_key_file)
94
+ rsa = OpenSSL::PKey.read File.read(private_key_file)
95
+ cert = OpenSSL::X509::Certificate.new
96
+ cert.version = 2
97
+ cert.serial = 0
98
+ name = OpenSSL::X509::Name.new([['CN', 'Azure Management Certificate']])
99
+ cert.subject = cert.issuer = name
100
+ cert.not_before = Time.now
101
+ cert.not_after = cert.not_before + (60*60*24*365)
102
+ cert.public_key = rsa.public_key
103
+ cert.sign(rsa, OpenSSL::Digest::SHA1.new)
104
+ cert
105
+ end
106
+
107
+ def initialize_external_logger(logger)
108
+ Loggerx.initialize_external_logger(logger)
109
+ end
110
+ end
111
+
112
+ # Logger
113
+ module Logger
114
+ class << self
115
+ attr_accessor :logger
116
+
117
+ def info(msg)
118
+ if logger.nil?
119
+ puts msg.bold.white
120
+ else
121
+ logger.info(msg)
122
+ end
123
+ end
124
+
125
+ def error_with_exit(msg)
126
+ if logger.nil?
127
+ puts msg.bold.red
128
+ else
129
+ logger.error(msg)
130
+ end
131
+
132
+ raise msg.bold.red
133
+ end
134
+
135
+ def warn(msg)
136
+ if logger.nil?
137
+ puts msg.yellow
138
+ else
139
+ logger.warn(msg)
140
+ end
141
+
142
+ msg
143
+ end
144
+
145
+ def error(msg)
146
+ if logger.nil?
147
+ puts msg.bold.red
148
+ else
149
+ logger.error(msg)
150
+ end
151
+
152
+ msg
153
+ end
154
+
155
+ def exception_message(msg)
156
+ if logger.nil?
157
+ puts msg.bold.red
158
+ else
159
+ logger.warn(msg)
160
+ end
161
+
162
+ raise msg.bold.red
163
+ end
164
+
165
+ def success(msg)
166
+ msg_with_new_line = msg + "\n"
167
+ if logger.nil?
168
+ print msg_with_new_line.green
169
+ else
170
+ logger.info(msg)
171
+ end
172
+ end
173
+
174
+ def initialize_external_logger(logger)
175
+ @logger = logger
176
+ end
177
+ end
178
+ end
179
+ end
180
+ end
181
+
182
+ class String
183
+ { reset: 0,
184
+ bold: 1,
185
+ dark: 2,
186
+ underline: 4,
187
+ blink: 5,
188
+ orange: 6,
189
+ negative: 7,
190
+ black: 30,
191
+ red: 31,
192
+ green: 32,
193
+ yellow: 33,
194
+ blue: 34,
195
+ magenta: 35,
196
+ cyan: 36,
197
+ white: 37,
198
+ }.each do |key, value|
199
+ define_method key do
200
+ "\e[#{value}m" + self + "\e[0m"
201
+ end
202
+ end
203
+ end
204
+
205
+ # Code validate private/public IP acceptable ranges.
206
+ class IPAddr
207
+ PRIVATE_RANGES = [
208
+ IPAddr.new('10.0.0.0/8'),
209
+ IPAddr.new('172.16.0.0/12'),
210
+ IPAddr.new('192.168.0.0/16')
211
+ ]
212
+
213
+ def private?
214
+ return false unless self.ipv4?
215
+ PRIVATE_RANGES.each do |ipr|
216
+ return true if ipr.include?(self)
217
+ end
218
+ false
219
+ end
220
+
221
+ def public?
222
+ !private?
223
+ end
224
+
225
+ class << self
226
+ def validate_ip_and_prefix(ip, cidr)
227
+ if cidr.to_s.empty?
228
+ raise "Cidr is missing for IP '#{ip}'."
229
+ elsif valid?(ip)
230
+ raise "Ip address '#{ip}' is invalid."
231
+ elsif !IPAddr.new(ip).private?
232
+ raise "Ip Address #{ip} must be private."
233
+ end
234
+ end
235
+
236
+ def validate_address_space(ip)
237
+ if ip.split('/').size != 2
238
+ raise "Cidr is invalid for IP #{ip}."
239
+ elsif valid?(ip)
240
+ raise "Address space '#{ip}' is invalid."
241
+ end
242
+ end
243
+
244
+ def address_prefix(ip, cidr)
245
+ ip + '/' + cidr.to_s
246
+ end
247
+
248
+ def valid?(ip)
249
+ (IPAddr.new(ip) rescue nil).nil?
250
+ end
251
+ end
252
+ end
253
+
254
+ Azure::Loggerx = Azure::Core::Logger
@@ -0,0 +1,39 @@
1
+ #-------------------------------------------------------------------------
2
+ # # Copyright (c) Microsoft and contributors. All rights reserved.
3
+ #
4
+ # The MIT License(MIT)
5
+
6
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ # of this software and associated documentation files(the "Software"), to deal
8
+ # in the Software without restriction, including without limitation the rights
9
+ # to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
10
+ # copies of the Software, and to permit persons to whom the Software is
11
+ # furnished to do so, subject to the following conditions :
12
+
13
+ # The above copyright notice and this permission notice shall be included in
14
+ # all copies or substantial portions of the Software.
15
+
16
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
19
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ # THE SOFTWARE.
23
+ #--------------------------------------------------------------------------
24
+ module Azure::Storage
25
+ module Queue
26
+ class Message
27
+ def initialize
28
+ yield self if block_given?
29
+ end
30
+ attr_accessor :id
31
+ attr_accessor :insertion_time
32
+ attr_accessor :expiration_time
33
+ attr_accessor :dequeue_count
34
+ attr_accessor :message_text
35
+ attr_accessor :time_next_visible
36
+ attr_accessor :pop_receipt
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,37 @@
1
+ #-------------------------------------------------------------------------
2
+ # # Copyright (c) Microsoft and contributors. All rights reserved.
3
+ #
4
+ # The MIT License(MIT)
5
+
6
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ # of this software and associated documentation files(the "Software"), to deal
8
+ # in the Software without restriction, including without limitation the rights
9
+ # to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
10
+ # copies of the Software, and to permit persons to whom the Software is
11
+ # furnished to do so, subject to the following conditions :
12
+
13
+ # The above copyright notice and this permission notice shall be included in
14
+ # all copies or substantial portions of the Software.
15
+
16
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
19
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ # THE SOFTWARE.
23
+ #--------------------------------------------------------------------------
24
+ module Azure::Storage
25
+ module Queue
26
+ class Queue
27
+
28
+ def initialize
29
+ @metadata = {}
30
+ yield self if block_given?
31
+ end
32
+
33
+ attr_accessor :name
34
+ attr_accessor :metadata
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,580 @@
1
+ #-------------------------------------------------------------------------
2
+ # # Copyright (c) Microsoft and contributors. All rights reserved.
3
+ #
4
+ # The MIT License(MIT)
5
+
6
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ # of this software and associated documentation files(the "Software"), to deal
8
+ # in the Software without restriction, including without limitation the rights
9
+ # to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
10
+ # copies of the Software, and to permit persons to whom the Software is
11
+ # furnished to do so, subject to the following conditions :
12
+
13
+ # The above copyright notice and this permission notice shall be included in
14
+ # all copies or substantial portions of the Software.
15
+
16
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
19
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ # THE SOFTWARE.
23
+ #--------------------------------------------------------------------------
24
+ require 'azure/storage/service/storage_service'
25
+ require 'azure/storage/queue/serialization'
26
+
27
+ module Azure::Storage
28
+ module Queue
29
+ include Azure::Storage::Service
30
+ class QueueService < StorageService
31
+
32
+ def initialize(options = {})
33
+ super(nil, nil, options)
34
+ @host = @client.storage_queue_host
35
+ end
36
+
37
+ # Public: Get a list of Queues from the server
38
+ #
39
+ # ==== Attributes
40
+ #
41
+ # * +options+ - Hash. Optional parameters.
42
+ #
43
+ # ==== Options
44
+ #
45
+ # Accepted key/value pairs in options parameter are:
46
+ # * +:prefix+ - String. Filters the results to return only containers
47
+ # whose name begins with the specified prefix. (optional)
48
+ # * +:marker+ - String. An identifier the specifies the portion of the
49
+ # list to be returned. This value comes from the property
50
+ # Azure::Service::EnumerationResults.continuation_token when there
51
+ # are more containers available than were returned. The
52
+ # marker value may then be used here to request the next set
53
+ # of list items. (optional)
54
+ # * +:max_results+ - Integer. Specifies the maximum number of containers to return.
55
+ # If max_results is not specified, or is a value greater than
56
+ # 5,000, the server will return up to 5,000 items. If it is set
57
+ # to a value less than or equal to zero, the server will return
58
+ # status code 400 (Bad Request). (optional)
59
+ # * +:metadata+ - Boolean. Specifies whether or not to return the container metadata.
60
+ # (optional, Default=false)
61
+ # * +:timeout+ - Integer. A timeout in seconds.
62
+ #
63
+ # NOTE: Metadata requested with the :metadata parameter must have been stored in
64
+ # accordance with the naming restrictions imposed by the 2009-09-19 version of the queue
65
+ # service. Beginning with that version, all metadata names must adhere to the naming
66
+ # conventions for C# identifiers.
67
+ #
68
+ # See http://msdn.microsoft.com/en-us/library/azure/dd179466
69
+ #
70
+ # Any metadata with invalid names which were previously stored, will be returned with the
71
+ # key "x-ms-invalid-name" in the metadata hash. This may contain multiple values and be an
72
+ # Array (vs a String if it only contains a single value).
73
+ #
74
+ # Returns an Azure::Service::EnumerationResults
75
+ def list_queues(options={})
76
+ query = { }
77
+ query["prefix"] = options[:prefix] if options[:prefix]
78
+ query["marker"] = options[:marker] if options[:marker]
79
+ query["maxresults"] = options[:max_results].to_s if options[:max_results]
80
+ query["include"] = "metadata" if options[:metadata] == true
81
+ query["timeout"] = options[:timeout].to_s if options[:timeout]
82
+
83
+ uri = collection_uri(query)
84
+ response = call(:get, uri)
85
+
86
+ Serialization.queue_enumeration_results_from_xml(response.body)
87
+ end
88
+
89
+ # Public: Clears all messages from the queue.
90
+ #
91
+ # If a queue contains a large number of messages, Clear Messages may time out
92
+ # before all messages have been deleted. In this case the Queue service will
93
+ # return status code 500 (Internal Server Error), with the additional error
94
+ # code OperationTimedOut. If the operation times out, the client should
95
+ # continue to retry Clear Messages until it succeeds, to ensure that all
96
+ # messages have been deleted.
97
+ #
98
+ # ==== Attributes
99
+ #
100
+ # * +queue_name+ - String. The name of the queue.
101
+ # * +options+ - Hash. Optional parameters.
102
+ #
103
+ # ==== Options
104
+ #
105
+ # Accepted key/value pairs in options parameter are:
106
+ # * +:timeout+ - Integer. A timeout in seconds.
107
+ #
108
+ # See http://msdn.microsoft.com/en-us/library/azure/dd179454
109
+ #
110
+ # Returns nil on success
111
+ def clear_messages(queue_name, options={})
112
+ query = { }
113
+ query["timeout"] = options[:timeout].to_s if options[:timeout]
114
+ uri = messages_uri(queue_name, query)
115
+ call(:delete, uri)
116
+ nil
117
+ end
118
+
119
+ # Public: Creates a new queue under the storage account.
120
+ #
121
+ # ==== Attributes
122
+ #
123
+ # * +queue_name+ - String. The queue name.
124
+ # * +options+ - Hash. Optional parameters.
125
+ #
126
+ # ==== Options
127
+ #
128
+ # Accepted key/value pairs in options parameter are:
129
+ # * +:metadata+ - Hash. A hash of user defined metadata.
130
+ # * +:timeout+ - Integer. A timeout in seconds.
131
+ #
132
+ # See http://msdn.microsoft.com/en-us/library/azure/dd179342
133
+ #
134
+ # Returns nil on success
135
+ def create_queue(queue_name, options={})
136
+ query = { }
137
+ query["timeout"] = options[:timeout].to_s if options[:timeout]
138
+
139
+ uri = queue_uri(queue_name, query)
140
+
141
+ headers = { }
142
+ Service::StorageService.add_metadata_to_headers(options[:metadata] || {}, headers) if options[:metadata]
143
+
144
+ call(:put, uri, nil, headers)
145
+ nil
146
+ end
147
+
148
+ # Public: Deletes a queue.
149
+ #
150
+ # ==== Attributes
151
+ #
152
+ # * +queue_name+ - String. The queue name.
153
+ # * +options+ - Hash. Optional parameters.
154
+ #
155
+ # ==== Options
156
+ #
157
+ # Accepted key/value pairs in options parameter are:
158
+ # * +:timeout+ - Integer. A timeout in seconds.
159
+ #
160
+ # See http://msdn.microsoft.com/en-us/library/azure/dd179436
161
+ #
162
+ # Returns nil on success
163
+ def delete_queue(queue_name, options={})
164
+ query = { }
165
+ query["timeout"] = options[:timeout].to_s if options[:timeout]
166
+
167
+ uri = queue_uri(queue_name, query)
168
+
169
+ call(:delete, uri)
170
+ nil
171
+ end
172
+
173
+ # Public: Returns queue properties, including user-defined metadata.
174
+ #
175
+ # ==== Attributes
176
+ #
177
+ # * +queue_name+ - String. The queue name.
178
+ # * +options+ - Hash. Optional parameters.
179
+ #
180
+ # ==== Options
181
+ #
182
+ # Accepted key/value pairs in options parameter are:
183
+ # * +:timeout+ - Integer. A timeout in seconds.
184
+ #
185
+ # See http://msdn.microsoft.com/en-us/library/azure/dd179384
186
+ #
187
+ # Returns a tuple of (approximate_message_count, metadata)
188
+ # * approximate_messages_count - Integer. The approximate number of messages in the queue. This number is not
189
+ # lower than the actual number of messages in the queue, but could be higher.
190
+ # * metadata - Hash. The queue metadata (Default: {})
191
+ #
192
+ def get_queue_metadata(queue_name, options={})
193
+ query = { "comp" => "metadata" }
194
+ query["timeout"] = options[:timeout].to_s if options[:timeout]
195
+
196
+ uri = queue_uri(queue_name, query)
197
+
198
+ response = call(:get, uri)
199
+
200
+ approximate_messages_count = response.headers["x-ms-approximate-messages-count"]
201
+ metadata = Serialization.metadata_from_headers(response.headers)
202
+
203
+ return approximate_messages_count.to_i, metadata
204
+ end
205
+
206
+ # Public: Sets user-defined metadata on the queue. To delete queue metadata, call
207
+ # this API with an empty hash in the metadata parameter.
208
+ #
209
+ # ==== Attributes
210
+ #
211
+ # * +queue_name+ - String. The queue name.
212
+ # * +metadata+ - Hash. A hash of user defined metadata
213
+ # * +options+ - Hash. Optional parameters.
214
+ #
215
+ # ==== Options
216
+ #
217
+ # Accepted key/value pairs in options parameter are:
218
+ # * +:timeout+ - Integer. A timeout in seconds.
219
+ #
220
+ # See http://msdn.microsoft.com/en-us/library/azure/dd179348
221
+ #
222
+ # Returns nil on success
223
+ def set_queue_metadata(queue_name, metadata, options={})
224
+ query = { "comp" => "metadata" }
225
+ query["timeout"] = options[:timeout].to_s if options[:timeout]
226
+
227
+ uri = queue_uri(queue_name, query)
228
+
229
+ headers ={}
230
+ Service::StorageService.add_metadata_to_headers(metadata || {}, headers)
231
+
232
+ call(:put, uri, nil, headers)
233
+ nil
234
+ end
235
+
236
+ # Public: Gets the access control list (ACL) for the queue.
237
+ #
238
+ # ==== Attributes
239
+ #
240
+ # * +queue_name+ - String. The queue name.
241
+ # * +options+ - Hash. Optional parameters.
242
+ #
243
+ # ==== Options
244
+ #
245
+ # Accepted key/value pairs in options parameter are:
246
+ # * +:timeout+ - Integer. A timeout in seconds.
247
+ #
248
+ # See http://msdn.microsoft.com/en-us/library/azure/jj159101
249
+ #
250
+ # Returns a list of Azure::Storage::Entity::SignedIdentifier instances
251
+ def get_queue_acl(queue_name, options={})
252
+ query = { "comp" => "acl" }
253
+ query["timeout"] = options[:timeout].to_s if options[:timeout]
254
+
255
+ response = call(:get, queue_uri(queue_name, query))
256
+
257
+ signed_identifiers = []
258
+ signed_identifiers = Serialization.signed_identifiers_from_xml(response.body) unless response.body == nil or response.body.length < 1
259
+ signed_identifiers
260
+ end
261
+
262
+ # Public: Sets the access control list (ACL) for the queue.
263
+ #
264
+ # ==== Attributes
265
+ #
266
+ # * +queue_name+ - String. The queue name.
267
+ # * +options+ - Hash. Optional parameters.
268
+ #
269
+ # ==== Options
270
+ #
271
+ # Accepted key/value pairs in options parameter are:
272
+ # * +:signed_identifiers+ - Array. A list of Azure::Storage::Entity::SignedIdentifier instances
273
+ # * +:timeout+ - Integer. A timeout in seconds.
274
+ #
275
+ # See http://msdn.microsoft.com/en-us/library/azure/jj159099
276
+ #
277
+ # Returns nil on success
278
+ def set_queue_acl(queue_name, options={})
279
+ query = { "comp" => "acl" }
280
+ query["timeout"] = options[:timeout].to_s if options[:timeout]
281
+
282
+ uri =queue_uri(queue_name, query)
283
+ body = nil
284
+ body = Serialization.signed_identifiers_to_xml(options[:signed_identifiers]) if options[:signed_identifiers] && options[:signed_identifiers].length > 0
285
+
286
+ call(:put, uri, body, {})
287
+ nil
288
+ end
289
+
290
+ # Public: Adds a message to the queue and optionally sets a visibility timeout for the message.
291
+ #
292
+ # ==== Attributes
293
+ #
294
+ # * +queue_name+ - String. The queue name.
295
+ # * +message_text+ - String. The message contents. Note that the message content must be in a format that may be encoded with UTF-8.
296
+ # * +options+ - Hash. Optional parameters.
297
+ #
298
+ # ==== Options
299
+ #
300
+ # Accepted key/value pairs in options parameter are:
301
+ # * +:visibility_timeout+ - Integer. Specifies the new visibility timeout value, in seconds, relative to server
302
+ # time. The new value must be larger than or equal to 0, and cannot be larger than 7
303
+ # days. The visibility timeout of a message cannot be set to a value later than the
304
+ # expiry time. :visibility_timeout should be set to a value smaller than the
305
+ # time-to-live value. If not specified, the default value is 0.
306
+ # * +:message_ttl+ - Integer. Specifies the time-to-live interval for the message, in seconds. The maximum
307
+ # time-to-live allowed is 7 days. If not specified, the default time-to-live is 7 days.
308
+ # * +:encode+ - Boolean. If set to true, the message will be base64 encoded.
309
+ # * +:timeout+ - Integer. A timeout in seconds.
310
+ #
311
+ # See http://msdn.microsoft.com/en-us/library/azure/dd179346
312
+ #
313
+ # Returns nil on success
314
+ def create_message(queue_name, message_text, options={})
315
+ query = { }
316
+
317
+ unless options.empty?
318
+ query["visibilitytimeout"] = options[:visibility_timeout] if options[:visibility_timeout]
319
+ query["messagettl"] = options[:message_ttl] if options[:message_ttl]
320
+ query["timeout"] = options[:timeout].to_s if options[:timeout]
321
+ end
322
+
323
+ uri = messages_uri(queue_name, query)
324
+ body = Serialization.message_to_xml(message_text, options[:encode])
325
+
326
+ call(:post, uri, body, {})
327
+ nil
328
+ end
329
+
330
+ # Public: Deletes a specified message from the queue.
331
+ #
332
+ # ==== Attributes
333
+ #
334
+ # * +queue_name+ - String. The queue name.
335
+ # * +message_id+ - String. The id of the message.
336
+ # * +pop_receipt+ - String. The valid pop receipt value returned from an earlier call to the Get Messages or
337
+ # Update Message operation.
338
+ # * +options+ - Hash. Optional parameters.
339
+ #
340
+ # ==== Options
341
+ #
342
+ # Accepted key/value pairs in options parameter are:
343
+ # * +:timeout+ - Integer. A timeout in seconds.
344
+ #
345
+ # See http://msdn.microsoft.com/en-us/library/azure/dd179347
346
+ #
347
+ # Returns nil on success
348
+ #
349
+ # Remarks:
350
+ #
351
+ # When a message is successfully deleted, it is immediately marked for deletion and is no longer accessible to
352
+ # clients. The message is later removed from the queue during garbage collection.
353
+ #
354
+ # After a client retrieves a message with the Get Messages operation, the client is expected to process and
355
+ # delete the message. To delete the message, you must have two items of data returned in the response body of
356
+ # the Get Messages operation:
357
+ #
358
+ # * The message ID, an opaque GUID value that identifies the message in the queue.
359
+ #
360
+ # * A valid pop receipt, an opaque value that indicates that the message has been retrieved.
361
+ #
362
+ # The message ID is returned from the previous Get Messages operation. The pop receipt is returned from the most
363
+ # recent Get Messages or Update Message operation. In order for the Delete Message operation to succeed, the pop
364
+ # receipt specified on the request must match the pop receipt returned from the Get Messages or Update Message
365
+ # operation.
366
+ #
367
+ # Pop receipts remain valid until one of the following events occurs:
368
+ #
369
+ # * The message has expired.
370
+ #
371
+ # * The message has been deleted using the last pop receipt received either from Get Messages or Update Message.
372
+ #
373
+ # * The invisibility time has elapsed and the message has been dequeued by a Get Messages request. When the
374
+ # invisibility time elapses, the message becomes visible again. If it is retrieved by another Get Messages
375
+ # request, the returned pop receipt can be used to delete or update the message.
376
+ #
377
+ # * The message has been updated with a new visibility timeout. When the message is updated, a new pop receipt
378
+ # will be returned.
379
+ #
380
+ # If the message has already been deleted when Delete Message is called, the Queue service returns status code
381
+ # 404 (Not Found).
382
+ #
383
+ # If a message with a matching pop receipt is not found, the service returns status code 400 (Bad Request), with
384
+ # additional error information indicating that the cause of the failure was a mismatched pop receipt.
385
+ #
386
+ def delete_message(queue_name, message_id, pop_receipt, options={})
387
+ query = { "popreceipt" => pop_receipt }
388
+ query["timeout"] = options[:timeout].to_s if options[:timeout]
389
+
390
+ uri = message_uri(queue_name, message_id, query)
391
+
392
+ call(:delete, uri)
393
+ nil
394
+ end
395
+
396
+ # Public: Retrieves one or more messages from the front of the queue, without changing the message visibility.
397
+ #
398
+ # ==== Attributes
399
+ #
400
+ # * +queue_name+ - String. The queue name.
401
+ # * +options+ - Hash. Optional parameters.
402
+ #
403
+ # ==== Options
404
+ #
405
+ # Accepted key/value pairs in options parameter are:
406
+ # * +:number_of_messages+ - Integer. How many messages to return. (optional, Default: 1)
407
+ # * +:decode+ - Boolean. Boolean value indicating if the message should be base64 decoded.
408
+ # * +:timeout+ - Integer. A timeout in seconds.
409
+ #
410
+ # See http://msdn.microsoft.com/en-us/library/azure/dd179472
411
+ #
412
+ # Returns a list of Azure::Storage::Entity::Queue::Message instances
413
+ def peek_messages(queue_name, options={})
414
+ number_of_messages=1
415
+ number_of_messages = options[:number_of_messages] if options[:number_of_messages]
416
+
417
+ query = { "peekonly" => "true", "numofmessages"=> number_of_messages.to_s }
418
+ query["timeout"] = options[:timeout].to_s if options[:timeout]
419
+
420
+ uri = messages_uri(queue_name, query)
421
+ response = call(:get, uri)
422
+
423
+ messages = Serialization.queue_messages_from_xml(response.body, options[:decode])
424
+ messages
425
+ end
426
+
427
+ # Public: Retrieves one or more messages from the front of the queue.
428
+ #
429
+ # ==== Attributes
430
+ #
431
+ # * +queue_name+ - String. The queue name.
432
+ # * +visibility_timeout+ - Integer. The new visibility timeout value, in seconds, relative to server time.
433
+ # * +options+ - Hash. Optional parameters.
434
+ #
435
+ # ==== Options
436
+ #
437
+ # Accepted key/value pairs in options parameter are:
438
+ # * +:number_of_messages+ - Integer. How many messages to return. (optional, Default: 1)
439
+ # * +:timeout+ - Integer. A timeout in seconds.
440
+ # * +:decode+ - Boolean. Boolean value indicating if the message should be base64 decoded.
441
+ #
442
+ # See http://msdn.microsoft.com/en-us/library/azure/dd179474
443
+ #
444
+ # Returns a list of Azure::Storage::Entity::Queue::Message instances
445
+ def list_messages(queue_name, visibility_timeout, options={})
446
+ number_of_messages=1
447
+ number_of_messages = options[:number_of_messages] if options[:number_of_messages]
448
+
449
+ query = { "visibilitytimeout" => visibility_timeout.to_s, "numofmessages"=> number_of_messages.to_s }
450
+ query["timeout"] = options[:timeout].to_s if options[:timeout]
451
+
452
+ uri = messages_uri(queue_name, query)
453
+ response = call(:get, uri)
454
+
455
+ messages = Serialization.queue_messages_from_xml(response.body, options[:decode])
456
+ messages
457
+ end
458
+
459
+ # Public: Adds a message to the queue and optionally sets a visibility timeout for the message.
460
+ #
461
+ # ==== Attributes
462
+ #
463
+ # * +queue_name+ - String. The queue name.
464
+ # * +message_id+ - String. The id of the message.
465
+ # * +pop_receipt+ - String. The valid pop receipt value returned from an earlier call to the Get Messages or
466
+ # update Message operation.
467
+ # * +message_text+ - String. The message contents. Note that the message content must be in a format that may
468
+ # be encoded with UTF-8.
469
+ # * +visibility_timeout+ - Integer. The new visibility timeout value, in seconds, relative to server time.
470
+ # * +options+ - Hash. Optional parameters.
471
+ #
472
+ # ==== Options
473
+ #
474
+ # Accepted key/value pairs in options parameter are:
475
+ # * +:encode+ - Boolean. If set to true, the message will be base64 encoded.
476
+ # * +:timeout+ - Integer. A timeout in seconds.
477
+ #
478
+ # See http://msdn.microsoft.com/en-us/library/azure/hh452234
479
+ #
480
+ # Returns a tuple of (pop_receipt, time_next_visible)
481
+ # * pop_receipt - String. The pop receipt of the queue message.
482
+ # * time_next_visible - String. A UTC date/time value that represents when the message will be visible on the queue.
483
+ #
484
+ # Remarks:
485
+ #
486
+ # An Update Message operation will fail if the specified message does not exist in the queue, or if the
487
+ # specified pop receipt does not match the message.
488
+ #
489
+ # A pop receipt is returned by the Get Messages operation or the Update Message operation. Pop receipts
490
+ # remain valid until one of the following events occurs:
491
+ #
492
+ # * The message has expired.
493
+ #
494
+ # * The message has been deleted using the last pop receipt received either from Get Messages or
495
+ # Update Message.
496
+ #
497
+ # * The invisibility time has elapsed and the message has been dequeued by a Get Messages request. When
498
+ # the invisibility time elapses, the message becomes visible again. If it is retrieved by another
499
+ # Get Messages request, the returned pop receipt can be used to delete or update the message.
500
+ #
501
+ # * The message has been updated with a new visibility timeout. When the message is updated, a new pop
502
+ # receipt will be returned.
503
+ #
504
+ # The Update Message operation can be used to continually extend the invisibility of a queue message. This
505
+ # functionality can be useful if you want a worker role to "lease" a queue message. For example, if a worker
506
+ # role calls Get Messages and recognizes that it needs more time to process a message, it can continually
507
+ # extend the message's invisibility until it is processed. If the worker role were to fail during processing,
508
+ # eventually the message would become visible again and another worker role could process it.
509
+ #
510
+ def update_message(queue_name, message_id, pop_receipt, message_text, visibility_timeout, options={})
511
+ query = { "visibilitytimeout" => visibility_timeout.to_s, "popreceipt" => pop_receipt }
512
+ query["timeout"] = options[:timeout].to_s if options[:timeout]
513
+
514
+ uri = message_uri(queue_name, message_id, query)
515
+ body = Serialization.message_to_xml(message_text, options[:encode])
516
+
517
+ response = call(:put, uri, body, {})
518
+ new_pop_receipt = response.headers["x-ms-popreceipt"]
519
+ time_next_visible = response.headers["x-ms-time-next-visible"]
520
+ return new_pop_receipt, time_next_visible
521
+ end
522
+
523
+ # Protected: Generate the URI for the collection of queues.
524
+ #
525
+ # ==== Attributes
526
+ #
527
+ # * +query+ - A Hash of query parameters (default: {}).
528
+ #
529
+ # Returns a URI.
530
+ protected
531
+ def collection_uri(query={})
532
+ query.update({:comp => 'list', :include => 'metadata'})
533
+ generate_uri("", query)
534
+ end
535
+
536
+ # Protected: Generate the URI for a given queue.
537
+ #
538
+ # ==== Attributes
539
+ #
540
+ # * +queue_name+ - The name of the queue.
541
+ # * +query+ - A Hash of query parameters (default: {}).
542
+ #
543
+ # Returns a URI.
544
+ protected
545
+ def queue_uri(queue_name, query={})
546
+ return queue_name if queue_name.kind_of? ::URI
547
+ generate_uri(queue_name, query)
548
+ end
549
+
550
+ # Protected: Generate the messages URI for the given queue.
551
+ #
552
+ # ==== Attributes
553
+ #
554
+ # * +queue_name+ - The name of the queue.
555
+ # * +query+ - A Hash of query parameters (default: {}).
556
+ #
557
+ # Returns a URI.
558
+ protected
559
+ def messages_uri(queue_name, query={})
560
+ generate_uri("#{queue_name}/messages", query)
561
+ end
562
+
563
+ # Protected: Generate the URI for a given message
564
+ #
565
+ # ==== Attributes
566
+ #
567
+ # * +queue_name+ - The name of the queue.
568
+ # * +message_id+ - The id of the message.
569
+ # * +query+ - A Hash of query parameters (default: {}).
570
+ #
571
+ # Returns a URI.
572
+ protected
573
+ def message_uri(queue_name, message_id, query={})
574
+ generate_uri("#{queue_name}/messages/#{message_id}", query)
575
+ end
576
+ end
577
+ end
578
+ end
579
+
580
+ Azure::Storage::QueueService = Azure::Storage::Queue::QueueService