foreman_api 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2012 Red Hat, Inc.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,37 @@
1
+ = Foreman API bindings for Ruby
2
+
3
+ == Summary
4
+
5
+ require 'foreman_api'
6
+ architectures = ForemanApi::Resources::Architecture.new(:base_url => 'http://localhost:3000',
7
+ :username => 'admin',
8
+ :password => 'changeme')
9
+
10
+ pp architectures.index
11
+ [[{"architecture"=>{"id"=>5, "name"=>"i386"}},
12
+ {"architecture"=>{"id"=>9, "name"=>"ppc"}},
13
+ {"architecture"=>{"id"=>14, "name"=>"x86_64"}}],
14
+ "[{\"architecture\":{\"name\":\"i386\",\"id\":5}},{\"architecture\":{\"name\":\"ppc\",\"id\":9}},{\"architecture\":{\"name\":\"x86_64\",\"id\":14}}]"]
15
+ => nil
16
+
17
+ == Description
18
+
19
+ This gem contains Foreman API bindings for the Ruby language. The bindings are generated from API documentation using {Apidoc}[https://github.com/Pajk/apipie-rails] tool.
20
+
21
+ The bindings brings support for new versioned API which is not complete yet. The number of supported controllers is limited but more are comming soon.
22
+
23
+ === Authentication
24
+
25
+ Foreman API supports authentication with username/password and OAuth. For use of OAuth with the bindings you only have to change the params
26
+
27
+ architectures = ForemanApi::Resources::Architecture.new(:base_url => 'http://localhost:3000',
28
+ :oauth => { :consumer_key => 'mykey',
29
+ :consumer_secret => 'shhhh' })
30
+
31
+ == License
32
+
33
+ The bindings are released under MIT license
34
+
35
+
36
+
37
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/foreman_api/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Martin Bačovský"]
6
+ gem.email = ["mbacovsk@redhat.com"]
7
+ gem.description = %q{Helps you to use Foreman's API calls from your app}
8
+ gem.summary = %q{Ruby bindings for Forman's rest API}
9
+ gem.homepage = "http://github.com/mbacovsky/foreman_api"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "foreman_api"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = ForemanApi::VERSION
17
+
18
+ gem.add_dependency 'json'
19
+ gem.add_dependency 'rest-client', '>= 1.6.1'
20
+ gem.add_dependency 'oauth'
21
+ end
@@ -0,0 +1,13 @@
1
+ require 'foreman_api/base'
2
+ require 'foreman_api/version'
3
+
4
+ resource_files = Dir[File.expand_path('../foreman_api/resources/*.rb', __FILE__)]
5
+ resource_files.each { |f| require f }
6
+
7
+ module ForemanApi
8
+ def self.client_config
9
+ @client_config ||= { :base_url => "http://localhost:3000",
10
+ :oauth => { :consumer_key => 'consumer',
11
+ :consumer_secret => 'shhhh' } }
12
+ end
13
+ end
@@ -0,0 +1,48 @@
1
+ require 'rest_client'
2
+ require 'oauth'
3
+ require 'json'
4
+ require 'foreman_api/rest_client_oauth'
5
+
6
+ module ForemanApi
7
+ class Base
8
+ attr_reader :client
9
+
10
+ def initialize(config = ForemanApi.client_config)
11
+ @client = RestClient::Resource.new(config[:base_url],
12
+ :user => config[:username],
13
+ :password => config[:password],
14
+ :oauth => config[:oauth],
15
+ :headers => { :content_type => 'application/json',
16
+ :accept => 'application/json' })
17
+ end
18
+
19
+ def call(method, path, payload = nil)
20
+ if payload && (method == :post || method == :put)
21
+ # TODO: is this neccessary? RestClient converts it automaticaly for me.
22
+ payload = payload.to_json
23
+ end
24
+ ret = client[path].send(*([method, payload].compact))
25
+ data = begin
26
+ JSON.parse(ret.body)
27
+ rescue JSON::ParserError
28
+ ret.body
29
+ end
30
+ return data, ret
31
+ end
32
+
33
+ def validate_params!(options, valid_keys)
34
+ return unless options.is_a?(Hash)
35
+ invalid_keys = options.keys - (valid_keys.is_a?(Hash) ? valid_keys.keys : valid_keys)
36
+ raise ArgumentError, "Invalid keys: #{invalid_keys.join(", ")}" unless invalid_keys.empty?
37
+
38
+ if valid_keys.is_a? Hash
39
+ valid_keys.each do |key, keys|
40
+ if options[key]
41
+ validate_params!(options[key], keys)
42
+ end
43
+ end
44
+ end
45
+ end
46
+
47
+ end
48
+ end
@@ -0,0 +1,29 @@
1
+ module ForemanApi
2
+ module Resources
3
+ class Architecture < ForemanApi::Base
4
+
5
+ def index
6
+ call(:get, "/api/architectures")
7
+ end
8
+
9
+ def show(id)
10
+ call(:get, "/api/architectures/#{id}")
11
+ end
12
+
13
+ def create(params = {})
14
+ validate_params!(params, {"architecture"=>["name"]})
15
+ call(:post, "/api/architectures", params)
16
+ end
17
+
18
+ def update(id, params = {})
19
+ validate_params!(params, {"architecture"=>["name"]})
20
+ call(:put, "/api/architectures/#{id}", params)
21
+ end
22
+
23
+ def destroy(id)
24
+ call(:delete, "/api/architectures/#{id}")
25
+ end
26
+
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,29 @@
1
+ module ForemanApi
2
+ module Resources
3
+ class Bookmark < ForemanApi::Base
4
+
5
+ def index
6
+ call(:get, "/api/bookmarks")
7
+ end
8
+
9
+ def show(id)
10
+ call(:get, "/api/bookmarks/#{id}")
11
+ end
12
+
13
+ def create(params = {})
14
+ validate_params!(params, {"bookmark"=>["name", "controller", "query"]})
15
+ call(:post, "/api/bookmarks", params)
16
+ end
17
+
18
+ def update(id, params = {})
19
+ validate_params!(params, {"bookmark"=>["name", "controller", "query"]})
20
+ call(:put, "/api/bookmarks/#{id}", params)
21
+ end
22
+
23
+ def destroy(id)
24
+ call(:delete, "/api/bookmarks/#{id}")
25
+ end
26
+
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,15 @@
1
+ module ForemanApi
2
+ module Resources
3
+ class Home < ForemanApi::Base
4
+
5
+ def index
6
+ call(:get, "/api")
7
+ end
8
+
9
+ def status
10
+ call(:get, "/api/status")
11
+ end
12
+
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,34 @@
1
+ module ForemanApi
2
+ module Resources
3
+ class OperatingSystem < ForemanApi::Base
4
+
5
+ def index
6
+ call(:get, "/api/operatingsystems")
7
+ end
8
+
9
+ def show(id)
10
+ call(:get, "/api/operatingsystems/#{id}")
11
+ end
12
+
13
+ def create(params = {})
14
+ validate_params!(params, {"operatingsystem"=>["name", "major", "minor"]})
15
+ call(:post, "/api/operatingsystems", params)
16
+ end
17
+
18
+ def update(id, params = {})
19
+ validate_params!(params, {"operatingsystem"=>["name", "major", "minor"]})
20
+ call(:put, "/api/operatingsystems/#{id}", params)
21
+ end
22
+
23
+ def destroy(id)
24
+ call(:delete, "/api/operatingsystems/#{id}")
25
+ end
26
+
27
+ def bootfiles(id, params = {})
28
+ validate_params!(params, ["medium", "architecture"])
29
+ call(:get, "/api/operatingsystems/#{id}/bootfiles", :params => params)
30
+ end
31
+
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,29 @@
1
+ module ForemanApi
2
+ module Resources
3
+ class User < ForemanApi::Base
4
+
5
+ def index
6
+ call(:get, "/api/users")
7
+ end
8
+
9
+ def show(id)
10
+ call(:get, "/api/users/#{id}")
11
+ end
12
+
13
+ def create(params = {})
14
+ validate_params!(params, {"user"=>["login", "firstname", "lastname", "mail", "admin"]})
15
+ call(:post, "/api/users", params)
16
+ end
17
+
18
+ def update(id, params = {})
19
+ validate_params!(params, {"user"=>["login", "firstname", "lastname", "mail", "admin"]})
20
+ call(:put, "/api/users/#{id}", params)
21
+ end
22
+
23
+ def destroy(id)
24
+ call(:delete, "/api/users/#{id}")
25
+ end
26
+
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,29 @@
1
+ unless RestClient.const_defined? :OAUTH_EXTENSION
2
+ RestClient::OAUTH_EXTENSION = lambda do |request, args|
3
+ if args[:oauth]
4
+ uri = URI.parse args[:url]
5
+ default_options = { :site => "#{uri.scheme}://#{uri.host}:#{uri.port.to_s}",
6
+ :request_token_path => "",
7
+ :authorize_path => "",
8
+ :access_token_path => "" }
9
+ options = default_options.merge args[:oauth][:options] || { }
10
+ consumer = OAuth::Consumer.new(args[:oauth][:consumer_key], args[:oauth][:consumer_secret], options)
11
+
12
+
13
+ method_to_http_request = { :get => Net::HTTP::Get,
14
+ :post => Net::HTTP::Post,
15
+ :put => Net::HTTP::Put,
16
+ :delete => Net::HTTP::Delete }
17
+
18
+ http_request = method_to_http_request[args[:method]].
19
+ new(args[:url]) # create Net::HTTPRequest to get oauth header,
20
+ # because RestClient::Request is not supported by Oauth
21
+ consumer.sign!(http_request)
22
+ request['Authorization'] = http_request['Authorization'] # add oauth header to rest_client request
23
+ end
24
+ end
25
+ end
26
+
27
+ unless RestClient.before_execution_procs.include? RestClient::OAUTH_EXTENSION
28
+ RestClient.add_before_execution_proc &RestClient::OAUTH_EXTENSION
29
+ end
@@ -0,0 +1,3 @@
1
+ module ForemanApi
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: foreman_api
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - "Martin Ba\xC4\x8Dovsk\xC3\xBD"
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-08-09 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: json
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: rest-client
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ hash: 13
43
+ segments:
44
+ - 1
45
+ - 6
46
+ - 1
47
+ version: 1.6.1
48
+ type: :runtime
49
+ version_requirements: *id002
50
+ - !ruby/object:Gem::Dependency
51
+ name: oauth
52
+ prerelease: false
53
+ requirement: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ hash: 3
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ type: :runtime
63
+ version_requirements: *id003
64
+ description: Helps you to use Foreman's API calls from your app
65
+ email:
66
+ - mbacovsk@redhat.com
67
+ executables: []
68
+
69
+ extensions: []
70
+
71
+ extra_rdoc_files: []
72
+
73
+ files:
74
+ - Gemfile
75
+ - MIT-LICENSE
76
+ - README.rdoc
77
+ - Rakefile
78
+ - foreman_api.gemspec
79
+ - lib/foreman_api.rb
80
+ - lib/foreman_api/base.rb
81
+ - lib/foreman_api/resources/architecture.rb
82
+ - lib/foreman_api/resources/bookmark.rb
83
+ - lib/foreman_api/resources/home.rb
84
+ - lib/foreman_api/resources/operating_system.rb
85
+ - lib/foreman_api/resources/user.rb
86
+ - lib/foreman_api/rest_client_oauth.rb
87
+ - lib/foreman_api/version.rb
88
+ homepage: http://github.com/mbacovsky/foreman_api
89
+ licenses: []
90
+
91
+ post_install_message:
92
+ rdoc_options: []
93
+
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ hash: 3
102
+ segments:
103
+ - 0
104
+ version: "0"
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ hash: 3
111
+ segments:
112
+ - 0
113
+ version: "0"
114
+ requirements: []
115
+
116
+ rubyforge_project:
117
+ rubygems_version: 1.8.11
118
+ signing_key:
119
+ specification_version: 3
120
+ summary: Ruby bindings for Forman's rest API
121
+ test_files: []
122
+