1

I need to include res-client into my module in order to use RestClient::Resource.

I use the method in the module in a Chef recipe (ruby_block resource).

When my ruby_block resource tries to run the method in the module it output this error:

ERROR: cannot load such file -- rest-client

The following is my module residing in libraries folder:

module SecretServer
  def fetch_token
    payload = { :username => "***", :password => "***", :grant_type => "****"}
uri = ***
    request = RestClient::Resource.new(uri, :verify_ssl => false)
    post_request = request.post payload, :content_type => 'application/x-www-form-urlencoded'
    token = JSON.parse(post_request)["access_token"]
    return token
  end
end

require 'rest-client'
Chef::Recipe.include(SecretServer)
Chef::Resource.include(SecretServer)

The following is the resource that calls the function in module:

ruby_block 'parse data' do
  block do
   res = fetch_token
   puts res
   end 
end

This is just one of the several recipes in my cookbook. This recipe runs after the target node is almost ready and 'bundle install' has been run on the target node.

I also install rest-client in the target node. I tried each of following resource before my ruby_block resource:

chef_gem 'rest-client' do
  action :install
end

gem_package 'rest-client' do
  action :install
end

My question is how to include 'rest-client' and utilize it in Chef recipes?

1 Answers1

0

Long ago, the HTTP clients lived together in harmony. Then everything changed when the JSON gem attacked. Only the Chef::HTTP was still to be included with Chef as all the other clients were under too much flux to include.

We don't include that gem anymore, so to use it, you would have to install it yourself either via a cookbook gem dependency or a chef_gem resource. But for simple stuff you can just use our Chef::HTTP::SimpleJSON client instead:

Chef::HTTP::SimpleJson.new(uri).post("", args)["access_token"]

Or something like that (specifics depend on if you must use form encoding or if the server also speaks JSON).

coderanger
  • 52,400
  • 4
  • 52
  • 75
  • I tried Chef::HTTP (HTTPRequest, BasicClient). The issue is I have to disable ssl verification but I was not able to disable it with Chef::HTTP. I also need to add that I install rest-client in the target node before I call my module function (fetch_token) however i still get the error when I try to include rest-client in my module – Hosein Zarifi Jun 24 '18 at 02:59
  • We do not offer that option, nor should you ever use. Disabling TLS is unsafe. – coderanger Jun 24 '18 at 03:03
  • And if you are installing the gem, please update your question to show how exactly. – coderanger Jun 24 '18 at 03:04
  • I need to disable ssl verification because of the infrastructure that I am working on. I also added the part that i install rest-client – Hosein Zarifi Jun 25 '18 at 04:04
  • So if you're installing via a `chef_gem` resource, you need to delay the `require` until after the gem is installed. The easy way to do that is just move it to inside your helper method. – coderanger Jun 26 '18 at 03:44