# class Puppet::Indirector::JSON

The base class for JSON indirection terminus implementations.

This should generally be preferred to the YAML base for any future implementations, since it is ~ three times faster despite being pure Ruby rather than a C implementation.

### Public Instance Methods

destroy(request) click to toggle source

    # File lib/puppet/indirector/json.rb, line 23
    def destroy(request)
      File.unlink(path(request.key))
    rescue => detail
      unless detail.is_a? Errno::ENOENT
        raise Puppet::Error, "Could not destroy #{self.name} #{request.key}: #{detail}"
      end
      1                           # emulate success...
    end

find(request) click to toggle source

    # File lib/puppet/indirector/json.rb, line 10
    def find(request)
      load_json_from_file(path(request.key), request.key)
    end

path(name, ext = '.json') click to toggle source

Return the path to a given node’s file.

    # File lib/puppet/indirector/json.rb, line 39
    def path(name, ext = '.json')
      if name =~ Puppet::Indirector::BadNameRegexp then
        Puppet.crit("directory traversal detected in #{self.class}: #{name.inspect}")
        raise ArgumentError, "invalid key"
      end

      base = Puppet.run_mode.master? ? Puppet[:server_datadir] : Puppet[:client_datadir]
      File.join(base, self.class.indirection_name.to_s, name.to_s + ext)
    end

save(request) click to toggle source

    # File lib/puppet/indirector/json.rb, line 14
    def save(request)
      filename = path(request.key)
      FileUtils.mkdir_p(File.dirname(filename))

      Puppet::Util.replace_file(filename, 0660) {|f| f.print to_json(request.instance) }
    rescue TypeError => detail
      Puppet.log_exception "Could not save #{self.name} #{request.key}: #{detail}"
    end

search(request) click to toggle source

    # File lib/puppet/indirector/json.rb, line 32
    def search(request)
      Dir.glob(path(request.key)).collect do |file|
        load_json_from_file(file, request.key)
      end
    end