commit 997bfddb253fc30c6406483eb3d0caf1c6cc27a5
Author: Joseph Magen <joseph@isratrade.co.il>
Date:   Tue Dec 24 09:27:56 2013 +0200

    fixes #3928 - API v2 - update os_default_templates for operating system

diff --git a/app/controllers/api/v2/os_default_templates_controller.rb b/app/controllers/api/v2/os_default_templates_controller.rb
new file mode 100644
index 000000000..6d4b98d03
--- /dev/null
+++ b/app/controllers/api/v2/os_default_templates_controller.rb
@@ -0,0 +1,69 @@
+module Api
+  module V2
+    class OsDefaultTemplatesController < V2::BaseController
+
+      include Api::Version2
+      include Api::TaxonomyScope
+
+      before_filter :find_required_nested_object
+      before_filter :find_resource, :only => %w{show update destroy}
+
+      api :GET, '/operatingsystems/:operatingsystem_id/os_default_templates', 'List os default templates for operating system'
+      param :operatingsystem_id, String, :desc => 'id of operating system'
+      param :page, String, :desc => 'paginate results'
+      param :per_page, String, :desc => 'number of entries per request'
+
+      def index
+        @os_default_templates = nested_obj.os_default_templates.paginate(paginate_options)
+      end
+
+      api :GET, '/operatingsystems/:operatingsystem_id/os_default_templates/:id', 'Show a os default template kind for operating system'
+      param :operatingsystem_id, String, :desc => 'id of operating system'
+      param :id, :number, :required => true
+
+      def show
+      end
+
+      def_param_group :os_default_template do
+        param :os_default_template, Hash, :required => true do
+          param :template_kind_id, :number
+          param :config_template_id, :number
+        end
+      end
+
+      api :POST, '/operatingsystems/:operatingsystem_id/os_default_templates/', 'Create a os default template for operating system'
+      param :operatingsystem_id, String, :desc => 'id of operating system'
+      param_group :os_default_template
+
+      def create
+        @os_default_template = nested_obj.os_default_templates.new(params[:os_default_template])
+        process_response @os_default_template.save
+      end
+
+      api :PUT, '/operatingsystems/:operatingsystem_id/os_default_templates/:id', 'Update a os default template for operating system'
+      param :operatingsystem_id, String, :desc => 'id of operating system'
+      param :id, String, :required => true
+      param_group :os_default_template
+
+      def update
+        process_response @os_default_template.update_attributes(params[:os_default_template])
+      end
+
+      api :DELETE, '/operatingsystems/:operatingsystem_id/os_default_templates/:id', 'Delete a os default template for operating system'
+      param :operatingsystem_id, String, :desc => 'id of operating system'
+      param :id, String, :required => true
+
+      def destroy
+        process_response @os_default_template.destroy
+      end
+
+      private
+
+      def allowed_nested_id
+        %w(operatingsystem_id)
+      end
+
+
+    end
+  end
+end
diff --git a/app/services/foreman/access_permissions.rb b/app/services/foreman/access_permissions.rb
index f49e7995e..d0f375011 100644
--- a/app/services/foreman/access_permissions.rb
+++ b/app/services/foreman/access_permissions.rb
@@ -394,20 +394,24 @@ Foreman::AccessControl.map do |map|
   map.security_block :operatingsystems do |map|
     map.permission :view_operatingsystems, { :operatingsystems => [:index, :show, :bootfiles, :auto_complete_search],
                                              :"api/v1/operatingsystems" => [:index, :show, :bootfiles],
-                                             :"api/v2/operatingsystems" => [:index, :show, :bootfiles]
+                                             :"api/v2/operatingsystems" => [:index, :show, :bootfiles],
+                                             :"api/v2/os_default_templates" => [:index, :show]
                                             }
     map.permission :create_operatingsystems, {:operatingsystems => [:new, :create],
                                              :"api/v1/operatingsystems" => [:create],
                                              :"api/v2/operatingsystems" => [:create],
+                                             :"api/v2/os_default_templates" => [:create]
                                             }
     map.permission :edit_operatingsystems, {:operatingsystems => [:edit, :update],
                                        :"api/v1/operatingsystems" => [:update],
                                        :"api/v2/operatingsystems" => [:update],
-                                       :"api/v2/parameters" => [:create, :update, :destroy, :reset]
+                                       :"api/v2/parameters" => [:create, :update, :destroy, :reset],
+                                       :"api/v2/os_default_templates" => [:create, :update, :destroy]
                                      }
     map.permission :destroy_operatingsystems, {:operatingsystems => [:destroy],
                                              :"api/v1/operatingsystems" => [:destroy],
-                                             :"api/v2/operatingsystems" => [:destroy]
+                                             :"api/v2/operatingsystems" => [:destroy],
+                                             :"api/v2/os_default_templates" => [:create]
                                               }
   end
 
diff --git a/app/views/api/v2/config_templates/base.json.rabl b/app/views/api/v2/config_templates/base.json.rabl
index 09b6fea0f..46827752f 100644
--- a/app/views/api/v2/config_templates/base.json.rabl
+++ b/app/views/api/v2/config_templates/base.json.rabl
@@ -1,3 +1,3 @@
 object @config_template
 
-attributes :id, :name
+attributes :id, :name, :template_kind_id, :template_kind_name
diff --git a/app/views/api/v2/config_templates/main.json.rabl b/app/views/api/v2/config_templates/main.json.rabl
index 2d259dec3..3e2e1af96 100644
--- a/app/views/api/v2/config_templates/main.json.rabl
+++ b/app/views/api/v2/config_templates/main.json.rabl
@@ -2,4 +2,4 @@ object @config_template
 
 extends "api/v2/config_templates/base"
 
