Class: Puppet::Indirector::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/vendor/puppet/indirector/request.rb

Overview

This class encapsulates all of the information you need to make an Indirection call, and as a a result also handles REST calls. It’s somewhat analogous to an HTTP Request object, except tuned for our Indirector.

Constant Summary collapse

OPTION_ATTRIBUTES =
[:ip, :node, :authenticated, :ignore_terminus, :ignore_cache, :instance, :environment]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(indirection_name, method, key_or_instance, options_or_instance = {}) ⇒ Request

Returns a new instance of Request.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/vendor/puppet/indirector/request.rb', line 52

def initialize(indirection_name, method, key_or_instance, options_or_instance = {})
  if options_or_instance.is_a? Hash
    options = options_or_instance
    @instance = nil
  else
    options  = {}
    @instance = options_or_instance
  end

  self.indirection_name = indirection_name
  self.method = method

  set_attributes(options)

  @options = options.inject({}) { |hash, ary| hash[ary[0].to_sym] = ary[1]; hash }

  if key_or_instance.is_a?(String) || key_or_instance.is_a?(Symbol)
    key = key_or_instance
  else
    @instance ||= key_or_instance
  end

  if key
    # If the request key is a URI, then we need to treat it specially,
    # because it rewrites the key.  We could otherwise strip server/port/etc
    # info out in the REST class, but it seemed bad design for the REST
    # class to rewrite the key.

    if key.to_s =~ /^\w+:\// and not Puppet::Util.absolute_path?(key.to_s) # it's a URI
      set_uri_key(key)
    else
      @key = key
    end
  end

  @key = @instance.name if ! @key and @instance
end

Instance Attribute Details

#authenticatedObject

Returns the value of attribute authenticated.



9
10
11
# File 'lib/vendor/puppet/indirector/request.rb', line 9

def authenticated
  @authenticated
end

#ignore_cacheObject

Returns the value of attribute ignore_cache.



9
10
11
# File 'lib/vendor/puppet/indirector/request.rb', line 9

def ignore_cache
  @ignore_cache
end

#ignore_terminusObject

Returns the value of attribute ignore_terminus.



9
10
11
# File 'lib/vendor/puppet/indirector/request.rb', line 9

def ignore_terminus
  @ignore_terminus
end

#indirection_nameObject

Returns the value of attribute indirection_name.



13
14
15
# File 'lib/vendor/puppet/indirector/request.rb', line 13

def indirection_name
  @indirection_name
end

#instanceObject

Returns the value of attribute instance.



9
10
11
# File 'lib/vendor/puppet/indirector/request.rb', line 9

def instance
  @instance
end

#ipObject

Returns the value of attribute ip.



9
10
11
# File 'lib/vendor/puppet/indirector/request.rb', line 9

def ip
  @ip
end

#keyObject

Returns the value of attribute key.



9
10
11
# File 'lib/vendor/puppet/indirector/request.rb', line 9

def key
  @key
end

#methodObject

Returns the value of attribute method.



9
10
11
# File 'lib/vendor/puppet/indirector/request.rb', line 9

def method
  @method
end

#nodeObject

Returns the value of attribute node.



9
10
11
# File 'lib/vendor/puppet/indirector/request.rb', line 9

def node
  @node
end

#optionsObject

Returns the value of attribute options.



9
10
11
# File 'lib/vendor/puppet/indirector/request.rb', line 9

def options
  @options
end

#portObject

Returns the value of attribute port.



11
12
13
# File 'lib/vendor/puppet/indirector/request.rb', line 11

def port
  @port
end

#protocolObject

Returns the value of attribute protocol.



11
12
13
# File 'lib/vendor/puppet/indirector/request.rb', line 11

def protocol
  @protocol
end

#serverObject

Returns the value of attribute server.



11
12
13
# File 'lib/vendor/puppet/indirector/request.rb', line 11

def server
  @server
end

#uriObject

