Skip to content

Commit

Permalink
Merge pull request #2 from hw-cookbooks/cookstyle_bot/cookstyle_6_17_5
Browse files Browse the repository at this point in the history
Signed-off-by: Tim Smith <[email protected]>
  • Loading branch information
tas50 authored Sep 12, 2020
2 parents 7eb5394 + 25c0692 commit 03f51c3
Show file tree
Hide file tree
Showing 13 changed files with 210 additions and 239 deletions.
2 changes: 1 addition & 1 deletion Berksfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
site :opscode
source 'https://supermarket.chef.io'

metadata
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
source "https://rubygems.org"
source 'https://rubygems.org'

gem 'test-kitchen', git: 'git://github.com/opscode/test-kitchen.git', branch: '1.0'
gem 'kitchen-lxc'
Expand Down
10 changes: 5 additions & 5 deletions attributes/api.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#
# Author:: Joe Williams (<[email protected]>)
# Cookbook Name:: bprobe
# Cookbook:: bprobe
# Attributes:: api
#
# Copyright 2011, Boundary
# Copyright:: 2011, Boundary
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -18,10 +18,10 @@
# limitations under the License.
#

default[:boundary][:api][:hostname] = "api.boundary.com"
default[:boundary][:api][:hostname] = 'api.boundary.com'

# this is your organization id
default[:boundary][:api][:org_id] = "yourid"
default[:boundary][:api][:org_id] = 'yourid'

# this is your api key (not the install token)
default[:boundary][:api][:key] = "yourkey"
default[:boundary][:api][:key] = 'yourkey'
12 changes: 6 additions & 6 deletions attributes/bprobe.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#
# Author:: Joe Williams (<[email protected]>)
# Cookbook Name:: bprobe
# Cookbook:: bprobe
# Attributes:: bprobe
#
# Copyright 2011, Boundary
# Copyright:: 2011, Boundary
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -18,10 +18,10 @@
# limitations under the License.
#

default[:boundary][:bprobe][:bin][:path] = "/usr/local"
default[:boundary][:bprobe][:etc][:path] = "/etc/bprobe"
default[:boundary][:bprobe][:bin][:path] = '/usr/local'
default[:boundary][:bprobe][:etc][:path] = '/etc/bprobe'

default[:boundary][:bprobe][:collector][:hostname] = "collector.boundary.com"
default[:boundary][:bprobe][:collector][:port] = "4740"
default[:boundary][:bprobe][:collector][:hostname] = 'collector.boundary.com'
default[:boundary][:bprobe][:collector][:port] = '4740'

default[:boundary][:bprobe][:tags] = []
194 changes: 87 additions & 107 deletions libraries/boundary_api.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
##
# Author:: Joe Williams (<[email protected]>)
# Cookbook Name:: bprobe
# Cookbook:: bprobe
# Library:: boundary_api
#
# Copyright 2011, Boundary
# Copyright:: 2011, Boundary
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -25,95 +25,85 @@

module Boundary
module API

def create_meter_request(new_resource)
begin
url = build_url(new_resource, :create)
headers = generate_headers()
body = {:name => new_resource.name}.to_json

Chef::Log.info("Creating meter [#{new_resource.name}]")
response = http_request(:post, url, headers, body)

rescue Exception => e
Chef::Log.error("Could not create meter [#{new_resource.name}], failed with #{e}")
end
url = build_url(new_resource, :create)
headers = generate_headers()
body = { name: new_resource.name }.to_json

Chef::Log.info("Creating meter [#{new_resource.name}]")
response = http_request(:post, url, headers, body)
rescue Exception => e
Chef::Log.error("Could not create meter [#{new_resource.name}], failed with #{e}")
end

def apply_cloud_tags(new_resource)
if node[:ec2]
Chef::Log.debug("This meter seems to be on EC2, applying ec2 based tags")
if node['ec2']
Chef::Log.debug('This meter seems to be on EC2, applying ec2 based tags')

if node[:ec2][:security_groups].length > 0
node[:ec2][:security_groups].each do |group|
if node['ec2']['security_groups'].length > 0
node['ec2']['security_groups'].each do |group|
apply_an_tag(new_resource, group)
end
end

if node[:ec2][:placement_availability_zone]
apply_an_tag(new_resource, node[:ec2][:placement_availability_zone])
if node['ec2']['placement_availability_zone']
apply_an_tag(new_resource, node['ec2']['placement_availability_zone'])
end

