Class: Chef::Knife::Raw
Constant Summary
collapse
- ACCEPT_ENCODING =
"Accept-Encoding".freeze
- ENCODING_GZIP_DEFLATE =
"gzip;q=1.0,deflate;q=0.6,identity;q=0.3".freeze
Instance Attribute Summary
Attributes inherited from Chef::Knife
#name_args, #ui
Instance Method Summary
collapse
Methods inherited from Chef::Knife
#api_key, #apply_computed_config, category, common_name, #config_file_settings, #configure_chef, #create_object, #delete_object, deps, #format_rest_error, guess_category, #highlight_config_error, #humanize_exception, #humanize_http_exception, inherited, #initialize, list_commands, load_commands, load_deps, #locate_config_file, #merge_configs, msg, #noauth_rest, #parse_options, #read_config_file, reset_subcommands!, #rest, run, #run_with_pretty_exceptions, #server_url, #show_usage, snake_case_name, subcommand_category, subcommand_class_from, subcommand_loader, subcommands, subcommands_by_category, ui, unnamed?, use_separate_defaults?, #username
#convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #snake_case_basename
#enforce_path_sanity
Constructor Details
This class inherits a constructor from Chef::Knife
Instance Method Details
#api_request(chef_rest, method, url, headers = {}, data = false) ⇒ Object
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
|
# File 'lib/chef/knife/raw.rb', line 55
def api_request(chef_rest, method, url, ={}, data=false)
json_body = data
= (chef_rest, method, url, , json_body)
chef_rest.retriable_rest_request(method, url, json_body, ) do |rest_request|
response = rest_request.call {|r| r.read_body}
response_body = chef_rest.decompress_body(response)
if response.kind_of?(Net::HTTPSuccess)
if config[:pretty] && response['content-type'] =~ /json/
JSON.pretty_generate(JSON.parse(response_body, :create_additions => false))
else
response_body
end
elsif redirect_location = redirected_to(response)
raise "Redirected to #{create_url(redirect_location)}"
follow_redirect {api_request(:GET, create_url(redirect_location))}
else
response.body.replace(chef_rest.decompress_body(response)) if response.body.respond_to?(:replace)
if response['content-type'] =~ /json/
exception = response_body
msg = "HTTP Request Returned #{response.code} #{response.message}: "
msg << (exception["error"].respond_to?(:join) ? exception["error"].join(", ") : exception["error"].to_s)
Chef::Log.info(msg)
end
puts response.body
response.error!
end
end
end
|
94
95
96
97
98
99
100
101
102
103
104
|
# File 'lib/chef/knife/raw.rb', line 94
def (chef_rest, method, url, ={}, json_body=false, raw=false)
['Accept'] = "application/json" unless raw
["Content-Type"] = 'application/json' if json_body
['Content-Length'] = json_body.bytesize.to_s if json_body
[Chef::REST::RESTRequest::ACCEPT_ENCODING] = Chef::REST::RESTRequest::ENCODING_GZIP_DEFLATE
.merge!(chef_rest.(method, url, json_body)) if chef_rest.sign_requests?
.merge!(Chef::Config[:custom_http_headers]) if Chef::Config[:custom_http_headers]
end
|
#redirected_to(response) ⇒ Object
48
49
50
51
52
53
|
# File 'lib/chef/knife/raw.rb', line 48
def redirected_to(response)
return nil unless response.kind_of?(Net::HTTPRedirection)
return nil if response.kind_of?(Net::HTTPNotModified)
response['location']
end
|
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/chef/knife/raw.rb', line 25
def run
if name_args.length == 0
show_usage
ui.fatal("You must provide the path you want to hit on the server")
exit(1)
elsif name_args.length > 1
show_usage
ui.fatal("Only one path accepted for knife raw")
exit(1)
end
path = name_args[0]
data = false
if config[:input]
data = IO.read(config[:input])
end
chef_rest = Chef::REST.new(Chef::Config[:chef_server_url])
puts api_request(chef_rest, config[:method].to_sym, chef_rest.create_url(name_args[0]), {}, data)
end
|