Class: Puppet::Indirector::ResourceType::Parser

Inherits:
Code show all
Defined in:
lib/puppet/indirector/resource_type/parser.rb

Overview

The main terminus for Puppet::Resource::Type

This exposes the known resource types from within Puppet. Only find and search are supported. When a request is received, Puppet will attempt to load all resource types (by parsing manifests and modules) and returns a description of the resource types found. The format of these objects is documented at Resource::Type.

Constant Summary

Constants included from Util

Util::AbsolutePathPosix, Util::AbsolutePathWindows, Util::DEFAULT_POSIX_MODE, Util::DEFAULT_WINDOWS_MODE

Constants included from Util::POSIX

Util::POSIX::LOCALE_ENV_VARS, Util::POSIX::USER_ENV_VARS

Constants included from Util::SymbolicFileMode

Util::SymbolicFileMode::SetGIDBit, Util::SymbolicFileMode::SetUIDBit, Util::SymbolicFileMode::StickyBit, Util::SymbolicFileMode::SymbolicMode, Util::SymbolicFileMode::SymbolicSpecialToBit

Constants included from Util::Docs

Util::Docs::HEADER_LEVELS

Instance Attribute Summary

Attributes included from Util::Docs

#doc, #nodoc

Instance Method Summary collapse

Methods inherited from Terminus

abstract_terminus?, const2name, #indirection, indirection_name, inherited, #initialize, mark_as_abstract_terminus, #model, model, #name, name2const, register_terminus_class, terminus_class, terminus_classes, #terminus_type, #validate, #validate_key, #validate_model

Methods included from Util::InstanceLoader

#instance_docs, #instance_hash, #instance_load, #instance_loader, #instance_loading?, #loaded_instance, #loaded_instances

Methods included from Util

absolute_path?, benchmark, chuser, clear_environment, default_env, deterministic_rand, deterministic_rand_int, exit_on_fail, get_env, get_environment, logmethods, merge_environment, path_to_uri, pretty_backtrace, replace_file, safe_posix_fork, set_env, symbolizehash, thinmark, uri_to_path, which, withenv, withumask

Methods included from Util::POSIX

#get_posix_field, #gid, #idfield, #methodbyid, #methodbyname, #search_posix_field, #uid

Methods included from Util::SymbolicFileMode

#normalize_symbolic_mode, #symbolic_mode_to_int, #valid_symbolic_mode?

Methods included from Util::Docs

#desc, #dochook, #doctable, #markdown_definitionlist, #markdown_header, #nodoc?, #pad, scrub

Constructor Details

This class inherits a constructor from Puppet::Indirector::Terminus

Instance Method Details

#allow_remote_requests?Boolean



104
105
106
107
# File 'lib/puppet/indirector/resource_type/parser.rb', line 104

def allow_remote_requests?
  Puppet.deprecation_warning("The resource_type endpoint is deprecated in favor of the environment_classes endpoint. See https://docs.puppet.com/puppetserver/latest/puppet-api/v3/environment_classes.html")
  super
end

#find(request) ⇒ Puppet::Resource::Type?

Find will return the first resource_type with the given name. It is not possible to specify the kind of the resource type.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/puppet/indirector/resource_type/parser.rb', line 25

def find(request)
  Puppet.override(:squelch_parse_errors => true) do
    krt = resource_types_in(request.environment)

    # This is a bit ugly.
    [:hostclass, :definition, :application, :node].each do |type|
      # We have to us 'find_<type>' here because it will
      # load any missing types from disk, whereas the plain
      # '<type>' method only returns from memory.
      if r = krt.send("find_#{type}", request.key)
        return r
      end
    end
    nil
  end
end

#resource_types_in(environment) ⇒ Object



99
100
101
102
# File 'lib/puppet/indirector/resource_type/parser.rb', line 99

def resource_types_in(environment)
  environment.check_for_reparse
  environment.known_resource_types
end

#search(request) ⇒ Array<Puppet::Resource::Type>?

Search for resource types using a regular expression. Unlike ‘find`, this allows you to filter the results by the “kind” of the resource type (“class”, “defined_type”, or “node”). All three are searched if no `kind` filter is given. This also accepts the special string “`*`” to return all resource type objects.



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
89
90
91
92
93
94
95
96
97
# File 'lib/puppet/indirector/resource_type/parser.rb', line 57

def search(request)
  Puppet.override(:squelch_parse_errors => true) do

    krt = resource_types_in(request.environment)
    # Make sure we've got all of the types loaded.
    krt.loader.import_all

    result_candidates = case request.options[:kind]
        when "class"
          krt.hostclasses.values
        when "defined_type"
          krt.definitions.values
        when "application"
          krt.applications.values
        when "node"
          krt.nodes.values
        when nil
          result_candidates = [krt.hostclasses.values, krt.definitions.values, krt.applications.values, krt.nodes.values]
        else
          raise ArgumentError, "Unrecognized kind filter: " +
                    "'#{request.options[:kind]}', expected one " +
                    " of 'class', 'defined_type', 'application', or 'node'."
      end

    result = result_candidates.flatten.reject { |t| t.name == "" }
    return nil if result.empty?
    return result if request.key == "*"

    # Strip the regex of any wrapping slashes that might exist
    key = request.key.sub(/^\//, '').sub(/\/$/, '')
    begin
      regex = Regexp.new(key)
    rescue => detail
      raise ArgumentError, "Invalid regex '#{request.key}': #{detail}", detail.backtrace
    end

    result.reject! { |t| t.name.to_s !~ regex }
    return nil if result.empty?
    result
  end
end