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

Add two controllers to glue around Kavlan's users API

parent 6e811e59
# Copyright (c) 2009-2011 Cyril Rohr, INRIA Rennes - Bretagne Atlantique
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
class VlansUsersAllController < ApplicationController
include Vlans
# List users
def index
allow :get
expires_in 60.seconds
result = @kavlan.users
result['items'].each do |item|
item['links'] = links_for_item(item)
end
result['links'] = links_for_collection
respond_to do |format|
format.g5kcollectionjson { render json: result }
format.json { render json: result }
end
end
# Display the vlans allowed for a user
def show
allow :get
expires_in 60.seconds
result = @kavlan.users(params[:user_id])
result['links'] = links_for_item(result)
respond_to do |format|
format.g5kitemjson { render json: result }
format.json { render json: result }
end
end
protected
def collection_path
site_vlans_users_path(params[:site_id])
end
def parent_path
site_vlans_path(params[:site_id])
end
def links_for_item(item)
links = []
links.push({
'rel' => 'self',
'href' => uri_to(File.join(collection_path, item['uid'])),
'type' => api_media_type(:g5kitemjson)
},
{
'rel' => 'parent',
'href' => uri_to(File.join(collection_path)),
'type' => api_media_type(:g5kcollectionjson)
})
links
end
def links_for_collection
[
{
'rel' => 'self',
'href' => uri_to(collection_path),
'type' => api_media_type(:g5kcollectionjson)
},
{
'rel' => 'parent',
'href' => uri_to(parent_path),
'type' => api_media_type(:g5kcollectionjson)
}
]
end
end
# Copyright (c) 2009-2011 Cyril Rohr, INRIA Rennes - Bretagne Atlantique
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
class VlansUsersController < ApplicationController
include Vlans
# List users
def index
allow :get
expires_in 60.seconds
result = @kavlan.vlan_users(params[:vlan_id])
result.delete('uid')
result['items'].each do |item|
item['links'] = links_for_item(item)
end
result['links'] = links_for_collection
respond_to do |format|
format.g5kcollectionjson { render json: result }
format.json { render json: result }
end
end
# Display the rights on a vlan for a user
def show
allow :get
expires_in 60.seconds
result = @kavlan.vlan_users(params[:vlan_id], params[:id])
result['links'] = links_for_collection
respond_to do |format|
format.g5kitemjson { render json: result }
format.json { render json: result }
end
end
# Remove rights for user on a vlan
def destroy
ensure_authenticated!
allow :delete
result = @kavlan.delete_user(params[:vlan_id], params[:id])
if result.code.to_i == 403
raise Forbidden, 'Not enough privileges on Kavlan resources'
end
render plain: '',
status: result.code
end
# Add rights for user on a vlan
def add
ensure_authenticated!
allow :put
result = @kavlan.add_user(params[:vlan_id], params[:id])
if result.code.to_i == 403
raise Forbidden, "Not enough privileges on Kavlan resources"
end
render plain: '',
status: result.code
end
protected
def collection_path
params[:id] ? site_vlan_vlans_user_path : site_vlan_vlans_users_path
end
def parent_path
if params[:id]
site_vlan_vlans_users_path
else
File.join(site_vlans_path, params[:vlan_id])
end
end
def links_for_item(item)
links = []
links.push({
'rel' => 'self',
'href' => uri_to(File.join(collection_path, item['uid'])),
'type' => api_media_type(:g5kitemjson)
},
{
'rel' => 'parent',
'href' => uri_to(File.join(collection_path)),
'type' => api_media_type(:g5kcollectionjson)
})
links
end
def links_for_collection
self_media_type = params[:id] ? :g5kitemjson : :g5kcollectionjson
parent_media_type = params[:id] ? :g5kcollectionjson : :g5kitemjson
[
{
'rel' => 'self',
'href' => uri_to(collection_path),
'type' => api_media_type(self_media_type)
},
{
'rel' => 'parent',
'href' => uri_to(parent_path),
'type' => api_media_type(parent_media_type)
}
]
end
end
......@@ -25,11 +25,20 @@ Api::Application.routes.draw do
resources :network_equipments, only: %i[index show]
resources :sites, only: %i[index show] do
get '/vlans/users' => 'vlans_users_all#index'
get '/vlans/users/:user_id' => 'vlans_users_all#show'
resources :vlans, only: %i[index show] do
member do
put 'dhcpd' => 'vlans#dhcpd'
match 'dhcpd' => 'errors#err_method_not_allowed', :via => [:all]
end
resources :vlans_users, path: '/users', only: %i[index show destroy] do
member do
put '/' => 'vlans_users#add'
end
end
end
member do
......
# Copyright (c) 2009-2011 Cyril Rohr, INRIA Rennes - Bretagne Atlantique
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require 'spec_helper'
describe VlansUsersAllController do
render_views
before do
@base_expected_uri = 'http://api-out.local/sites/rennes/internal/kavlanapi/'
@headers_return = { 'Content-Type' => 'application/json' }
stub_request(:get, @base_expected_uri).
with(
headers: {
'Accept'=>'application/json',
'Host'=>'api-out.local',
}).
to_return(status: 200, body: fixture('kavlan-rennes-root.json'), headers: @headers_return)
end
describe 'GET /vlans/users' do
it 'should return all users having a vlan' do
stub_request(:get, File.join(@base_expected_uri, 'users')).
with(
headers: {
'Accept'=>'application/json',
'Host'=>'api-out.local',
}).
to_return(status: 200, body: fixture('kavlan-rennes-users.json'), headers: @headers_return)
get :index, params: { site_id: 'rennes', format: :json }
expect(response.status).to eq(200)
expect(json).to be_a(Hash)
expect(json['total']).to eq(1)
expect(json['items'].length).to eq(1)
expect(json['items'].first['uid']).to eq('snoir')
expect(json['items'].first['links']).to eq([
{
'rel' => 'self',
'href' => '/sites/rennes/vlans/users/snoir',
'type' => api_media_type(:g5kitemjson)
},
{
'rel' => 'parent',
'href' => '/sites/rennes/vlans/users',
'type' => api_media_type(:g5kcollectionjson)
}
])
expect(json['links']).to eq([
{
'href' => '/sites/rennes/vlans/users',
'rel' => 'self',
'type' => api_media_type(:g5kcollectionjson)
},
{
'href' => '/sites/rennes/vlans',
'rel' => 'parent',
'type' => api_media_type(:g5kcollectionjson)
}
])
end
it 'should get vlans assigned for user' do
stub_request(:get, File.join(@base_expected_uri, 'users/snoir')).
with(
headers: {
'Accept'=>'application/json',
'Host'=>'api-out.local',
}).
to_return(status: 200, body: fixture('kavlan-rennes-users-snoir.json'), headers: @headers_return)
get :show, params: { site_id: 'rennes', user_id: 'snoir', format: :json }
expect(response.status).to eq(200)
expect(json).to be_a(Hash)
expect(json['uid']).to eq('snoir')
expect(json.length).to eq(3)
expect(json['vlans']).to be_a(Array)
expect(json['vlans']).to eq(['1'])
expect(json['links']).to eq([
{
'href' => '/sites/rennes/vlans/users/snoir',
'rel' => 'self',
'type' => api_media_type(:g5kitemjson)
},
{
'href' => '/sites/rennes/vlans/users',
'rel' => 'parent',
'type' => api_media_type(:g5kcollectionjson)
}
])
end
end
end
# Copyright (c) 2009-2011 Cyril Rohr, INRIA Rennes - Bretagne Atlantique
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require 'spec_helper'
describe VlansUsersController do
render_views
before do
@base_expected_uri = 'http://api-out.local/sites/rennes/internal/kavlanapi/'
@headers_return = { 'Content-Type' => 'application/json' }
stub_request(:get, @base_expected_uri).
with(
headers: {
'Accept'=>'application/json',
'Host'=>'api-out.local',
}).
to_return(status: 200, body: fixture('kavlan-rennes-root.json'), headers: @headers_return)
end
describe 'GET /vlans/:id/users' do
it 'should have users for vlan 1' do
stub_request(:get, File.join(@base_expected_uri, '1', 'users')).
with(
headers: {
'Accept'=>'application/json',
'Host'=>'api-out.local',
}).
to_return(status: 200, body: fixture('kavlan-rennes-vlan-users-1.json'), headers: @headers_return)
get :index, params: { site_id: 'rennes', vlan_id: '1', format: :json }
expect(response.status).to eq(200)
expect(json).to be_a(Hash)
expect(json['total']).to eq(1)
expect(json['items'].length).to eq(1)
expect(json['items'].first['uid']).to eq('snoir')
expect(json['items'].first['links']).to eq([
{
'rel' => 'self',
'href' => '/sites/rennes/vlans/1/users/snoir',
'type' => api_media_type(:g5kitemjson)
},
{
'rel' => 'parent',
'href' => '/sites/rennes/vlans/1/users',
'type' => api_media_type(:g5kcollectionjson)
}
])
expect(json['links']).to eq([
{
'rel' => 'self',
'href' => '/sites/rennes/vlans/1/users',
'type' => api_media_type(:g5kcollectionjson)
},
{
'rel' => 'parent',
'href' => '/sites/rennes/vlans/1',
'type' => api_media_type(:g5kitemjson)
}
])
end
it 'should return user as authorized' do
stub_request(:get, File.join(@base_expected_uri, '1', 'users', 'snoir')).
with(
headers: {
'Accept'=>'application/json',
'Host'=>'api-out.local',
}).
to_return(status: 200, body: fixture('kavlan-rennes-vlan-users-1-snoir.json'), headers: @headers_return)
get :show, params: { site_id: 'rennes', vlan_id: '1', id: 'snoir', format: :json }
expect(response.status).to eq(200)
expect(json).to be_a(Hash)
expect(json.length).to eq(3)
expect(json['uid']).to eq('snoir')
expect(json['status']).to eq('authorized')
expect(json['links']).to eq([{"rel"=>"self", "href"=>"/sites/rennes/vlans/1/users/snoir", "type"=>"application/vnd.grid5000.item+json"}, {"rel"=>"parent", "href"=>"/sites/rennes/vlans/1/users", "type"=>"application/vnd.grid5000.collection+json"}])
end
it 'should return user as unauthorized' do
stub_request(:get, File.join(@base_expected_uri, '5', 'users', 'snoir')).
with(
headers: {
'Accept'=>'application/json',
'Host'=>'api-out.local',
}).
to_return(status: 404, headers: @headers_return)
get :show, params: { site_id: 'rennes', vlan_id: '5', id: 'snoir', format: :json }
expect(response.status).to eq(200)
expect(json).to be_a(Hash)
expect(json.length).to eq(3)
expect(json['uid']).to eq('snoir')
expect(json['status']).to eq('unauthorized')
expect(json['links']).to eq([{"rel"=>"self", "href"=>"/sites/rennes/vlans/5/users/snoir", "type"=>"application/vnd.grid5000.item+json"}, {"rel"=>"parent", "href"=>"/sites/rennes/vlans/5/users", "type"=>"application/vnd.grid5000.collection+json"}])
end
end
end
{"uid":"snoir","vlans":["1"],"links":[{"href":"/3.0/sites/rennes/vlans/users/snoir","rel":"self","type":"application/vnd.grid5000.item+json"},{"href":"/3.0/sites/rennes/vlans/users","rel":"parent","type":"application/vnd.grid5000.item+json"}]}
\ No newline at end of file
{"offset":0,"total":1,"items":[{"uid":"snoir","links":[{"href":"/3.0/sites/rennes/vlans/users/snoir","rel":"self","type":"application/vnd.grid5000.item+json"},{"href":"/3.0/sites/rennes/vlans/users","rel":"parent","type":"application/vnd.grid5000.item+json"}]}],"links":[{"href":"/3.0/sites/rennes/vlans/users","rel":"self","type":"application/vnd.grid5000.item+json"},{"href":"/3.0/sites/rennes/vlans","rel":"parent","type":"application/vnd.grid5000.item+json"}]}
{"1":"authorized","links":[{"href":"/3.0/sites/rennes/vlans/1","rel":"self","type":"application/vnd.grid5000.item+json"},{"href":"/3.0/sites/rennes/vlans","rel":"parent","type":"application/vnd.grid5000.collection+json"}]}
\ No newline at end of file
{"uid":"users","offset":0,"total":1,"items":[{"uid":"snoir","links":[{"href":"/3.0/sites/rennes/vlans/1/users/snoir","rel":"self","type":"application/vnd.grid5000.item+json"},{"href":"/3.0/sites/rennes/vlans/1/users","rel":"parent","type":"application/vnd.grid5000.item+json"}]}],"links":[{"href":"/3.0/sites/rennes/vlans/1","rel":"self","type":"application/vnd.grid5000.item+json"},{"href":"/3.0/sites/rennes/vlans","rel":"parent","type":"application/vnd.grid5000.collection+json"}]}
\ No newline at end of file
Supports Markdown
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