puppet-rest 0.0.3

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.
Files changed (46) hide show
  1. checksums.yaml +15 -0
  2. data/.gitignore +7 -0
  3. data/.pryrc +58 -0
  4. data/Gemfile +10 -0
  5. data/Gemfile.lock +73 -0
  6. data/LICENSE.txt +20 -0
  7. data/README.md +81 -0
  8. data/Rakefile +3 -0
  9. data/lib/puppet-rest.rb +43 -0
  10. data/lib/puppet-rest/db/client.rb +42 -0
  11. data/lib/puppet-rest/db/config.rb +83 -0
  12. data/lib/puppet-rest/db/connection.rb +26 -0
  13. data/lib/puppet-rest/db/connection/fact-names.rb +9 -0
  14. data/lib/puppet-rest/db/connection/facts.rb +11 -0
  15. data/lib/puppet-rest/db/connection/nodes.rb +32 -0
  16. data/lib/puppet-rest/db/connection/resources.rb +11 -0
  17. data/lib/puppet-rest/db/entities/base.rb +100 -0
  18. data/lib/puppet-rest/db/entities/fact.rb +9 -0
  19. data/lib/puppet-rest/db/entities/node.rb +22 -0
  20. data/lib/puppet-rest/db/entities/resource.rb +12 -0
  21. data/lib/puppet-rest/db/request.rb +74 -0
  22. data/lib/puppet-rest/error.rb +42 -0
  23. data/lib/puppet-rest/identity_map.rb +4 -0
  24. data/lib/puppet-rest/monkey_patches/array.rb +7 -0
  25. data/lib/puppet-rest/monkey_patches/enumerable.rb +11 -0
  26. data/lib/puppet-rest/monkey_patches/hash.rb +49 -0
  27. data/lib/puppet-rest/monkey_patches/mash.rb +219 -0
  28. data/lib/puppet-rest/pe/client.rb +42 -0
  29. data/lib/puppet-rest/pe/config.rb +93 -0
  30. data/lib/puppet-rest/pe/connection.rb +25 -0
  31. data/lib/puppet-rest/pe/connection/ca_cert.rb +10 -0
  32. data/lib/puppet-rest/pe/connection/catalog.rb +10 -0
  33. data/lib/puppet-rest/pe/connection/node.rb +10 -0
  34. data/lib/puppet-rest/pe/entities/base.rb +100 -0
  35. data/lib/puppet-rest/pe/entities/catalog.rb +16 -0
  36. data/lib/puppet-rest/pe/entities/node.rb +9 -0
  37. data/lib/puppet-rest/pe/request.rb +77 -0
  38. data/lib/puppet-rest/response/client_error.rb +33 -0
  39. data/lib/puppet-rest/response/parse_json.rb +26 -0
  40. data/lib/puppet-rest/version.rb +3 -0
  41. data/puppet-rest.gemspec +28 -0
  42. data/spec/certificates/ca.pem +35 -0
  43. data/spec/certificates/cert.pem +35 -0
  44. data/spec/certificates/pk.pem +51 -0
  45. data/spec/spec_helper.rb +9 -0
  46. metadata +143 -0
