Created
March 12, 2012 21:11
-
-
Save ohadlevy/2024700 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require "rubygems" | |
require "rest_client" | |
require "json" | |
require "uri" | |
require "logger" | |
require "pry" | |
class Resource | |
attr_reader :url, :user, :password | |
def initialize(args) | |
raise("Must provide a protocol and host when initialising the connection") unless (url =~ /^http/) | |
# Each request is limited to 60 seconds | |
connect_params = {:timeout => 60, :open_timeout => 10, :headers => { :accept => :json }}.merge(args) | |
@resource = RestClient::Resource.new(url, connect_params) | |
end | |
def logger | |
@log ||= Logger.new(STDOUT) | |
end | |
private | |
# Decodes the JSON response if no HTTP error has been detected | |
# If an HTTP error is received then the error message is saves into @error | |
# Returns: Response, if the operation is GET, or true for POST, PUT and DELETE. | |
# OR: false if a HTTP error is detected | |
# TODO: add error message handling | |
def parse response | |
if response and response.code >= 200 and response.code < 300 | |
return response.body.empty? ? true : JSON.parse(response.body) | |
else | |
false | |
end | |
rescue => e | |
logger.warn "Failed to parse response: #{response} -> #{e}" | |
false | |
end | |
# Perform GET operation on the supplied path | |
def get path = nil | |
# This ensures that an extra "/" is not generated | |
if path | |
@resource[URI.escape(path)].get | |
else | |
@resource.get | |
end | |
end | |
# Perform POST operation with the supplied payload on the supplied path | |
def post payload, path = "" | |
@resource[path].post payload | |
end | |
# Perform PUT operation with the supplied payload on the supplied path | |
def put payload, path = "" | |
logger.debug "PUT: #{payload.inspect}" | |
@resource[path].put payload | |
end | |
# Perform DELETE operation on the supplied path | |
def delete path | |
@resource[path].delete | |
end | |
end | |
class Host < Resource | |
attr_reader :fqdn | |
def initialize args | |
@fqdn = args.delete(:fqdn) || raise("must provide an fqdn") | |
@url = args.delete(:url) + "/hosts/#{fqdn}" | |
super args | |
end | |
def clear | |
@response = nil | |
end | |
def attributes | |
fetch.keys | |
end | |
private | |
def method_missing(method, *args) | |
super(method, *args) | |
rescue NoMethodError | |
method_name = method.to_s | |
#setter method | |
if method_name =~ /=$/ | |
update method_name.gsub(/\s*=\s*/,''), args.first | |
#getter | |
else | |
fetch[method_name] | |
end | |
end | |
def fetch | |
@response ||= parse(get)["host"] | |
end | |
def update key, value | |
clear | |
parse put({:host => {key => value}}) | |
end | |
end | |
host = Host.new :url => "https://foreman", :user => "admin", :password => "changeme", :fqdn => "foreman.lan" | |
# host.attributes | |
# host.comment | |
#host.comment = "updated ..." | |
binding.pry |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment