Skip to content
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

(GH-1493) Validate metadata when running or showing tasks #1542

Merged

Conversation

nicklewis
Copy link
Contributor

@nicklewis nicklewis commented Jan 16, 2020

We now validate metadata during the construction of a Bolt::Task object,
which means a warning will be logged when running or showing a task if that
task contains metadata fields that are unknown to Bolt.

In some cases, an unknown metadata field may signal a typo or that certain
keys (such as parameter specifications) are in the wrong place in the file.
In others, it may mean that new features have been added to the task spec
that the current version of Bolt doesn't understand and may not do the
right thing with. In both cases, it's helpful to alert the user to a
potential problem.

This also includes a refactor of the Bolt::Task class as well as a change to
make bolt task show use an instance of the Bolt::Task class (which allows
this warning to appear in that case).

Closes #1493

@nicklewis nicklewis requested review from a team as code owners January 16, 2020 23:26
@nicklewis nicklewis force-pushed the GH-1493-warn-on-unknown-task-metadata branch 2 times, most recently from d41aeb2 to b0b5055 Compare January 16, 2020 23:42
@puppetcla
Copy link

CLA signed by all contributors.

@nicklewis nicklewis force-pushed the GH-1493-warn-on-unknown-task-metadata branch 4 times, most recently from eb54868 to 494b87b Compare January 17, 2020 22:52
lib/bolt/task.rb Outdated Show resolved Hide resolved
lib/bolt/task.rb Show resolved Hide resolved
lib/bolt/task.rb Outdated Show resolved Hide resolved
@nicklewis nicklewis force-pushed the GH-1493-warn-on-unknown-task-metadata branch 3 times, most recently from f696b44 to d4dd57e Compare January 23, 2020 23:19
@@ -83,7 +83,7 @@ def get_validated_task(task_name, params = nil)
raise Bolt::Error.unknown_task(task_name) unless tasksig

Bolt::Task::Run.validate_params(tasksig, params) if params
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need this line (or function) with these changes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this line validates that the parameters provided are acceptable by the task. It might make sense in the future to combine the logic on Bolt::Task::Run into Bolt::Task so this would be like:

task = Bolt::Task.from_task_signature(tasksig)
task.validate_params(params) if params
task

But for now I don't think it fits with this PR.

}
end

def validate_metadata
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it'd be useful to have a test to verify that this warns with extra keys present - https://github.com/puppetlabs/bolt/blob/master/spec/integration/parsing_spec.rb seems like a good place for that? The sample::params task already has an extra top-level anything key, if we want to just use that.

@lucywyman
Copy link
Contributor

Changelog needs a rebase :P

@nicklewis
Copy link
Contributor Author

Rebased

@nicklewis nicklewis force-pushed the GH-1493-warn-on-unknown-task-metadata branch from d4dd57e to 9f01efd Compare January 24, 2020 22:11
Copy link
Contributor

@lucywyman lucywyman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comments above

@puppetcla
Copy link

CLA signed by all contributors.

@lucywyman lucywyman force-pushed the GH-1493-warn-on-unknown-task-metadata branch from b0a4843 to e1debf1 Compare January 27, 2020 22:15
nicklewis and others added 4 commits January 27, 2020 14:44
The previous use of Struct here didn't have many benefits, as it's
basically a proper Class. It was also sloppy about what it accepted as
arguments, as the metaprogramming involved made it possible to use both
keyword or string keys, and made every parameter effectively optional.

This is now written as an ordinary class with a positional initialize
method. For the common case where we have a TaskSignature object that we
want to turn into a Bolt::Task, there is now a from_task_signature
factory method. This simplifies the code and makes it easier to keep
track of data.
Previously, the get_task_info method used by `bolt task show` expected
to receive a task hash generated from a TaskSignature object. As we now
have a Bolt::Task class, it makes sense to use that as the standard
interface. This also ensures that whether we're inspecting or running a
task, we always have a Bolt::Task object to work with.
We now validate metadata during the construction of a Bolt::Task object,
which means a warning will be logged when running or showing a task if
that task contains metadata fields that are unknown to Bolt.

In some cases, an unknown metadata field may signal a typo or that
certain keys (such as parameter specifications) are in the wrong place
in the file. In others, it may mean that new features have been added to
the task spec that the current version of Bolt doesn't understand and
may not do the right thing with. In both cases, it's helpful to alert
the user to a potential problem.
This adds an integration test to ensure a warning is raised when
metadata for a task is invalid.
@lucywyman lucywyman force-pushed the GH-1493-warn-on-unknown-task-metadata branch from e1debf1 to 3c43652 Compare January 27, 2020 22:45
@lucywyman lucywyman merged commit 8385d0e into puppetlabs:master Jan 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Warn when user has additional properties in task metadata
4 participants