Sha256: 888f3cb8f8c8cf29996a4aece0e7147c1f40557858ffa1dab09a988ec0988d46

Contents?: true

Size: 1.9 KB

Versions: 151

Compression:

Stored size: 1.9 KB

Contents

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

Version data entries

151 entries across 151 versions & 5 rubygems

Version Path
puppet-retrospec-0.12.2 vendor/gems/puppet-3.7.3/lib/puppet/indirector/couch.rb
puppet-3.8.7 lib/puppet/indirector/couch.rb
puppet-3.8.7-x86-mingw32 lib/puppet/indirector/couch.rb
puppet-3.8.7-x64-mingw32 lib/puppet/indirector/couch.rb
puppet-3.8.6 lib/puppet/indirector/couch.rb
puppet-3.8.6-x86-mingw32 lib/puppet/indirector/couch.rb
puppet-retrospec-0.12.1 vendor/gems/puppet-3.7.3/lib/puppet/indirector/couch.rb
puppet-3.8.6-x64-mingw32 lib/puppet/indirector/couch.rb
puppet-retrospec-0.12.0 vendor/gems/puppet-3.7.3/lib/puppet/indirector/couch.rb
puppet-3.8.5 lib/puppet/indirector/couch.rb
puppet-3.8.5-x86-mingw32 lib/puppet/indirector/couch.rb
puppet-3.8.5-x64-mingw32 lib/puppet/indirector/couch.rb
puppet-3.8.4 lib/puppet/indirector/couch.rb
puppet-3.8.4-x86-mingw32 lib/puppet/indirector/couch.rb
puppet-3.8.4-x64-mingw32 lib/puppet/indirector/couch.rb
puppet-retrospec-0.11.0 vendor/gems/puppet-3.7.3/lib/puppet/indirector/couch.rb
puppet-retrospec-0.10.0 vendor/gems/puppet-3.7.3/lib/puppet/indirector/couch.rb
puppet-retrospec-0.9.1 vendor/gems/puppet-3.7.3/lib/puppet/indirector/couch.rb
puppet-retrospec-0.9.0 vendor/gems/puppet-3.7.3/lib/puppet/indirector/couch.rb
puppet-retrospec-0.8.1 vendor/gems/puppet-3.7.3/lib/puppet/indirector/couch.rb