Module: ChefPlugin::ChefResourceApi

Included in:
ChefApi
Defined in:
lib/smart_proxy_chef_plugin/chef_resource_api.rb

Instance Method Summary collapse

Instance Method Details

#create_action(name, plural) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/smart_proxy_chef_plugin/chef_resource_api.rb', line 53

def create_action(name, plural)
  post "/#{plural}" do
    logger.debug "Creating #{name} with parameters: " + params.inspect

    object = get_connection.send(plural).new(params[name])
    if object.save
      logger.debug "#{name.capitalize} #{params[:id]} created"
      object.to_json
    else
      log_halt 400, {:errors => object.errors}.to_json
    end
  end
end

#delete_action(name, plural) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/smart_proxy_chef_plugin/chef_resource_api.rb', line 26

def delete_action(name, plural)
  delete "/#{plural}/:id" do
    logger.debug "Starting deletion of #{name} #{params[:id]}"

    if (result = get_connection.send(plural).delete(params[:id]))
      logger.debug "#{name.capitalize} #{params[:id]} deleted"
      { :result => result }.to_json
    else
      log_halt 400, "#{name.capitalize} #{params[:id]} could not be deleted" unless result
    end
  end
end

#list_action(plural) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/smart_proxy_chef_plugin/chef_resource_api.rb', line 16

def list_action(plural)
  get "/#{plural}" do
    logger.debug "Listing #{plural}"

    # to workaround chef-api issue, see https://github.com/sethvargo/chef-api/pull/34 for more details
    resources = get_connection.send(plural).all
    resources.map(&:to_hash).to_json
  end
end

#resource(name, options = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/smart_proxy_chef_plugin/chef_resource_api.rb', line 4

def resource(name, options = {})
  @name = name
  @options = options
  plural = get_plural_name

  show_action(name, plural) if actions.include?(:show)
  create_action(name, plural) if actions.include?(:create)
  update_action(name, plural) if actions.include?(:update)
  delete_action(name, plural) if actions.include?(:delete)
  list_action(plural) if actions.include?(:list)
end

#show_action(name, plural) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/smart_proxy_chef_plugin/chef_resource_api.rb', line 67

def show_action(name, plural)
  get "/#{plural}/:id" do
    logger.debug "Showing #{name} #{params[:id]}"

    if (object = get_connection.send(plural).fetch(params[:id]))
      object.to_json
    else
      log_halt 404, "#{name.capitalize} #{params[:id]} not found"
    end
  end
end

#update_action(name, plural) ⇒ Object

currently broken at least for clients - see github.com/sethvargo/chef-api/issues/33



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/smart_proxy_chef_plugin/chef_resource_api.rb', line 40

def update_action(name, plural)
  put "/#{plural}/:id" do
    logger.debug "Updating #{name} with parameters: " + params.inspect

    if (object = get_connection.send(plural).update(params[:id], params[name]))
      logger.debug "#{name.capitalize} #{params[:id]} updated"
      object.to_json
    else
      log_halt 400, {:errors => object.errors}.to_json
    end
  end
end