if node[:ec2][:instance_type]
apply_an_tag(new_resource, node[:ec2][:instance_type])
if node['ec2']['instance_type']
apply_an_tag(new_resource, node['ec2']['instance_type'])
end
end
end

def apply_meter_tags(new_resource)
Chef::Log.debug("This meter currently has these attribute based tags [#{node[:boundary][:bprobe][:tags]}]")
Chef::Log.debug("This meter currently has these attribute based tags [#{node['boundary']['bprobe']['tags']}]")

tags = node[:boundary][:bprobe][:tags]
tags = node['boundary']['bprobe']['tags']

if tags.length > 0
tags.each do |tag|
apply_an_tag(new_resource, tag)
end
else
Chef::Log.debug("No meter tags to apply.")
Chef::Log.debug('No meter tags to apply.')
end
end

def apply_an_tag(new_resource, tag)
begin
url = build_url(new_resource, :tags)
headers = generate_headers()
url = build_url(new_resource, :tags)
headers = generate_headers()

Chef::Log.info("Applying meter tag [#{tag}]")
Chef::Log.info("Applying meter tag [#{tag}]")

http_request(:put, "#{url}/#{tag}", headers, "")
rescue Exception => e
Chef::Log.error("Could not apply meter tag, failed with #{e}")
end
http_request(:put, "#{url}/#{tag}", headers, '')
rescue Exception => e
Chef::Log.error("Could not apply meter tag, failed with #{e}")
end

def delete_meter_request(new_resource)
begin
url = build_url(new_resource, :delete)
headers = generate_headers()

Chef::Log.info("Deleting meter [#{new_resource.name}]")
response = http_request(:delete, url, headers)
url = build_url(new_resource, :delete)
headers = generate_headers()

rescue Exception => e
Chef::Log.error("Could not delete meter [#{new_resource.name}], failed with #{e}")
end
Chef::Log.info("Deleting meter [#{new_resource.name}]")
response = http_request(:delete, url, headers)
rescue Exception => e
Chef::Log.error("Could not delete meter [#{new_resource.name}], failed with #{e}")
end

def save_meter_id_attribute(new_resource)
if Chef::Config[:solo]
Chef::Log.debug("chef-solo run, not attempting to save id attribute.")
Chef::Log.debug('chef-solo run, not attempting to save id attribute.')
else
begin
meter_id = get_meter_id(new_resource)

if meter_id
node.set[:boundary][:bprobe][:id] = meter_id
node.normal['boundary']['bprobe'][:id] = meter_id
node.save
else
Chef::Log.error("Could not save meter id as node attribute (nil response)!")
Chef::Log.error('Could not save meter id as node attribute (nil response)!')
end

rescue Exception => e
Chef::Log.error("Could not save meter id as node attribute, failed with #{e}")
end
Expand All @@ -122,11 +112,11 @@ def save_meter_id_attribute(new_resource)

def delete_meter_id_attribute
if Chef::Config[:solo]
Chef::Log.debug("chef-solo run, not attempting to delete attribute.")
Chef::Log.debug('chef-solo run, not attempting to delete attribute.')
else
begin
if node[:boundary][:bprobe]
node[:boundary].delete(:bprobe)
if node['boundary']['bprobe']
node['boundary'].delete(:bprobe)
node.save
end

Expand All @@ -140,86 +130,77 @@ def delete_meter_id_attribute
end
end

def generate_headers()
def generate_headers
auth = auth_encode()
{"Authorization" => "Basic #{auth}", "Content-Type" => "application/json"}
{ 'Authorization' => "Basic #{auth}", 'Content-Type' => 'application/json' }
end

def auth_encode()
auth = Base64.encode64("#{node[:boundary][:api][:key]}:").strip
auth.gsub("\n","")
def auth_encode
auth = Base64.encode64("#{node['boundary']['api']['key']}:").strip
auth.gsub("\n", '')
end

