class ChefZero::Endpoints::PolicyGroupPolicyEndpoint

/organizations/ORG/policy_groups/GROUP/policies/NAME

in the data store, this REST path actually stores the revision ID of ${policy_name} that's currently associated with ${policy_group}.

Public Instance Methods

delete(request) click to toggle source

DELETE /organizations/ORG/policy_groups/GROUP/policies/NAME

# File lib/chef_zero/endpoints/policy_group_policy_endpoint.rb, line 65
def delete(request)
  # Save the existing association.
  current_revision_id = parse_json(get_data(request))

  # delete the association.
  delete_data(request)

  # return the full policy document at the no-longer-associated revision.
  policy_name = request.rest_path[5]
  policy_path = request.rest_path[0..1] + ["policies", policy_name,
                                           "revisions", current_revision_id]

  full_policy_doc = parse_json(get_data(request, policy_path))
  full_policy_doc = ChefData::DataNormalizer.normalize_policy(full_policy_doc, policy_name, current_revision_id)
  json_response(200, full_policy_doc)
end
get(request) click to toggle source

GET /organizations/ORG/policy_groups/GROUP/policies/NAME

# File lib/chef_zero/endpoints/policy_group_policy_endpoint.rb, line 14
def get(request)
  policy_name = request.rest_path[5]

  # fetch /organizations/{organization}/policies/{policy_name}/revisions/{revision_id}
  revision_id = parse_json(get_data(request))
  result = get_data(request, request.rest_path[0..1] +
                             ["policies", policy_name, "revisions", revision_id])
  result = ChefData::DataNormalizer.normalize_policy(parse_json(result), policy_name, revision_id)
  json_response(200, result)
end
put(request) click to toggle source

PUT /organizations/ORG/policy_groups/GROUP/policies/NAME

# File lib/chef_zero/endpoints/policy_group_policy_endpoint.rb, line 43
def put(request)
  policyfile_data = parse_json(request.body)
  policy_name = request.rest_path[5]
  revision_id = policyfile_data["revision_id"]

  # If the policy revision being submitted does not exist, create it.
  # Storage: /organizations/ORG/policies/POLICY/revisions/REVISION
  policyfile_path = request.rest_path[0..1] + ["policies", policy_name, "revisions", revision_id]
  unless exists_data?(request, policyfile_path)
    create_data(request, policyfile_path[0..-2], revision_id, request.body, :create_dir)
  end

  # if named policy exists and the given revision ID exists, associate the revision ID with the policy
  # group.
  # Storage: /organizations/ORG/policies/POLICY/revisions/REVISION
  response_code = exists_data?(request) ? 200 : 201
  set_data(request, nil, to_json(revision_id), :create, :create_dir)

  already_json_response(response_code, request.body)
end