Returns the value of attribute uri.



11
12
13
# File 'lib/vendor/puppet/indirector/request.rb', line 11

def uri
  @uri
end

Instance Method Details

#authenticated?Boolean

Is this an authenticated request?

Returns:

  • (Boolean)


18
19
20
21
# File 'lib/vendor/puppet/indirector/request.rb', line 18

def authenticated?
  # Double negative, so we just get true or false
  ! ! authenticated
end

#environmentObject



23
24
25
# File 'lib/vendor/puppet/indirector/request.rb', line 23

def environment
  @environment ||= Puppet::Node::Environment.new
end

#environment=(env) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/vendor/puppet/indirector/request.rb', line 27

def environment=(env)
  @environment = if env.is_a?(Puppet::Node::Environment)
    env
  else
    Puppet::Node::Environment.new(env)
  end
end

#escaped_keyObject



35
36
37
# File 'lib/vendor/puppet/indirector/request.rb', line 35

def escaped_key
  URI.escape(key)
end

#ignore_cache?Boolean

LAK:NOTE This is a messy interface to the cache, and it’s only used by the Configurer class. I decided it was better to implement it now and refactor later, when we have a better design, than to spend another month coming up with a design now that might not be any better.

Returns:

  • (Boolean)


44
45
46
# File 'lib/vendor/puppet/indirector/request.rb', line 44

def ignore_cache?
  ignore_cache
end

#ignore_terminus?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/vendor/puppet/indirector/request.rb', line 48

def ignore_terminus?
  ignore_terminus
end

#indirectionObject

Look up the indirection based on the name provided.



91
92
93
# File 'lib/vendor/puppet/indirector/request.rb', line 91

def indirection
  Puppet::Indirector::Indirection.instance(indirection_name)
end

#modelObject

Raises:

  • (ArgumentError)


100
101
102
103
# File 'lib/vendor/puppet/indirector/request.rb', line 100

def model
  raise ArgumentError, "Could not find indirection '#{indirection_name}'" unless i = indirection
  i.model
end

#plural?Boolean

Are we trying to interact with multiple resources, or just one?

Returns:

  • (Boolean)


115
116
117
# File 'lib/vendor/puppet/indirector/request.rb', line 115

def plural?
  method == :search
end

#query_stringObject

Create the query string, if options are present.



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/vendor/puppet/indirector/request.rb', line 120

def query_string
  return "" unless options and ! options.empty?
  "?" + options.collect do |key, value|
    case value
    when nil; next
    when true, false; value = value.to_s
    when Fixnum, Bignum, Float; value = value # nothing
    when String; value = CGI.escape(value)
    when Symbol; value = CGI.escape(value.to_s)
    when Array; value = CGI.escape(YAML.dump(value))
    else
      raise ArgumentError, "HTTP REST queries cannot handle values of type '#{value.class}'"
    end

    "#{key}=#{value}"
  end.join("&")
end

#remote?Boolean

Returns:

  • (Boolean)


153
154
155
# File 'lib/vendor/puppet/indirector/request.rb', line 153

def remote?
  self.node or self.ip
end

#to_hashObject



138
139
140
141
142
143
144
145
146
147
# File 'lib/vendor/puppet/indirector/request.rb', line 138

def to_hash
  result = options.dup

  OPTION_ATTRIBUTES.each do |attribute|
    if value = send(attribute)
      result[attribute] = value
    end
  end
  result
end

#to_sObject



149
150
151
# File 'lib/vendor/puppet/indirector/request.rb', line 149

def to_s
  return(uri ? uri : "/#{indirection_name}/#{key}")
end

#use_cache?Boolean

Should we allow use of the cached object?

Returns:

  • (Boolean)


106
107
108
109
110
111
112
# File 'lib/vendor/puppet/indirector/request.rb', line 106

def use_cache?
  if defined?(@use_cache)
    ! ! use_cache
  else
    true
  end
end