Module: EasyType::Provider

Defined in:
lib/easy_type/provider.rb

Overview

EasyType is a flushable provider. To use this provider, you have to add certain information to the type definition. You MUST define following attributes on the type

on_create do
  "create user #{self[:name]}"
end

on_modify do
  "alter user #{self[:name]}"
end

on_destroy do
  "drop user #{self[:name]}"
end

for all properties you MUST add

on_apply do
 "identified by #{resource[:password]}"
end

rubocop: disable Metrics/ModuleLength rubocop: disable Metrics/AbcSize

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#property_flushObject (readonly)

Returns the value of attribute property_flush.



35
36
37
# File 'lib/easy_type/provider.rb', line 35

def property_flush
  @property_flush
end

#property_hashObject (readonly)

Returns the value of attribute property_hash.



35
36
37
# File 'lib/easy_type/provider.rb', line 35

def property_hash
  @property_hash
end

Class Method Details

.deduct_identityObject



53
54
55
56
57
58
59
60
# File 'lib/easy_type/provider.rb', line 53

def self.deduct_identity
  stack_entry   = caller.select { |l| l.include?('puppet/provider') }.first
  provider_dir  = File.dirname(stack_entry)
  module_name   = stack_entry.scan(MODULE_NAME).flatten.first
  type_name     = stack_entry.scan(TYPE_NAME).flatten.first
  provider_name = stack_entry.scan(PROVIDER).flatten.first
  ensure_identification(module_name, type_name, provider_name, provider_dir) if module_name && type_name
end

.ensure_identification(module_name, type_name, provider_name, provider_dir) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/easy_type/provider.rb', line 63

def self.ensure_identification(module_name, type_name, provider_name, provider_dir)
  file_name = Pathname.new(provider_dir) + '.identity.rb'
  return unless identity_breached?(file_name, type_name, module_name)
  Puppet.warninging 'Module identity has been breached. Trying to repair and continue...'
  content = <<CODE
class Puppet::Type::#{type_name.capitalize}::Provider#{provider_name.capitalize}
  @type_name   = '#{type_name}'
  @module_name = '#{module_name}'
end
CODE
  replace_identity_content(file_name, content)
end

.identity_breached?(file_name, _type_name, _module_name) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
85
86
# File 'lib/easy_type/provider.rb', line 82

def self.identity_breached?(file_name, _type_name, _module_name)
  current_content = File.exist?(file_name) ? File.open(file_name, 'r', &:read) : ''
  matches = current_content.scan(/@type_name   = '(.*)'\n  @module_name = '(.*)'/).flatten
  !matches[0, 2]
end

.included(parent) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/easy_type/provider.rb', line 42

def self.included(parent)
  deduct_identity
  parent.extend(ClassMethods)
  # Load the contents of the last catalog. We use this to determine of we are making a 
  # corrective or an adaptive change.
  # 
  @@history ||= Puppet::Transaction::Persistence.new
  @@history.load if @@history.data == {}
end

.replace_identity_content(file_name, content) ⇒ Object



77
78
79
80
# File 'lib/easy_type/provider.rb', line 77

def self.replace_identity_content(file_name, content)
  File.open(file_name, 'w') { |file| file.write(content) }
  FileUtils.chmod(0o755, file_name)
end

Instance Method Details

#createObject

Create the resource based on:

- The values in the property_hash
- the command set on the Type
- The on_create value of the Type
- The on_apply values of all the specified parameters and properties


114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/easy_type/provider.rb', line 114

def create
  load_identity
  ::Entitlement::Entitlement.entitled?("#{module_name}##{type_name}#create")
  if disable_corrective_ensure? && corrective_ensure?
    fail "Corrective ensure present requested by catalog, but disabled by parameter disable_corrective_ensure."
  else
    @property_flush = @resource
    @property_hash[:ensure] ||= :present
    dump(:create)
    execute_from_type('create')
    @property_flush = {}
  end
end

#destroyObject

Destroy the resource based on:

- the command set on the Type
- The on_destroy value of the Type


133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/easy_type/provider.rb', line 133

def destroy
  load_identity
  ::Entitlement::Entitlement.entitled?("#{module_name}##{type_name}#destroy")
  if disable_corrective_ensure? && corrective_ensure?
    fail "Corrective ensure absent requested by catalog, but disabled by parameter disable_corrective_ensure."
  else
    dump(:destroy)
    execute_from_type('destroy')
    @property_hash.clear
    @property_flush = {}
  end
end

#exists?Boolean

Checks if the resource exists. It does that by checking if the ensure property in the property_hash contains :present

Returns:

  • (Boolean)

    true if it exsist, false if it doesn’t exist



100
101
102
# File 'lib/easy_type/provider.rb', line 100

def exists?
  !@property_hash[:ensure].nil?
end

#flushObject

Modify the resource based on:

- The values in the property_hash
- the command set on the Type
- The on_modify value of the Type
- The on_apply values of all the specified parameters and properties


153
154
155
156
157
158
159
160
161
162
163
# File 'lib/easy_type/provider.rb', line 153

def flush
  load_identity
  ::Entitlement::Entitlement.entitled?("#{module_name}##{type_name}#update")
  return unless @property_flush && @property_flush != {}
  if disable_corrective_change? && corrective_change?
    fail "Corrective change present requested by catalog, but disabled by parameter disable_corrective_change."
  else
    dump(:modify)
    execute_from_type('modify')
  end
end

#initialize(value = {}) ⇒ Object



89
90
91
92
# File 'lib/easy_type/provider.rb', line 89

def initialize(value = {})
  super(value)
  @property_flush = {}
end

#to_hashObject



104
105
106
# File 'lib/easy_type/provider.rb', line 104

def to_hash
  @property_hash
end