Module: Sensu::API::Routes::Stashes

Included in:
Sensu::API::Routes
Defined in:
lib/sensu/api/routes/stashes.rb

Constant Summary collapse

STASHES_URI =
/^\/stashes$/
STASH_URI =
/^\/stash(?:es)?\/(.*)$/

Instance Method Summary collapse

Instance Method Details

#delete_stashObject

DELETE /stash/:path or /stashes/:path



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/sensu/api/routes/stashes.rb', line 35

def delete_stash
  path = parse_uri(STASH_URI).first
  @redis.exists("stash:#{path}") do |stash_exists|
    if stash_exists
      @redis.srem("stashes", path) do
        @redis.del("stash:#{path}") do
          no_content!
        end
      end
    else
      not_found!
    end
  end
end

#get_stashObject

GET /stash/:path or /stashes/:path



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/sensu/api/routes/stashes.rb', line 22

def get_stash
  path = parse_uri(STASH_URI).first
  @redis.get("stash:#{path}") do |stash_json|
    unless stash_json.nil?
      @response_content = Sensu::JSON.load(stash_json)
      respond
    else
      not_found!
    end
  end
end

#get_stashesObject

GET /stashes



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
79
# File 'lib/sensu/api/routes/stashes.rb', line 51

def get_stashes
  @response_content = []
  @redis.smembers("stashes") do |stashes|
    unless stashes.empty?
      stashes = pagination(stashes)
      stashes.each_with_index do |path, index|
        @redis.get("stash:#{path}") do |stash_json|
          @redis.ttl("stash:#{path}") do |ttl|
            unless stash_json.nil?
              item = {
                :path => path,
                :content => Sensu::JSON.load(stash_json),
                :expire => ttl
              }
              @response_content << item
            else
              @redis.srem("stashes", path)
            end
            if index == stashes.length - 1
              respond
            end
          end
        end
      end
    else
      respond
    end
  end
end

#post_stashObject

POST /stash/:path or /stashes/:path



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/sensu/api/routes/stashes.rb', line 9

def post_stash
  path = parse_uri(STASH_URI).first
  read_data do |data|
    @redis.set("stash:#{path}", Sensu::JSON.dump(data)) do
      @redis.sadd("stashes", path) do
        @response_content = {:path => path}
        created!
      end
    end
  end
end

#post_stashesObject

POST /stashes



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/sensu/api/routes/stashes.rb', line 82

def post_stashes
  rules = {
    :path => {:type => String, :nil_ok => false},
    :content => {:type => Hash, :nil_ok => false},
    :expire => {:type => Integer, :nil_ok => true}
  }
  read_data(rules) do |data|
    stash_key = "stash:#{data[:path]}"
    @redis.set(stash_key, Sensu::JSON.dump(data[:content])) do
      @redis.sadd("stashes", data[:path]) do
        @response_content = {:path => data[:path]}
        if data[:expire]
          @redis.expire(stash_key, data[:expire]) do
            created!
          end
        else
          created!
        end
      end
    end
  end
end