1111# Deutsche Post E-POST Development GmbH - 2014, 2015
1212#
1313
14- #require 'pry'
15-
1614require 'rubygems'
1715require 'nokogiri'
1816
1917Puppet ::Type . type ( :onevnet ) . provide ( :cli ) do
20- desc " onevnet provider"
18+ desc ' onevnet provider'
2119
22- has_command ( :onevnet , " onevnet" ) do
20+ has_command ( :onevnet , ' onevnet' ) do
2321 environment :HOME => '/root' , :ONE_AUTH => '/var/lib/one/.one/one_auth'
2422 end
2523
2927 def create
3028 file = Tempfile . new ( "onevnet-#{ resource [ :name ] } " )
3129 builder = Nokogiri ::XML ::Builder . new do |xml |
32- xml . VNET do
33- xml . NAME resource [ :name ]
34- xml . BRIDGE resource [ :bridge ]
35- xml . PHYDEV do
36- resource [ :phydev ]
37- end if resource [ :phydev ]
38- xml . VLAN_ID do
39- resource [ :vlanid ]
40- end if resource [ :vlanid ]
41- xml . TEMPLATE do
42- xml . DNS do
43- resource [ :dnsservers ]
44- end
45- end if resource [ :dnsservers ]
46- xml . TEMPLATE do
47- xml . GATEWAY do
48- resource [ :gateway ]
49- end
50- end if resource [ :gateway ]
51- xml . TEMPLATE do
52- xml . NETWORK_MASK do
53- resource [ :netmask ]
54- end
55- end if resource [ :netmask ]
56- xml . TEMPLATE do
57- xml . NETWORK_ADDRESS do
58- resource [ :network_address ]
59- end
60- end if resource [ :network_address ]
61- xml . CONTEXT do
62- resource [ :context ] . each do |k , v |
63- xml . send ( k . upcase , v )
64- end if resource [ :context ]
65- end
66- end
67- # end xml vnet do
30+ xml . VNET do
31+ xml . NAME resource [ :name ]
32+ xml . BRIDGE resource [ :bridge ]
33+ xml . PHYDEV resource [ :phydev ] if resource [ :phydev ]
34+ xml . VLAN_ID resource [ :vlanid ] if resource [ :vlanid ]
35+ xml . DNS resource [ :dnsservers ] . join ( ' ' ) if resource [ :dnsservers ]
36+ xml . GATEWAY resource [ :gateway ] if resource [ :gateway ]
37+ xml . NETWORK_MASK resource [ :netmask ] if resource [ :netmask ]
38+ xml . NETWORK_ADDRESS resource [ :network_address ] if resource [ :network_address ]
39+ xml . CONTEXT resource [ :context ] . each { |k , v | xml . send ( k . upcase , v ) } if resource [ :context ]
40+ end
6841 end
69- # end builder
7042 tempfile = builder . to_xml
7143 file . write ( tempfile )
7244 file . close
@@ -89,31 +61,29 @@ def exists?
8961
9062 # Return the full hash of all existing onevnet resources
9163 def self . instances
92- vnets = Nokogiri ::XML ( onevnet ( 'list' , '-x' ) ) . root . xpath ( '/VNET_POOL/VNET' )
93- #pry.binding
94- vnets . collect do |vnet |
95- new (
96- :name => vnet . xpath ( './NAME' ) . text ,
97- :ensure => :present ,
98- :bridge => vnet . xpath ( './BRIDGE' ) . text ,
99- :context => nil ,
100- :dnsservers => ( vnet . xpath ( './TEMPLATE/DNS' ) . text . split ( " " ) unless vnet . xpath ( './TEMPLATE/DNS' ) . nil? ) ,
101- :gateway => ( vnet . xpath ( './TEMPLATE/GATEWAY' ) . text unless vnet . xpath ( './TEMPLATE/GATEWAY' ) . nil? ) ,
102- :netmask => ( vnet . xpath ( './TEMPLATE/NETWORK_MASK' ) . text unless vnet . xpath ( './TEMPLATE/NETWORK_MASK' ) . nil? ) ,
103- :network_address => ( vnet . xpath ( './TEMPLATE/NETWORK_ADDRESS' ) . text unless vnet . xpath ( './TEMPLATE/NETWORK_ADDRESS' ) . nil? ) ,
104- :model => ( vnet . xpath ( './TEMPLATE/MODEL' ) . text unless vnet . xpath ( './TEMPLATE/MODEL' ) . nil? ) ,
105- :phydev => vnet . xpath ( './PHYDEV' ) . text ,
106- :vlanid => vnet . xpath ( './VLAN_ID' ) . text
107- )
108- end
64+ vnets = Nokogiri ::XML ( onevnet ( 'list' , '-x' ) ) . root . xpath ( '/VNET_POOL/VNET' )
65+ vnets . collect do |vnet |
66+ new (
67+ :ensure => :present ,
68+ :name => vnet . xpath ( './NAME' ) . text ,
69+ :bridge => vnet . xpath ( './BRIDGE' ) . text ,
70+ :phydev => vnet . xpath ( './PHYDEV' ) . text ,
71+ :vlanid => vnet . xpath ( './VLAN_ID' ) . text ,
72+ :context => nil ,
73+ :dnsservers => ( vnet . xpath ( './TEMPLATE/DNS' ) . text . split ( ' ' ) unless vnet . xpath ( './TEMPLATE/DNS' ) . nil? ) ,
74+ :gateway => ( vnet . xpath ( './TEMPLATE/GATEWAY' ) . text unless vnet . xpath ( './TEMPLATE/GATEWAY' ) . nil? ) ,
75+ :netmask => ( vnet . xpath ( './TEMPLATE/NETWORK_MASK' ) . text unless vnet . xpath ( './TEMPLATE/NETWORK_MASK' ) . nil? ) ,
76+ :network_address => ( vnet . xpath ( './TEMPLATE/NETWORK_ADDRESS' ) . text unless vnet . xpath ( './TEMPLATE/NETWORK_ADDRESS' ) . nil? ) ,
77+ :model => ( vnet . xpath ( './TEMPLATE/MODEL' ) . text unless vnet . xpath ( './TEMPLATE/MODEL' ) . nil? )
78+ )
79+ end
10980 end
11081
11182 def self . prefetch ( resources )
11283 vnets = instances
11384 resources . keys . each do |name |
114- if provider = vnets . find { |vnet | vnet . name == name }
115- resources [ name ] . provider = provider
116- end
85+ provider = vnets . find { |vnet | vnet . name == name }
86+ resources [ name ] . provider = provider unless provider . nil?
11787 end
11888 end
11989
@@ -122,24 +92,22 @@ def flush
12292 file << @property_hash . map { |k , v |
12393 unless resource [ k ] . nil? or resource [ k ] . to_s . empty? or [ :name , :provider , :ensure ] . include? ( k )
12494 case k
125- when :vlanid
126- [ 'VLAN_ID' , v ]
127- when :addressrange
128- k . each_pair do |key , value |
129- end
130- when :dnsservers
131- [ 'DNS' , "\" #{ v . join ( " " ) } \" " ]
132- when :netmask
133- [ 'NETWORK_MASK' , v ]
134- else
135- [ k . to_s . upcase , v ]
95+ when :vlanid
96+ [ 'VLAN_ID' , v ]
97+ when :addressrange
98+ k . each_pair { |key , value |}
99+ when :dnsservers
100+ [ 'DNS' , "\" #{ v . join ( ' ' ) } \" " ]
101+ when :netmask
102+ [ 'NETWORK_MASK' , v ]
103+ else
104+ [ k . to_s . upcase , v ]
136105 end
137106 end
138- } . map { |a | "#{ a [ 0 ] } = #{ a [ 1 ] } " unless a . nil? } . join ( "\n " )
107+ } . map { |a | "#{ a [ 0 ] } = #{ a [ 1 ] } " unless a . nil? } . join ( "\n " )
139108 file . close
140109 self . debug ( IO . read file . path )
141110 onevnet ( 'update' , resource [ :name ] , file . path , '--append' ) unless @property_hash . empty?
142111 file . delete
143112 end
144-
145113end
0 commit comments