Class: ChefZero::Endpoints::PolicyGroupPolicyEndpoint

Inherits:
RestBase
  • Object
show all
Defined in:
lib/chef_zero/endpoints/policy_group_policy_endpoint.rb

Overview

/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.

Constant Summary

Constants inherited from RestBase

RestBase::DEFAULT_REQUEST_VERSION, RestBase::DEFAULT_RESPONSE_VERSION

Instance Attribute Summary

Attributes inherited from RestBase

#server

Instance Method Summary collapse

Methods inherited from RestBase

#accepts?, #already_json_response, #build_uri, build_uri, #call, #check_api_version, #create_data, #create_data_dir, #data_store, #delete_data, #delete_data_dir, #error, #exists_data?, #exists_data_dir?, #get_data, #get_data_or_else, #hashify_list, #head_request, #initialize, #json_only, #json_response, #list_data, #list_data_or_else, #parse_json, #policy_name_invalid?, #populate_defaults, rfc2396_parser, #set_data, #text_response, #to_json

Constructor Details

This class inherits a constructor from ChefZero::RestBase

Instance Method Details

#delete(request) ⇒ Object

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



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# 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) ⇒ Object

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



14
15
16
17
18
19
20
21
22
23
# 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) ⇒ Object

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



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# 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