Class: Puppet::ResourceApi::Transport::Wrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/resource_api/transport/wrapper.rb

Overview

Puppet::ResourceApi::Transport::Wrapper` to interface between the Util::NetworkDevice

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, url_or_config_or_transport) ⇒ Wrapper


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/puppet/resource_api/transport/wrapper.rb', line 9

def initialize(name, url_or_config_or_transport)
  if url_or_config_or_transport.is_a? String
    url = URI.parse(url_or_config_or_transport)
    raise "Unexpected url '#{url_or_config_or_transport}' found. Only file:/// URLs for configuration supported at the moment." unless url.scheme == 'file'
    raise "Trying to load config from '#{url.path}, but file does not exist." if url && !File.exist?(url.path)
    config = self.class.deep_symbolize(Hocon.load(url.path, syntax: Hocon::ConfigSyntax::HOCON) || {})
  elsif url_or_config_or_transport.is_a? Hash
    config = url_or_config_or_transport
  elsif transport_class?(name, url_or_config_or_transport)
    @transport = url_or_config_or_transport
  end

  @transport ||= Puppet::ResourceApi::Transport.connect(name, config)
  @schema = Puppet::ResourceApi::Transport.list[name]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object


41
42
43
44
45
46
47
# File 'lib/puppet/resource_api/transport/wrapper.rb', line 41

def method_missing(method_name, *args, &block)
  if @transport.respond_to? method_name
    @transport.send(method_name, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#schemaObject (readonly)

Returns the value of attribute schema


7
8
9
# File 'lib/puppet/resource_api/transport/wrapper.rb', line 7

def schema
  @schema
end

#transportObject (readonly)

Returns the value of attribute transport


7
8
9
# File 'lib/puppet/resource_api/transport/wrapper.rb', line 7

def transport
  @transport
end

Class Method Details

.deep_symbolize(obj) ⇒ Object


50
51
52
53
54
# File 'lib/puppet/resource_api/transport/wrapper.rb', line 50

def self.deep_symbolize(obj)
  return obj.each_with_object({}) { |(k, v), memo| memo[k.to_sym] = deep_symbolize(v); } if obj.is_a? Hash
  return obj.each_with_object([]) { |v, memo| memo << deep_symbolize(v); } if obj.is_a? Array
  obj
end

Instance Method Details

#factsObject


31
32
33
34
35
# File 'lib/puppet/resource_api/transport/wrapper.rb', line 31

def facts
  context = Puppet::ResourceApi::PuppetContext.new(@schema)
  # @transport.facts + custom_facts  # look into custom facts work by TP
  @transport.facts(context)
end

#respond_to_missing?(name, _include_private) ⇒ Boolean


37
38
39
# File 'lib/puppet/resource_api/transport/wrapper.rb', line 37

def respond_to_missing?(name, _include_private)
  (@transport.respond_to? name) || super
end

#transport_class?(name, transport) ⇒ Boolean


25
26
27
28
29
# File 'lib/puppet/resource_api/transport/wrapper.rb', line 25

def transport_class?(name, transport)
  class_name = name.split('_').map { |e| e.capitalize }.join
  expected = Puppet::Transport.const_get(class_name).to_s
  expected == transport.class.to_s
end