Skip to content

Commit

Permalink
Merge pull request #124 from tuxmea/provider_to_nokigiri
Browse files Browse the repository at this point in the history
Provider to nokigiri
  • Loading branch information
rwaffen committed Jan 23, 2015
2 parents cf5648e + 701b043 commit ce37b4e
Show file tree
Hide file tree
Showing 14 changed files with 255 additions and 369 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ group :development, :test do
gem 'puppetlabs_spec_helper', :require => false
gem 'puppet-lint', :require => false
gem 'simplecov', :require => false
gem 'nokogiri', '<= 1.5.10'
end

group :integration do
Expand Down
6 changes: 5 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ require 'puppet-lint'
desc 'Run the tests'
RSpec::Core::RakeTask.new(:do_test) do |t|
t.rspec_opts = ['--color', '-f d']
t.pattern = 'spec/*/*_spec.rb'
file_list = FileList['spec/**/*_spec.rb']
%w(support fixtures acceptance).each do |exclude|
file_list = file_list.exclude("spec/#{exclude}/**/*_spec.rb")
end
t.pattern = file_list
end

desc 'Generate the docs'
Expand Down
52 changes: 26 additions & 26 deletions lib/puppet/provider/onedatastore/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
# Deutsche Post E-POST Development GmbH - 2014
#

require 'rexml/document'
require 'erb'
require 'tempfile'
require 'nokogiri'

Puppet::Type.type(:onedatastore).provide(:cli) do
desc "onedatastore provider"
Expand All @@ -26,18 +24,20 @@

def create
file = Tempfile.new("onedatastore-#{resource[:name]}")
template = ERB.new <<-EOF
NAME = <%= resource[:name] %>
TM_MAD = <%= resource[:tm] %>
TYPE = <%= resource[:type].to_s.upcase %>
<% if resource[:safe_dirs] %>
SAFE_DIRS = <%= resource[:safe_dirs].join(' ') %>
<% end %>
<% if resource[:dm] %>
DS_MAD = <%= resource[:dm] %>
<% end %>
EOF
tempfile = template.result(binding)
builder = Nokogiri::XML::Builder.new do |xml|
xml.DATASTORE do
xml.NAME resource[:name]
xml.TM_MAD resource[:tm]
xml.TYPE resource[:type].to_s_upcase
xml.SAFE_DIRS do
xml.send(resource[:safe_dirs].join(' '))
end if resource[:safe_dirs]
xml.DS_MAD do
xml.send(resource[:dm])
end if resource[:dm]
end
end
tempfile = builder.to_xml
file.write(tempfile)
file.close
onedatastore('create', file.path)
Expand All @@ -55,17 +55,17 @@ def exists?
end

def self.instances
REXML::Document.new(onedatastore('list', '-x')).elements.collect("DATASTORE_POOL/DATASTORE") do |datastore|
new(
:name => datastore.elements["NAME"].text,
:ensure => :present,
:type => datastore.elements["TEMPLATE/TYPE"].text,
:dm => (datastore.elements["TEMPLATE/DS_MAD"].text unless datastore.elements["TEMPLATE/DS_MAD"].nil?),
:safe_dirs => (datastore.elements["TEMPLATE/SAFE_DIRS"].text.split(' ') unless datastore.elements["TEMPLATE/SAFE_DIRS"].nil?),
:tm => ( datastore.elements["TEMPLATE/TM_MAD"].text unless datastore.elements["TEMPLATE/TM_MAD"].nil?),
:disktype => {0 => 'file', 1 => 'block', 2 => 'rdb'}[datastore.elements["DISK_TYPE"].text]
)
end
Nokogiri::XML(onedatastore('list','-x')).root.xpath('/DATASTORE_POOL/DATASTORE').map do |datastore|
new(
:name => datastore.xpath('./NAME').text,
:ensure => :present,
:type => datastore.xpath('./TEMPLATE/TYPE').text,
:dm => (datastore.xpath('./TEMPLATE/DS_MAD').text unless datastore.xpath('./TEMPLATE/DS_MAD').nil?),
:safe_dirs => (datastore.xpath('./TEMPLATE/SAFE_DIRS').text unless datastore.xpath('./TEMPLATE/SAFE_DIRS').nil?),
:tm => (datastore.xpath('./TEMPLATE/TM_MAD').text unless datastore.xpath('./TEMPLATE/TM_MAD').nil?),
:disktype => {0 => 'file', 1 => 'block', 2 => 'rdb'}[datastore.xpath('./DISK_TYPE').text]
)
end
end

