diff options
| author | Rein Henrichs <rein@puppetlabs.com> | 2010-06-22 15:46:56 -0700 |
|---|---|---|
| committer | test branch <puppet-dev@googlegroups.com> | 2010-02-17 06:50:53 -0800 |
| commit | 1c5b67d3156873cf3f97aca4d8ca3c6707fc089f (patch) | |
| tree | 4c3bd16432df2b47df0060ae77fe03a14f590b1e /lib/puppet | |
| parent | 432db2593f15de200767ec16c14f433f718a44d9 (diff) | |
| download | puppet-1c5b67d3156873cf3f97aca4d8ca3c6707fc089f.tar.gz puppet-1c5b67d3156873cf3f97aca4d8ca3c6707fc089f.tar.xz puppet-1c5b67d3156873cf3f97aca4d8ca3c6707fc089f.zip | |
[#4055] Refactor of abstract Couch terminus, more specs
* Cleaner implementation of abstract Couch terminus
* More thoroughly tested facts Couch terminus
Diffstat (limited to 'lib/puppet')
| -rw-r--r-- | lib/puppet/indirector/couch.rb | 42 |
1 files changed, 24 insertions, 18 deletions
diff --git a/lib/puppet/indirector/couch.rb b/lib/puppet/indirector/couch.rb index b328f5cb7..dc3e49529 100644 --- a/lib/puppet/indirector/couch.rb +++ b/lib/puppet/indirector/couch.rb @@ -4,37 +4,43 @@ 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 self.db; @db ||= CouchRest.database! Puppet[:couchdb_url] end def db; self.class.db end def find(request) - attributes_of db.get(id_for(request)) - rescue RestClient::ResourceNotFound - Puppet.debug "No couchdb document with id: #{id_for(request)}" - return nil + attributes_of get(request) 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 save(request) - raise ArgumentError, "PUT does not accept options" unless request.options.empty? + def get(request) + db.get(id_for(request)) + rescue RestClient::ResourceNotFound + Puppet.debug "No couchdb document with id: #{id_for(request)}" + return nil + end - # Try to find an existing document. - doc = db.get(id_for(request)) - doc.merge(hash_from(request)) + def update(request) + doc = get request + return unless doc + doc.merge!(hash_from(request)) doc.save - rescue RestClient::ResourceNotFound - # Document does not yet exist, create it - db.save_doc hash_from(request) + return true end - private + 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 @@ -51,7 +57,7 @@ class Puppet::Indirector::Couch < Puppet::Indirector::Terminus # instance that is returned by save. # def attributes_of(response) - response.reject{|k,v| k =~ /^(_rev|puppet_)/ } + response && response.reject{|k,v| k =~ /^(_rev|puppet_)/ } end def document_type_for(request) |
