Project

General

Profile

Revision d2e54db4

Added by David Davis over 7 years ago

Fixes #10029 - Docker registry API

View differences:

app/controllers/api/v2/registries_controller.rb
1
module Api
2
  module V2
3
    class RegistriesController < ::Api::V2::BaseController
4
      before_filter :find_resource, :except => %w(index create)
5

  
6
      resource_description do
7
        resource_id 'registries'
8
        api_version 'v2'
9
        api_base_url '/docker/api/v2'
10
      end
11

  
12
      def_param_group :registry do
13
        param :registry, Hash, :required => true, :action_aware => true do
14
          param :name, String, :required => true
15
          param :url, String, :required => true
16
          param :description, String
17
          param :username, String
18
          param :password, String
19
        end
20
      end
21

  
22
      api :GET, '/registries/', N_('List all docker registries')
23
      param_group :search_and_pagination, ::Api::V2::BaseController
24
      def index
25
        @registries = DockerRegistry.search_for(params[:search], :order => params[:order])
26
                      .paginate(:page => params[:page])
27
      end
28

  
29
      api :GET, '/registries/:id', N_("Show a docker registry")
30
      param :id, :identifier, :required => true
31
      def show
32
      end
33

  
34
      api :POST, '/registries/', N_('Create a docker registry')
35
      param_group :registry, :as => :create
36
      def create
37
        @registry = DockerRegistry.new(params[:registry])
38
        process_response @registry.save
39
      end
40

  
41
      api :PUT, '/registries/:id', N_('Update a docker registry')
42
      param :id, :identifier, :required => true
43
      param_group :registry, :as => :update
44
      def update
45
        process_response @registry.update_attributes(params[:registry])
46
      end
47

  
48
      api :DELETE, '/registries/:id/', N_('Delete a docker registry')
49
      param :id, :identifier, :required => true
50
      def destroy
51
        process_response @registry.destroy
52
      end
53

  
54
      private
55

  
56
      def resource_class
57
        DockerRegistry
58
      end
59

  
60
      def docker_registry_url(registry)
61
        registry_url(registry)
62
      end
63
    end
64
  end
65
end
app/views/api/v2/registries/base.json.rabl
1
object @registry
2

  
3
attributes :id, :name
app/views/api/v2/registries/index.json.rabl
1
collection @registries
2

  
3
extends 'api/v2/registries/main'
app/views/api/v2/registries/main.json.rabl
1
object @registry
2

  
3
extends 'api/v2/registries/base'
4

  
5
attributes :url, :description, :created_at, :updated_at
6

  
app/views/api/v2/registries/show.json.rabl
1
object @registry
2

  
3
extends "api/v2/registries/main"
4

  
5
node do |container|
6
  partial("api/v2/taxonomies/children_nodes", :object => container)
7
end
config/routes.rb
38 38
            end
39 39
          end
40 40
        end
41
        resources :registries, :except => [:new, :edit]
41 42
      end
42 43
    end
43 44
  end
lib/foreman_docker/engine.rb
79 79
        end
80 80

  
81 81
        security_block :registries do
82
          permission :view_registries, { :registries => [:index, :show] },
82
          permission :view_registries,
83
                     { :registries => [:index, :show],
84
                       :'api/v2/registries' => [:index, :show] },
83 85
                     :resource_type => 'DockerRegistry'
84
          permission :create_registries, { :registries  => [:new, :create, :update, :edit] },
86
          permission :create_registries,
87
                     { :registries  => [:new, :create, :update, :edit],
88
                       :'api/v2/registries' => [:create, :update] },
85 89
                     :resource_type => 'DockerRegistry'
86
          permission :destroy_registries, { :registries => [:destroy] },
90
          permission :destroy_registries,
91
                     { :registries => [:destroy],
92
                       :'api/v2/registries' => [:destroy] },
87 93
                     :resource_type => 'DockerRegistry'
88 94
        end
89 95

  
test/functionals/api/v2/registries_controller_test.rb
1
require 'test_plugin_helper'
2

  
3
module Api
4
  module V2
5
    class RegistriesControllerTest < ActionController::TestCase
6
      setup do
7
        @registry = FactoryGirl.create(:docker_registry)
8
      end
9

  
10
      test 'index returns a list of all containers' do
11
        get :index, {}, set_session_user
12
        assert_response :success
13
        assert_template 'index'
14
      end
15

  
16
      test 'index can be filtered by name' do
17
        %w(thomas clayton wolfe).each do |name|
18
          FactoryGirl.create(:docker_registry, :name => name)
19
        end
20
        get :index, { :search => 'name = thomas' }, set_session_user
21
        assert_response :success
22
        assert_equal 1, assigns(:registries).length
23
      end
24

  
25
      test 'creates a new registry with valid params' do
26
        docker_attrs = FactoryGirl.attributes_for(:docker_registry)
27
        post :create, :registry => docker_attrs
28
        assert_response :success
29
      end
30

  
31
      test 'does not create a new registry with invalid params' do
32
        docker_attrs = FactoryGirl.attributes_for(:docker_registry)
33
        docker_attrs.delete(:name)
34
        post :create, :registry => docker_attrs
35
        assert_response 422
36
      end
37

  
38
      test 'shows a docker registry' do
39
        get :show, :id => @registry.id
40
        assert_response :success
41
      end
42

  
43
      test 'update a docker registry' do
44
        put :update, :id => @registry.id, :registry => { :name => 'hello_world' }
45
        assert_response :success
46
        assert DockerRegistry.exists?(:name => 'hello_world')
47
      end
48

  
49
      test 'deletes a docker registry' do
50
        delete :destroy, :id => @registry.id
51
        assert_response :success
52
        refute DockerRegistry.exists?(@registry.id)
53
      end
54
    end
55
  end
56
end

Also available in: Unified diff