Module: Azure::StorageManagement::Serialization

Extended by:
Core::Utility
Defined in:
lib/azure/storage_management/serialization.rb

Overview

Storage management serialization module is responsible for converting the objects to XML and vice versa.

Class Method Summary collapse

Class Method Details

.add_options_to_xml(xml, options = {}) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/azure/storage_management/serialization.rb', line 165

def self.add_options_to_xml(xml, options = {})
  gre = options[:geo_replication_enabled]
  xml.GeoReplicationEnabled(
    gre
  ) unless gre.nil? || !(gre.is_a?(TrueClass) || gre.is_a?(FalseClass))
  xml.ExtendedProperties do
    options[:extended_properties].each do |name, value|
      xml.ExtendedProperty do
        xml.Name name
        xml.Value value
      end unless (name.to_s.empty?) || (value.to_s.empty?)
    end
  end unless options[:extended_properties].to_s.empty?
  xml.AccountType options[:account_type] if options[:account_type]
end

.regenerate_storage_account_keys_to_xml(key_type) ⇒ Object



192
193
194
195
196
197
198
199
200
201
# File 'lib/azure/storage_management/serialization.rb', line 192

def self.(key_type)
  builder = Nokogiri::XML::Builder.new do |xml|
    xml.RegenerateKeys(
      'xmlns' => 'http://schemas.microsoft.com/windowsazure'
    ) do
      xml.KeyType(key_type)
    end
  end
  builder.doc.to_xml
end

.storage_account_keys_from_xml(storage_xml) ⇒ Object



181
182
183
184
185
186
187
188
189
190
# File 'lib/azure/storage_management/serialization.rb', line 181

def self.(storage_xml)
  storage_xml.css('StorageService')
  storage_service_xml = storage_xml.css('StorageService').first
  service_key_xml = storage_service_xml.css('StorageServiceKeys').first
   = StorageAccountKeys.new
  .url = xml_content(storage_service_xml, 'Url')
  .primary_key = xml_content(service_key_xml, 'Primary')
  .secondary_key = xml_content(service_key_xml, 'Secondary')
  
end

.storage_services_from_xml(storage_xml) ⇒ Object



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
# File 'lib/azure/storage_management/serialization.rb', line 45

def self.storage_services_from_xml(storage_xml)
  storage_accounts = []
  storage_services_xml = storage_xml.css('StorageService')
  storage_services_xml.each do |storage_service_xml|
     = StorageAccount.new

    .url = xml_content(storage_service_xml, 'Url')
    .name = xml_content(
      storage_service_xml, 'ServiceName'
    )
    storage_service_properties = storage_service_xml.css(
      'StorageServiceProperties'
    )
    .description = xml_content(
      storage_service_properties, 'Description'
    )
    .affinity_group = xml_content(
      storage_service_properties, 'AffinityGroup'
    )
    .location = xml_content(
      storage_service_properties, 'Location'
    )
    .label = Base64.decode64(
      xml_content(storage_service_properties, 'Label')
    )
    .status = xml_content(
      storage_service_properties, 'Status'
    )
    .endpoints = storage_service_properties.css(
      'Endpoints Endpoint'
    ).map { |endpoint| endpoint.content }
    .geo_replication_enabled = xml_content(
      storage_service_properties, 'GeoReplicationEnabled'
    )
    . = xml_content(
      storage_service_properties, 'AccountType'
    )
    .geo_primary_region = xml_content(
      storage_service_properties, 'GeoPrimaryRegion'
    )
    .status_of_primary = xml_content(
      storage_service_properties, 'StatusOfPrimary'
    )
    .last_geo_failover_time = xml_content(
      storage_service_properties, 'LastGeoFailoverTime'
    )
    .geo_secondary_region = xml_content(
      storage_service_properties, 'GeoSecondaryRegion'
    )
    .status_of_secondary = xml_content(
      storage_service_properties, 'StatusOfSecondary'
    )
    .creation_time = xml_content(
      storage_service_properties, 'CreationTime'
    )
    .extended_properties = storage_service_xml.css(
      'ExtendedProperties ExtendedProperty'
    ).map do |prop|
      {
        name: xml_content(prop, 'Name'),
        value: xml_content(prop, 'Value')
      }
    end

    storage_accounts << 
  end

  storage_accounts.compact
end

.storage_services_to_xml(name, options = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/azure/storage_management/serialization.rb', line 24

def self.storage_services_to_xml(name, options = {})
  builder = Nokogiri::XML::Builder.new do |xml|
    xml.CreateStorageServiceInput(
      'xmlns' => 'http://schemas.microsoft.com/windowsazure'
    ) do
      xml.ServiceName(name)
      label = options[:label] || name
      xml.Label(Base64.encode64(label))
      xml.Description options[:description]\
        || 'Explicitly created storage service'
      unless options[:affinity_group_name].nil?
        xml.AffinityGroup options[:affinity_group_name]
      else
        xml.Location options[:location]
      end
      add_options_to_xml(xml, options)
    end
  end
  builder.doc.to_xml
end

.storage_update_to_xml(options) ⇒ Object



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
# File 'lib/azure/storage_management/serialization.rb', line 115

def self.storage_update_to_xml(options)
  # Cannot update if options is nil or empty
  fail 'No options specified' if options.empty?

  # Either one of Label, or Description is required.
  if (options[:label].nil? || options[:label].empty?) &&
      (options[:description].nil? || options[:description].empty?)
    fail 'Either one of Label or Description'\
      ' has to be provided. Both cannot be empty'
  end

  # The input param may not be nil or empty, but the values inside may
  # be. Check if atleast one value exists before proceeding.
  is_empty = true
  options.each do |option, value|
    case option
    when :description, :label
      is_empty = value.nil? || value.empty?
    when :geo_replication_enabled
      is_empty = !(value.is_a?(TrueClass) || value.is_a?(FalseClass))
    when :extended_properties
      value.each do |p, v|
        is_empty = ((p.nil? || p.empty?) || (v.nil? || v.empty?))
        break unless is_empty
      end
    end
    break unless is_empty
  end

  # Raise a RuntimeError if no options were provided
  fail 'No Options Specified' if is_empty

  builder = Nokogiri::XML::Builder.new do |xml|
    xml.UpdateStorageServiceInput(
      'xmlns' => 'http://schemas.microsoft.com/windowsazure'
    ) do
      # Check if label is nil. Use description only if label is nil
      if options[:label].nil? || options[:label].empty?
        desc = options[:description]
        xml.Description(desc) unless desc.nil? || desc.empty?
      else
        label = Base64.encode64(options[:label])
        xml.Label(label) unless label.nil? || label.empty?
      end
      add_options_to_xml(xml, options)
    end
  end
  builder.to_xml
end