Skip to content

Commit

Permalink
DRY RestClient
Browse files Browse the repository at this point in the history
  • Loading branch information
joekiller committed Dec 9, 2014
1 parent e945e3f commit b09536d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
16 changes: 10 additions & 6 deletions libraries/s3_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,14 @@ def self.get_md5_from_s3(bucket,url,path,aws_access_key_id,aws_secret_access_key
end

def self.get_digests_from_s3(bucket,url,path,aws_access_key_id,aws_secret_access_key,token)
require 'rest-client'
RestClient.proxy = ENV['http_proxy']
client = self.client
now, auth_string = get_s3_auth("HEAD", bucket,path,aws_access_key_id,aws_secret_access_key, token)

headers = build_headers(now, auth_string, token)

url = "https://#{bucket}.s3.amazonaws.com" if url.nil?

response = RestClient.head("#{url}#{path}", headers)
response = client.head("#{url}#{path}", headers)

etag = response.headers[:etag].gsub('"','')
digest = response.headers[:x_amz_meta_digest]
Expand All @@ -41,14 +40,13 @@ def self.get_digests_from_s3(bucket,url,path,aws_access_key_id,aws_secret_access
end

def self.get_from_s3(bucket,url,path,aws_access_key_id,aws_secret_access_key,token)
require 'rest-client'
RestClient.proxy = ENV['http_proxy']
client = self.client
now, auth_string = get_s3_auth("GET", bucket,path,aws_access_key_id,aws_secret_access_key, token)

url = "https://#{bucket}.s3.amazonaws.com" if url.nil?

headers = build_headers(now, auth_string, token)
response = RestClient::Request.execute(:method => :get, :url => "#{url}#{path}", :raw_response => true, :headers => headers)
response = client::Request.execute(:method => :get, :url => "#{url}#{path}", :raw_response => true, :headers => headers)

return response
end
Expand Down Expand Up @@ -124,4 +122,10 @@ def self.verify_md5_checksum(checksum, file)

local_md5.hexdigest == s3_md5
end

def self.client
require 'rest-client'
RestClient.proxy = ENV['http_proxy']
RestClient
end
end
9 changes: 4 additions & 5 deletions providers/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
action :create do
rest_client_gem = Chef::Resource::ChefGem.new('rest-client', @run_context)
rest_client_gem.run_action :install
require 'rest-client'
RestClient.proxy = ENV['http_proxy']
client = S3FileLib::client
download = true

# handle key specified without leading slash
Expand All @@ -23,12 +22,12 @@
if aws_access_key_id.nil? && aws_secret_access_key.nil? && token.nil?
instance_profile_base_url = 'http://169.254.169.254/latest/meta-data/iam/security-credentials/'
begin
instance_profiles = RestClient.get(instance_profile_base_url)
rescue RestClient::ResourceNotFound, Errno::ETIMEDOUT # we can either 404 on an EC2 instance, or timeout on non-EC2
instance_profiles = client.get(instance_profile_base_url)
rescue client::ResourceNotFound, Errno::ETIMEDOUT # we can either 404 on an EC2 instance, or timeout on non-EC2
raise ArgumentError.new 'No credentials provided and no instance profile on this machine.'
end
instance_profile_name = instance_profiles.split.first
instance_profile = JSON.load(RestClient.get(instance_profile_base_url + instance_profile_name))
instance_profile = JSON.load(client.get(instance_profile_base_url + instance_profile_name))

aws_access_key_id = instance_profile['AccessKeyId']
aws_secret_access_key = instance_profile['SecretAccessKey']
Expand Down

0 comments on commit b09536d

Please sign in to comment.