Class: ForemanAP::ForemanAPI
- Inherits:
-
Object
- Object
- ForemanAP::ForemanAPI
- Defined in:
- lib/foreman_vm/foreman_api.rb
Overview
Convenience methods for accessing the REST API of Foreman.
Instance Attribute Summary collapse
-
#log ⇒ Object
Returns the value of attribute log.
Instance Method Summary collapse
-
#get_id(path, name, key = 'name') ⇒ Object
Get the ID of a resource in Foreman.
-
#initialize(uri, user, password) ⇒ ForemanAPI
constructor
Create an object.
-
#request(method, path, payload) ⇒ Object
Send a request to the Foreman API.
Constructor Details
#initialize(uri, user, password) ⇒ ForemanAPI
Create an object.
uri
-
The URI of the Foreman server.
user
-
The username to login with.
password
-
The password to login with.
16 17 18 19 20 21 |
# File 'lib/foreman_vm/foreman_api.rb', line 16 def initialize(uri, user, password) @uri = uri @user = user @password = password @log = Logger.new(STDERR) end |
Instance Attribute Details
#log ⇒ Object
Returns the value of attribute log.
10 11 12 |
# File 'lib/foreman_vm/foreman_api.rb', line 10 def log @log end |
Instance Method Details
#get_id(path, name, key = 'name') ⇒ Object
Get the ID of a resource in Foreman.
path
-
The path to the resource, underneath of /api/v2
name
-
The name to lookup
key
-
Either ‘name’ or ‘title’. Don’t ask why there is a difference.
70 71 72 73 74 75 76 |
# File 'lib/foreman_vm/foreman_api.rb', line 70 def get_id(path, name, key='name') raise 'name is required' if name.nil? res = request(:get, "/#{path}?search=#{key}%20=%20\"#{URI.escape(name)}\"", {}) id = JSON.parse(res.body)['results'][0]['id'] raise 'Unable to get the ID for #{path}/#{key}=#{name}' if id.nil? id end |
#request(method, path, payload) ⇒ Object
Send a request to the Foreman API.
method
-
The HTTP method; can be :post, :put, :get, or :delete
path
-
The path to the resource, underneath of /api/v2.
payload
-
The data to provide along with the request.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/foreman_vm/foreman_api.rb', line 27 def request(method, path, payload) uri = URI.parse(@uri) path = '/api/v2/' + path @log.debug "getting #{path}" # Build the request case method when :post req = Net::HTTP::Post.new(path, initheader = {'Content-Type' =>'application/json'}) when :put req = Net::HTTP::Put.new(path, initheader = {'Content-Type' =>'application/json'}) when :get req = Net::HTTP::Get.new(path, initheader = {'Content-Type' =>'application/json'}) when :delete req = Net::HTTP::Delete.new(path, initheader = {'Content-Type' =>'application/json'}) else raise 'Unsuported method' end req.basic_auth @user, @password req.body = payload.to_json # Submit the request http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE #XXX-FIXME insecure response = http.start { |h| http.request(req) } @log.debug "Response #{response.code} #{response.}: #{response.body}" if response.code != '200' @log.error "Error #{response.code} calling the Foreman API: #{response.body}" raise 'Error calling the Foreman API' end return response end |