Skip to content

Commit 0e8f7bb

Browse files
committed
Adding an http_request resource, that lets you send arbitrary http requests as resources. A nice model for similar things - a gift to @ezmobius. Fixes CHEF-25
1 parent c126bc2 commit 0e8f7bb

7 files changed

Lines changed: 406 additions & 0 deletions

File tree

chef/Manifest.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ lib/chef/provider/remote_file.rb
7373
lib/chef/provider/script.rb
7474
lib/chef/provider/sysctl.rb
7575
lib/chef/provider/template.rb
76+
lib/chef/provider/http_request.rb
7677
lib/chef/queue.rb
7778
lib/chef/recipe.rb
7879
lib/chef/resource.rb
@@ -91,6 +92,7 @@ lib/chef/resource/ruby.rb
9192
lib/chef/resource/script.rb
9293
lib/chef/resource/sysctl.rb
9394
lib/chef/resource/template.rb
95+
lib/chef/resource/http_request.rb
9496
lib/chef/resource_collection.rb
9597
lib/chef/resource_definition.rb
9698
lib/chef/rest.rb

chef/lib/chef/platform.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class Platform
5858
:bash => Chef::Provider::Script,
5959
:csh => Chef::Provider::Script,
6060
:user => Chef::Provider::User::Useradd,
61+
:http_request => Chef::Provider::HttpRequest,
6162
}
6263
}
6364

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#
2+
# Author:: Adam Jacob (<adam@opscode.com>)
3+
# Copyright:: Copyright (c) 2008 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 'tempfile'
20+
21+
class Chef
22+
class Provider
23+
class HttpRequest < Chef::Provider
24+
25+
attr_accessor :rest
26+
27+
def load_current_resource
28+
@rest = Chef::REST.new(@new_resource.url)
29+
end
30+
31+
# Send a GET request to @new_resource.url, with ?message=@new_resource.message
32+
def action_get
33+
body = @rest.run_request(
34+
:GET,
35+
@rest.create_url("#{@new_resource.url}?message=#{@new_resource.message}"),
36+
false,
37+
10,
38+
false
39+
)
40+
@new_resource.updated = true
41+
Chef::Log.info("#{@new_resource} GET to #{@new_resource.url} successful")
42+
Chef::Log.debug("#{@new_resource} GET request response: #{body}")
43+
end
44+
45+
# Send a PUT request to @new_resource.url, with the message as the payload
46+
def action_put
47+
body = @rest.run_request(
48+
:PUT,
49+
@rest.create_url("#{@new_resource.url}"),
50+
@new_resource.message,
51+
10,
52+
false
53+
)
54+
@new_resource.updated = true
55+
Chef::Log.info("#{@new_resource} PUT to #{@new_resource.url} successful")
56+
Chef::Log.debug("#{@new_resource} PUT request response: #{body}")
57+
end
58+
59+
# Send a POST request to @new_resource.url, with the message as the payload
60+
def action_post
61+
body = @rest.run_request(
62+
:POST,
63+
@rest.create_url("#{@new_resource.url}"),
64+
@new_resource.message,
65+
10,
66+
false
67+
)
68+
@new_resource.updated = true
69+
Chef::Log.info("#{@new_resource} POST to #{@new_resource.url} successful")
70+
Chef::Log.debug("#{@new_resource} POST request response: #{body}")
71+
end
72+
73+
# Send a DELETE request to @new_resource.url
74+
def action_delete
75+
body = @rest.run_request(
76+
:DELETE,
77+
@rest.create_url("#{@new_resource.url}"),
78+
false,
79+
10,
80+
false
81+
)
82+
@new_resource.updated = true
83+
Chef::Log.info("#{@new_resource} DELETE to #{@new_resource.url} successful")
84+
Chef::Log.debug("#{@new_resource} DELETE request response: #{body}")
85+
end
86+
87+
end
88+
end
89+
end
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#
2+
# Author:: Adam Jacob (<adam@opscode.com>)
3+
# Copyright:: Copyright (c) 2008 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/resource'
20+
21+
class Chef
22+
class Resource
23+
class HttpRequest < Chef::Resource
24+
25+
def initialize(name, collection=nil, node=nil)
26+
super(name, collection, node)
27+
@resource_name = :http_request
28+
@message = name
29+
@url = nil
30+
@action = :create
31+
@allowed_actions.push(:get, :put, :post, :delete, :head, :options)
32+
end
33+
34+
def url(args=nil)
35+
set_or_return(
36+
:url,
37+
args,
38+
:kind_of => String
39+
)
40+
end
41+
42+
def message(args=nil)
43+
set_or_return(
44+
:message,
45+
args,
46+
:kind_of => String
47+
)
48+
end
49+
50+
end
51+
end
52+
end
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
#
2+
# Author:: Adam Jacob (<adam@opscode.com>)
3+
# Copyright:: Copyright (c) 2008 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 File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "spec_helper"))
20+
21+
describe Chef::Provider::HttpRequest, "initialize" do
22+
before(:each) do
23+
@node = mock("Chef::Node", :null_object => true)
24+
@new_resource = mock("Chef::Resource::HttpRequest",
25+
:null_object => true,
26+
:name => "adam",
27+
:url => "http://www.opscode.com",
28+
:message => "is cool"
29+
)
30+
@provider = Chef::Provider::HttpRequest.new(@node, @new_resource)
31+
end
32+
33+
it "should return a Chef::Provider::HttpRequest" do
34+
@provider.should be_a_kind_of(Chef::Provider::HttpRequest)
35+
end
36+
end
37+
38+
describe Chef::Provider::HttpRequest, "load_current_resource" do
39+
before(:each) do
40+
@node = mock("Chef::Node", :null_object => true)
41+
@new_resource = mock("Chef::Resource::HttpRequest",
42+
:null_object => true,
43+
:name => "adam",
44+
:url => "http://www.opscode.com",
45+
:message => "is cool"
46+
)
47+
@provider = Chef::Provider::HttpRequest.new(@node, @new_resource)
48+
end
49+
50+
it "should set up a Chef::REST client" do
51+
Chef::REST.should_receive(:new).with(@new_resource.url)
52+
@provider.load_current_resource
53+
end
54+
end
55+
56+
describe Chef::Provider::HttpRequest, "action_get" do
57+
before(:each) do
58+
@node = mock("Chef::Node", :null_object => true)
59+
@new_resource = mock("Chef::Resource::HttpRequest",
60+
:null_object => true,
61+
:name => "adam",
62+
:url => "http://www.opscode.com",
63+
:message => "is cool"
64+
)
65+
@rest = mock("Chef::REST",
66+
:null_object => true,
67+
:create_url => "http://www.opscode.com",
68+
:run_request => "you made it!"
69+
)
70+
@provider = Chef::Provider::HttpRequest.new(@node, @new_resource)
71+
@provider.rest = @rest
72+
end
73+
74+
it "should create the url with a message argument" do
75+
@rest.should_receive(:create_url).with("#{@new_resource.url}?message=#{@new_resource.message}")
76+
@provider.action_get
77+
end
78+
79+
it "should run a GET request" do
80+
@rest.should_receive(:run_request).with(:GET, @rest.create_url, false, 10, false)
81+
@provider.action_get
82+
end
83+
84+
it "should update the resource" do
85+
@new_resource.should_receive(:updated=).with(true)
86+
@provider.action_get
87+
end
88+
end
89+
90+
describe Chef::Provider::HttpRequest, "action_put" do
91+
before(:each) do
92+
@node = mock("Chef::Node", :null_object => true)
93+
@new_resource = mock("Chef::Resource::HttpRequest",
94+
:null_object => true,
95+
:name => "adam",
96+
:url => "http://www.opscode.com",
97+
:message => "is cool"
98+
)
99+
@rest = mock("Chef::REST",
100+
:null_object => true,
101+
:create_url => "http://www.opscode.com",
102+
:run_request => "you made it!"
103+
)
104+
@provider = Chef::Provider::HttpRequest.new(@node, @new_resource)
105+
@provider.rest = @rest
106+
end
107+
108+
it "should create the url" do
109+
@rest.should_receive(:create_url).with("#{@new_resource.url}")
110+
@provider.action_put
111+
end
112+
113+
it "should run a PUT request with the message as the payload" do
114+
@rest.should_receive(:run_request).with(:PUT, @rest.create_url, @new_resource.message, 10, false)
115+
@provider.action_put
116+
end
117+
118+
it "should update the resource" do
119+
@new_resource.should_receive(:updated=).with(true)
120+
@provider.action_put
121+
end
122+
end
123+
124+
describe Chef::Provider::HttpRequest, "action_post" do
125+
before(:each) do
126+
@node = mock("Chef::Node", :null_object => true)
127+
@new_resource = mock("Chef::Resource::HttpRequest",
128+
:null_object => true,
129+
:name => "adam",
130+
:url => "http://www.opscode.com",
131+
:message => "is cool"
132+
)
133+
@rest = mock("Chef::REST",
134+
:null_object => true,
135+
:create_url => "http://www.opscode.com",
136+
:run_request => "you made it!"
137+
)
138+
@provider = Chef::Provider::HttpRequest.new(@node, @new_resource)
139+
@provider.rest = @rest
140+
end
141+
142+
it "should create the url" do
143+
@rest.should_receive(:create_url).with("#{@new_resource.url}")
144+
@provider.action_post
145+
end
146+
147+
it "should run a PUT request with the message as the payload" do
148+
@rest.should_receive(:run_request).with(:POST, @rest.create_url, @new_resource.message, 10, false)
149+
@provider.action_post
150+
end
151+
152+
it "should update the resource" do
153+
@new_resource.should_receive(:updated=).with(true)
154+
@provider.action_post
155+
end
156+
end
157+
158+
describe Chef::Provider::HttpRequest, "action_delete" do
159+
before(:each) do
160+
@node = mock("Chef::Node", :null_object => true)
161+
@new_resource = mock("Chef::Resource::HttpRequest",
162+
:null_object => true,
163+
:name => "adam",
164+
:url => "http://www.opscode.com",
165+
:message => "is cool"
166+
)
167+
@rest = mock("Chef::REST",
168+
:null_object => true,
169+
:create_url => "http://www.opscode.com",
170+
:run_request => "you made it!"
171+
)
172+
@provider = Chef::Provider::HttpRequest.new(@node, @new_resource)
173+
@provider.rest = @rest
174+
end
175+
176+
it "should create the url" do
177+
@rest.should_receive(:create_url).with("#{@new_resource.url}")
178+
@provider.action_delete
179+
end
180+
181+
it "should run a DELETE request" do
182+
@rest.should_receive(:run_request).with(:DELETE, @rest.create_url, false, 10, false)
183+
@provider.action_delete
184+
end
185+
186+
it "should update the resource" do
187+
@new_resource.should_receive(:updated=).with(true)
188+
@provider.action_delete
189+
end
190+
end

0 commit comments

Comments
 (0)