Module: OmfRc::ResourceProxyDSL::ClassMethods
- Included in:
- OmfRc::ResourceProxy::Application, OmfRc::ResourceProxy::Net, OmfRc::ResourceProxy::Node, OmfRc::ResourceProxy::Wlan, Util::Hostapd, Util::Ip, Util::Iw, Util::Mod, Util::Sysfs, Util::Wpa
- Defined in:
- omf_rc/lib/omf_rc/resource_proxy_dsl.rb
Overview
Methods defined here will be available in resource/utility definition files
Instance Method Summary (collapse)
- - (Object) call_hook(hook_name, context, *params)
-
- (Object) configure(name, opts = {}) {|resource, value| ... }
Register a configurable property.
-
- (Object) configure_all(®ister_block)
Configure multiple properties when operations need to be completed in order, or the all operations are transactional.
-
- (Object) extend_configure(configure_name)
Extend existing configure definition.
-
- (Object) extend_hook(hook_name)
Extend existing hook definition by alias existing method name as “orig_”.
-
- (Object) extend_request(request_name)
Extend existing request definition.
-
- (Object) extend_work(work_name)
Extend existing work definition by alias existing method name as “orig_”.
-
- (Object) hook(name) {|resource| ... }
Register some hooks which can be called at certain stage of the operation.
- - (Object) namespace(ns_prefix, ns_href) (also: #ns)
-
- (Object) property(name, opts = {})
Define internal property.
-
- (Object) register_proxy(name, opts = {})
Register a named proxy entry with factory class, normally this should be done in the proxy module.
-
- (Object) request(name, opts = {}) {|resource| ... }
Register a property that could be requested.
-
- (Object) utility(name)
Include the utility by providing a name.
-
- (Object) work(name) {|resource| ... }
Define an arbitrary method to do some work, can be included in configure & request block.
Instance Method Details
- (Object) call_hook(hook_name, context, *params)
150 151 152 |
# File 'omf_rc/lib/omf_rc/resource_proxy_dsl.rb', line 150 def call_hook(hook_name, context, *params) context.send(hook_name, *params) if context.respond_to? hook_name end |
- (Object) configure(name, opts = {}) {|resource, value| ... }
Register a configurable property
Please note that the result of the last line in the configure block will be returned via 'inform' message. If you want to make sure that user get a proper notification about the configure operation, simply use the last line to return such notification
220 221 222 223 224 225 226 227 |
# File 'omf_rc/lib/omf_rc/resource_proxy_dsl.rb', line 220 def configure(name, opts = {}, ®ister_block) unless opts[:if] && !opts[:if].call define_method("configure_#{name.to_s}") do |*args, &block| args[0] = Hashie::Mash.new(args[0]) if args[0].class == Hash register_block.call(self, *args, block) if register_block end end end |
- (Object) configure_all(®ister_block)
Configure multiple properties when operations need to be completed in order, or the all operations are transactional
246 247 248 249 250 |
# File 'omf_rc/lib/omf_rc/resource_proxy_dsl.rb', line 246 def configure_all(®ister_block) define_method("configure_all") do |*args, &block| register_block.call(self, *args, block) if register_block end end |
- (Object) extend_configure(configure_name)
Extend existing configure definition
Slightly different to extend_hook, the actual method_name defined by a configure property is “configure_”
366 367 368 369 |
# File 'omf_rc/lib/omf_rc/resource_proxy_dsl.rb', line 366 def extend_configure(configure_name) configure_name = configure_name.to_s alias_method "orig_configure_#{configure_name}", "configure_#{configure_name}" end |
- (Object) extend_hook(hook_name)
Extend existing hook definition by alias existing method name as “orig_”
329 330 331 332 |
# File 'omf_rc/lib/omf_rc/resource_proxy_dsl.rb', line 329 def extend_hook(hook_name) hook_name = hook_name.to_s alias_method "orig_#{hook_name}", hook_name end |
- (Object) extend_request(request_name)
Extend existing request definition
375 376 377 378 |
# File 'omf_rc/lib/omf_rc/resource_proxy_dsl.rb', line 375 def extend_request(request_name) request_name = request_name.to_s alias_method "orig_request_#{request_name}", "request_#{request_name}" end |
- (Object) extend_work(work_name)
Extend existing work definition by alias existing method name as “orig_”
338 339 340 341 |
# File 'omf_rc/lib/omf_rc/resource_proxy_dsl.rb', line 338 def extend_work(work_name) work_name = work_name.to_s alias_method "orig_#{work_name}", work_name end |
- (Object) hook(name) {|resource| ... }
Register some hooks which can be called at certain stage of the operation
Currently the system supports these hooks:
-
before_ready, called when a resource created, before creating an associated pubsub topic
-
before_release, called before a resource released
-
before_create, called before parent creates the child resource. (in the context of parent resource)
-
after_create, called after parent creates the child resource.
-
after_initial_configured, called after child resource created, and initial set of properties have been configured.
The sequence of execution is:
-
before_create
-
before_ready
-
after_create
-
after_initial_configured
-
before_release
143 144 145 146 147 |
# File 'omf_rc/lib/omf_rc/resource_proxy_dsl.rb', line 143 def hook(name, ®ister_block) define_method(name) do |*args, &block| register_block.call(self, *args, block) if register_block end end |
- (Object) namespace(ns_prefix, ns_href) Also known as: ns
380 381 382 383 384 |
# File 'omf_rc/lib/omf_rc/resource_proxy_dsl.rb', line 380 def namespace(ns_prefix, ns_href) define_method("namespace") do { ns_prefix => ns_href } end end |
- (Object) property(name, opts = {})
Define internal property. Refer to options section to see supported options.
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 |
# File 'omf_rc/lib/omf_rc/resource_proxy_dsl.rb', line 406 def property(name, opts = {}) opts = Hashie::Mash.new(opts) define_method("default_property_#{name}") do |*args, &block| self.property[name] ||= opts[:default] end if opts.access.instance_of? Array access = opts.access elsif opts.access.instance_of? Symbol access = case opts.access when :configure [:configure, :request] when :init_only [:init, :request] when :read_only [:request] else raise ArgumentError, "Unknown property access mode '#{opts.access}'" end end access ||= DEFAULT_PROP_ACCESS access.each do |a| case a when :configure define_method("configure_#{name}") do |val| self.property[name] = val end when :init define_method("initialise_#{name}") do |val| self.property[name] = val end when :request define_method("request_#{name}") do self.property[name] end else raise ArgumentError, "Unnown access type '#{a}'" end end end |
- (Object) register_proxy(name, opts = {})
Register a named proxy entry with factory class, normally this should be done in the proxy module
57 58 59 60 61 62 63 64 65 |
# File 'omf_rc/lib/omf_rc/resource_proxy_dsl.rb', line 57 def register_proxy(name, opts = {}) name = name.to_sym opts = Hashie::Mash.new(opts) if opts[:create_by] && !opts[:create_by].kind_of?(Array) opts[:create_by] = [opts[:create_by]] end opts[:proxy_module] = self OmfRc::ResourceFactory.register_proxy(name => opts) end |
- (Object) request(name, opts = {}) {|resource| ... }
Register a property that could be requested
272 273 274 275 276 277 278 279 |
# File 'omf_rc/lib/omf_rc/resource_proxy_dsl.rb', line 272 def request(name, opts = {}, ®ister_block) unless opts[:if] && !opts[:if].call define_method("request_#{name.to_s}") do |*args, &block| args[0] = Hashie::Mash.new(args[0]) if args[0].class == Hash register_block.call(self, *args, block) if register_block end end end |
- (Object) utility(name)
Include the utility by providing a name
The utility file can be added to the default utility directory UTIL_DIR, or defined inline.
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'omf_rc/lib/omf_rc/resource_proxy_dsl.rb', line 168 def utility(name) name = name.to_s begin # In case of module defined inline include "OmfRc::Util::#{name.camelize}".constantize extend "OmfRc::Util::#{name.camelize}".constantize rescue NameError begin # Then we try to require the file and include the module require "#{UTIL_DIR}/#{name}" include "OmfRc::Util::#{name.camelize}".constantize extend "OmfRc::Util::#{name.camelize}".constantize rescue LoadError => le logger.error le. rescue NameError => ne logger.error ne. end end end |
- (Object) work(name) {|resource| ... }
Define an arbitrary method to do some work, can be included in configure & request block
297 298 299 300 301 |
# File 'omf_rc/lib/omf_rc/resource_proxy_dsl.rb', line 297 def work(name, ®ister_block) define_method(name) do |*args, &block| register_block.call(self, *args, block) if register_block end end |