runcible / lib / runcible / instance.rb @ df1b6e2a
1 |
module Runcible |
---|---|
2 |
class Instance |
3 |
attr_accessor :resources
|
4 |
attr_accessor :extensions
|
5 |
|
6 |
# Initialize a Runcible instance
|
7 |
#
|
8 |
# @param [Hash] config
|
9 |
# @option config [String] :user Pulp username
|
10 |
# @option config [String] :oauth Oauth credentials
|
11 |
# @option config [Hash] :headers Additional headers e.g. content-type => "application/json"
|
12 |
# @option config [String] :url Scheme and hostname for the pulp server e.g. https://localhost/
|
13 |
# @option config [String] :api_path URL path for the api e.g. pulp/api/v2/
|
14 |
# @option config [String] :timeout Timeout in seconds for the connection (defaults to rest client's default)
|
15 |
# @option config [String] :open_timeout timeout in seconds for the connection
|
16 |
# to open(defaults to rest client's default)
|
17 |
# @option config [Hash] :http_auth Needed when using simple http auth
|
18 |
# @option http_auth [String] :password The password to use for simple auth
|
19 |
def initialize(config = {}) |
20 |
@config = {
|
21 |
:api_path => '/pulp/api/v2/', |
22 |
:url => 'https://localhost', |
23 |
:user => '', |
24 |
:http_auth => {:password => {} }, |
25 |
:headers => {:content_type => 'application/json', |
26 |
:accept => 'application/json'}, |
27 |
:logging => {}
|
28 |
}.merge(config).with_indifferent_access |
29 |
|
30 |
initialize_wrappers |
31 |
end
|
32 |
|
33 |
# Update an existing config value
|
34 |
# @param [String, Symbol] key The key of the config to update
|
35 |
# @param [Object] value The value of the config to update
|
36 |
def update_config(key, value) |
37 |
@config[key] = value
|
38 |
end
|
39 |
|
40 |
attr_reader :config
|
41 |
|
42 |
class << self |
43 |
# rubocop:disable Style/ClassVars
|
44 |
def resource_classes |
45 |
@@resource_classes ||= gather_classes('resources') |
46 |
@@resource_classes
|
47 |
end
|
48 |
|
49 |
def extension_classes |
50 |
@@extension_classes ||= gather_classes('extensions') |
51 |
@@extension_classes
|
52 |
end
|
53 |
|
54 |
private |
55 |
|
56 |
def gather_classes(type) |
57 |
const = Runcible
|
58 |
const = const.const_get(type.camelize) |
59 |
path = File.dirname(__FILE__) + "/#{type}/*.rb" |
60 |
base_names = Dir.glob(path).map { |f| File.basename(f, '.rb') } |
61 |
base_names.map { |name| const.const_get(name.camelize) } |
62 |
end
|
63 |
end
|
64 |
|
65 |
private |
66 |
|
67 |
def initialize_wrappers |
68 |
self.resources = Wrapper.new('resources') |
69 |
self.extensions = Wrapper.new('extensions') |
70 |
|
71 |
self.class.resource_classes.each do |runcible_class| |
72 |
instance = runcible_class.new(@config)
|
73 |
self.resources.set_instance(accessible_class(runcible_class), instance)
|
74 |
end
|
75 |
|
76 |
self.class.extension_classes.each do |runcible_class| |
77 |
instance = runcible_class.new(@config)
|
78 |
self.extensions.set_instance(accessible_class(runcible_class), instance)
|
79 |
end
|
80 |
end
|
81 |
|
82 |
def accessible_class(class_object) |
83 |
#converts a class (Runcible::Resources::ConsumerGroup) to a user friendly name:
|
84 |
# (e.g. consumer_group)
|
85 |
class_object.name.split('::').last.underscore
|
86 |
end
|
87 |
end
|
88 |
|
89 |
#Wrapper class to provide access to instances
|
90 |
class Wrapper |
91 |
def initialize(name) |
92 |
@name = name
|
93 |
@methods = []
|
94 |
end
|
95 |
|
96 |
def set_instance(attr_name, instance) |
97 |
@methods << attr_name
|
98 |
define_singleton_method(attr_name) { instance } |
99 |
end
|
100 |
|
101 |
def to_s |
102 |
"#{@name} - #{@methods.uniq.sort}"
|
103 |
end
|
104 |
end
|
105 |
end
|