-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabag-api.rb
More file actions
executable file
·90 lines (79 loc) · 1.99 KB
/
databag-api.rb
File metadata and controls
executable file
·90 lines (79 loc) · 1.99 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
# encoding: utf-8
#
# Copyright 2014 Dyn Inc.
#
# Authors: Kristian Van Der Vliet <kvandervliet@dyn.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require 'chef'
load 'databag-api.env'
def chef_rest(location)
conn = Chef::REST.new(
CHEF_SERVER_URL,
CHEF_CLIENT_NAME,
CHEF_CLIENT_KEY
)
begin
conn.get(location)
rescue Net::HTTPServerException => ex
$stderr.puts "#{location} : #{ex}"
end
end
def get_databag_list
chef_rest('data')
end
def get_databag(databag)
chef_rest("data/#{databag}")
end
def get_databag_item(databag, item)
chef_rest("data/#{databag}/#{item}")
end
get "/" do
content_type :json
response = {}
response[:items] = []
databag_list = get_databag_list()
response[:total] = databag_list.length
databag_list.each do |name, location|
databag = {}
databag["databag_name"] = name
databag["databag"] = File.join(API_BASE_URL, name)
response[:items].push(databag)
end
response.to_json
end
get "/all" do
content_type :json
response = {}
databag_list = get_databag_list()
databag_list.to_json
end
get "/:databag" do
content_type :json
name = params[:databag]
my_databag = get_databag(name)
response = {}
response[:name] = name
response[:items] = []
my_databag.each do |item, location|
response[:items].push File.join(API_BASE_URL, name, item)
end
response.to_json
end
get "/:databag/:item" do
content_type :json
name = params[:databag]
item = params[:item]
item = get_databag_item(name, item)
item.to_json
end