mirror of
https://github.com/inspec/inspec
synced 2024-11-15 01:17:08 +00:00
CFINSPEC-128 Initial draft of resource generator
Signed-off-by: Sonu Saha <sonu.saha@progress.com>
This commit is contained in:
parent
270ad5fa4b
commit
81a04aacd5
8 changed files with 204 additions and 0 deletions
|
@ -10,6 +10,7 @@ module InspecPlugins
|
||||||
|
|
||||||
require_relative "cli_profile"
|
require_relative "cli_profile"
|
||||||
require_relative "cli_plugin"
|
require_relative "cli_plugin"
|
||||||
|
require_relative "cli_resource"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
73
lib/plugins/inspec-init/lib/inspec-init/cli_resource.rb
Normal file
73
lib/plugins/inspec-init/lib/inspec-init/cli_resource.rb
Normal file
|
@ -0,0 +1,73 @@
|
||||||
|
require_relative "renderer"
|
||||||
|
|
||||||
|
module InspecPlugins
|
||||||
|
module Init
|
||||||
|
class CLI < Inspec.plugin(2, :cli_command)
|
||||||
|
#-------------------------------------------------------------------#
|
||||||
|
# inspec init resource
|
||||||
|
#-------------------------------------------------------------------#
|
||||||
|
desc "resource RESOURCE_NAME [options]", "Generates an InSpec resource, which can extend the scope of InSpec resources support"
|
||||||
|
# General options
|
||||||
|
option :prompt, type: :boolean, default: true, desc: "Interactively prompt for information to put in your generated resource."
|
||||||
|
|
||||||
|
# Templating vars
|
||||||
|
option :supports_platform, type: :string, default: "linux", desc: "the platform supported by this resource"
|
||||||
|
option :description, type: :string, default: "Resource description ...", desc: "the description of this resource"
|
||||||
|
option :class_name, type: :string, default: "", desc: "Class Name for your resource."
|
||||||
|
|
||||||
|
def resource(resource_name)
|
||||||
|
template_vars = {
|
||||||
|
name: File.join("temp", resource_name),
|
||||||
|
resource_name: resource_name,
|
||||||
|
}
|
||||||
|
resource_vars_from_opts
|
||||||
|
template_vars.merge!(options)
|
||||||
|
template_path = "resources"
|
||||||
|
|
||||||
|
render_opts = {
|
||||||
|
templates_path: TEMPLATES_PATH,
|
||||||
|
overwrite: options[:overwrite],
|
||||||
|
file_rename_map: make_rename_map(resource_name),
|
||||||
|
}
|
||||||
|
renderer = InspecPlugins::Init::Renderer.new(ui, render_opts)
|
||||||
|
renderer.render_with_values(template_path, "resource", template_vars)
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def make_rename_map(resource_name)
|
||||||
|
{
|
||||||
|
File.join("libraries", "inspec-resource-template.erb") => File.join("libraries", resource_name + ".rb"),
|
||||||
|
File.join("docs", "resource-doc.erb") => File.join("docs", resource_name + ".md"),
|
||||||
|
File.join("tests", "functional", "inspec-resource-test-template.erb") => File.join("tests", "functional", resource_name + "_test.rb"),
|
||||||
|
File.join("tests", "unit", "inspec-resource-test-template.erb") => File.join("tests", "unit", resource_name + "_test.rb"),
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def resource_vars_from_opts
|
||||||
|
options[:class_name] ||= options[:resource_name]
|
||||||
|
|
||||||
|
if options[:prompt] && ui.interactive?
|
||||||
|
options.dup.merge(prompt_for_options)
|
||||||
|
else
|
||||||
|
ui.error("You requested interactive prompting for the template variables, but this does not seem to be an interactive terminal.")
|
||||||
|
ui.exit(:usage_error)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def prompt_for_options # rubocop: disable Metrics/AbcSize
|
||||||
|
option_defs = self.class.all_commands["resource"].options
|
||||||
|
options_order = {
|
||||||
|
supports_platform: {},
|
||||||
|
description: {},
|
||||||
|
class_name: {},
|
||||||
|
}
|
||||||
|
|
||||||
|
options_order.each do |opt_name, _|
|
||||||
|
opt_def = option_defs[opt_name]
|
||||||
|
options[opt_name] = ui.prompt.ask("Enter " + opt_def.description + ":", default: options[opt_name])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
BIN
lib/plugins/inspec-init/templates/resources/.DS_Store
vendored
Normal file
BIN
lib/plugins/inspec-init/templates/resources/.DS_Store
vendored
Normal file
Binary file not shown.
|
@ -0,0 +1,74 @@
|
||||||
|
+++
|
||||||
|
title = "<%= resource_name %> resource"
|
||||||
|
draft = false
|
||||||
|
gh_repo = "inspec"
|
||||||
|
platform = "<%= supports_platform %>"
|
||||||
|
|
||||||
|
[menu]
|
||||||
|
[menu.inspec]
|
||||||
|
title = "<%= resource_name %>"
|
||||||
|
identifier = "inspec/resources/os/<%= resource_name %>.md <%= resource_name %> resource"
|
||||||
|
parent = "inspec/resources/os"
|
||||||
|
+++
|
||||||
|
|
||||||
|
Use the `<%= resource_name %>` Chef InSpec audit resource to test the ...
|
||||||
|
|
||||||
|
|
||||||
|
## Availability
|
||||||
|
|
||||||
|
### Installation
|
||||||
|
|
||||||
|
This resource is distributed along with Chef InSpec itself. You can use it automatically.
|
||||||
|
|
||||||
|
## Syntax
|
||||||
|
|
||||||
|
An `<%= resource_name %>` Chef InSpec audit resource ...
|
||||||
|
|
||||||
|
describe <%= resource_name %> do
|
||||||
|
its('property') { should some_matcher 'value' }
|
||||||
|
end
|
||||||
|
where
|
||||||
|
|
||||||
|
- `'property'` is some property of specific this resource
|
||||||
|
- `'value'` is the value of ...
|
||||||
|
|
||||||
|
## Properties
|
||||||
|
|
||||||
|
- Properties of the resources: `property1`, `property2`
|
||||||
|
|
||||||
|
## Matchers
|
||||||
|
<%# Correct the url %>
|
||||||
|
For a full list of available matchers, please visit our [matchers page](/inspec/matchers/). The matcher of the resources are: `matcher1`, `matcher2`
|
||||||
|
|
||||||
|
### matcher1
|
||||||
|
|
||||||
|
The `matcher1` matcher tests the ...:
|
||||||
|
|
||||||
|
it { should matcher1("Value") }
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
The following examples show how to use this Chef InSpec audit resource.
|
||||||
|
|
||||||
|
### Example 1
|
||||||
|
|
||||||
|
`property1` returns ...
|
||||||
|
|
||||||
|
describe <%= resource_name %> do
|
||||||
|
its('property2') { should eq 'some_expected_value' }
|
||||||
|
end
|
||||||
|
|
||||||
|
### Example 2
|
||||||
|
|
||||||
|
<%# Make matcher and property aligns %>
|
||||||
|
`matcher1` does the ...
|
||||||
|
|
||||||
|
describe <%= resource_name %> do
|
||||||
|
it { should matcher1("Value") }
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
{{< note >}}
|
||||||
|
|
||||||
|
Important information regarding the use of this resource. Replace it with appropriate message.
|
||||||
|
|
||||||
|
{{< /note >}}
|
BIN
lib/plugins/inspec-init/templates/resources/libraries/.DS_Store
vendored
Normal file
BIN
lib/plugins/inspec-init/templates/resources/libraries/.DS_Store
vendored
Normal file
Binary file not shown.
|
@ -0,0 +1,54 @@
|
||||||
|
# Uncomment the below line to add required gems and files by the resource
|
||||||
|
# require ""
|
||||||
|
# require_relative ""
|
||||||
|
|
||||||
|
# Change module if required
|
||||||
|
module Inspec::Resources
|
||||||
|
class <%= class_name %> < Inspec.resource(1)
|
||||||
|
name "<%= resource_name %>"
|
||||||
|
|
||||||
|
# Restrict to only run on the below platforms (if none were given, all OS's supported)
|
||||||
|
supports platform: "<%= supports_platform %>"
|
||||||
|
|
||||||
|
desc "<%= description %>"
|
||||||
|
|
||||||
|
example <<~EXAMPLE
|
||||||
|
describe "<%= resource_name %>" do
|
||||||
|
its() { }
|
||||||
|
end
|
||||||
|
describe "<%= resource_name %>" do
|
||||||
|
it { }
|
||||||
|
end
|
||||||
|
EXAMPLE
|
||||||
|
|
||||||
|
# Resource initialization
|
||||||
|
def initialize
|
||||||
|
return skip_resource "The `<%= resource_name %>` resource is not yet available on your OS." unless inspec.os.<%= supports_platform %>?
|
||||||
|
# Initialize required path/params/configs
|
||||||
|
end
|
||||||
|
|
||||||
|
def resource_matcher?()
|
||||||
|
return "positive or negative expectations specific to this resource instance"
|
||||||
|
end
|
||||||
|
|
||||||
|
def resource_property
|
||||||
|
# Implementation of the property specific to this resource
|
||||||
|
end
|
||||||
|
|
||||||
|
def resource_id
|
||||||
|
# replace value specific unique to this individual resource instance
|
||||||
|
return "something special"
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_s
|
||||||
|
"<%= resource_name %>"
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
# Methods to help the resource's public methods
|
||||||
|
def resource_helper_method()
|
||||||
|
# Add
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1 @@
|
||||||
|
# Some functional test code
|
|
@ -0,0 +1 @@
|
||||||
|
# Some unit test code
|
Loading…
Reference in a new issue