diff --git a/lib/puppet/parser/functions/foreman.rb b/lib/puppet/parser/functions/foreman.rb deleted file mode 100644 index ae10468f0..000000000 --- a/lib/puppet/parser/functions/foreman.rb +++ /dev/null @@ -1,143 +0,0 @@ -# Query Foreman -# -# The foreman() parser takes a hash value with parameters to execute the query. -# -# To use foreman(), first create a hash. This sample hash will contain -# parameters to let us get a list of 'hosts' that match our search parameters. -# -# $f = { item => 'hosts', -# search => 'hostgroup=Grid', -# per_page => '20', -# foreman_url => 'https://foreman', -# foreman_user => 'my_api_foreman_user', -# foreman_pass => 'my_api_foreman_pass' } -# -# 'item' may be: environments, fact_values, hosts, hostgroups, puppetclasses, smart_proxies, subnets -# 'search' is your actual search query. -# 'per_page' specifies the maximum number of results you'd like to receive. -# This defaults to '20' which is consistent with what you'd get from -# Foreman if you didn't specify anything. -# 'foreman_url' is your actual foreman server address -# 'foreman_user' is the username of an account with API access -# 'foreman_pass' is the password of an account with API access -# 'filter_result' string or array with attribites to return -# if a string is given foreman() returns an array only with given attributes -# in case of an array is given foreman() returns an array of hashes selecting only -# attributes given in array -# in case of an given hash foreman() returns an array of hashes selecting only -# attribute keys given in hash renamed to values of given keys. This can be used -# to rename keys in result -# 'timeout' is the Foreman request timeout in seconds as an integer. -# This defaults to five seconds. -# -# Then, use a variable to capture its output: -# $hosts = foreman($f) -# -# Note: If you're using this in a template, you may be receiving an array of -# hashes. So you might need to use two loops to get the values you need. -# -# Happy Foreman API-ing! - -require "cgi" -require "yaml" -require "net/http" -require "net/https" -require "uri" -require "timeout" - -module Puppet::Parser::Functions - newfunction(:foreman, :type => :rvalue) do |args| - # parse an args hash - raise Puppet::ParseError, "Foreman: Must supply a Hash to foreman(), not a #{args[0].class}" unless args[0].is_a? Hash - args_hash = args[0] - item = args_hash["item"] - search = args_hash["search"] - per_page = args_hash["per_page"] || "20" - use_tfmproxy = args_hash["use_tfmproxy"] || false - foreman_url = args_hash["foreman_url"] || "https://localhost" # defaults: all-in-one - foreman_user = args_hash["foreman_user"] || "admin" # has foreman/puppet - foreman_pass = args_hash["foreman_pass"] || "changeme" # on the same box - filter_result = args_hash['filter_result'] || false - timeout = (args_hash['timeout'] || 5).to_i - - # extend this as required - searchable_items = %w{ environments fact_values hosts hostgroups puppetclasses smart_proxies subnets } - raise Puppet::ParseError, "Foreman: Invalid item to search on: #{item}, must be one of #{searchable_items.join(", ")}." unless searchable_items.include?(item) - raise Puppet::ParseError, "Foreman: Invalid filter_result: #{filter_result}, must be a String or an Array" unless filter_result.is_a? String or filter_result.is_a? Array or filter_result.is_a? Hash or filter_result == false - - begin - path = "/api/#{CGI.escape(item)}?search=#{CGI.escape(search)}&per_page=#{CGI.escape(per_page)}" - - req = Net::HTTP::Get.new(path) - req['Content-Type'] = 'application/json' - req['Accept'] = 'application/json' - - if use_tfmproxy - configfile = '/etc/foreman-proxy/settings.yml' - configfile = use_tfmproxy if use_tfmproxy.is_a? String - raise Puppet::ParseError, "File #{configfile} not found while use_tfmproxy is enabled" unless File.exists?(configfile) - tfmproxy = YAML.load(File.read(configfile)) - uri = URI.parse(tfmproxy[:foreman_url]) - http = Net::HTTP.new(uri.host, uri.port) - http.use_ssl = true - http.ca_file = tfmproxy[:foreman_ssl_ca] - http.cert = OpenSSL::X509::Certificate.new(File.read(tfmproxy[:foreman_ssl_cert])) - http.key = OpenSSL::PKey::RSA.new(File.read(tfmproxy[:foreman_ssl_key]), nil) - http.verify_mode = OpenSSL::SSL::VERIFY_PEER - else - uri = URI.parse(foreman_url) - http = Net::HTTP.new(uri.host, uri.port) - req.basic_auth(foreman_user, foreman_pass) - http.use_ssl = true if uri.scheme == 'https' - http.verify_mode = OpenSSL::SSL::VERIFY_NONE if http.use_ssl? - end - results = Timeout::timeout(timeout) { PSON.parse http.request(req).body } - rescue Exception => e - raise Puppet::ParseError, "Failed to contact Foreman at #{foreman_url}: #{e}" - end - - # Filter results - if filter_result != false and results.has_key?('results') - filtered_results = Array.new - - if filter_result.is_a? String - # filter into an array - results['results'].each do |result| - if result.has_key?(filter_result) - filtered_results << result[filter_result] - end - end - elsif filter_result.is_a? Array - # filter into an array of hashes by given key - results['results'].each do |result| - resulthash = Hash.new - result.each do |key,value| - if filter_result.include? key - resulthash[key] = result[key] - end - end - if resulthash != {} - filtered_results << resulthash - end - end - else - # filter into an array of hashes while rename keys - results['results'].each do |result| - resulthash = Hash.new - result.each do |key,value| - if filter_result.include? key - resulthash[filter_result[key]] = result[key] - end - end - if resulthash != {} - filtered_results << resulthash - end - end - end - return filtered_results - end - - # return unfiltered - return results - end -end diff --git a/lib/puppet/parser/functions/smartvar.rb b/lib/puppet/parser/functions/smartvar.rb deleted file mode 100644 index 07915dde9..000000000 --- a/lib/puppet/parser/functions/smartvar.rb +++ /dev/null @@ -1,41 +0,0 @@ -# Query Foreman in order to resolv a smart variable -# Foreman holds all the value names and their possible values, -# this function simply ask foreman for the right value for this host. - - -require "cgi" -require "net/http" -require "net/https" -require "uri" -require "timeout" - -module Puppet::Parser::Functions - newfunction(:smartvar, :type => :rvalue) do |args| - #URL to query - foreman_url = "http://foreman" - foreman_user = "admin" - foreman_pass = "changeme" - - var = args[0] - raise Puppet::ParseError, "Must provide a variable name to search for" if var.nil? - - fqdn = lookupvar("fqdn") - - uri = URI.parse(foreman_url) - http = Net::HTTP.new(uri.host, uri.port) - http.use_ssl = true if uri.scheme == 'https' - - path = "/hosts/#{CGI.escape(fqdn)}/lookup_keys/#{CGI.escape(var)}" - req = Net::HTTP::Get.new(path) - req.basic_auth(foreman_user, foreman_pass) - req['Content-Type'] = 'application/json' - req['Accept'] = 'application/json' - - begin - Timeout::timeout(5) { PSON.parse(http.request(req).body)["value"] } - rescue Exception => e - raise Puppet::ParseError, "Failed to contact Foreman #{e}" - end - - end -end diff --git a/spec/functions/foreman_spec.rb b/spec/functions/foreman_spec.rb deleted file mode 100644 index e5afe55f3..000000000 --- a/spec/functions/foreman_spec.rb +++ /dev/null @@ -1,77 +0,0 @@ -require 'spec_helper' -require 'webmock' - -describe 'foreman' do - it 'should exist' do - expect(Puppet::Parser::Functions.function('foreman')).to eq 'function_foreman' - end - - it 'should throw an error with no arguments' do - is_expected.to run.with_params().and_raise_error(Puppet::ParseError) - end - - it 'should succeed with no timeout specified' do - stub_request(:get, "https://foreman.example.com/api/hosts?per_page=20&search=hostgroup=Grid"). - with(basic_auth: ['my_api_foreman_user', 'my_api_foreman_pass']). - to_return(:status => 200, :body => '{"total":0,"subtotal":0,"page":1,"per_page":20,"search":"hostgroup=Grid","sort":{"by":null,"order":null},"results":[]}', :headers => {}) - - is_expected.to run.with_params( - 'item' => 'hosts', - 'search' => 'hostgroup=Grid', - 'per_page' => '20', - 'foreman_url' => 'https://foreman.example.com', - 'foreman_user' => 'my_api_foreman_user', - 'foreman_pass' => 'my_api_foreman_pass' - ) - end - - it 'should succeed with a non-default timeout specified' do - stub_request(:get, "https://foreman.example.com/api/hosts?per_page=20&search=hostgroup=Grid"). - with(basic_auth: ['my_api_foreman_user', 'my_api_foreman_pass']). - to_return(:status => 200, :body => '{"total":0,"subtotal":0,"page":1,"per_page":20,"search":"hostgroup=Grid","sort":{"by":null,"order":null},"results":[]}', :headers => {}) - - is_expected.to run.with_params( - 'item' => 'hosts', - 'search' => 'hostgroup=Grid', - 'per_page' => '20', - 'foreman_url' => 'https://foreman.example.com', - 'foreman_user' => 'my_api_foreman_user', - 'foreman_pass' => 'my_api_foreman_pass', - 'timeout' => '15' - ) - end - - it 'should throw an "execution expired" error when the timeout is exceeded' do - stub_request(:get, "https://foreman.example.com/api/hosts?per_page=20&search=hostgroup=Grid"). - with(basic_auth: ['my_api_foreman_user', 'my_api_foreman_pass']). - to_return(body: lambda { |request| sleep(2) ; '{"total":0,"subtotal":0,"page":1,"per_page":20,"search":"hostgroup=Grid","sort":{"by":null,"order":null},"results":[]}' }) - - is_expected.to run.with_params( - 'item' => 'hosts', - 'search' => 'hostgroup=Grid', - 'per_page' => '20', - 'foreman_url' => 'https://foreman.example.com', - 'foreman_user' => 'my_api_foreman_user', - 'foreman_pass' => 'my_api_foreman_pass', - 'timeout' => '1' - ).and_raise_error(/execution expired/) - end - - it 'should not throw an "execution expired" error with the default timeout' do - stub_request(:get, "https://foreman.example.com/api/hosts?per_page=20&search=hostgroup=Grid"). - with(basic_auth: ['my_api_foreman_user', 'my_api_foreman_pass']). - to_return(body: lambda { |request| sleep(2) ; '{"total":0,"subtotal":0,"page":1,"per_page":20,"search":"hostgroup=Grid","sort":{"by":null,"order":null},"results":[]}' }) - - is_expected.to run.with_params( - 'item' => 'hosts', - 'search' => 'hostgroup=Grid', - 'per_page' => '20', - 'foreman_url' => 'https://foreman.example.com', - 'foreman_user' => 'my_api_foreman_user', - 'foreman_pass' => 'my_api_foreman_pass' - ) - end - - # TODO: Test functionality of the actual function. - -end diff --git a/spec/functions/smartvar_spec.rb b/spec/functions/smartvar_spec.rb deleted file mode 100644 index 09d2a013c..000000000 --- a/spec/functions/smartvar_spec.rb +++ /dev/null @@ -1,14 +0,0 @@ -require 'spec_helper' - -describe 'smartvar' do - it 'should exist' do - expect(Puppet::Parser::Functions.function('smartvar')).to eq 'function_smartvar' - end - - it 'should throw an error with no arguments' do - is_expected.to run.with_params().and_raise_error(Puppet::ParseError) - end - - # TODO: Test functionality of the actual function. - -end