-attributes :snippet, :audit_comment, :template_kind_id, :template_kind_name, :created_at, :updated_at
+attributes :snippet, :audit_comment, :created_at, :updated_at
diff --git a/app/views/api/v2/os_default_templates/base.json.rabl b/app/views/api/v2/os_default_templates/base.json.rabl
new file mode 100644
index 000000000..03b032315
--- /dev/null
+++ b/app/views/api/v2/os_default_templates/base.json.rabl
@@ -0,0 +1,3 @@
+object @os_default_template
+
+attributes :id, :config_template_id, :config_template_name, :template_kind_id, :template_kind_name
diff --git a/app/views/api/v2/os_default_templates/create.json.rabl b/app/views/api/v2/os_default_templates/create.json.rabl
new file mode 100644
index 000000000..df8d01e4d
--- /dev/null
+++ b/app/views/api/v2/os_default_templates/create.json.rabl
@@ -0,0 +1,3 @@
+object @os_default_template
+
+extends "api/v2/os_default_templates/show"
diff --git a/app/views/api/v2/os_default_templates/index.json.rabl b/app/views/api/v2/os_default_templates/index.json.rabl
new file mode 100644
index 000000000..0168a212a
--- /dev/null
+++ b/app/views/api/v2/os_default_templates/index.json.rabl
@@ -0,0 +1,3 @@
+collection @os_default_templates
+
+extends "api/v2/os_default_templates/main"
diff --git a/app/views/api/v2/os_default_templates/main.json.rabl b/app/views/api/v2/os_default_templates/main.json.rabl
new file mode 100644
index 000000000..bd4748074
--- /dev/null
+++ b/app/views/api/v2/os_default_templates/main.json.rabl
@@ -0,0 +1,4 @@
+object @os_default_template
+
+extends "api/v2/os_default_templates/base"
+
diff --git a/app/views/api/v2/os_default_templates/show.json.rabl b/app/views/api/v2/os_default_templates/show.json.rabl
new file mode 100644
index 000000000..7d5ea1999
--- /dev/null
+++ b/app/views/api/v2/os_default_templates/show.json.rabl
@@ -0,0 +1,3 @@
+object @os_default_template
+
+extends "api/v2/os_default_templates/main"
diff --git a/app/views/api/v2/os_default_templates/update.json.rabl b/app/views/api/v2/os_default_templates/update.json.rabl
new file mode 100644
index 000000000..df8d01e4d
--- /dev/null
+++ b/app/views/api/v2/os_default_templates/update.json.rabl
@@ -0,0 +1,3 @@
+object @os_default_template
+
+extends "api/v2/os_default_templates/show"
diff --git a/config/routes/api/v2.rb b/config/routes/api/v2.rb
index 1a4b0d17b..36760f11b 100644
--- a/config/routes/api/v2.rb
+++ b/config/routes/api/v2.rb
@@ -78,6 +78,7 @@ Foreman::Application.routes.draw do
             delete '/', :to => :reset
           end
         end
+        resources :os_default_templates, :except => [:new, :edit]
       end
 
       resources :puppetclasses, :except => [:new, :edit] do
diff --git a/test/functional/api/v2/os_default_templates_controller_test.rb b/test/functional/api/v2/os_default_templates_controller_test.rb
new file mode 100644
index 000000000..0eaab087d
--- /dev/null
+++ b/test/functional/api/v2/os_default_templates_controller_test.rb
@@ -0,0 +1,52 @@
+require 'test_helper'
+
+class Api::V2::OsDefaultTemplatesControllerTest < ActionController::TestCase
+
+  test 'should get os_default_templates for os' do
+    get :index, {:operatingsystem_id => operatingsystems(:centos5_3).to_param }
+    assert_response :success
+    assert_not_nil assigns(:os_default_templates)
+    results = ActiveSupport::JSON.decode(@response.body)
+    assert_equal 6, results['results'].length
+  end
+
+  test 'should show os_default_template' do
+    get :show, { :operatingsystem_id => operatingsystems(:redhat).to_param, :id => os_default_templates(:one) }
+    assert_response :success
+    assert_not_nil assigns(:os_default_template)
+    show_response = ActiveSupport::JSON.decode(@response.body)
+    assert !show_response.empty?
+  end
+
+  test 'should create os_default_template for os' do
+    # remove all os default templates and then create one below
+    OsDefaultTemplate.delete_all
+    assert_difference('OsDefaultTemplate.count') do
+      post :create, { :operatingsystem_id => operatingsystems(:redhat).to_param, :os_default_template => {:config_template_id => config_templates(:mystring).id,
+                                                                                                          :template_kind_id => template_kinds(:gpxe).id}
+                    }
+    end
+    assert_response :success
+    assert_not_nil assigns(:os_default_template)
+  end
+
+  test 'should update os_default_template for os' do
+    # current fixtures has pxekickstart for PXELinux template kind.  Update it to pxe_local_default
+    put :update, { :operatingsystem_id => operatingsystems(:redhat).to_param,  :id => os_default_templates(:one),
+                   :os_default_template => {:config_template_id => config_templates(:pxe_local_default).id, :template_kind_id => template_kinds(:pxelinux).id}
+                    }
+    assert_response :success
+    assert_not_nil assigns(:os_default_template)
+    response = ActiveSupport::JSON.decode(@response.body)
+    assert !response.empty?
+    assert_equal response['config_template_id'], config_templates(:pxe_local_default).id
+  end
+
+  test 'should destroy os_default_template for os' do
+    assert_difference('OsDefaultTemplate.count', -1) do
+      delete :destroy, { :operatingsystem_id => operatingsystems(:redhat).to_param, :id => os_default_templates(:one) }
+    end
+    assert_response :success
+  end
+
+end
