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,113 @@
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/serialization'
25
+
26
+ require 'azure/storage/queue/queue'
27
+ require 'azure/storage/queue/message'
28
+
29
+ module Azure::Storage
30
+ module Queue
31
+ module Serialization
32
+ include Service::Serialization
33
+
34
+ def self.queue_messages_from_xml(xml, decode)
35
+ xml = slopify(xml)
36
+
37
+ expect_node("QueueMessagesList", xml)
38
+ results = []
39
+ return results unless (xml > "QueueMessage").any?
40
+
41
+ if xml.QueueMessage.count == 0
42
+ results.push(queue_message_from_xml(xml.QueueMessage, decode))
43
+ else
44
+ xml.QueueMessage.each { |message_node|
45
+ results.push(queue_message_from_xml(message_node, decode))
46
+ }
47
+ end
48
+
49
+ results
50
+ end
51
+
52
+ def self.queue_message_from_xml(xml, decode)
53
+ xml = slopify(xml)
54
+ expect_node("QueueMessage", xml)
55
+
56
+ Message.new do |msg|
57
+ msg.id = xml.MessageId.text if (xml > "MessageId").any?
58
+ msg.insertion_time = xml.InsertionTime.text if (xml > "InsertionTime").any?
59
+ msg.expiration_time = xml.ExpirationTime.text if (xml > "ExpirationTime").any?
60
+ msg.dequeue_count = xml.DequeueCount.text.to_i if (xml > "DequeueCount").any?
61
+ msg.message_text = xml.MessageText.text if (xml > "MessageText").any?
62
+ msg.time_next_visible = xml.TimeNextVisible.text if (xml > "TimeNextVisible").any?
63
+ msg.pop_receipt = xml.PopReceipt.text if (xml > "PopReceipt").any?
64
+
65
+ msg.message_text = Base64.decode64(msg.message_text) if decode
66
+ end
67
+ end
68
+
69
+ def self.message_to_xml(message_text, encode)
70
+ if encode
71
+ builder = Nokogiri::XML::Builder.new do |xml|
72
+ xml.QueueMessage { xml.MessageText Base64.encode64(message_text) }
73
+ end
74
+ else
75
+ builder = Nokogiri::XML::Builder.new(:encoding => 'utf-8') do |xml|
76
+ xml.QueueMessage { xml.MessageText message_text.encode("utf-8") }
77
+ end
78
+ end
79
+ builder.to_xml
80
+ end
81
+
82
+ def self.queue_enumeration_results_from_xml(xml)
83
+ xml = slopify(xml)
84
+ expect_node("EnumerationResults", xml)
85
+
86
+ results = enumeration_results_from_xml(xml, Azure::Service::EnumerationResults.new)
87
+
88
+ return results unless (xml > "Queues").any? && ((xml > "Queues") > "Queue").any?
89
+
90
+ if xml.Queues.Queue.count == 0
91
+ results.push(queue_from_xml(xml.Queues.Queue))
92
+ else
93
+ xml.Queues.Queue.each { |queue_node|
94
+ results.push(queue_from_xml(queue_node))
95
+ }
96
+ end
97
+
98
+ results
99
+ end
100
+
101
+ def self.queue_from_xml(xml)
102
+ xml = slopify(xml)
103
+ expect_node("Queue", xml)
104
+
105
+ queue = Queue.new
106
+ queue.name = xml.Name.text if (xml > "Name").any?
107
+ queue.metadata = metadata_from_xml(xml.Metadata) if (xml > "Metadata").any?
108
+
109
+ queue
110
+ end
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,35 @@
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 Service
26
+ class AccessPolicy
27
+ def initialize
28
+ yield self if block_given?
29
+ end
30
+ attr_accessor :start
31
+ attr_accessor :expiry
32
+ attr_accessor :permission
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,36 @@
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
+ module Azure::Storage
26
+ module Service
27
+ class Cors
28
+ def initialize
29
+ @cors_rules = []
30
+ yield self if block_given?
31
+ end
32
+
33
+ attr_accessor :cors_rules
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,46 @@
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
+ module Azure::Storage
26
+ module Service
27
+ class CorsRule
28
+ def initialize
29
+ # All CORS rule elements are required if the CorsRule element is specified.
30
+ # The request will fail with error code 400 (Bad Request) if any element is missing.
31
+ @allowed_origins = []
32
+ @allowed_methods = []
33
+ @exposed_headers = []
34
+ @allowed_headers = []
35
+ @max_age_in_seconds = 0
36
+ yield self if block_given?
37
+ end
38
+
39
+ attr_accessor :allowed_origins
40
+ attr_accessor :allowed_methods
41
+ attr_accessor :max_age_in_seconds
42
+ attr_accessor :exposed_headers
43
+ attr_accessor :allowed_headers
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,30 @@
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
25
+ module Service
26
+ class EnumerationResults < Array
27
+ attr_accessor :continuation_token
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,45 @@
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/retention_policy'
25
+
26
+ module Azure::Storage
27
+ module Service
28
+ class Logging
29
+ def initialize
30
+ @version = "1.0"
31
+ @delete = false
32
+ @read = false
33
+ @write = false
34
+ @retention_policy = RetentionPolicy.new
35
+ yield self if block_given?
36
+ end
37
+
38
+ attr_accessor :version
39
+ attr_accessor :delete
40
+ attr_accessor :read
41
+ attr_accessor :write
42
+ attr_accessor :retention_policy
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,43 @@
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/retention_policy'
25
+
26
+ module Azure::Storage
27
+ module Service
28
+ class Metrics
29
+ def initialize
30
+ @version = "1.0"
31
+ @enabled = false
32
+ @include_apis = false
33
+ @retention_policy = RetentionPolicy.new
34
+ yield self if block_given?
35
+ end
36
+
37
+ attr_accessor :version
38
+ attr_accessor :enabled
39
+ attr_accessor :include_apis
40
+ attr_accessor :retention_policy
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,35 @@
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 Service
26
+ class RetentionPolicy
27
+ def initialize
28
+ @enabled = false
29
+ yield self if block_given?
30
+ end
31
+ attr_accessor :enabled
32
+ attr_accessor :days
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,308 @@
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 'nokogiri'
25
+
26
+ require 'azure/storage/service/enumeration_results'
27
+ require 'azure/storage/service/signed_identifier'
28
+ require 'azure/storage/service/access_policy'
29
+ require 'azure/storage/service/storage_service_properties'
30
+ require 'azure/storage/service/logging'
31
+ require 'azure/storage/service/metrics'
32
+ require 'azure/storage/service/retention_policy'
33
+ require 'azure/storage/service/cors'
34
+ require 'azure/storage/service/cors_rule'
35
+
36
+ module Azure::Storage
37
+ module Service
38
+ module Serialization
39
+ module ClassMethods
40
+
41
+ def signed_identifiers_from_xml(xml)
42
+ xml = slopify(xml)
43
+ expect_node("SignedIdentifiers", xml)
44
+
45
+ identifiers = []
46
+ return identifiers unless (xml > "SignedIdentifier").any?
47
+
48
+ if xml.SignedIdentifier.count == 0
49
+ identifiers.push(signed_identifier_from_xml(xml.SignedIdentifier))
50
+ else
51
+ xml.SignedIdentifier.each { |identifier_node|
52
+ identifiers.push(signed_identifier_from_xml(identifier_node))
53
+ }
54
+ end
55
+
56
+ identifiers
57
+ end
58
+
59
+ def signed_identifiers_to_xml(signed_identifiers)
60
+ builder = Nokogiri::XML::Builder.new(:encoding=>"utf-8") do |xml|
61
+ xml.SignedIdentifiers {
62
+ signed_identifiers.each do |identifier|
63
+ xml.SignedIdentifier {
64
+ xml.Id identifier.id
65
+ xml.AccessPolicy {
66
+ xml.Start identifier.access_policy.start
67
+ xml.Expiry identifier.access_policy.expiry
68
+ xml.Permission identifier.access_policy.permission
69
+ }
70
+ }
71
+ end
72
+ }
73
+ end
74
+ builder.to_xml
75
+ end
76
+
77
+ def signed_identifier_from_xml(xml)
78
+ xml = slopify(xml)
79
+ expect_node("SignedIdentifier", xml)
80
+
81
+ SignedIdentifier.new do |identifier|
82
+ identifier.id = xml.Id.text if (xml > "Id").any?
83
+ identifier.access_policy = access_policy_from_xml(xml.AccessPolicy) if (xml > "AccessPolicy").any?
84
+ end
85
+ end
86
+
87
+ def access_policy_from_xml(xml)
88
+ xml = slopify(xml)
89
+ expect_node("AccessPolicy", xml)
90
+
91
+ AccessPolicy.new do |policy|
92
+ policy.start = xml.Start.text if (xml > "Start").any?
93
+ policy.expiry = xml.Expiry.text if (xml > "Expiry").any?
94
+ policy.permission = xml.Permission.text if (xml > "Permission").any?
95
+ end
96
+ end
97
+
98
+ def enumeration_results_from_xml(xml, results)
99
+ xml = slopify(xml)
100
+ expect_node("EnumerationResults", xml)
101
+
102
+ results = results || Azure::Service::EnumerationResults.new;
103
+
104
+ results.continuation_token = xml.NextMarker.text if (xml > "NextMarker").any?
105
+ results
106
+ end
107
+
108
+ def metadata_from_xml(xml)
109
+ xml = slopify(xml)
110
+ expect_node("Metadata", xml)
111
+
112
+ metadata = {}
113
+
114
+ xml.children.each { |meta_node|
115
+
116
+ key = meta_node.name.downcase
117
+ if metadata.has_key? key
118
+ metadata[key] = [metadata[key]] unless metadata[key].respond_to? :push
119
+ metadata[key].push(meta_node.text)
120
+ else
121
+ metadata[key] = meta_node.text
122
+ end
123
+ }
124
+ metadata
125
+ end
126
+
127
+ def metadata_from_headers(headers)
128
+ metadata = {}
129
+
130
+ headers.each { |k, v|
131
+ if key = k[/^x-ms-meta-(.*)/, 1]
132
+ if metadata.has_key? key
133
+ metadata[key] = [metadata[key]] unless metadata[key].respond_to? :push
134
+ metadata[key].push(v)
135
+ else
136
+ metadata[key] = v
137
+ end
138
+ end
139
+ }
140
+
141
+ metadata
142
+ end
143
+
144
+ def retention_policy_to_xml(retention_policy, xml)
145
+ xml.RetentionPolicy {
146
+ xml.Enabled retention_policy.enabled
147
+ xml.Days retention_policy.days if retention_policy.enabled && retention_policy.days
148
+ } if retention_policy
149
+ end
150
+
151
+ def retention_policy_from_xml(xml)
152
+ xml = slopify(xml)
153
+ expect_node("RetentionPolicy", xml)
154
+
155
+ RetentionPolicy.new do |policy|
156
+ policy.enabled = to_bool(xml.Enabled.text) if (xml > "Enabled").any?
157
+ policy.days = xml.Days.text.to_i if (xml > "Days").any?
158
+ end
159
+ end
160
+
161
+ def metrics_to_xml_children(metrics, xml)
162
+ return unless metrics
163
+ xml.Version metrics.version
164
+ xml.Enabled metrics.enabled
165
+ xml.IncludeAPIs metrics.include_apis if metrics.enabled
166
+ retention_policy_to_xml(metrics.retention_policy, xml) if metrics.retention_policy
167
+ end
168
+
169
+ def hour_metrics_to_xml(metrics, xml)
170
+ xml.HourMetrics {
171
+ metrics_to_xml_children(metrics, xml)
172
+ } if metrics
173
+ end
174
+
175
+ def minute_metrics_to_xml(metrics, xml)
176
+ xml.MinuteMetrics {
177
+ metrics_to_xml_children(metrics, xml)
178
+ } if metrics
179
+ end
180
+
181
+ def metrics_from_xml(xml)
182
+ xml = slopify(xml)
183
+
184
+ Metrics.new do |metrics|
185
+ metrics.version = xml.Version.text if (xml > "Version").any?
186
+ metrics.enabled = to_bool(xml.Enabled.text) if (xml > "Enabled").any?
187
+ metrics.include_apis = to_bool(xml.IncludeAPIs.text) if (xml > "IncludeAPIs").any?
188
+ metrics.retention_policy = retention_policy_from_xml(xml.RetentionPolicy)
189
+ end
190
+ end
191
+
192
+ def logging_to_xml(logging, xml)
193
+ xml.Logging {
194
+ xml.Version logging.version
195
+ xml.Delete logging.delete
196
+ xml.Read logging.read
197
+ xml.Write logging.write
198
+ retention_policy_to_xml(logging.retention_policy, xml)
199
+ } if logging
200
+ end
201
+
202
+ def logging_from_xml(xml)
203
+ xml = slopify(xml)
204
+ expect_node("Logging", xml)
205
+
206
+ Logging.new do |logging|
207
+ logging.version = xml.Version.text if (xml > "Version").any?
208
+ logging.delete = to_bool(xml.Delete.text) if (xml > "Delete").any?
209
+ logging.read = to_bool(xml.Read.text) if (xml > "Read").any?
210
+ logging.write = to_bool(xml.Write.text) if (xml > "Write").any?
211
+ logging.retention_policy = retention_policy_from_xml(xml.RetentionPolicy)
212
+ end
213
+ end
214
+
215
+ def cors_to_xml(cors, xml)
216
+ xml.Cors {
217
+ cors.cors_rules.to_a.each do |cors_rule|
218
+ cors_rule_to_xml(cors_rule, xml)
219
+ end
220
+ }
221
+ end
222
+
223
+ def cors_rule_to_xml(cors_rule, xml)
224
+ xml.CorsRule {
225
+ xml.AllowedOrigins cors_rule.allowed_origins.join(",") if cors_rule.allowed_origins
226
+ xml.AllowedMethods cors_rule.allowed_methods.join(",") if cors_rule.allowed_methods
227
+ xml.MaxAgeInSeconds cors_rule.max_age_in_seconds if cors_rule.max_age_in_seconds
228
+ xml.ExposedHeaders cors_rule.exposed_headers.join(",") if cors_rule.exposed_headers
229
+ xml.AllowedHeaders cors_rule.allowed_headers.join(",") if cors_rule.allowed_headers
230
+ }
231
+ end
232
+
233
+ def cors_from_xml(xml)
234
+ xml = slopify(xml)
235
+ expect_node("Cors", xml)
236
+
237
+ Cors.new do |cors|
238
+ cors.cors_rules = xml.children.to_a.map {|child| cors_rule_from_xml(child)}
239
+ end
240
+ end
241
+
242
+ def cors_rule_from_xml(xml)
243
+ xml = slopify(xml)
244
+ expect_node("CorsRule", xml)
245
+
246
+ CorsRule.new do |cors_rule|
247
+ cors_rule.allowed_origins = ary_from_node(xml.AllowedOrigins) if (xml > "AllowedOrigins").any?
248
+ cors_rule.allowed_methods = ary_from_node(xml.AllowedMethods) if (xml > "AllowedMethods").any?
249
+ cors_rule.max_age_in_seconds = xml.MaxAgeInSeconds.text.to_i if (xml > "MaxAgeInSeconds").any?
250
+ cors_rule.exposed_headers = ary_from_node(xml.ExposedHeaders) if (xml > "ExposedHeaders").any?
251
+ cors_rule.allowed_headers = ary_from_node(xml.AllowedHeaders) if (xml > "AllowedHeaders").any?
252
+ end
253
+ end
254
+
255
+ def ary_from_node(node)
256
+ node.text.split(",").map {|s| s.strip}
257
+ end
258
+
259
+ def service_properties_to_xml(properties)
260
+ builder = Nokogiri::XML::Builder.new(:encoding => 'utf-8') do |xml|
261
+ xml.StorageServiceProperties {
262
+ xml.DefaultServiceVersion(properties.default_service_version) if properties.default_service_version
263
+ logging_to_xml(properties.logging, xml) if properties.logging
264
+ hour_metrics_to_xml(properties.hour_metrics, xml) if properties.hour_metrics
265
+ minute_metrics_to_xml(properties.minute_metrics, xml) if properties.minute_metrics
266
+ cors_to_xml(properties.cors, xml) if properties.cors
267
+ }
268
+ end
269
+ builder.to_xml
270
+ end
271
+
272
+ def service_properties_from_xml(xml)
273
+ xml = slopify(xml)
274
+ expect_node("StorageServiceProperties", xml)
275
+
276
+ StorageServiceProperties.new do |props|
277
+ props.default_service_version = xml.DefaultServiceVersion.text if (xml > "DefaultServiceVersion").any?
278
+ props.logging = logging_from_xml(xml.Logging)
279
+ props.hour_metrics = metrics_from_xml(xml.HourMetrics)
280
+ props.minute_metrics = metrics_from_xml(xml.MinuteMetrics)
281
+ props.cors = cors_from_xml(xml.Cors)
282
+ end
283
+ end
284
+
285
+ def to_bool(s)
286
+ (s || "").downcase == 'true'
287
+ end
288
+
289
+ def slopify(xml)
290
+ node = (xml.is_a? String) ? Nokogiri.Slop(xml).root : xml
291
+ node.slop! if node.is_a? Nokogiri::XML::Document unless node.respond_to? :method_missing
292
+ node = node.root if node.is_a? Nokogiri::XML::Document
293
+ node
294
+ end
295
+
296
+ def expect_node(node_name, xml)
297
+ raise "Xml is not a #{node_name} node." unless xml.name == node_name
298
+ end
299
+ end
300
+
301
+ extend ClassMethods
302
+
303
+ def self.included( other )
304
+ other.extend( ClassMethods )
305
+ end
306
+ end
307
+ end
308
+ end