runcible / lib / runcible / instance.rb @ 20160073
1 |
# Copyright (c) 2013 Red Hat Inc.
|
---|---|
2 |
#
|
3 |
# MIT License
|
4 |
#
|
5 |
# Permission is hereby granted, free of charge, to any person obtaining
|
6 |
# a copy of this software and associated documentation files (the
|
7 |
# "Software"), to deal in the Software without restriction, including
|
8 |
# without limitation the rights to use, copy, modify, merge, publish,
|
9 |
# distribute, sublicense, and/or sell copies of the Software, and to
|
10 |
# permit persons to whom the Software is furnished to do so, subject to
|
11 |
# the following conditions:
|
12 |
#
|
13 |
# The above copyright notice and this permission notice shall be
|
14 |
# included in all copies or substantial portions of the Software.
|
15 |
#
|
16 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17 |
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18 |
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19 |
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20 |
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21 |
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22 |
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
23 |
|
24 |
module Runcible |
25 |
class Instance |
26 |
|
27 |
def self.resource_classes |
28 |
@@resource_classes ||= gather_classes("resources") |
29 |
@@resource_classes
|
30 |
end
|
31 |
|
32 |
def self.extension_classes |
33 |
@@extension_classes ||= gather_classes("extensions") |
34 |
@@extension_classes
|
35 |
end
|
36 |
|
37 |
attr_accessor :resources
|
38 |
attr_accessor :extensions
|
39 |
|
40 |
# Initialize a Runcible instance
|
41 |
#
|
42 |
# @param [Hash] config
|
43 |
# @option config [String] :user Pulp username
|
44 |
# @option config [String] :oauth Oauth credentials
|
45 |
# @option config [Hash] :headers Additional headers e.g. content-type => "application/json"
|
46 |
# @option config [String] :url Scheme and hostname for the pulp server e.g. https://localhost/
|
47 |
# @option config [String] :api_path URL path for the api e.g. pulp/api/v2/
|
48 |
# @option config [String] :timeout Timeout in seconds for the connection (defaults to rest client's default)
|
49 |
# @option config [String] :open_timeout timeout in seconds for the connection to open(defaults to rest client's default)
|
50 |
# @option config [Hash] :http_auth Needed when using simple http auth
|
51 |
# @option http_auth [String] :password The password to use for simple auth
|
52 |
def initialize(config={}) |
53 |
@config = {
|
54 |
:api_path => '/pulp/api/v2/', |
55 |
:url => 'https://localhost', |
56 |
:user => '', |
57 |
:http_auth => {:password => {} }, |
58 |
:headers => {:content_type => 'application/json', |
59 |
:accept => 'application/json'}, |
60 |
:logging => {}
|
61 |
}.merge(config).with_indifferent_access |
62 |
|
63 |
|
64 |
initialize_wrappers(config) |
65 |
end
|
66 |
|
67 |
# Update an existing config value
|
68 |
# @param [String, Symbol] key The key of the config to update
|
69 |
# @param [Object] value The value of the config to update
|
70 |
def update_config(key, value) |
71 |
@config[key] = value
|
72 |
end
|
73 |
|
74 |
def config |
75 |
@config
|
76 |
end
|
77 |
|
78 |
private |
79 |
|
80 |
def initialize_wrappers(config) |
81 |
self.resources = Wrapper.new('resources') |
82 |
self.extensions = Wrapper.new('extensions') |
83 |
|
84 |
self.class.resource_classes.each do |runcible_class| |
85 |
instance = runcible_class.new(@config)
|
86 |
self.resources.set_instance(accessible_class(runcible_class), instance)
|
87 |
end
|
88 |
|
89 |
self.class.extension_classes.each do |runcible_class| |
90 |
instance = runcible_class.new(@config)
|
91 |
self.extensions.set_instance(accessible_class(runcible_class), instance)
|
92 |
end
|
93 |
end
|
94 |
|
95 |
def accessible_class(class_object) |
96 |
#converts a class (Runcible::Resources::ConsumerGroup) to a user friendly name:
|
97 |
# (e.g. consumer_group)
|
98 |
class_object.name.split("::").last.underscore
|
99 |
end
|
100 |
|
101 |
def self.gather_classes(type) |
102 |
const = Runcible
|
103 |
const = const.const_get(type.camelize) |
104 |
path = File.dirname(__FILE__) + "/#{type}/*.rb" |
105 |
base_names = Dir.glob(path).collect{|f| File.basename(f, '.rb') } |
106 |
classes = base_names.collect{|name| const.const_get(name.camelize)} |
107 |
end
|
108 |
|
109 |
end
|
110 |
|
111 |
#Wrapper class to provide access to instances
|
112 |
class Wrapper |
113 |
|
114 |
def initialize(name) |
115 |
@name = name
|
116 |
@methods = []
|
117 |
end
|
118 |
|
119 |
def set_instance(attr_name, instance) |
120 |
@methods << attr_name
|
121 |
define_singleton_method(attr_name) { instance } |
122 |
end
|
123 |
|
124 |
def to_s |
125 |
"#{@name} - #{@methods.uniq.sort.to_s}"
|
126 |
end
|
127 |
end
|
128 |
|
129 |
end
|
130 |
|