@@ -0,0 +1,26 @@
1
+ module PuppetRestClient::DB
2
+ class Connection
3
+ include PuppetRestClient::DB::Connection::Nodes
4
+ include PuppetRestClient::DB::Connection::FactNames
5
+ include PuppetRestClient::DB::Connection::Facts
6
+ include PuppetRestClient::DB::Connection::Resources
7
+ include PuppetRestClient::DB::Request
8
+
9
+ Config::VALID_OPTIONS_KEYS.each do |key|
10
+ attr_accessor key
11
+ end
12
+
13
+ def initialize(attrs=Mash.new)
14
+ attrs = PuppetRestClient::DB.options.merge(attrs)
15
+
16
+ #unless attrs[:client_key].is_a?(OpenSSL::PKey::RSA)
17
+ # attrs[:client_key] = OpenSSL::PKey::RSA.new(attrs[:client_key])
18
+ #end
19
+
20
+ PuppetRestClient::DB::Config::VALID_OPTIONS_KEYS.each do |key|
21
+ instance_variable_set("@#{key}".to_sym, attrs[key])
22
+ end
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,9 @@
1
+ module PuppetRestClient::DB
2
+ class Connection
3
+ module FactNames
4
+ def fact_names
5
+ get api_path('fact-names')
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ module PuppetRestClient::DB
2
+ class Connection
3
+ module Facts
4
+ def facts(name)
5
+ get(api_path("facts/#{name}")).map {|fact|
6
+ PuppetRestClient::DB::Fact.get_or_new fact
7
+ }
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,32 @@
1
+ module PuppetRestClient::DB
2
+ class Connection
3
+ module Nodes
4
+ def nodes(options=Mash.new)
5
+ get(api_path('nodes')).map {|node|
6
+ n = PuppetRestClient::DB::Node.get_or_new(node)
7
+ n.connection = self
8
+ n
9
+ }
10
+ end
11
+
12
+ def node(name)
13
+ attributes = get(api_path("nodes/#{name}"))
14
+ n = PuppetRestClient::DB::Node.get_or_new(attributes)
15
+ n.connection = self
16
+ n
17
+ end
18
+
19
+ def node_facts(name)
20
+ get(api_path("nodes/#{name}/facts")).map {|fact|
21
+ PuppetRestClient::DB::Fact.get_or_new(fact)
22
+ }
23
+ end
24
+
25
+ def node_resources(name)
26
+ get(api_path("nodes/#{name}/resources")).map {|resource|
27
+ PuppetRestClient::DB::Resource.get_or_new(resource)
28
+ }
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,11 @@
1
+ module PuppetRestClient::DB
2
+ class Connection
3
+ module Resources
4
+ def resources(options=Mash.new)
5
+ get(api_path("resources/#{name}")).map {|resource|
6
+ PuppetRestClient::DB::Resource.get_or_new resource
7
+ }
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,100 @@
1
+ module PuppetRestClient::DB
2
+ class Base
3
+ attr_accessor :attrs
4
+ alias :to_hash :attrs
5
+
6
+ @@identity_map = PuppetRestClient::IdentityMap.new
7
+
8
+ def self.identity_map
9
+ @@identity_map
10
+ end # def self.identity_map
11
+
12
+ # Define methods that retrieve the value from an initialized instance variable Hash, using the attribute as a key
13
+ #
14
+ # @overload self.attr_reader(attr)
15
+ # @param attr [Symbol]
16
+ # @overload self.attr_reader(attrs)
17
+ # @param attrs [Array<Symbol>]
18
+ def self.attr_reader(*attrs)
19
+ attrs.each do |attribute|
20
+ class_eval do
21
+ define_method attribute do
22
+ @attrs[attribute.to_s]
23
+ end
24
+ define_method "#{attribute}=" do |value|
25
+ @attrs[attribute.to_s] = value
26
+ end
27
+ end
28
+ end
29
+ end # def self.attr_reader
30
+
31
+ def self.get(attrs=Mash.new)
32
+ @@identity_map[self] ||= {}
33
+ if attrs['title']
34
+ @@identity_map[self][attrs['title']] && @@identity_map[self][attrs['title']].update(attrs)
35
+ elsif attrs['resource']
36
+ @@identity_map[self][attrs['resource']] && @@identity_map[self][attrs['resource']].update(attrs)
37
+ else
38
+ @@identity_map[self][Marshal.dump(attrs)]
39
+ end
40
+ end # def self.get
41
+
42
+ # Retrieve an object from the identity map or initialize a new object
43
+ def self.get_or_new(attrs=Mash.new)
44
+ self.get(attrs) || self.new(attrs)
45
+ end # def self.get_or_new
46
+
47
+ # Initializes a new object
48
+ #
49
+ # @param attrs [Hash]
50
+ # @return [PuppetRestClient::Base]
51
+ def initialize(attrs=Mash.new)
52
+ self.class.attr_reader *attrs.keys
53
+ attrs.stringify_keys!
54
+ self.update attrs
55
+ if attrs['title']
56
+ @@identity_map[self.class] ||= {}
57
+ @@identity_map[self.class][attrs['title']] = self
58
+ elsif attrs['resource']
59
+ @@identity_map[self.class] ||= {}
60
+ @@identity_map[self.class][attrs['resource']] = self
61
+ else
62
+ @@identity_map[self.class] ||= {}
63
+ @@identity_map[self.class][Marshal.dump(attrs)] = self
64
+ end
65
+ end # def initialize
66
+
67
+ # Fetches an attribute of an object using hash notation
68
+ #
69
+ # @param method [String, Symbol] Message to send to the object
70
+ def [](method)
71
+ self.__send__(method.to_sym)
72
+ rescue NoMethodError
73
+ nil
74
+ end # def []
75
+
76
+ # Update the attributes of an object
77
+ #
78
+ # @param attrs [Hash]
79
+ # @return [PuppetRestClient::Base]
80
+ def update(attrs)
81
+ @attrs = attrs
82
+ self
83
+ end # def update
84
+
85
+ # @return [String]
86
+ def resource
87
+ @attrs['resource']
88
+ end # def id
89
+
90
+ # @return [String]
91
+ def title
92
+ @attrs['title']
93
+ end # def name
94
+
95
+ def keys
96
+ @attrs.keys
97
+ end # def keys
98
+
99
+ end
100
+ end
@@ -0,0 +1,9 @@
1
+ module PuppetRestClient::DB
2
+ class Fact < PuppetRestClient::DB::Base
3
+ attr_reader :name, :value, :certname
4
+
5
+ def initialize(attrs=Mash.new)
6
+ super
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,22 @@
1
+ module PuppetRestClient::DB
2
+ class Node < PuppetRestClient::DB::Base
3
+ attr_reader :name, :deactivated, :catalog_timestamp, :facts_timestamp, :report_timestamp
4
+ attr_accessor :connection
5
+
6
+ def initialize(attrs = Mash.new)
7
+ attrs['name'] = attrs['certname'] if attrs['name'].nil? && !attrs['certname'].nil?
8
+ super attrs
9
+ @connection = nil
10
+ end
11
+
12
+ def facts
13
+ return [] if @connection.nil?
14
+ @connection.node_facts self.name
15
+ end
16
+
17
+ def resources
18
+ return [] if @connection.nil?
19
+ @connection.node_resources self.name
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,12 @@
1
+ module PuppetRestClient::DB
2
+ class Resource < PuppetRestClient::DB::Base
3
+ attr_reader :parameters, :sourceline, :sourcefile, :exported, :tags, :title, :type, :resource, :certname
4
+
5
+ def initialize(attrs=Mash.new)
6
+ super
7
+ @attrs['parameters'] ||= Mash.new
8
+ @attrs['tags'] ||= Mash.new
9
+ end
10
+
11
+ end
12
+ end
@@ -0,0 +1,74 @@
1
+ module PuppetRestClient::DB
2
+ module Request
3
+
4
+ def get(path, params=nil, options=Mash.new)
5
+ request(:get, path, params, options)
6
+ end
7
+
8
+ def api_path(entity)
9
+ '/%s/%s' % [api_version, entity]
10
+ end
11
+
12
+ private
13
+
14
+ def connection
15
+ # return @connection if defined? @connection
16
+
17
+ default_options = {
18
+ :headers => {
19
+ :accept => 'application/json',
20
+ :content_type => 'application/json',
21
+ :user_agent => user_agent
22
+ }
23
+ }
24
+
25
+ options = default_options.deep_merge(connection_options)
26
+
27
+ # @connection = Faraday.new(PuppetRestClient.server_url, options, &PuppetRestClient.middleware)
28
+ Faraday.new(server_url, options, &middleware)
29
+ end
30
+
31
+ def request(method, path, params, options)
32
+ json_params = params ? MultiJson.dump(params) : ''
33
+ uri = server_url
34
+ uri = URI(uri) unless uri.respond_to?(:host)
35
+ full_path = uri.path + URI(path).path
36
+ full_path_and_params = uri.path + path
37
+
38
+ #puts "server_url = #{server_url}"
39
+ #puts "full_path_and_params = #{full_path_and_params}"
40
+ #puts "method = #{method}"
41
+
42
+ headers = {} #signature_headers(method.to_s.upcase.to_sym, full_path, json_params)
43
+ # puts headers.inspect
44
+ # puts client_key.inspect
45
+ response = connection.run_request(method.to_sym, full_path_and_params, nil, headers) do |request|
46
+ request.options[:raw] = true if options[:raw]
47
+
48
+ #puts request.inspect
49
+
50
+ unless params.nil?
51
+ if request.method == :post || :put
52
+ request.body = json_params
53
+ else
54
+ request.params.update params
55
+ end
56
+ end
57
+ yield request if block_given?
58
+ end
59
+
60
+ if options[:raw]
61
+ return response
62
+ end
63
+
64
+ if options[:json]
65
+ return MultiJson.dump(response.body)
66
+ end
67
+
68
+ return response.body
69
+ #rescue Faraday::Error::ClientError
70
+ # raise PuppetRestClient::Error::ClientError
71
+ end
72
+
73
+ end
74
+ end
@@ -0,0 +1,42 @@
1
+ module PuppetRestClient
2
+ class Error < StandardError
3
+ attr_reader :http_headers, :wrapped_exception
4
+
5
+ def initialize(exception=$!, http_headers={})
6
+ @http_headers = http_headers
7
+ if exception.respond_to?(:backtrace)
8
+ super(exception.message)
9
+ @wrapped_exception = exception
10
+ else
11
+ super(exception.to_s)
12
+ end
13
+ end
14
+
15
+ def backtrace
16
+ @wrapped_exception ? @wrapped_exception.backtrace : super
17
+ end
18
+
19
+ end
20
+
21
+ class Error::BadRequest < PuppetRestClient::Error
22
+ end
23
+
24
+ class Error::Unauthorized < PuppetRestClient::Error
25
+ end
26
+
27
+ class Error::Forbidden < PuppetRestClient::Error
28
+ end
29
+
30
+ class Error::NotFound < PuppetRestClient::Error
31
+ end
32
+
33
+ class Error::NotAcceptable < PuppetRestClient::Error
34
+ end
35
+
36
+ class Error::Conflict < PuppetRestClient::Error
37
+ end
38
+
39
+ class Error::ClientError < PuppetRestClient::Error
40
+ end
41
+
42
+ end
@@ -0,0 +1,4 @@
1
+ module PuppetRestClient
2
+ class IdentityMap < Hash
3
+ end
4
+ end
@@ -0,0 +1,7 @@
1
+ class Array
2
+
3
+ def extract_options!
4
+ last.is_a?(::Hash) ? pop : {}
5
+ end # def extract_options!
6
+
7
+ end
@@ -0,0 +1,11 @@
1
+ module Enumerable
2
+
3
+ def threaded_map
4
+ threads = []
5
+ each do |object|
6
+ threads << Thread.new{yield object}
7
+ end
8
+ threads.map(&:value)
9
+ end
10
+
11
+ end
@@ -0,0 +1,49 @@
1
+ class Hash
2
+
3
+ # Return a hash that includes everything but the given keys.
4
+ #
5
+ # @param keys [Array, Set]
6
+ # @return [Hash]
7
+ def except(*keys)
8
+ self.dup.except!(*keys)
9
+ end
10
+
11
+ # Replaces the hash without the given keys.
12
+ #
13
+ # @param keys [Array, Set]
14
+ # @return [Hash]
15
+ def except!(*keys)
16
+ keys.each{|key| delete(key)}
17
+ self
18
+ end
19
+
20
+ # Merges self with another hash, recursively
21
+ #
22
+ # @param hash [Hash] The hash to merge
23
+ # @return [Hash]
24
+ def deep_merge(hash)
25
+ target = self.dup
26
+ hash.keys.each do |key|
27
+ if hash[key].is_a?(Hash) && self[key].is_a?(Hash)
28
+ target[key] = target[key].deep_merge(hash[key])
29
+ next
30
+ end
31
+ target[key] = hash[key]
32
+ end
33
+ target
34
+ end
35
+
36
+ def stringify_keys!
37
+ keys.each do |key|
38
+ self[key.to_s] = delete(key)
39
+ end
40
+ self
41
+ end unless defined? stringify_keys
42
+
43
+ def symbolize_keys!
44
+ keys.each do |key|
45
+ self[(key.to_sym rescue key) || key] = delete(key)
46
+ end
47
+ self
48
+ end unless defined? symbolize_keys
49
+ end