This repository has been archived by the owner on Aug 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 81
(maint) Pay down tech debt #349
Merged
glennsarti
merged 10 commits into
puppetlabs-toy-chest:master
from
jpogran:maint-pay-down-tech-debt
Apr 25, 2018
Merged
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ef94d6c
(maint) Extract type cleaning
jpogran 70d3fed
(maint) Extract type document builder
jpogran b04791c
(maint) Extracts type building
jpogran 27efe52
(maint) Extract type import
jpogran 90962f8
(maint) Cleanup rake task descriptions
jpogran 4b282c9
(maint) Remove dsc:module:skeleton task
jpogran a5e3579
(maint) Remove unused code from Manager
jpogran 7f0876e
(maint) Standardize folder paths in build tasks
jpogran 7693dfd
(maint) Remove unused variables from dsc.rake
jpogran ffc7ed9
(maint) Rename global variables
jpogran 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 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 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 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 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,146 @@ | ||
module Dsc | ||
class TypeBuilder < Manager | ||
|
||
def build_dsc_types | ||
type_pathes = [] | ||
resources.each do |resource| | ||
if resource.friendlyname | ||
build_dsc_type(resource, @type_template_file, @target_module_path, @puppet_type_subpath, @module_path, type_pathes) | ||
build_dsc_type_spec(resource, @type_spec_template_file, @target_module_path, @puppet_type_spec_subpath, @module_path, type_pathes) | ||
else | ||
puts "#{resource.name} from #{resource.dsc_module} has invalid mof (no friendlyname defined)" | ||
puts "#{resource.name} will not be usable with puppet" | ||
end | ||
end | ||
type_pathes | ||
end | ||
|
||
def build_dsc_type(resource, type_template_file, target_module_path, puppet_type_subpath, module_path, type_pathes) | ||
type_template = File.open(type_template_file, 'r').read | ||
type_erb = ERB.new(type_template, nil, '-') | ||
puppet_type_path = "#{target_module_path}/#{puppet_type_subpath}" | ||
|
||
FileUtils.mkdir_p(puppet_type_path) unless File.exists?(puppet_type_path) | ||
|
||
File.open("#{puppet_type_path}/dsc_#{resource.friendlyname.downcase}.rb", 'w+') do |file| | ||
file.write(type_erb.result(binding)) | ||
pn = Pathname.new(file.path).expand_path.relative_path_from(module_path) | ||
type_pathes << "Add type - #{pn.to_s}" | ||
end | ||
end | ||
|
||
def build_dsc_type_spec(resource, type_spec_template_file, target_module_path, puppet_type_spec_subpath, module_path, type_pathes) | ||
whitelist = ['archive','environment','file','group','groupset','log','package', | ||
'processset','registry','script','service','serviceset','user','windowsfeature', | ||
'windowsfeatureset','windowsoptionalfeature','windowsoptionalfeatureset','windowsprocess'] | ||
|
||
type_spec_template = File.open(type_spec_template_file, 'r').read | ||
type_spec_erb = ERB.new(type_spec_template, nil, '-') | ||
puppet_type_spec_path = "#{target_module_path}/#{puppet_type_spec_subpath}" | ||
|
||
FileUtils.mkdir_p(puppet_type_spec_path) unless File.exists?(puppet_type_spec_path) | ||
|
||
if whitelist.include?(resource.friendlyname.downcase) | ||
File.open("#{puppet_type_spec_path}/dsc_#{resource.friendlyname.downcase}_spec.rb", 'w+') do |file| | ||
file.write(type_spec_erb.result(binding)) | ||
pn = Pathname.new(file.path).expand_path.relative_path_from(module_path) | ||
type_pathes << "Add type spec - #{pn.to_s}" | ||
end | ||
end | ||
end | ||
|
||
def format_type_value(type_value) | ||
case | ||
when type_value.class.name == 'Symbol' | ||
":#{type_value}" | ||
when type_value.class.name == 'String' | ||
"'#{type_value}'" | ||
when type_value.class.name == 'Numeric' | ||
"#{type_value}" | ||
else | ||
type_value | ||
end | ||
end | ||
|
||
def format_newvalues(values) | ||
output = [] | ||
values.each do |v| | ||
if v.respond_to?('downcase') | ||
output << format_type_value(v) | ||
output << format_type_value(v.downcase) | ||
else | ||
output << format_type_value(v) | ||
end | ||
end | ||
output.join(', ') | ||
end | ||
|
||
def get_spec_test_value(type) | ||
@spec_test_values = { | ||
'string' => 'foo', | ||
'string[]' => ['foo','bar','spec'], | ||
'MSFT_Credential' => { 'user' => 'user', 'password' => 'password' }, | ||
'MSFT_KeyValuePair' => { 'somekey' => 'somevalue' }, | ||
'MSFT_KeyValuePair[]' => { 'somekey' => 'somevalue', 'somekey2' => 'somevalue2' }, | ||
'bool' => true, | ||
'boolean' => true, | ||
'munged_bools' => ['true','false','True', 'False', :true, :false], | ||
'munged_ints' => ['16','-16','32', '-32'], | ||
'munged_uints' => ['16','32','64'], | ||
'int_not_bool_mungeable' => -16, | ||
'uint_not_bool_mungeable' => 16, | ||
'datetime' => '20140711', | ||
'int8' => -16, | ||
'int8[]' => [-128, 0, 127], | ||
'int16' => -16, | ||
'int16[]' => [-16, 32, -64], | ||
'int32' => -32, | ||
'int32[]' => [-32, 64, -128], | ||
'int64' => -64, | ||
'int64[]' => [-64, 128, -256], | ||
'sint8' => -128, | ||
'sint8[]' => [-128, 0, 127], | ||
'sint16' => -16, | ||
'sint16[]' => [-16, 32, -64], | ||
'sint32' => -32, | ||
'sint32[]' => [-32, 64, -128], | ||
'sint64' => -64, | ||
'sint64[]' => [-64, 128, -256], | ||
'uint8' => 1, | ||
'uint8[]' => [0, 16, 255], | ||
'uint16' => 16, | ||
'uint16[]' => [16, 32, 64], | ||
'uint32' => 32, | ||
'uint32[]' => [32, 64, 128], | ||
'uint64' => 64, | ||
'uint64[]' => [64, 128, 256], | ||
'real32' => '32.000', | ||
'real64' => '64.000', | ||
} | ||
# generate a hash here for any EmbeddedInstance type not seen yet | ||
if ! @spec_test_values[type] | ||
|
||
type_name = type.gsub(/\[\]$/, '') # strip [] off end of MOF type name | ||
values = cim_classes_with_path | ||
.select{ |c| c[:klass].name == type_name } | ||
.first[:klass] | ||
.features | ||
.map do |prop| | ||
# use first value in ValueMap if present | ||
if prop.qualifiers['Values'] | ||
val = prop.qualifiers['Values'].value.first | ||
else | ||
val = get_spec_test_value(prop.type.to_s) | ||
end | ||
[prop.name, val] | ||
end | ||
|
||
@spec_test_values[type] = Hash[ values ] | ||
end | ||
|
||
@spec_test_values[type] | ||
end | ||
|
||
end | ||
|
||
end |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: typo. Won't stop a merge though