Search code examples
chef-infrachef-recipecookbook

Error logging in RUBY block in chef


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


Solution

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