def build_url(new_resource, action)
case action
when :create
"https://#{node[:boundary][:api][:hostname]}/#{node[:boundary][:api][:org_id]}/meters"
"https://#{node['boundary']['api']['hostname']}/#{node['boundary']['api']['org_id']}/meters"
when :search
"https://#{node[:boundary][:api][:hostname]}/#{node[:boundary][:api][:org_id]}/meters?name=#{new_resource.name}"
"https://#{node['boundary']['api']['hostname']}/#{node['boundary']['api']['org_id']}/meters?name=#{new_resource.name}"
when :meter
meter_id = get_meter_id(new_resource)
"https://#{node[:boundary][:api][:hostname]}/#{node[:boundary][:api][:org_id]}/meters/#{meter_id}"
"https://#{node['boundary']['api']['hostname']}/#{node['boundary']['api']['org_id']}/meters/#{meter_id}"
when :certificates
meter_id = get_meter_id(new_resource)
"https://#{node[:boundary][:api][:hostname]}/#{node[:boundary][:api][:org_id]}/meters/#{meter_id}"
"https://#{node['boundary']['api']['hostname']}/#{node['boundary']['api']['org_id']}/meters/#{meter_id}"
when :delete
meter_id = get_meter_id(new_resource)
"https://#{node[:boundary][:api][:hostname]}/#{node[:boundary][:api][:org_id]}/meters/#{meter_id}"
"https://#{node['boundary']['api']['hostname']}/#{node['boundary']['api']['org_id']}/meters/#{meter_id}"
when :tags
meter_id = get_meter_id(new_resource)
"https://#{node[:boundary][:api][:hostname]}/#{node[:boundary][:api][:org_id]}/meters/#{meter_id}/tags"
"https://#{node['boundary']['api']['hostname']}/#{node['boundary']['api']['org_id']}/meters/#{meter_id}/tags"
end
end

def meter_exists?(new_resource)
begin
url = build_url(new_resource, :search)
headers = generate_headers()
url = build_url(new_resource, :search)
headers = generate_headers()

response = http_request(:get, url, headers)
response = http_request(:get, url, headers)

if response
body = JSON.parse(response.body)
if response
body = JSON.parse(response.body)

if body == []
false
else
true
end
else
Chef::Application.fatal!("Could not determine if meter exists (nil response)!")
end
rescue Exception => e
Chef::Application.fatal!("Could not determine if meter exists, failed with #{e}")
!(body == [])
else
raise('Could not determine if meter exists (nil response)!')
end
rescue Exception => e
raise("Could not determine if meter exists, failed with #{e}")
end

def get_meter_id(new_resource)
begin
url = build_url(new_resource, :search)
headers = generate_headers()

response = http_request(:get, url, headers)

if response
body = JSON.parse(response.body)
if body[0]
if body[0]["id"]
body[0]["id"]
else
Chef::Application.fatal!("Could not get meter id (nil response)!")
end
url = build_url(new_resource, :search)
headers = generate_headers()

response = http_request(:get, url, headers)

if response
body = JSON.parse(response.body)
if body[0]
if body[0]['id']
body[0]['id']
else
Chef::Application.fatal!("Could not get meter id (nil response)!")
raise('Could not get meter id (nil response)!')
end
else
Chef::Application.fatal!("Could not get meter id (nil response)!")
raise('Could not get meter id (nil response)!')
end

rescue Exception => e
Chef::Application.fatal!("Could not get meter id, failed with #{e}")
nil
else
raise('Could not get meter id (nil response)!')
end
rescue Exception => e
raise("Could not get meter id, failed with #{e}")
nil
end

def http_request(method, url, headers, body = nil)
Expand All @@ -230,7 +211,7 @@ def http_request(method, url, headers, body = nil)
uri = URI(url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
#http.ssl_version = "SSLv3"
# http.ssl_version = "SSLv3"
http.ca_file = "#{Chef::Config[:file_cache_path]}/cacert.pem"
http.verify_mode = OpenSSL::SSL::VERIFY_PEER

Expand All @@ -246,13 +227,13 @@ def http_request(method, url, headers, body = nil)
when :delete
req = Net::HTTP::Delete.new(uri.request_uri)
else
Chef::Log.error("Unsupported http method (nil response)!")
Chef::Log.error('Unsupported http method (nil response)!')
nil
end

headers.each{|k,v|
headers.each do |k, v|
req[k] = v
}
end
response = http.request(req)

Chef::Log.debug("Response Body: #{response.body}")
Expand All @@ -266,14 +247,13 @@ def http_request(method, url, headers, body = nil)
end

def bad_response?(method, url, response)
case response
when Net::HTTPSuccess
false
else
true
case response
when Net::HTTPSuccess
false
else
true
Chef::Log.error("Got a #{response.code} for #{method} to #{url}")
end
end
end

end
end
Loading

0 comments on commit 03f51c3

Please sign in to comment.