-1

I have the following recipe which has a ruby block. Ideally it is supposed to run a curl command but i have modified it to run as a ruby block using the below URI

ruby_block 'Run Curl API' do
  block do
require 'net/http'
require 'uri'
require 'json'

uri = URI.parse("http://#{server}")
request = Net::HTTP::Post.new(uri)
request.content_type = "application/json"
request.body = JSON.dump({
  "customerId" => "#{customerId}",
  "cloudName" => "#{cloudName}",
  "vpcId" => "#{vpcId}",
})

req_options = {
  use_ssl: uri.scheme == "https",
}

response = Net::HTTP.start(uri.hostname, uri.port, req_options) do       |http|
  http.request(request)
end
# response.code
# response.body
  action :run
end

How would i induce a chef::log so, that if the attribute ""customerId" => "#{customerId}", is not defined or provided it should be shown as a critical error??

Thank you

anish anil
  • 2,299
  • 7
  • 21
  • 41
  • Could you please elaborate, I have the following added, request.body = JSON.dump({ "customerId" => "#{customerId}", Chef::Log.info("CustomerID not provided") – anish anil May 18 '18 at 03:11

1 Answers1

2

Inside your ruby_block you can use Chef::Application.fatal!('message') and put it in the necessary check for undefined customerId.

gsone
  • 1,188
  • 8
  • 25
  • Could you please elaborate, I have the following added, request.body = JSON.dump({ "customerId" => "#{customerId}", Chef::Log.info("CustomerID not provided") or should it now be Chef::Application.fatal!('CustomerID not provided') – anish anil May 18 '18 at 03:13
  • `unless customerId Chef::Application.fatal!('CustomerID not provided') end` – gsone May 18 '18 at 08:22
  • This is how i achieved it Chef::Application.fatal!("node['main']['dir'] must be set for this cookbook to run") if node['main']['dir'] == ''" – anish anil May 22 '18 at 08:11
  • hey glad that u do it. – gsone May 22 '18 at 08:47