Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added post-run validation to provider #207

Merged
merged 7 commits into from
Oct 13, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 69 additions & 3 deletions lib/puppet/provider/onedatastore/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
# Based upon initial work from Ken Barber
# Modified by Martin Alfke
# Modified by Robert Waffen <robert.waffen@epost-dev.de>
# Modified by Arne Hilmann
# Modified by Gerald Schmidt
#
# Copyright
# initial provider had no copyright
Expand All @@ -25,11 +27,15 @@
mk_resource_methods

def self.get_attributes
[:name, :tm_mad, :type, :safe_dirs, :ds_mad, :disk_type, :driver, :bridge_list,
get_checked_attributes + [:safe_dirs, :driver, :bridge_list,
:ceph_host, :ceph_user, :ceph_secret, :pool_name, :staging_dir, :base_path,
:ensure, :cluster, :cluster_id]
end

def self.get_checked_attributes
[:name, :ds_mad, :tm_mad, :disk_type, :type]
end

def create
file = Tempfile.new("onedatastore-#{resource[:name]}")
builder = Nokogiri::XML::Builder.new do |xml|
Expand All @@ -43,11 +49,43 @@ def create
file.write(tempfile)
file.close
self.debug "Adding new datastore using: #{tempfile}"
onedatastore('create', file.path)
begin
onedatastore('create', file.path)
post_validate_change
rescue Exception => e
destroy
raise e
end
file.delete

@property_hash[:ensure] = :present
end

def post_validate_change
unless resource[:self_test]
self.debug ":self_test not defined"
return
end

self.debug ":self_test defined"

[1..3].each do
if is_status_success?
break
end
sleep 30
end

unless is_status_success?
Puppet.debug("#{__method__}: attempts_max exceeded")
raise "Failed to apply resource: status not 'ready'"
end

unless is_obj_valid?
raise "Failed to apply resources; object doesn't match parameters"
end
end

def destroy
self.debug "Deleting datastore #{resource[:name]}"
onedatastore('delete', resource[:name])
Expand Down Expand Up @@ -87,6 +125,34 @@ def self.get_datastore(xml)
datastore_hash
end

def is_status_success?
# see https://github.com/OpenNebula/one/blob/master/include/Datastore.h
# ll. 68ff.
#
# enum DatastoreState
# {
# READY = 0, /** < Datastore ready to use */
# DISABLED = 1 /** < System Datastore can not be used */
# };
status_ready = 0
datastore = Nokogiri::XML(onedatastore('show', resource[:name], '-x')).root.xpath('DATASTORE')
(datastore.xpath('STATE').text.to_i == status_ready)
end

def is_obj_valid?
datastore = self.class.get_datastore(Nokogiri::XML(onedatastore('show', resource[:name], '-x')).xpath('DATASTORE'))

self.class.get_checked_attributes.each do |item|
val = datastore[item]
res_val = resource[item].to_s
if val != res_val
Puppet.debug("Value mismatch: '#{val}' != '#{res_val}' for item '#{item}'")
return false
end
end
true
end

def self.instances
datastores = Nokogiri::XML(onedatastore('list', '-x')).xpath('/DATASTORE_POOL/DATASTORE')
datastores.collect do |datastore|
Expand Down Expand Up @@ -114,7 +180,7 @@ def flush

file.write(tempfile)
file.close
self.debug "Updating datastore using:\n#{tempfile}"
Puppet.debug("Updating datastore using:\n#{tempfile}")
onedatastore('update', resource[:name], file.path, '--append') unless @property_hash.empty?
file.delete
end
Expand Down
8 changes: 8 additions & 0 deletions lib/puppet/type/onedatastore.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@
end
end

newparam(
:self_test,
:boolean => true
) do
desc 'Param to activate self-test: defaults to false'
end

newproperty(:type) do
desc 'Choose type of datastore. Valid values: images, system, files'
defaultto :IMAGE_DS
Expand Down Expand Up @@ -87,4 +94,5 @@
desc 'Array of safe directories'
end


end
32 changes: 16 additions & 16 deletions spec/acceptance/onedatastore_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class { 'one':
after(:each) do
pp = <<-EOS
onedatastore { 'nfs_ds':
ensure => absent,
ensure => absent,
}
EOS

Expand All @@ -28,8 +28,8 @@ class { 'one':
it 'should idempotently run' do
pp = <<-EOS
onedatastore { 'nfs_ds':
tm_mad => 'shared',
type => 'system_ds',
tm_mad => 'shared',
type => 'system_ds',
}
EOS

Expand Down Expand Up @@ -93,8 +93,8 @@ class { 'one':
it 'should idempotently run' do
pp = <<-EOS
onedatastore { 'production':
ds_mad => 'fs',
tm_mad => 'shared',
ds_mad => 'fs',
tm_mad => 'shared',
}
EOS

Expand All @@ -107,8 +107,8 @@ class { 'one':
it 'should idempotently run' do
pp = <<-EOS
onedatastore { 'vmfs_ds':
ds_mad => 'vmfs',
tm_mad => 'vmfs',
ds_mad => 'vmfs',
tm_mad => 'vmfs',
}
EOS

Expand All @@ -121,8 +121,8 @@ class { 'one':
it 'should idempotently run' do
pp = <<-EOS
onedatastore { 'lvm_ds':
ds_mad => 'fs_lvm',
tm_mad => 'fs_lvm',
ds_mad => 'fs_lvm',
tm_mad => 'fs_lvm',
}
EOS

Expand All @@ -143,7 +143,7 @@ class { 'one':
ceph_secret => 'cephsecret',
pool_name => 'cephpoolname',
disk_type => 'rbd',
bridge_list => 'host1 host2 host3'
bridge_list => 'host1 host2 host3',
}
EOS

Expand All @@ -156,7 +156,7 @@ class { 'one':
it 'should idempotently run' do
pp =<<-EOS
onedatastore { 'nfs_ds':
ensure => absent,
ensure => absent,
}
EOS

Expand All @@ -169,7 +169,7 @@ class { 'one':
it 'should idempotently run' do
pp =<<-EOS
onedatastore { 'kernels':
ensure => absent,
ensure => absent,
}
EOS

Expand All @@ -182,7 +182,7 @@ class { 'one':
it 'should idempotently run' do
pp =<<-EOS
onedatastore { 'production':
ensure => absent,
ensure => absent,
}
EOS

Expand All @@ -195,7 +195,7 @@ class { 'one':
it 'should idempotently run' do
pp =<<-EOS
onedatastore { 'vmfs_ds':
ensure => absent,
ensure => absent,
}
EOS

Expand All @@ -208,7 +208,7 @@ class { 'one':
it 'should idempotently run' do
pp =<<-EOS
onedatastore { 'lvm_ds':
ensure => absent,
ensure => absent,
}
EOS

Expand All @@ -221,7 +221,7 @@ class { 'one':
it 'should idempotently run' do
pp =<<-EOS
onedatastore { 'ceph_ds':
ensure => absent,
ensure => absent,
}
EOS

Expand Down