def self.prefetch(resources)
Expand Down
22 changes: 10 additions & 12 deletions lib/puppet/provider/onehost/cli.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
require 'rexml/document'
require 'erb'
require 'tempfile'
require 'nokogiri'

Puppet::Type.type(:onehost).provide(:cli) do
desc "onehost provider"
Expand All @@ -26,15 +24,15 @@ def exists?
end

def self.instances
REXML::Document.new(onehost('list', '-x')).elements.collect("HOST_POOL/HOST") do |host|
new(
:name => host.elements["NAME"].text,
:ensure => :present,
:im_mad => host.elements["IM_MAD"].text,
:vm_mad => host.elements["VM_MAD"].text,
:vn_mad => host.elements["VN_MAD"].text
)
end
Nokogiri::XML(onehost('list','-x')).root.xpath('/HOST_POOL/HOST') do | host|
new(
:name => host.xpath('./NAME').text,
:ensure => :present,
:im_mad => host.xpath('./IM_MAD').text,
:vm_mad => host.xpath('./VM_MAD').text,
:vn_mad => host.xpath('./VN_MAD').text
)
end
end

def self.prefetch(resources)
Expand Down
92 changes: 52 additions & 40 deletions lib/puppet/provider/oneimage/cli.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
require 'rexml/document'
require 'tempfile'
require 'erb'
require 'nokogiri'

Puppet::Type.type(:oneimage).provide(:cli) do
desc "oneimage provider"
Expand All @@ -16,23 +14,42 @@ def create
file = Tempfile.new("oneimage-#{resource[:name]}")
File.chmod(0644, file.path)

template = ERB.new <<-EOF
NAME = "<%= resource[:name] %>"
<% if resource[:description] %>DESCRIPTION = "<%= resource[:description] %>"<% end%>
<% if resource[:type] %>TYPE = <%= resource[:type].to_s.upcase %><% end%>
<% if resource[:persistent] %>PERSISTENT = <%= resource[:persistent] %><% end%>
<% if resource[:dev_prefix] %>DEV_PREFIX = "<%= resource[:dev_prefix] %>"<% end%>
<% if resource[:driver] %>DRIVER = "<%= resource[:driver] %>"<% end %>
<% if resource[:path] %>PATH = <%= resource[:path] %><% end%>
<% if resource[:source] %>SOURCE = <%= resource[:source] %><% end%>
<% if resource[:fstype] %>FSTYPE = <%= resource[:fstype] %><% end%>
<% if resource[:size] %>SIZE = <%= resource[:size] %><% end%>
EOF

tempfile = template.result(binding)
self.debug "Creating image using tempfile: #{tempfile}"
builder = Nokogiri::XML::Builder.new do |xml|
xml.IMAGE do
xml.NAME resource[:name]
xml.DESCRIPTION do
resource[:description]
end if resource[:description]
xml.TYPE do
resource[:type].to_s_upcase
end if resource[:type]
xml.PERSISTENT do
resource[:persistent]
end if resource[:persistent]
xml.DEV_PREFIX do
resource[:dev_prefix]
end if resource[:dev_prefix]
xml.DRIVER do
resource[:driver]
end if resource[:driver]
xml.PATH do
resource[:path]
end if resource[:path]
xml.SOURCE do
resource[:source]
end if resource[:source]
xml.FSTYPE do
resource[:fstype]
end if resource[:fstype]
xml.SIZE do
resource[:size]
end if resource[:size]
end
end
tempfile = builder.to_xml
file.write(tempfile)
file.close
self.debug "Creating image using tempfile: #{tempfile}"
oneimage('create', '-d', resource[:datastore], file.path)
@property_hash[:ensure] = :present
end
Expand All @@ -50,28 +67,23 @@ def exists?

