summaryrefslogtreecommitdiff
path: root/chef-server-api/app/controllers/nodes.rb
blob: eb8eb3b0c77e7420dd7ca288d670c4d8ab9ff3d0 (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
#
# Author:: Adam Jacob (<adam@opscode.com>)
# Author:: Christopher Brown (<cb@opscode.com>)
# Copyright:: Copyright (c) 2008 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.
#

require 'chef' / 'node'

class ChefServerApi::Nodes < ChefServerApi::Application
  
  provides :json
  
  before :authenticate_every 
  before :fix_up_node_id
  
  def index
    @node_list = Chef::Node.list 
    display(@node_list.collect { |n| absolute_slice_url(:node, escape_node_id(n)) })
  end

  def show
    begin
      @node = Chef::Node.load(params[:id])
    rescue Net::HTTPServerException => e
      raise NotFound, "Cannot load node #{params[:id]}"
    end
    @node.couchdb_rev = nil
    display @node
  end

  def create
    @node = params["inflated_object"]
    exists = true 
    begin
      Chef::Node.load(@node.name)
    rescue Net::HTTPServerException
      exists = false
    end
    raise Forbidden, "Node already exists" if exists
    self.status = 201
    @node.save
    display({ :uri => absolute_slice_url(:node, escape_node_id(@node.name)) })
  end

  def update
    begin
      @node = Chef::Node.load(params[:id])
    rescue Net::HTTPServerException => e
      raise NotFound, "Cannot load node #{params[:id]}"
    end

    updated = params['inflated_object']
    @node.run_list.reset(updated.run_list)
    @node.attribute = updated.attribute
    @node.save
    @node.couchdb_rev = nil
    display(@node)
  end

  def destroy
    begin
      @node = Chef::Node.load(params[:id])
    rescue Net::HTTPServerException => e 
      raise NotFound, "Cannot load node #{params[:id]}"
    end
    @node.destroy
    @node.couchdb_rev = nil
    display @node
  end

  def cookbooks
    begin
      @node = Chef::Node.load(params[:id])
    rescue Net::HTTPServerException => e 
      raise NotFound, "Cannot load node #{params[:id]}"
    end
   
    display(load_all_files(params[:id]))
  end
  
end