summaryrefslogtreecommitdiff
path: root/lib/chef_zero/endpoints/policy_groups_endpoint.rb
blob: c22bf34c7cd0fc437d573d3a5a07fedadf2df85c (plain)
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
require "ffi_yajl"
require_relative "../rest_base"
require_relative "../chef_data/data_normalizer"

module ChefZero
  module Endpoints
    # /organizations/ORG/policy_groups
    #
    # in the data store, this REST path actually stores the revision ID of ${policy_name} that's currently
    # associated with ${policy_group}.
    class PolicyGroupsEndpoint < RestBase
      # GET /organizations/ORG/policy_groups
      def get(request)
        # each policy group has policies and associated revisions under
        # /policy_groups/{group name}/policies/{policy name}.
        response_data = {}
        list_data(request).each do |group_name|
          group_path = request.rest_path + [group_name]
          policy_list = list_data(request, group_path + ["policies"])

          # build the list of policies with their revision ID associated with this policy group.
          policies = {}
          policy_list.each do |policy_name|
            revision_id = parse_json(get_data(request, group_path + ["policies", policy_name]))
            policies[policy_name] = { revision_id: revision_id }
          end

          response_data[group_name] = {
            uri: build_uri(request.base_uri, group_path),
          }
          response_data[group_name][:policies] = policies unless policies.empty?
        end

        json_response(200, response_data)
      end
    end
  end
end