Skip to content

Commit

Permalink
Merge pull request #1218 from jeremymv2/jeremymv2/node_http_head
Browse files Browse the repository at this point in the history
support HEAD http request on named node endpoint
  • Loading branch information
markan authored Jul 12, 2018
2 parents 670aac6 + 6174f5f commit 651ef2a
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 3 deletions.
4 changes: 4 additions & 0 deletions oc-chef-pedant/lib/pedant/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ def get(url, requestor, opts={}, &validator)
authenticated_request :GET, url, requestor, opts, &validator
end

def head(url, requestor, opts={}, &validator)
authenticated_request :HEAD, url, requestor, opts, &validator
end

def put(url, requestor, opts={}, &validator)
authenticated_request :PUT, url, requestor, opts, &validator
end
Expand Down
6 changes: 6 additions & 0 deletions oc-chef-pedant/lib/pedant/rspec/node_util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,12 @@ def validates_run_list
}
end

let(:head_success_response) do
{
:status => 200
}
end

let(:fetch_node_list_empty_response) do
{
:status => 200,
Expand Down
19 changes: 19 additions & 0 deletions oc-chef-pedant/spec/api/nodes/complete_endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,25 @@
end
end # GET /nodes/<name>

context 'HEAD /nodes/<name>' do
let(:request_method) { :HEAD }
let(:request_url) { api_url("/nodes/#{node_name}") }

context 'for a nonexistent node' do
let(:node_name){nonexistent_node_name}
it 'returns a 404', :smoke do
should look_like http_404_response
end
end

context 'for an existing node' do
include_context 'with temporary testing node'
it 'returns a 200', :smoke do
should look_like head_success_response
end
end
end # HEAD /nodes/<name>

context 'GET /environments/<environment_name>/nodes' do
let(:request_method) { :GET }
let(:request_url) { api_url("/environments/#{environment_name}/nodes") }
Expand Down
12 changes: 9 additions & 3 deletions src/oc_erchef/apps/oc_chef_wm/src/chef_wm_named_node.erl
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ request_type() ->
"nodes".

allowed_methods(Req, State) ->
{['GET', 'PUT', 'DELETE'], Req, State}.
{['HEAD', 'GET', 'PUT', 'DELETE'], Req, State}.

validate_request(Method, Req, State) when Method == 'GET';
Method == 'HEAD';
Method == 'DELETE' ->
{Req, State};
validate_request('PUT', Req, #base_state{resource_state = NodeState} = State) ->
Expand Down Expand Up @@ -87,8 +88,13 @@ auth_info(Req, #base_state{chef_db_context = DbContext,
end.

to_json(Req, #base_state{resource_state = NodeState} = State) ->
#node_state{chef_node = Node} = NodeState,
{chef_db_compression:decompress(Node#chef_node.serialized_object), Req, State}.
case wrq:method(Req) of
'HEAD' ->
{<<>>, Req, State};
_ ->
#node_state{chef_node = Node} = NodeState,
{chef_db_compression:decompress(Node#chef_node.serialized_object), Req, State}
end.

from_json(Req, #base_state{resource_state = #node_state{chef_node = Node,
node_data = NodeData}} = State) ->
Expand Down
2 changes: 2 additions & 0 deletions src/oc_erchef/apps/oc_chef_wm/src/oc_chef_action.erl
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ log_action(Req, #base_state{resource_state = ResourceState,
case wrq:method(Req) of
'GET' ->
ok;
'HEAD' ->
ok;
_ElseMethod -> %% POST, PUT, DELETE
case ResourceMod of
oc_chef_wm_authenticate_user -> %% POST to authenticate_user should not be an action.
Expand Down
2 changes: 2 additions & 0 deletions src/oc_erchef/apps/oc_chef_wm/src/oc_chef_wm_base.erl
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,8 @@ http_method_to_authz_perm(#wm_reqdata{}=Req) ->
http_method_to_authz_perm(wrq:method(Req));
http_method_to_authz_perm('DELETE') ->
delete;
http_method_to_authz_perm('HEAD') ->
read;
http_method_to_authz_perm('GET') ->
read;
http_method_to_authz_perm('POST') ->
Expand Down

0 comments on commit 651ef2a

Please sign in to comment.