summaryrefslogtreecommitdiff
path: root/chef-server-api/config/router.rb
blob: 5883534c62bbc3da139dc07d02fe92dcb59de5f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#
# Author:: Adam Jacob (<adam@opscode.com>)
# Author:: Daniel DeLeo (<dan@opscode.com>)
# Copyright:: Copyright (c) 2008-2010 Opscode, Inc.
# License:: Apache License, Version 2.0
#
# 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.
#

Merb::Router.prepare do
  resources :users

  # Nodes
  resources :nodes, :id => /[^\/]+/
  match('/nodes/:id/cookbooks',
        :id => /[^\/]+/,
        :method => 'get').
    to(:controller => "nodes", :action => "cookbooks")
  # Roles
  resources :roles

  # Status
  match("/status").to(:controller => "status", :action => "index").name(:status)

  # Clients
  match("/clients", :method=>"post").to(:controller=>'clients', :action=>'create')
  match("/clients", :method=>"get").to(:controller=>'clients', :action=>'index').name(:clients)
  match("/clients/:id", :id => /[\w\.-]+/, :method=>"get").to(:controller=>'clients', :action=>'show').name(:client)
  match("/clients/:id", :id => /[\w\.-]+/, :method=>"put").to(:controller=>'clients', :action=>'update')
  match("/clients/:id", :id => /[\w\.-]+/, :method=>"delete").to(:controller=>'clients', :action=>'destroy')

  # Search
  resources :search
  match('/search/reindex', :method => 'post').to(:controller => "search", :action => "reindex")

  # Cookbooks
  match('/nodes/:id/cookbooks', :method => 'get').to(:controller => "nodes", :action => "cookbooks")

  match("/cookbooks",
        :method => 'get'
        ).to(:controller => "cookbooks", :action => "index")

  match("/cookbooks/_latest", :method=>'get').to(:controller=>'cookbooks',:action=>'index_latest')

  match("/cookbooks/_recipes", :method=>'get').to(:controller=>'cookbooks',:action=>'index_recipes')

  match("/cookbooks/:cookbook_name/:cookbook_version",
        :method => 'put',
        :cookbook_name => /[\w\.]+/,
        :cookbook_version => /\d+\.\d+\.\d+/
        ).to(:controller => "cookbooks", :action => "update")

  match("/cookbooks/:cookbook_name/:cookbook_version",
        :method => 'get',
        :cookbook_name => /[\w\.]+/,
        :cookbook_version => /\d+\.\d+\.\d+|_latest/
        ).to(:controller => "cookbooks", :action => "show").name(:cookbook_version)

  match("/cookbooks/:cookbook_name/:cookbook_version",
        :method => 'delete',
        :cookbook_name => /[\w\.]+/,
        :cookbook_version => /\d+\.\d+\.\d+|_latest/
        ).to(:controller => "cookbooks", :action => "destroy")

  match("/cookbooks/:cookbook_name",
        :method => 'get',
        :cookbook_name => /[\w\.]+/
        ).to(:controller => "cookbooks", :action => "show_versions").name(:cookbook)

  match("/cookbooks/:cookbook_name/:cookbook_version/files/:checksum",
        :cookbook_name => /[\w\.]+/,
        :cookbook_version => /(\d+\.\d+\.\d+|_latest)/
        ).to(
             :controller => "cookbooks",
             :action => "show_file"
             ).name(:cookbook_file)

  # Sandbox
  match('/sandboxes', :method => 'get').to(:controller => "sandboxes", :action => "index").name(:sandboxes)
  match('/sandboxes', :method => 'post').to(:controller => "sandboxes", :action => "create")
  match('/sandboxes/:sandbox_id', :method => 'get', :sandbox_id => /[\w\.]+/).to(:controller => "sandboxes", :action => "show").name(:sandbox)
  match('/sandboxes/:sandbox_id', :method => 'put', :sandbox_id => /[\w\.]+/).to(:controller => "sandboxes", :action => "update")
  match('/sandboxes/:sandbox_id/:checksum', :method => 'put', :sandbox_id => /[\w\.]+/, :checksum => /[\w\.]+/).to(:controller => "sandboxes", :action => "upload_checksum").name(:sandbox_checksum)
  match('/sandboxes/:sandbox_id/:checksum', :method => 'get', :sandbox_id => /[\w\.]+/, :checksum => /[\w\.]+/).to(:controller => "sandboxes", :action => "download_checksum")

  # Data
  match("/data/:data_bag_id/:id", :method => 'get').to(:controller => "data_item", :action => "show").name("data_bag_item")
  match("/data/:data_bag_id", :method => 'post').to(:controller => "data_item", :action => "create").name("create_data_bag_item")
  match("/data/:data_bag_id/:id", :method => 'put').to(:controller => "data_item", :action => "update").name("update_data_bag_item")
  match("/data/:data_bag_id/:id", :method => 'delete').to(:controller => "data_item", :action => "destroy").name("destroy_data_bag_item")
  resources :data, :controller => "data_bags"

  match('/').to(:controller => 'main', :action =>'index').name(:top)

  # Need to monkey patch Merb so that it inflates JSON input with a higher
  # recursion depth allowed (the default is 19). See CHEF-1292/PL-538.
  module Merb
    class Request
      # ==== Returns
      # Hash:: Parameters from body if this is a JSON request.
      #
      # ==== Notes
      # If the JSON object parses as a Hash, it will be merged with the
      # parameters hash.  If it parses to anything else (such as an Array, or
      # if it inflates to an Object) it will be accessible via the inflated_object
      # parameter.
      #
      # :api: private
      def json_params
        @json_params ||= begin
          if Merb::Const::JSON_MIME_TYPE_REGEXP.match(content_type)
            begin
              # Call Chef's JSON utility instead of the default in Merb, 
              # JSON.parse.
              jobj = Chef::JSON.from_json(raw_post)
              jobj = jobj.to_mash if jobj.is_a?(Hash)
            rescue JSON::ParserError
              jobj = Mash.new
            end
            jobj.is_a?(Hash) ? jobj : { :inflated_object => jobj }
          end
        end
      end

    end
  end

end