Class: Puppet::ResourceApi::TypeDefinition

Inherits:
BaseTypeDefinition show all
Defined in:
lib/puppet/resource_api/type_definition.rb

Overview

RSAPI Resource Type

Instance Attribute Summary

Attributes inherited from BaseTypeDefinition

#attributes, #definition

Instance Method Summary collapse

Methods inherited from BaseTypeDefinition

#check_schema, #check_schema_keys, #check_schema_values, #insyncable_attributes, #name, #namevars, #notify_schema_errors

Constructor Details

#initialize(definition) ⇒ TypeDefinition

Returns a new instance of TypeDefinition.



10
11
12
# File 'lib/puppet/resource_api/type_definition.rb', line 10

def initialize(definition)
  super(definition, :attributes)
end

Instance Method Details

#create_attribute_in(type, attribute_name, param_or_property, parent, options) ⇒ Object

This call creates a new parameter or property with all work-arounds or customizations required by the Resource API applied. Under the hood, this maps to the relevant DSL methods in Puppet::Type. See puppet.com/docs/puppet/6.0/custom_types.html#reference-5883 for details.

type: the Resource API Type the attribute is being created in attribute_name: the name of the attribute being created param_or_property: Whether to call the :newparam or :newproperty method parent: The type of attribute to create: Property, ReadOnly, or Parameter options: The hash of attribute options, including type, desc, default, and behaviour



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/puppet/resource_api/type_definition.rb', line 55

def create_attribute_in(type, attribute_name, param_or_property, parent, options)
  type.send(param_or_property, attribute_name.to_sym, parent: parent) do
    if options[:desc]
      desc "#{options[:desc]} (a #{options[:type]})"
    end

    # The initialize method is called when puppet core starts building up
    # type objects. The core passes in a hash of shape { resource:
    # #<Puppet::Type::TypeName> }. We use this to pass through the
    # required configuration data to the parent (see
    # Puppet::ResourceApi::Property, Puppet::ResourceApi::Parameter and
    # Puppet::ResourceApi::ReadOnlyParameter).
    define_method(:initialize) do |resource_hash|
      super(type.name, self.class.data_type, attribute_name, resource_hash, type)
    end

    # get pops data type object for this parameter or property
    define_singleton_method(:data_type) do
      @rsapi_data_type ||= Puppet::ResourceApi::DataTypeHandling.parse_puppet_type(
        attribute_name,
        options[:type],
      )
    end

    # from ValueCreator call create_values which makes alias values and
    # default values for properties and params
    Puppet::ResourceApi::ValueCreator.create_values(
      self,
      data_type,
      param_or_property,
      options,
    )
  end
end

#ensurable?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/puppet/resource_api/type_definition.rb', line 14

def ensurable?
  attributes.key?(:ensure)
end

#feature?(feature) ⇒ Boolean

rubocop complains when this is named has_feature?

Returns:

  • (Boolean)


19
20
21
# File 'lib/puppet/resource_api/type_definition.rb', line 19

def feature?(feature)
  definition[:features]&.include?(feature)
end

#title_patternsObject



23
24
25
# File 'lib/puppet/resource_api/type_definition.rb', line 23

def title_patterns
  definition[:title_patterns] ||= []
end

#validate_schema(definition, attr_key) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/puppet/resource_api/type_definition.rb', line 27

def validate_schema(definition, attr_key)
  super(definition, attr_key)
  [:title, :provider, :alias, :audit, :before, :consume, :export, :loglevel, :noop, :notify, :require, :schedule, :stage, :subscribe, :tag].each do |name|
    raise Puppet::DevError, 'must not define an attribute called `%{name}`' % { name: name.inspect } if definition[attr_key].key? name
  end
  if definition.key?(:title_patterns) && !definition[:title_patterns].is_a?(Array)
    raise Puppet::DevError, '`:title_patterns` must be an array, not `%{other_type}`' % { other_type: definition[:title_patterns].class }
  end

  Puppet::ResourceApi::DataTypeHandling.validate_ensure(definition)

  definition[:features] ||= []
  supported_features = %w[supports_noop canonicalize custom_insync remote_resource simple_get_filter custom_generate].freeze
  unknown_features = definition[:features] - supported_features
  Puppet.warning("Unknown feature detected: #{unknown_features.inspect}") unless unknown_features.empty?
end