Commit ad366b9e authored by Samir Noir's avatar Samir Noir 🧀
Browse files

Enhance JSON output when adding nodes to a vlan via POST request

Before, the returned JSON contained only "ok" when a node was added to
vlan or "unknown_node", and nothing if the node was already present in
vlan.
Now, it will add a status and message field, for each case (successfully
added, unknown and already present in vlan).
parent 6ac9ef48
Pipeline #186057 waiting for manual action with stages
in 22 minutes and 11 seconds
......@@ -51,14 +51,32 @@ class VlansNodesController < ApplicationController
raise UnprocessableEntity, "Missing node list"
end
result = @kavlan.update_vlan_nodes(params[:vlan_id], params[:vlans_node][:_json])
if result.code.to_i == 403
http = @kavlan.update_vlan_nodes(params[:vlan_id], params[:vlans_node][:_json])
if http.code.to_i == 403
raise Forbidden, "Not enough privileges on Kavlan resources"
end
result = {}
kavlan_result = JSON.parse(http.body)
params[:vlans_node][:_json].each do |node|
result[node] = {}
if kavlan_result[node] == 'ok'
result[node][:status] = 'success'
result[node][:message] = 'Successfully added to vlan'
elsif kavlan_result[node] == 'unknown_node'
result[node][:status] = 'failure'
result[node][:message] = 'Unknown node'
else
result[node][:status] = 'unchanged'
result[node][:message] = 'Node already in vlan'
end
end
respond_to do |format|
format.g5kitemjson { render json: result.body }
format.json { render json: result.body }
format.g5kitemjson { render json: result }
format.json { render json: result }
end
end
......
......@@ -83,14 +83,14 @@ describe VlansNodesController do
stub_request(:post, File.join(@base_expected_uri, '1')).
with(
headers: {
'Accept'=>'application/json',
'Host'=>'api-out.local',
'Accept' => 'application/json',
'Host' => 'api-out.local',
},
body: {"nodes" => ['paravance-9.rennes.grid5000.fr', 'paravance-10.rennes.grid5000.fr']}
body: {'nodes' => ['paravance-9.rennes.grid5000.fr', 'paravance-10.rennes.grid5000.fr', 'parapide-14.rennes.grid5000.fr']}
).
to_return(status: 200,
body: '{"paravance-9.rennes.grid5000.fr":"ok",
"paravance-10.rennes.grid5000.fr":"ok"}',
"parapide-14.rennes.grid5000.fr":"unknown_node"}',
headers: @headers_return)
authenticate_as('snoir')
......@@ -100,14 +100,27 @@ describe VlansNodesController do
format: :json,
vlan_id: 1,
_json: ['paravance-9.rennes.grid5000.fr',
'paravance-10.rennes.grid5000.fr']
'paravance-10.rennes.grid5000.fr',
'parapide-14.rennes.grid5000.fr']
}
expect(response.status).to eq(200)
expect(json).to be_a(Hash)
expect(json.length).to eq(2)
expect(json).to eq({"paravance-9.rennes.grid5000.fr" => "ok",
"paravance-10.rennes.grid5000.fr" => "ok"})
expect(json.length).to eq(3)
expect(json).to eq({
'paravance-9.rennes.grid5000.fr' => {
'status' => 'success',
'message' => 'Successfully added to vlan'
},
'paravance-10.rennes.grid5000.fr' => {
'status' => 'unchanged',
'message' => 'Node already in vlan'
},
'parapide-14.rennes.grid5000.fr' => {
'status' => 'failure',
'message' => 'Unknown node'
}
})
end
end
end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment