Skip to content

Commit 1ea9486

Browse files
committed
Adding cookbook listing and showing
1 parent 00aef66 commit 1ea9486

4 files changed

Lines changed: 83 additions & 4 deletions

File tree

chef/lib/chef/api_client.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,11 +232,11 @@ def save(new_key=false)
232232
r = Chef::REST.new(Chef::Config[:chef_server_url])
233233
# First, try and create a new registration
234234
begin
235-
r.post_rest("clients", {:name => self.name})
235+
r.post_rest("clients", {:name => self.name, :admin => self.admin })
236236
rescue Net::HTTPServerException => e
237237
# If that fails, go ahead and try and update it
238238
if e.response.code == "403"
239-
r.put_rest("clients/#{name}", { :name => self.name, :private_key => new_key })
239+
r.put_rest("clients/#{name}", { :name => self.name, :admin => self.admin, :private_key => new_key })
240240
else
241241
raise e
242242
end

chef/lib/chef/knife.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,11 @@ def bulk_delete(klass, fancy_name)
254254
Chef::Log.warn("Deleted #{fancy_name} #{name}")
255255
end
256256
end
257+
258+
def rest
259+
@rest ||= Chef::REST.new(Chef::Config[:chef_server_url])
260+
end
261+
257262
end
258263
end
259264

chef/lib/chef/knife/cookbook_list.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#
1818

1919
require 'chef/knife'
20-
require 'chef/node'
2120
require 'json'
2221

2322
class Chef
@@ -32,7 +31,7 @@ class CookbookList < Knife
3231
:description => "Show corresponding URIs"
3332

3433
def run
35-
json_pretty_print(format_list_for_display(Chef::Node.list))
34+
json_pretty_print(format_list_for_display(rest.get_rest('cookbooks')))
3635
end
3736
end
3837
end
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#
2+
# Author:: Adam Jacob (<adam@opscode.com>)
3+
# Copyright:: Copyright (c) 2009 Opscode, Inc.
4+
# License:: Apache License, Version 2.0
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
19+
require 'chef/knife'
20+
require 'json'
21+
require 'uri'
22+
23+
class Chef
24+
class Knife
25+
class CookbookShow < Knife
26+
27+
banner "Sub-Command: cookbook show COOKBOOK [PART] [FILENAME] (options)"
28+
29+
option :fqdn,
30+
:short => "-f FQDN",
31+
:long => "--fqdn FQDN",
32+
:description => "The FQDN of the host to see the file for"
33+
34+
option :platform,
35+
:short => "-p PLATFORM",
36+
:long => "--platform PLATFORM",
37+
:description => "The platform to see the file for"
38+
39+
option :platform_version,
40+
:short => "-V VERSION",
41+
:long => "--platform-version VERSION",
42+
:description => "The platform version to see the file for"
43+
44+
def run
45+
case @name_args.length
46+
when 3 # We are showing a specific file
47+
arguments = { :id => @name_args[2] }
48+
arguments[:fqdn] = config[:fqdn] if config.has_key?(:fqdn)
49+
arguments[:platform] = config[:platform] if config.has_key?(:platform)
50+
arguments[:version] = config[:platform_version] if config.has_key?(:platform_version)
51+
result = rest.get_rest("cookbooks/#{@name_args[0]}/#{@name_args[1]}?#{make_query_params(arguments)}")
52+
puts result
53+
when 2 # We are showing a specific part of the cookbook
54+
result = rest.get_rest("cookbooks/#{@name_args[0]}")
55+
json_pretty_print(result[@name_args[1]])
56+
when 1 # We are showing the whole cookbook data
57+
json_pretty_print(rest.get_rest("cookbooks/#{@name_args[0]}"))
58+
end
59+
end
60+
61+
def make_query_params(req_opts)
62+
query_part = ""
63+
req_opts.each do |key, value|
64+
query_part << "#{key}=#{URI.escape(value)}"
65+
end
66+
query_part
67+
end
68+
69+
end
70+
end
71+
end
72+
73+
74+
75+

0 commit comments

Comments
 (0)