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
91
92
93
94
95
96
97
98
99
100
|
module ChefZero
module Endpoints
# /organizations/NAME/policies
class OrganizationPoliciesEndpoint < RestBase
def hashify_list(list)
list.reduce({}) { |acc, obj| acc.merge( obj => {} ) }
end
def get(request)
# vanilla /policies.
if request.rest_path.last == "policies"
response_data = {}
policy_names = list_data_or_else(request, nil, [])
policy_names.each do |policy_name|
policy_path = request.rest_path + [policy_name]
policy_uri = build_uri(request.base_uri, policy_path)
revisions = list_data_or_else(request, policy_path + ["revisions"], {})
response_data[policy_name] = {
uri: policy_uri,
revisions: hashify_list(revisions)
}
end
return json_response(200, response_data)
end
# /policies/:policy_name
if request.rest_path[-2] == "policies"
if !exists_data_dir?(request)
return error(404, "Item not found" )
else
revisions = list_data(request, request.rest_path + ["revisions"])
data = { revisions: hashify_list(revisions) }
return json_response(200, data)
end
end
# /policies/:policy_name/revisions/:revision_id
if request.rest_path[-2] == "revisions"
if !exists_data?(request, nil)
return error(404, "Revision ID #{request.rest_path.last} not found" )
else
data = get_data(request)
return already_json_response(200, data)
end
end
end
def post(request)
if request.rest_path.last == "revisions"
# POST /policies/:policy_name/revisions
# we want to create /policies/{policy_name}/revisions/{revision_id}
policyfile_data = parse_json(request.body)
uri_policy_name = request.rest_path[-2]
if exists_data?(request, request.rest_path + [policyfile_data["revision_id"]])
return error(409, "Revision ID #{policyfile_data["revision_id"]} already exists.")
end
if policyfile_data["name"] != uri_policy_name
return error(400, "URI policy name #{uri_policy_name} does not match JSON policy name #{policyfile_data["name"]}")
end
revision_path = request.rest_path + [policyfile_data["revision_id"]]
set_data(request, revision_path, request.body, *set_opts)
return already_json_response(201, request.body)
end
end
def delete(request)
# /policies/:policy_name/revisions/:revision_id
if request.rest_path[-2] == "policies"
revisions = list_data(request, request.rest_path + ["revisions"])
data = { revisions: hashify_list(revisions) }
delete_data_dir(request, nil, :recursive)
return json_response(200, data)
end
if request.rest_path[-2] == "revisions"
if exists_data?(request)
policyfile_data = get_data(request)
delete_data(request)
return already_json_response(200, policyfile_data)
else
return error(404, "Revision ID #{request.rest_path.last} not found")
end
end
end
private
def set_opts
[ :create_dir ]
end
end
end
end
|