summaryrefslogtreecommitdiffstats
path: root/lib/puppet/indirector/rest.rb
blob: 690c79632c09c222b39b7fa8c70afb843582cb06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
require 'net/http'
require 'uri'

# Access objects via REST
class Puppet::Indirector::REST < Puppet::Indirector::Terminus
    def network_fetch(path)
        # TODO: url_encode path, set proper server + port
        Net::HTTP.get(URI.parse("http://127.0.0.1:34343/#{path}"))
    end
    
    def find(name, options = {})
      network_result = network_fetch("#{indirection.name}/#{name}")
      raise YAML.load(network_result) if network_result =~ %r{--- !ruby/exception}
      decoded_result = indirection.model.from_yaml(network_result)
    end
    
    def search(key, options = {})
      network_results = network_fetch("#{indirection.name}s/#{key}")
      raise YAML.load(network_results) if network_results =~ %r{--- !ruby/exception}
      decoded_results = network_results.collect do |result|
        indirection.model.from_yaml(result)
      end
    end
end