[PATCH configure] Use conductor api for provider creation

John Eckersberg jeckersb at redhat.com
Mon Jul 23 19:30:23 UTC 2012


This uses the conductor xml rest api to query and create providers.
Previous versions queried the html endpoints instead.

Besides the switch to the api, there are a few other related
improvements contained herein:

- Updated the valid_xpath? method to take a content_type argument.
  This is used to determine whether to use the nokogiri html or xml
  parser when processing the response body.

- Fixes a bug with contains/does_not_contain formatting.  The
  web_request type munges contains/does_not_contain parameters to be
  arrays.  However, when using if/unless, it is possible to pass a
  string as the value of contains/does_not_contain.  This patch munges
  the latter case as an array, and will then iterate over each xpath
  within the array and verify it.  Previously only one expression
  would be validated.
---
 .../aeolus/lib/puppet/provider/web_request/curl.rb |   32 +++++++++++++++-----
 recipes/aeolus/manifests/conductor/provider.pp     |    8 ++---
 2 files changed, 28 insertions(+), 12 deletions(-)

diff --git a/recipes/aeolus/lib/puppet/provider/web_request/curl.rb b/recipes/aeolus/lib/puppet/provider/web_request/curl.rb
index 926f198..688a7d7 100644
--- a/recipes/aeolus/lib/puppet/provider/web_request/curl.rb
+++ b/recipes/aeolus/lib/puppet/provider/web_request/curl.rb
@@ -99,8 +99,18 @@ class Curl::Easy
     valid_values.include?(response_code.to_s)
   end
 
-  def valid_xpath?(xpath="/")
-    !Nokogiri::HTML(body_str.to_s).xpath(xpath.to_s).empty?
+  def valid_xpath?(xpath="/", content_type="text/html")
+    parser = case
+             when content_type.match(/html/i)
+               Nokogiri::HTML
+             when content_type.match(/xml/i)
+               Nokogiri::XML
+             else
+               raise Puppet::Error,
+                     "Unable to determine parser for #{content_type}"
+             end
+
+    !parser.parse(body_str.to_s).xpath(xpath.to_s).empty?
   end
 
 end
@@ -240,14 +250,20 @@ Puppet::Type.type(:web_request).provide :curl do
                                was not expecting one of #{verify[:does_not_return].join(", ")}"
     end
 
-    if !verify[:contains].nil? &&
-       !result.valid_xpath?(verify[:contains])
-         raise Puppet::Error, "Expecting #{verify[:contains]} in the result"
+    if !verify[:contains].nil?
+      Puppet::Type::Web_request.munge_array_params(verify[:contains]).each do |xpath|
+        if !result.valid_xpath?(xpath, result.content_type)
+          raise Puppet::Error, "Expecting #{xpath} in the result"
+        end
+      end
     end
 
-    if !verify[:does_not_contain].nil? &&
-       result.valid_xpath?(verify[:does_not_contain])
-         raise Puppet::Error, "Not expecting #{verify[:does_not_contain]} in the result"
+    if !verify[:does_not_contain].nil?
+      Puppet::Type::Web_request.munge_array_params(verify[:does_not_contain]).each do |xpath|
+        if result.valid_xpath?(xpath, result.content_type)
+          raise Puppet::Error, "Not expecting #{xpath} in the result"
+        end
+      end
     end
   end
 
diff --git a/recipes/aeolus/manifests/conductor/provider.pp b/recipes/aeolus/manifests/conductor/provider.pp
index fbbf692..b925831 100644
--- a/recipes/aeolus/manifests/conductor/provider.pp
+++ b/recipes/aeolus/manifests/conductor/provider.pp
@@ -1,18 +1,18 @@
 # Create a new provider via the conductor
 define aeolus::conductor::provider($deltacloud_driver="",$url="", $deltacloud_provider="", $admin_login=""){
   web_request{ "provider-$name":
-    post         => "https://localhost/conductor/providers",
+    post         => "https://localhost/conductor/providers.xml",
     parameters  => { 'provider[name]'  => $name, 'provider[url]'   => $url,
                      'provider[provider_type_deltacloud_driver]' => $deltacloud_driver,
                      'provider[deltacloud_provider]' => $deltacloud_provider },
     returns     => '200',
     follow      => true,
-    contains    => "//img[@alt='Notices']", # in the case of an error, @alt='Warnings'
+    contains    => "/provider/name[text() = '$name']",
     use_cookies_at => "/tmp/aeolus-${admin_login}",
     log_to      => '/tmp/configure-provider-request.log',
     only_log_errors => true,
-    unless      => { 'get'             => 'https://localhost/conductor/providers',
-                     'contains'        => "//html/body//a[text() = '$name']" },
+    unless      => { 'get'             => 'https://localhost/conductor/providers.xml',
+                     'contains'        => "/providers/provider/name[text() = '$name']" },
     require    => [Service['aeolus-conductor'], Exec['grant_temp_admin_privs'], Exec['deltacloud-core-startup-wait']]
   }
 }
-- 
1.7.10.4




More information about the aeolus-devel mailing list