Class: HammerCLIForeman::Command
- Inherits:
-
HammerCLI::Apipie::Command
- Object
- HammerCLI::Apipie::Command
- HammerCLIForeman::Command
show all
- Defined in:
- lib/hammer_cli_foreman/commands.rb
Direct Known Subclasses
Architecture, AssociatedCommand, AuthSource, AuthSourceLdap, CommonParameter, HammerCLIForeman::CommonParameter::SetCommand, ComputeResource, ConfigGroup, CreateCommand, Domain, Environment, ExternalUsergroup, Filter, Host, Hostgroup, Image, Interface, ListCommand, Location, Medium, Model, OperatingSystem, OperatingSystem::DeleteOsDefaultTemplate, OperatingSystem::SetOsDefaultTemplate, Organization, Parameter::AbstractParameterCommand, PartitionTable, PuppetClass, Realm, Report, Role, Settings, SingleResourceCommand, SmartClassParameter, SmartProxy, SmartProxy::ImportPuppetClassesCommand, SmartProxy::RefreshFeaturesCommand, SmartVariable, Subnet, Template, Template::BuildPXEDefaultCommand, User, Usergroup
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.build_options(builder_params = {}) {|builder_params| ... } ⇒ Object
110
111
112
113
114
115
116
|
# File 'lib/hammer_cli_foreman/commands.rb', line 110
def self.build_options(builder_params={})
builder_params[:resource_mapping] ||= resource_name_mapping
builder_params = HammerCLIForeman::BuildParams.new(builder_params)
yield(builder_params) if block_given?
super(builder_params.to_hash, &nil)
end
|
.connection_name(resource_class) ⇒ Object
68
69
70
|
# File 'lib/hammer_cli_foreman/commands.rb', line 68
def self.connection_name(resource_class)
CONNECTION_NAME
end
|
.create_option_builder ⇒ Object
96
97
98
99
100
101
102
103
104
|
# File 'lib/hammer_cli_foreman/commands.rb', line 96
def self.create_option_builder
configurator = BuilderConfigurator.new(searchables, dependency_resolver)
builder = ForemanOptionBuilder.new(searchables)
builder.builders = []
builder.builders += configurator.builders_for(resource, resource.action(action)) if resource_defined?
builder.builders += super.builders
builder
end
|
.dependency_resolver ⇒ Object
156
157
158
|
# File 'lib/hammer_cli_foreman/commands.rb', line 156
def self.dependency_resolver
HammerCLIForeman::DependencyResolver.new
end
|
.resolver ⇒ Object
151
152
153
154
|
# File 'lib/hammer_cli_foreman/commands.rb', line 151
def self.resolver
api = HammerCLI.context[:api_connection].get("foreman")
HammerCLIForeman::IdResolver.new(api, HammerCLIForeman::Searchables.new)
end
|
.resource_config ⇒ Object
72
73
74
|
# File 'lib/hammer_cli_foreman/commands.rb', line 72
def self.resource_config
super.merge(HammerCLIForeman.resource_config)
end
|
.resource_name_mapping ⇒ Object
106
107
108
|
# File 'lib/hammer_cli_foreman/commands.rb', line 106
def self.resource_name_mapping
HammerCLIForeman::RESOURCE_NAME_MAPPING
end
|
.searchables ⇒ Object
160
161
162
163
|
# File 'lib/hammer_cli_foreman/commands.rb', line 160
def self.searchables
@searchables ||= HammerCLIForeman::Searchables.new
@searchables
end
|
Instance Method Details
#customized_options ⇒ Object
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
# File 'lib/hammer_cli_foreman/commands.rb', line 192
def customized_options
params = options
IdParamsFilter.new(:only_required => false).for_action(resource.action(action)).each do |api_param|
param_resource = HammerCLIForeman.param_to_resource(api_param.name)
if param_resource && respond_to?(HammerCLI.option_accessor_name("#{param_resource.singular_name}_id"))
resource_id = get_resource_id(param_resource, :scoped => true, :required => api_param.required?)
params[HammerCLI.option_accessor_name(api_param.name)] = resource_id if resource_id
end
end
IdArrayParamsFilter.new(:only_required => false).for_action(resource.action(action)).each do |api_param|
param_resource = HammerCLIForeman.param_to_resource(api_param.name)
if param_resource && respond_to?(HammerCLI.option_accessor_name("#{param_resource.singular_name}_ids"))
resource_ids = get_resource_ids(param_resource, :scoped => true, :required => api_param.required?)
params[HammerCLI.option_accessor_name(api_param.name)] = resource_ids if resource_ids
end
end
id_option_name = HammerCLI.option_accessor_name('id')
params[id_option_name] ||= get_identifier if respond_to?(id_option_name)
params
end
|
#dependency_resolver ⇒ Object
80
81
82
|
# File 'lib/hammer_cli_foreman/commands.rb', line 80
def dependency_resolver
self.class.dependency_resolver
end
|
#exception_handler_class ⇒ Object
88
89
90
91
92
93
94
|
# File 'lib/hammer_cli_foreman/commands.rb', line 88
def exception_handler_class
HammerCLI.constant_path(self.class.name.to_s).reverse.each do |mod|
return mod.exception_handler_class if mod.respond_to? :exception_handler_class
end
HammerCLIForeman::ExceptionHandler
end
|
#get_identifier ⇒ Object
118
119
120
121
|
# File 'lib/hammer_cli_foreman/commands.rb', line 118
def get_identifier
@identifier ||= get_resource_id(resource)
@identifier
end
|
#get_resource_id(resource, options = {}) ⇒ Object
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
# File 'lib/hammer_cli_foreman/commands.rb', line 123
def get_resource_id(resource, options={})
if options[:scoped]
opts = resolver.scoped_options(resource.singular_name, all_options)
else
opts = all_options
end
begin
resolver.send("#{resource.singular_name}_id", opts)
rescue HammerCLIForeman::MissingSearchOptions => e
if (options[:required] == true || resource_search_requested(resource, opts))
logger.info "Error occured while searching for #{resource.singular_name}"
raise e
end
end
end
|
#get_resource_ids(resource, options = {}) ⇒ Object
139
140
141
142
143
144
145
146
147
148
149
|
# File 'lib/hammer_cli_foreman/commands.rb', line 139
def get_resource_ids(resource, options={})
opts = resolver.scoped_options(resource.singular_name, all_options)
begin
resolver.send("#{resource.singular_name}_ids", opts)
rescue HammerCLIForeman::MissingSearchOptions => e
if (options[:required] == true || resource_search_requested(resource, opts, true))
logger.info "Error occured while searching for #{resource.name}"
raise e
end
end
end
|
#request_params ⇒ Object
219
220
221
222
223
224
225
226
227
228
|
# File 'lib/hammer_cli_foreman/commands.rb', line 219
def request_params
params = customized_options
params_pruned = method_options(params)
id_option_name = HammerCLI.option_accessor_name('id')
params_pruned['id'] = params[id_option_name] if params[id_option_name]
params_pruned
end
|
#resolver ⇒ Object
76
77
78
|
# File 'lib/hammer_cli_foreman/commands.rb', line 76
def resolver
self.class.resolver
end
|
#searchables ⇒ Object
84
85
86
|
# File 'lib/hammer_cli_foreman/commands.rb', line 84
def searchables
self.class.searchables
end
|
#send_request ⇒ Object
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
# File 'lib/hammer_cli_foreman/commands.rb', line 165
def send_request
transform_format(super)
rescue HammerCLIForeman::MissingSearchOptions => e
switches = self.class.find_options(:referenced_resource => e.resource.singular_name).map(&:long_switch)
if switches.empty?
error_message = _("Could not find %{resource}. Some search options were missing, please see --help.")
elsif switches.length == 1
error_message = _("Could not find %{resource}, please set option %{switches}.")
else
error_message = _("Could not find %{resource}, please set one of options %{switches}.")
end
raise MissingSearchOptions.new(
error_message % {
:resource => e.resource.singular_name,
:switches => switches.join(", ")
},
e.resource
)
end
|
188
189
190
|
# File 'lib/hammer_cli_foreman/commands.rb', line 188
def transform_format(data)
HammerCLIForeman.record_to_common_format(data)
end
|