Class: Puppet::ResourceApi::Property

Inherits:
Property
  • Object
show all
Defined in:
lib/puppet/resource_api/property.rb

Overview

Class containing property functionality for ResourceApi.

Instance Method Summary collapse

Constructor Details

#initialize(type_name, data_type, attribute_name, resource_hash, referrable_type = nil) ⇒ Property

This initialize takes arguments and sets up new property. parent class.

Parameters:

  • type_name

    the name of the Puppet Type

  • data_type

    the data type of property instance

  • attribute_name

    the name of attribue of the property

  • resource_hash

    the resource hash instance which is passed to the



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/puppet/resource_api/property.rb', line 16

def initialize(type_name, data_type, attribute_name, resource_hash, referrable_type = nil)
  @type_name = type_name
  @data_type = data_type
  @attribute_name = attribute_name
  @resource = resource_hash[:resource]
  @referrable_type = referrable_type

  # Do not want to define insync on the base class because
  # this overrides for everything instead of only for the
  # appropriate instance/class of the property.
  if self.class != Puppet::ResourceApi::Property
    # Define class method insync?(is) if the custom_insync feature flag is set
    if referrable_type&.type_definition&.feature?('custom_insync')
      def_custom_insync?
      if @attribute_name == :rsapi_custom_insync_trigger
        @change_to_s_value = 'Custom insync logic determined that this resource is out of sync'
      end
    # Define class method insync?(is) if the name is :ensure and custom_insync feature flag is not set
    elsif @attribute_name == :ensure
      def_ensure_insync?
    end
  end

  # Pass resource to parent Puppet class.
  super(**resource_hash)
end

Instance Method Details

#call_provider(_value) ⇒ Object

stop puppet from trying to call into the provider when no pre-defined values have been specified “This is not the provider you are looking for.” – Obi-Wan Kaniesobi.



145
# File 'lib/puppet/resource_api/property.rb', line 145

def call_provider(_value); end

#def_custom_insync?Boolean

Returns:

  • (Boolean)


95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/puppet/resource_api/property.rb', line 95

def def_custom_insync?
  define_singleton_method(:insync?) do |is|
    provider    = @referrable_type.my_provider
    context     = @referrable_type.context
    should_hash = @resource.rsapi_canonicalized_target_state
    is_hash     = @resource.rsapi_current_state
    title       = @resource.rsapi_title

    raise(Puppet::DevError, 'No insync? method defined in the provider; an insync? method must be defined if the custom_insync feature is defined for the type') unless provider.respond_to?(:insync?)

    provider_insync_result, change_message = provider.insync?(context, title, @attribute_name, is_hash, should_hash)

    unless provider_insync_result.nil? || change_message.nil? || change_message.empty?
      @change_to_s_value = change_message
    end

    case provider_insync_result
    when nil
      # If validating ensure and no custom insync was used, check if rs_value matches is.
      return rs_value.to_s == is.to_s if @attribute_name == :ensure
      # Otherwise, super and rely on Puppet::Property.insync?
      super(is)
    when TrueClass, FalseClass
      return provider_insync_result
    else
      # When returning anything else, raise a DevError for a non-idiomatic return
      raise(Puppet::DevError, "Custom insync for #{@attribute_name} returned a #{provider_insync_result.class} with a value of #{provider_insync_result.inspect} instead of true/false; insync? MUST return nil or the boolean true or false") # rubocop:disable Metrics/LineLength
    end
  end

  define_singleton_method(:change_to_s) do |current_value, newvalue|
    # As defined in the custom insync? method, it is sometimes useful to overwrite the default change messaging;
    # The enables a user to return a more useful change report than a strict "is to should" report.
    # If @change_to_s_value is not set, Puppet writes a generic change notification, like:
    #   Notice: /Stage[main]/Main/<type_name>[<name_hash>]/<property name>: <property name> changed <is value> to <should value>
    # If #change_to_s_value is *nil* Puppet writes a weird empty message like:
    #   Notice: /Stage[main]/Main/<type_name>[<name_hash>]/<property name>:
    @change_to_s_value || super(current_value, newvalue)
  end
end

#def_ensure_insync?Boolean

method overloaded only for the :ensure property, add option to check if the rs_value matches is. Only if the class is child of Puppet::ResourceApi::Property.

Returns:

  • (Boolean)


91
92
93
# File 'lib/puppet/resource_api/property.rb', line 91

def def_ensure_insync?
  define_singleton_method(:insync?) { |is| rs_value.to_s == is.to_s }
end

#rs_valueObject

used internally



84
85
86
# File 'lib/puppet/resource_api/property.rb', line 84

def rs_value
  @should ? @should.first : @should
end

#shouldtype

This method returns value of the property.

Returns:

  • (type)

    the property value



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/puppet/resource_api/property.rb', line 45

def should
  if @attribute_name == :ensure && rs_value.is_a?(String)
    rs_value.to_sym
  elsif rs_value == false
    # work around https://tickets.puppetlabs.com/browse/PUP-2368
    :false # rubocop:disable Lint/BooleanSymbol
  elsif rs_value == true
    # work around https://tickets.puppetlabs.com/browse/PUP-2368
    :true # rubocop:disable Lint/BooleanSymbol
  else
    rs_value
  end
end

#should=(value) ⇒ type

This method sets and returns value of the property and sets @shouldorig.

Parameters:

  • value

    the value to be set and clean

Returns:

  • (type)

    the property value



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/puppet/resource_api/property.rb', line 62

def should=(value)
  @shouldorig = value

  if @attribute_name == :ensure
    value = value.to_s
  end

  # Puppet requires the @should value to always be stored as an array. We do not use this
  # for anything else
  # @see Puppet::Property.should=(value)
  @should = [
    Puppet::ResourceApi::DataTypeHandling.mungify(
      @data_type,
      value,
      "#{@type_name}.#{@attribute_name}",
      Puppet::ResourceApi.caller_is_resource_app?,
    ),
  ]
end