Module: Azure::VirtualNetworkManagement::Serialization

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

Class Method Summary collapse

Class Method Details

.virtual_network_from_xml(network_xml) ⇒ Object



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

def self.virtual_network_from_xml(network_xml)
  virtual_networks = []
  virtual_network_services_xml = network_xml.css(
    'VirtualNetworkSites VirtualNetworkSite'
  )

  virtual_network_services_xml.each do |virtual_network_service_xml|
    virtual_network = VirtualNetwork.new
    virtual_network.name = xml_content(
      virtual_network_service_xml,
      'Name'
    )
    virtual_network.id = xml_content(
      virtual_network_service_xml,
      'Id'
    )
    virtual_network.affinity_group = xml_content(
      virtual_network_service_xml,
      'AffinityGroup'
    )
    virtual_network.location = xml_content(
      virtual_network_service_xml,
      'Location'
    )
    virtual_network.state = xml_content(
      virtual_network_service_xml,
      'State'
    )
    address_prefixes = virtual_network_service_xml.css(
      'AddressSpace AddressPrefixes AddressPrefix'
    )
    virtual_network.address_space = address_prefixes.map do |ap|
      ap.content
    end
    subnets_xml = virtual_network_service_xml.css('Subnets Subnet')
    virtual_network.subnets = subnets_xml.map do |sn|
      {
        name: xml_content(sn, 'Name'),
        address_prefix: xml_content(sn, 'AddressPrefix')
      }
    end
    dns_servers_xml = virtual_network_service_xml.css(
      'Dns DnsServers DnsServer'
    )
    virtual_network.dns_servers = dns_servers_xml.map do |dns|
      {
        name: xml_content(dns, 'Name'),
        ip_address: xml_content(dns, 'Address')
      }
    end

    virtual_networks << virtual_network
  end
  virtual_networks.compact
end

.virtual_network_to_xml(vnet_name, location, address_spaces, options = {}) ⇒ Object



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

def self.virtual_network_to_xml(vnet_name,
                                location,
                                address_spaces,
                                options = {})
  options[:dns] ||= {}
  options[:subnet] ||= {}
  options[:subnet].each do |subnet|
    IPAddr.validate_ip_and_prefix(subnet[:ip_address], subnet[:cidr])
  end
  address_spaces = address_spaces.compact.uniq.reject(&:empty?)
  address_spaces.each do |address_space|
    IPAddr.validate_address_space(address_space)
  end
  vnet_service = Azure::VirtualNetworkManagementService.new
  vnets = vnet_service.list_virtual_networks
  builder = Nokogiri::XML::Builder.new do |xml|
    xml.NetworkConfiguration(
      'xmlns' => 'http://schemas.microsoft.com/ServiceHosting'\
                 '/2011/07/NetworkConfiguration') do
      xml.VirtualNetworkConfiguration do
        xml.Dns do
          xml.DnsServers do
            dns = vnets.map(&:dns_servers)
            dns.flatten! unless dns.empty?
            dns_server_to_xml(xml, dns, options[:dns])
          end
        end
        xml.VirtualNetworkSites do
          vnets.each do |vnet|
            if vnet.name != vnet_name
              others = {
                subnet: vnet.subnets, dns: vnet.dns_servers
              }
              virtual_network_site(xml,
                                   vnet.name,
                                   vnet.location,
                                   vnet.affinity_group,
                                   vnet.address_space,
                                   others)
            end
          end
          virtual_network_site(xml,
                               vnet_name,
                               location,
                               nil,
                               address_spaces,
                               options)
        end
      end
    end
  end
  builder.doc.to_xml
end