# Return the full hash of all existing oneimage resources
def self.instances
REXML::Document.new(oneimage('list', '-x')).elements.collect("IMAGE_POOL/IMAGE") do |image|
elements = image.elements
new(
:name => elements["NAME"].text,
:ensure => :present,
:datastore => elements["DATASTORE"].text,
:description => elements["TEMPLATE/DESCRIPTION"].text,
:dev_prefix => elements["TEMPLATE/DEV_PREFIX"].text,
:disk_type => elements["DISK_TYPE"].text,
:driver => (elements["DRIVER"].text unless elements["DRIVER"].nil?),
:fstype => elements["FSTYPE"].text,
:path => (elements["TEMPLATE/PATH"] || elements["PATH"]).text,
:persistent => ((elements["TEMPLATE/PERSISTENT"] || elements["PERSISTENT"]).text == "1").to_s.to_sym,
:size => elements["SIZE"].text,
:source => (elements["TEMPLATE/SOURCE"] || elements["SOURCE"]).text,
:target => (elements["TARGET"].text unless elements["TARGET"].nil?),
:type => {
'0' => :OS,
'1' => :CDROM,
'5' => :CONTEXT,
}[(elements["TEMPLATE/TYPE"] || elements["TYPE"]).text]
)
Nokogiri::XML(oneimage('list','-x')).root.xpath('/IMAGE_POOL/IMAGE').map do |image|
new(
:name => image.xpath('./NAME').text,
:ensure => :present,
:datastore => image.xpath('./DATASTORE').text,
:description => image.xpath('./TEMPLATE/DESCRIPTION').text,
:dev_prefix => image.xpath('./TEMPLATE/DEV_PREFIX').text,
:disk_type => image.xpath('./DISK_TYPE').text,
:driver => (image.xpath('./DRIVER').text unless image.xpath('./DRIVER').nil?),
:fstype => image.xpath('./FSTYPE').text,
:path => (image.xpath('./TEMPLATE/PATH').text || image.xpath('./PATH').text),
:persistent => ((image.xpath('./TEMPLATE/PERSISTENT') || image.xpath('./PERSISTENT')).text == "1").to_s.to_sym,
:size => image.xpath('./SIZE').text,
:source => (image.xpath('./TEMPLATE/SOURCE') || image.xpath('./SOURCE')).text,
:target => (image.xpath('./TARGET').text unless image.xpath('./TARGET').nil?),
:type => { '0' => :OS, '1' => :CDROM, '5' => :CONTEXT }[(image.xpath('./TEMPLATE/TYPE') || image.xpath('./TYPE')).text]
)
end
end

Expand Down
140 changes: 58 additions & 82 deletions lib/puppet/provider/onetemplate/cli.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
require 'rexml/document'
require 'tempfile'
require 'erb'
require 'nokogiri'

Puppet::Type.type(:onetemplate).provide(:cli) do
desc "onetemplate provider"
Expand All @@ -14,69 +12,48 @@
# Create a VM template with onetemplate by passing in a temporary template definition file.
def create
file = Tempfile.new("onetemplate-#{resource[:name]}")
template = ERB.new <<-EOF
<TEMPLATE>
<NAME><%= resource[:name] %></NAME>
<MEMORY><%= resource[:memory] %></MEMORY>
<CPU><%= resource[:cpu] %></CPU>
<VCPU><%= resource[:vcpu] %></VCPU>
<% if resource[:os] %>
<OS>
<% resource[:os].each do |key, value| %>
<<%= key.upcase %>><%= value %></<%= key.upcase %>>
<% end %>
</OS>
<% end %>
<% if resource[:disks] %>
<% resource[:disks].each do |disk| %>
<DISK>
<% disk.each do |key, value| %>
<<%= key.upcase %>><%= value %></<%= key.upcase %>>
<% end %>
</DISK>
<% end %>
<% end %>
<% if resource[:nics] %>
<% resource[:nics].each do |nic| %>
<NIC>
<% nic.each do |key, value| %>
<<%= key.upcase %>><%= value %></<%= key.upcase %>>
<% end %>
</NIC>
<% end %>
<% end %>
<% if resource[:graphics] %>
<GRAPHICS>
<% resource[:graphics].each do |key, value| %>
<<%= key.upcase %>><%= value %></<%= key.upcase %>>
<% end %>
</GRAPHICS>
<% end %>
<% if resource[:features] %>
<FEATURES>
<% resource[:features].each do |key, value| %>
<<%= key.upcase %>><%= value %></<%= key.upcase %>>
<% end %>
</FEATURES>
<% end %>
<% if resource[:context] %>
<CONTEXT>
<% resource[:context].each do |key, value| %>
<<%= key.upcase %>><%= value %></<%= key.upcase %>>
<% end %>
</CONTEXT>
<% end %>
</TEMPLATE>
EOF

