blob: 243d33dd4f1a1132ab825f4edd92836b4f4461a5 (
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
class Puppet::Indirector::Couch < Puppet::Indirector::Terminus
# The CouchRest database instance. One database instance per Puppet runtime
# should be sufficient.
#
def self.db; @db ||= CouchRest.database! Puppet[:couchdb_url] end
def db; self.class.db end
def find(request)
attributes_of get(request)
end
def initialize(*args)
raise "Couch terminus not supported without couchrest gem" unless Puppet.features.couchdb?
super
end
# Create or update the couchdb document with the request's data hash.
#
def save(request)
raise ArgumentError, "PUT does not accept options" unless request.options.empty?
update(request) || create(request)
end
private
# RKH:TODO: Do not depend on error handling, check if the document exists
# first. (Does couchrest support this?)
#
def get(request)
db.get(id_for(request))
rescue RestClient::ResourceNotFound
Puppet.debug "No couchdb document with id: #{id_for(request)}"
return nil
end
def update(request)
doc = get request
return unless doc
doc.merge!(hash_from(request))
doc.save
true
end
def create(request)
db.save_doc hash_from(request)
end
# The attributes hash that is serialized to CouchDB as JSON. It includes
# metadata that is used to help aggregate data in couchdb. Add
# model-specific attributes in subclasses.
#
def hash_from(request)
{
"_id" => id_for(request),
"puppet_type" => document_type_for(request)
}
end
# The couchdb response stripped of metadata, used to instantiate the model
# instance that is returned by save.
#
def attributes_of(response)
response && response.reject{|k,v| k =~ /^(_rev|puppet_)/ }
end
def document_type_for(request)
request.indirection_name
end
# The id used to store the object in couchdb. Implemented in subclasses.
#
def id_for(request)
raise NotImplementedError
end
end
|