summaryrefslogtreecommitdiffstats
path: root/lib/puppet/indirector/rest.rb
blob: 8889dbc3e43f3831605efbdd5b0fab65cfb3f2d6 (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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
require 'net/http'
require 'uri'

# Access objects via REST
class Puppet::Indirector::REST < Puppet::Indirector::Terminus
    # Figure out the content type, turn that into a format, and use the format
    # to extract the body of the response.
    def deserialize(response)
        # Raise the http error if we didn't get a 'success' of some kind.
        response.error! unless response.code =~ /^2/

        # Convert the response to a deserialized object.
        model.convert_from(response['content-type'], response.body)
    end

    # Provide appropriate headers.
    def headers
        {"Accept" => model.supported_formats.join(", ")}
    end
  
    def network
        Puppet::Network::HttpPool.http_instance(Puppet[:server], Puppet[:masterport].to_i)
    end

    def rest_connection_details
        { :host => Puppet[:server], :port => Puppet[:masterport].to_i }
    end
    
    def find(request)
        deserialize network.get("/#{indirection.name}/#{request.key}", headers)
    end
    
    def search(request)
        if request.key
            path = "/#{indirection.name}/#{request.key}"
        else
            path = "/#{indirection.name}"
        end
        deserialize network.get(path, headers)
    end
    
    def destroy(request)
        deserialize network.delete("/#{indirection.name}/#{request.key}", headers)
    end
    
    def save(request)
        deserialize network.put("/#{indirection.name}/", request.instance.render, headers)
    end
end