-
Notifications
You must be signed in to change notification settings - Fork 43
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
Introduce a class to represent the answer file #337
Open
ehelms
wants to merge
1
commit into
theforeman:master
Choose a base branch
from
ehelms:refactor-answer-file
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
require 'kafo/answer_file/v1' | ||
|
||
module Kafo | ||
module AnswerFile | ||
|
||
def self.load_answers(filename, version) | ||
case version | ||
when 1 | ||
AnswerFile::V1.new(filename) | ||
else | ||
raise InvalidAnswerFileVersion | ||
end | ||
end | ||
|
||
end | ||
end |
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,39 @@ | ||
require 'yaml' | ||
|
||
module Kafo | ||
module AnswerFile | ||
class Base | ||
|
||
attr_reader :answers, :filename | ||
|
||
def initialize(filename) | ||
@filename = filename | ||
@answers = YAML.load_file(@filename) | ||
validate | ||
end | ||
|
||
def save(data, config_header) | ||
FileUtils.touch @filename | ||
File.chmod 0600, @filename | ||
File.open(@filename, 'w') { |f| f.write(config_header + format(YAML.dump(data))) } | ||
end | ||
|
||
def puppet_classes | ||
raise NoMethodError | ||
end | ||
|
||
def class_enabled?(puppet_class) | ||
raise NoMethodError | ||
end | ||
|
||
def parameters_for_class(puppet_class) | ||
raise NoMethodError | ||
end | ||
|
||
def validate | ||
raise NoMethodError | ||
end | ||
|
||
end | ||
end | ||
end |
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,33 @@ | ||
require 'kafo/answer_file/base' | ||
|
||
module Kafo | ||
module AnswerFile | ||
class V1 < Base | ||
|
||
def puppet_classes | ||
@answers.keys.sort | ||
end | ||
|
||
def parameters_for_class(puppet_class) | ||
params = @answers[puppet_class] | ||
params.is_a?(Hash) ? params : {} | ||
end | ||
|
||
def class_enabled?(puppet_class) | ||
value = @answers[puppet_class.is_a?(String) ? puppet_class : puppet_class.identifier] | ||
!!value || value.is_a?(Hash) | ||
end | ||
|
||
def validate | ||
invalid = @answers.reject do |puppet_class, value| | ||
value.is_a?(Hash) || [true, false].include?(value) | ||
end | ||
|
||
unless invalid.empty? | ||
fail InvalidAnswerFile, "Answer file at #{@filename} has invalid values for #{invalid.keys.join(', ')}. Please ensure they are either a hash or true/false." | ||
end | ||
end | ||
|
||
end | ||
end | ||
end |
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
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,6 @@ | ||
--- | ||
class_a: true | ||
class_b: | ||
key: value | ||
class_c: {} | ||
class_d: false |
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,4 @@ | ||
--- | ||
class_a: 'true' | ||
class_b: | ||
class_c: 1 |
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,38 @@ | ||||||
require 'test_helper' | ||||||
|
||||||
describe 'Kafo::AnswerFile' do | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Generally you actually use the class reference.
Suggested change
|
||||||
describe 'answer file version 1' do | ||||||
describe 'valid answer file' do | ||||||
let(:answer_file_path) { 'test/fixtures/answer_files/v1/basic-answers.yaml' } | ||||||
let(:answer_file) { Kafo::AnswerFile.load_answers(answer_file_path, 1) } | ||||||
|
||||||
it 'returns the sorted puppet classes' do | ||||||
_(answer_file.puppet_classes).must_equal(['class_a', 'class_b', 'class_c', 'class_d']) | ||||||
end | ||||||
|
||||||
it 'returns the parameters for a class' do | ||||||
_(answer_file.parameters_for_class('class_b')).must_equal({'key' => 'value'}) | ||||||
end | ||||||
|
||||||
it 'returns true for a class with a hash' do | ||||||
_(answer_file.class_enabled?('class_c')).must_equal(true) | ||||||
end | ||||||
|
||||||
it 'returns true for a class set to true' do | ||||||
_(answer_file.class_enabled?('class_a')).must_equal(true) | ||||||
end | ||||||
|
||||||
it 'returns false for a class set to false' do | ||||||
_(answer_file.class_enabled?('class_d')).must_equal(false) | ||||||
end | ||||||
end | ||||||
|
||||||
describe 'invalid answer file' do | ||||||
let(:answer_file_path) { 'test/fixtures/answer_files/v1/invalid-answers.yaml' } | ||||||
|
||||||
it 'exits with invalid_answer_file' do | ||||||
_(Proc.new { Kafo::AnswerFile.load_answers(answer_file_path, 1) } ).must_raise Kafo::InvalidAnswerFile | ||||||
end | ||||||
end | ||||||
end | ||||||
end |
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
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.
I'm not sure this makes sense in this format. Or here. What I wanted to do was document the current schema to allow then to document a Version 2 schema. I considered doing this in a comment in the file itself.
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.
Thoughts about using https://yardoc.org/ in the actual classes as well?
Also, it's not really valid. You can't use names and Enum only describes strings. really it's:
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.
I was trying to model the structure off of https://github.com/theforeman/puppet-pulpcore/blob/master/templates/apache-fragment.epp#L2
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.
How do you think to use yarddoc? Just for documenting the method calls or somehow for this schema?
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.
That's a struct and that works different. With that you describe what's essentially a hash, but with explicit keys. See https://puppet.com/docs/puppet/6/lang_data_abstract.html#lang_data_abstract_flexible-struct-data-type
You could certainly use a Struct to describe the scenario file (where we have a limited number of fields, each with their own rules, but not the answers file.
I'd consider both. In the class documentation you can give describe the structure and give examples while also documenting the individual methods. That said, a short bit in README about the answers file can make sense. I guess it's a bit of both.