-
Notifications
You must be signed in to change notification settings - Fork 43
Move parameter and property logic to separate classes #140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
DavidS
merged 14 commits into
puppetlabs:master
from
enterprisemodules:parameter_and_properties
Dec 6, 2018
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
890184b
(maint) move parameter and property logic to classes
02867d1
(maint) move value handling logic to ValueCreator
ee55a77
(maint) remove duplicated :default key check in ValueCreator
ad47c17
(maint) add ValueCreator docs and small restructure in ResourceApi
b7c636d
(maint) change parameter and properties class structure
8ace289
(maint) move the call_provider method to property
a0d1cbb
(maint) update parameter tests
DavidS 430f162
(maint) update property and read only parameter tests
48957a8
(maint) Delete unnecessary requires in resource_api.rb
a96abac
(maint) Update resource tests
003726f
(maint) Add class check for property class #insync? overload
f91bb12
(maint) Add rename in ValueCreator and update tests
87d8b0f
(maint) Change the definition of #insync?
83f99db
(maint) Update :ensure property tests
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
require 'puppet/util' | ||
require 'puppet/parameter' | ||
|
||
module Puppet; module ResourceApi; end; end # predeclare the main module # rubocop:disable Style/Documentation,Style/ClassAndModuleChildren | ||
|
||
# Class containing parameter functionality for ResourceApi. | ||
class Puppet::ResourceApi::Parameter < Puppet::Parameter | ||
attr_reader :value | ||
|
||
# This initialize takes arguments and sets up new parameter. | ||
# @param type_name the name of the Puppet Type | ||
# @param data_type the data type of parameter instance | ||
# @param attribute_name the name of attribue of the parameter | ||
# @param resource_hash the resource hash instance which is passed to the | ||
# parent class. | ||
def initialize(type_name, data_type, attribute_name, resource_hash) | ||
@type_name = type_name | ||
@data_type = data_type | ||
@attribute_name = attribute_name | ||
super(resource_hash) # Pass resource to parent Puppet class. | ||
end | ||
|
||
# This method assigns value to the parameter and cleans value. | ||
# @param value the value to be set and clean | ||
# @return [type] the cleaned value | ||
def value=(value) | ||
@value = Puppet::ResourceApi::DataTypeHandling.mungify( | ||
@data_type, | ||
value, | ||
"#{@type_name}.#{@attribute_name}", | ||
Puppet::ResourceApi.caller_is_resource_app?, | ||
) | ||
end | ||
|
||
# used internally | ||
# @returns the final mungified value of this parameter | ||
def rs_value | ||
@value | ||
end | ||
|
||
# puppet symbolizes some values through puppet/parameter/value.rb | ||
# (see .convert()), but (especially) Enums are strings. specifying a | ||
# munge block here skips the value_collection fallback in | ||
# puppet/parameter.rb's default .unsafe_munge() implementation. | ||
munge { |v| v } | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
require 'puppet/util' | ||
require 'puppet/property' | ||
|
||
module Puppet; module ResourceApi; end; end # predeclare the main module # rubocop:disable Style/Documentation,Style/ClassAndModuleChildren | ||
|
||
# Class containing property functionality for ResourceApi. | ||
class Puppet::ResourceApi::Property < Puppet::Property | ||
# This initialize takes arguments and sets up new property. | ||
# @param type_name the name of the Puppet Type | ||
# @param data_type the data type of property instance | ||
# @param attribute_name the name of attribue of the property | ||
# @param resource_hash the resource hash instance which is passed to the | ||
# parent class. | ||
def initialize(type_name, data_type, attribute_name, resource_hash) | ||
@type_name = type_name | ||
@data_type = data_type | ||
@attribute_name = attribute_name | ||
# Define class method insync?(is) if the name is :ensure | ||
def_insync? if @attribute_name == :ensure && self.class != Puppet::ResourceApi::Property | ||
# Pass resource to parent Puppet class. | ||
super(resource_hash) | ||
end | ||
|
||
# This method returns value of the property. | ||
# @return [type] the property value | ||
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 | ||
|
||
# This method sets and returns value of the property and sets @shouldorig. | ||
# @param value the value to be set and clean | ||
# @return [type] the property value | ||
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 | ||
|
||
# used internally | ||
# @returns the final mungified value of this property | ||
def rs_value | ||
@should ? @should.first : @should | ||
end | ||
|
||
# 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. | ||
def def_insync? | ||
define_singleton_method(:insync?) { |is| rs_value.to_s == is.to_s } | ||
end | ||
|
||
# puppet symbolizes some values through puppet/parameter/value.rb | ||
# (see .convert()), but (especially) Enums are strings. specifying a | ||
# munge block here skips the value_collection fallback in | ||
# puppet/parameter.rb's default .unsafe_munge() implementation. | ||
munge { |v| v } | ||
|
||
# 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. | ||
def call_provider(_value); end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
require 'puppet/util' | ||
require 'puppet/resource_api/parameter' | ||
|
||
# Class containing read only parameter functionality for ResourceApi. | ||
class Puppet::ResourceApi::ReadOnlyParameter < Puppet::ResourceApi::Parameter | ||
# This method raises error if the there is attempt to set value in parameter. | ||
# @return [Puppet::ResourceError] the error with information. | ||
def value=(value) | ||
raise Puppet::ResourceError, | ||
"Attempting to set `#{@attribute_name}` read_only attribute value " \ | ||
"to `#{value}`" | ||
end | ||
|
||
# used internally | ||
# @returns the final mungified value of this parameter | ||
def rs_value | ||
@value | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.