tempfile = template.result(binding)
builder = Nokogiri::XML::Builder.new do |xml|
xml.TEMPLATE do
xml.NAME resource[:name]
xml.MEMORY resource[:memory]
xml.CPU resource[:cpu]
xml.VCPU resource[:vcpu]
xml.OS do
resource[:os].each do |k, v|
xml.send(k.upcase, v)
end
end if resource[:os]
resource[:disks].each do |disk|
xml.DISK do
disk.each do |k, v|
xml.send(k.upcase, v)
end
end
end if resource[:disks]
resource[:nics].each do |nic|
xml.NIC do
nic.each do |k, v|
xml.send(k.upcase, v)
end
end
end if resource[:nics]
xml.GRAPHICS do
resource[:graphics].each do |k, v|
xml.send(k.upcase, v)
end
end if resource[:graphics]
xml.FEATURES do
resource[:features].each do |k, v|
xml.send(k.upcase, v)
end
end if resource[:features]
xml.CONTEXT do
resource[:context].each do |k, v|
xml.send(k.upcase, v)
end
end if resource[:context]
end
end
file.write(tempfile)
file.close
self.debug "Creating template using #{tempfile}"
Expand All @@ -98,22 +75,21 @@ def exists?

# Return the full hash of all existing onevm resources
def self.instances
REXML::Document.new(onetemplate('list', '-x')).elements.collect("VMTEMPLATE_POOL/VMTEMPLATE") do |template|
elements = template.elements
new(
:name => elements["NAME"].text,
:ensure => :present,
:context => Hash[elements.collect('TEMPLATE/CONTEXT/*') { |e| [e.name.downcase, e.text] } ],
:cpu => (elements["TEMPLATE/CPU"].text unless elements["TEMPLATE/CPU"].nil?),
:disks => elements.collect("TEMPLATE/DISK") { |e| Hash[e.elements.collect { |f| [f.name.downcase, f.text] }] },
:features => Hash[elements.collect('TEMPLATE/FEATURES/*') { |e| [e.name.downcase, { e.text => e.text, 'true' => true, 'false' => false }[e.text]] } ],
:graphics => Hash[elements.collect('TEMPLATE/GRAPHICS/*') { |e| [e.name.downcase, e.text] } ],
:memory => (elements["TEMPLATE/MEMORY"].text unless elements["TEMPLATE/MEMORY"].nil?),
:nics => elements.collect("TEMPLATE/NIC") { |e| Hash[e.elements.collect { |f| [f.name.downcase, f.text] }] },
:os => Hash[elements.collect('TEMPLATE/OS/*') { |e| [e.name.downcase, e.text] } ],
:vcpu => (elements["TEMPLATE/VCPU"].text unless elements["TEMPLATE/VCPU"].nil?)
)
end
Nokogiri::XML(onetemplate('list', '-x')).root.xpath('/VMTEMPLATE_POOL/VMTEMPLATE') do | template|
new(
:name => template.xpath('./NAME').text,
:ensure => :present,
:context => Hash[template.xpath('./TEMPLATE/CONTEXT/*').map { |e| [e.downcase, e.text] } ],
:cpu => (template.xpath('./TEMPLATE/CPU').text unless template.xapth('./TEMPLATE/CPU').nil?),
:disks => template.xapth('./TEMPLATE/DISK').map { |disk| Hash[disk.xpath('./*').map { |e| [e.name.downcase, e.text] } ] },
:features => Hash[template.xpath('./TEMPLATE/FEATURES/*').map { |e| [e.name.downcase, { e.text => e.text, 'true' => true, 'false' => false }[e.text]] } ],
:graphics => Hash[template.xpath('./TEMPLATE/GRAPHICS/*').map { |e| [e.name.downcase, e.text] } ],
:memory => (template.xpath('./TEMPLATE/MEMORY').text unless template.xpath('./TEMPLATE/MEMORY').nil?),
:nics => template.xpath('./TEMPLATE/NIC').map { |nic| Hash[nic.xpath('*').map { |e| [e.name.downcase, e.text] } ] },
:os => Hash[template.xpath('./TEMPLATE/OS/*').map { |e| [e.name.downcase, e.text] } ],
:vcpu => (template.xpath('./TEMPLATE/VCPU').text unless template.xpath('./TEMPLATE/VCPU').nil?)
)
end
end

def self.prefetch(resources)
Expand Down
Loading

0 comments on commit ce37b4e

Please sign in to comment.