Module: Puppet::Network::HTTP::API::V1

Included in:
Indirector::REST, Handler
Defined in:
lib/vendor/puppet/network/http/api/v1.rb

Constant Summary collapse

METHOD_MAP =

How we map http methods and the indirection name in the URI to an indirection method.

{
  "GET" => {
    :plural => :search,
    :singular => :find
  },
  "POST" => {
    :singular => :find,
  },
  "PUT" => {
    :singular => :save
  },
  "DELETE" => {
    :singular => :destroy
  },
  "HEAD" => {
    :singular => :head
  }
}

Instance Method Summary collapse

Instance Method Details

#indirection2uri(request) ⇒ Object



43
44
45
46
# File 'lib/vendor/puppet/network/http/api/v1.rb', line 43

def indirection2uri(request)
  indirection = request.method == :search ? pluralize(request.indirection_name.to_s) : request.indirection_name.to_s
  "/#{request.environment.to_s}/#{indirection}/#{request.escaped_key}#{request.query_string}"
end

#indirection_method(http_method, indirection) ⇒ Object

Raises:

  • (ArgumentError)


53
54
55
56
57
58
59
60
61
# File 'lib/vendor/puppet/network/http/api/v1.rb', line 53

def indirection_method(http_method, indirection)
  raise ArgumentError, "No support for http method #{http_method}" unless METHOD_MAP[http_method]

  unless method = METHOD_MAP[http_method][plurality(indirection)]
    raise ArgumentError, "No support for plural #{http_method} operations"
  end

  method
end

#plurality(indirection) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/vendor/puppet/network/http/api/v1.rb', line 67

def plurality(indirection)
  # NOTE This specific hook for facts is ridiculous, but it's a *many*-line
  # fix to not need this, and our goal is to move away from the complication
  # that leads to the fix being too long.
  return :singular if indirection == "facts"
  return :singular if indirection == "status"
  return :singular if indirection == "certificate_status"
  return :plural if indirection == "inventory"

  result = (indirection =~ /s$|_search$/) ? :plural : :singular

  indirection.sub!(/s$|_search$/, '')
  indirection.sub!(/statuse$/, 'status')

  result
end

#pluralize(indirection) ⇒ Object



63
64
65
# File 'lib/vendor/puppet/network/http/api/v1.rb', line 63

def pluralize(indirection)
  return(indirection == "status" ? "statuses" : indirection + "s")
end

#request_to_uri_and_body(request) ⇒ Object



48
49
50
51
# File 'lib/vendor/puppet/network/http/api/v1.rb', line 48

def request_to_uri_and_body(request)
  indirection = request.method == :search ? pluralize(request.indirection_name.to_s) : request.indirection_name.to_s
  ["/#{request.environment.to_s}/#{indirection}/#{request.escaped_key}", request.query_string.sub(/^\?/,'')]
end

#uri2indirection(http_method, uri, params) ⇒ Object

Raises:

  • (ArgumentError)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/vendor/puppet/network/http/api/v1.rb', line 25

def uri2indirection(http_method, uri, params)
  environment, indirection, key = uri.split("/", 4)[1..-1] # the first field is always nil because of the leading slash

  raise ArgumentError, "The environment must be purely alphanumeric, not '#{environment}'" unless environment =~ /^\w+$/
  raise ArgumentError, "The indirection name must be purely alphanumeric, not '#{indirection}'" unless indirection =~ /^\w+$/

  method = indirection_method(http_method, indirection)

  params[:environment] = Puppet::Node::Environment.new(environment)
  params.delete(:bucket_path)

  raise ArgumentError, "No request key specified in #{uri}" if key == "" or key.nil?

  key = URI.unescape(key)

  [indirection, method, key, params]
end