1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 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 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
|
# frozen_string_literal: true
#-------------------------------------------------------------------------
# # Copyright (c) Microsoft and contributors. All rights reserved.
#
# The MIT License(MIT)
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files(the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions :
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#--------------------------------------------------------------------------
require "nokogiri"
require "time"
require "azure/storage/common/service/enumeration_results"
require "azure/storage/common/service/signed_identifier"
require "azure/storage/common/service/access_policy"
require "azure/storage/common/service/storage_service_properties"
require "azure/storage/common/service/logging"
require "azure/storage/common/service/metrics"
require "azure/storage/common/service/retention_policy"
require "azure/storage/common/service/cors"
require "azure/storage/common/service/cors_rule"
require "azure/storage/common/service/storage_service_stats"
module Azure::Storage::Common
module Service
module Serialization
module ClassMethods
def signed_identifiers_from_xml(xml)
xml = slopify(xml)
expect_node("SignedIdentifiers", xml)
identifiers = []
return identifiers unless (xml > "SignedIdentifier").any?
if xml.SignedIdentifier.count == 0
identifiers.push(signed_identifier_from_xml(xml.SignedIdentifier))
else
xml.SignedIdentifier.each { |identifier_node|
identifiers.push(signed_identifier_from_xml(identifier_node))
}
end
identifiers
end
def signed_identifiers_to_xml(signed_identifiers)
builder = Nokogiri::XML::Builder.new(encoding: "utf-8") do |xml|
xml.SignedIdentifiers {
signed_identifiers.each do |identifier|
xml.SignedIdentifier {
xml.Id identifier.id
xml.AccessPolicy {
xml.Start identifier.access_policy.start
xml.Expiry identifier.access_policy.expiry
xml.Permission identifier.access_policy.permission
}
}
end
}
end
builder.to_xml
end
def signed_identifier_from_xml(xml)
xml = slopify(xml)
expect_node("SignedIdentifier", xml)
SignedIdentifier.new do |identifier|
identifier.id = xml.Id.text if (xml > "Id").any?
identifier.access_policy = access_policy_from_xml(xml.AccessPolicy) if (xml > "AccessPolicy").any?
end
end
def access_policy_from_xml(xml)
xml = slopify(xml)
expect_node("AccessPolicy", xml)
AccessPolicy.new do |policy|
policy.start = xml.Start.text if (xml > "Start").any?
policy.expiry = xml.Expiry.text if (xml > "Expiry").any?
policy.permission = xml.Permission.text if (xml > "Permission").any?
end
end
def enumeration_results_from_xml(xml, results)
xml = slopify(xml)
expect_node("EnumerationResults", xml)
results = results || Service::EnumerationResults.new;
results.continuation_token = xml.NextMarker.text if (xml > "NextMarker").any?
results
end
def metadata_from_xml(xml)
xml = slopify(xml)
expect_node("Metadata", xml)
metadata = {}
xml.children.each { |meta_node|
key = meta_node.name.downcase
if metadata.has_key? key
metadata[key] = [metadata[key]] unless metadata[key].respond_to? :push
metadata[key].push(meta_node.text)
else
metadata[key] = meta_node.text
end
}
metadata
end
def metadata_from_headers(headers)
metadata = {}
headers.each { |k, v|
if key = k[/^x-ms-meta-(.*)/, 1]
if metadata.has_key? key
metadata[key] = [metadata[key]] unless metadata[key].respond_to? :push
metadata[key].push(v)
else
metadata[key] = v
end
end
}
metadata
end
def retention_policy_to_xml(retention_policy, xml)
xml.RetentionPolicy {
xml.Enabled retention_policy.enabled
xml.Days retention_policy.days if retention_policy.enabled && retention_policy.days
} if retention_policy
end
def retention_policy_from_xml(xml)
xml = slopify(xml)
expect_node("RetentionPolicy", xml)
RetentionPolicy.new do |policy|
policy.enabled = to_bool(xml.Enabled.text) if (xml > "Enabled").any?
policy.days = xml.Days.text.to_i if (xml > "Days").any?
end
end
def metrics_to_xml_children(metrics, xml)
return unless metrics
xml.Version metrics.version
xml.Enabled metrics.enabled
xml.IncludeAPIs metrics.include_apis if metrics.enabled
retention_policy_to_xml(metrics.retention_policy, xml) if metrics.retention_policy
end
def hour_metrics_to_xml(metrics, xml)
xml.HourMetrics {
metrics_to_xml_children(metrics, xml)
} if metrics
end
def minute_metrics_to_xml(metrics, xml)
xml.MinuteMetrics {
metrics_to_xml_children(metrics, xml)
} if metrics
end
def metrics_from_xml(xml)
xml = slopify(xml)
Metrics.new do |metrics|
metrics.version = xml.Version.text if (xml > "Version").any?
metrics.enabled = to_bool(xml.Enabled.text) if (xml > "Enabled").any?
metrics.include_apis = to_bool(xml.IncludeAPIs.text) if (xml > "IncludeAPIs").any?
metrics.retention_policy = retention_policy_from_xml(xml.RetentionPolicy)
end
end
def logging_to_xml(logging, xml)
xml.Logging {
xml.Version logging.version
xml.Delete logging.delete
xml.Read logging.read
xml.Write logging.write
retention_policy_to_xml(logging.retention_policy, xml)
} if logging
end
def logging_from_xml(xml)
xml = slopify(xml)
expect_node("Logging", xml)
Logging.new do |logging|
logging.version = xml.Version.text if (xml > "Version").any?
logging.delete = to_bool(xml.Delete.text) if (xml > "Delete").any?
logging.read = to_bool(xml.Read.text) if (xml > "Read").any?
logging.write = to_bool(xml.Write.text) if (xml > "Write").any?
logging.retention_policy = retention_policy_from_xml(xml.RetentionPolicy)
end
end
def cors_to_xml(cors, xml)
xml.Cors {
cors.cors_rules.to_a.each do |cors_rule|
cors_rule_to_xml(cors_rule, xml)
end
}
end
def cors_rule_to_xml(cors_rule, xml)
xml.CorsRule {
xml.AllowedOrigins cors_rule.allowed_origins.join(",") if cors_rule.allowed_origins
xml.AllowedMethods cors_rule.allowed_methods.join(",") if cors_rule.allowed_methods
xml.MaxAgeInSeconds cors_rule.max_age_in_seconds if cors_rule.max_age_in_seconds
xml.ExposedHeaders cors_rule.exposed_headers.join(",") if cors_rule.exposed_headers
xml.AllowedHeaders cors_rule.allowed_headers.join(",") if cors_rule.allowed_headers
}
end
def cors_from_xml(xml)
xml = slopify(xml)
expect_node("Cors", xml)
Cors.new do |cors|
cors.cors_rules = xml.children.to_a.map { |child| cors_rule_from_xml(child) }
end
end
def cors_rule_from_xml(xml)
xml = slopify(xml)
expect_node("CorsRule", xml)
CorsRule.new do |cors_rule|
cors_rule.allowed_origins = ary_from_node(xml.AllowedOrigins) if (xml > "AllowedOrigins").any?
cors_rule.allowed_methods = ary_from_node(xml.AllowedMethods) if (xml > "AllowedMethods").any?
cors_rule.max_age_in_seconds = xml.MaxAgeInSeconds.text.to_i if (xml > "MaxAgeInSeconds").any?
cors_rule.exposed_headers = ary_from_node(xml.ExposedHeaders) if (xml > "ExposedHeaders").any?
cors_rule.allowed_headers = ary_from_node(xml.AllowedHeaders) if (xml > "AllowedHeaders").any?
end
end
def geo_replication_from_xml(xml)
xml = slopify(xml)
expect_node("GeoReplication", xml)
GeoReplication.new do |geo_replication|
geo_replication.status = xml.Status.text if (xml > "Status").any?
geo_replication.last_sync_time =
begin
Time.parse(xml.LastSyncTime.text) if (xml > "LastSyncTime").any?
rescue
nil
end
end
end
def ary_from_node(node)
node.text.split(",").map { |s| s.strip }
end
def service_properties_to_xml(properties)
builder = Nokogiri::XML::Builder.new(encoding: "utf-8") do |xml|
xml.StorageServiceProperties {
xml.DefaultServiceVersion(properties.default_service_version) if properties.default_service_version
logging_to_xml(properties.logging, xml) if properties.logging
hour_metrics_to_xml(properties.hour_metrics, xml) if properties.hour_metrics
minute_metrics_to_xml(properties.minute_metrics, xml) if properties.minute_metrics
cors_to_xml(properties.cors, xml) if properties.cors
}
end
builder.to_xml
end
def service_properties_from_xml(xml)
xml = slopify(xml)
expect_node("StorageServiceProperties", xml)
StorageServiceProperties.new do |props|
props.default_service_version = xml.DefaultServiceVersion.text if (xml > "DefaultServiceVersion").any?
props.logging = logging_from_xml(xml.Logging) if (xml > "Logging").any?
props.hour_metrics = metrics_from_xml(xml.HourMetrics) if (xml > "HourMetrics").any?
props.minute_metrics = metrics_from_xml(xml.MinuteMetrics) if (xml > "MinuteMetrics").any?
props.cors = cors_from_xml(xml.Cors) if (xml > "Cors").any?
end
end
def service_stats_from_xml(xml)
xml = slopify(xml)
expect_node("StorageServiceStats", xml)
StorageServiceStats.new do |stats|
stats.geo_replication = geo_replication_from_xml(xml.GeoReplication)
end
end
def to_bool(s)
(s || "").downcase == "true"
end
def slopify(xml)
node = (xml.is_a? String) ? Nokogiri.Slop(xml).root : xml
node.slop! if node.is_a? Nokogiri::XML::Document unless node.respond_to? :method_missing
node = node.root if node.is_a? Nokogiri::XML::Document
node
end
def expect_node(node_name, xml)
raise "Xml is not a #{node_name} node. xml:\n#{xml}" unless xml.name == node_name
end
end
extend ClassMethods
def self.included(other)
other.extend(ClassMethods)
end
end
end
end
|