-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This implements `pdk new provider` command to support use of the Resource API in modules. This requires the template changes from puppetlabs/pdk-templates#13 See the README on https://github.com/puppetlabs/puppet-resource_api for details
- Loading branch information
Showing
8 changed files
with
174 additions
and
2 deletions.
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,30 @@ | ||
module PDK::CLI | ||
@new_provider_cmd = @new_cmd.define_command do | ||
name 'provider' | ||
usage _('provider [options] <name>') | ||
summary _('[experimental] Create a new ruby provider named <name> using given options') | ||
|
||
PDK::CLI.template_url_option(self) | ||
|
||
run do |opts, args, _cmd| | ||
PDK::CLI::Util.ensure_in_module!( | ||
message: _('Providers can only be created from inside a valid module directory.'), | ||
log_level: :info, | ||
) | ||
|
||
provider_name = args[0] | ||
module_dir = Dir.pwd | ||
|
||
if provider_name.nil? || provider_name.empty? | ||
puts command.help | ||
exit 1 | ||
end | ||
|
||
unless Util::OptionValidator.valid_provider_name?(provider_name) | ||
raise PDK::CLI::ExitWithError, _("'%{name}' is not a valid provider name") % { name: defined_type_name } | ||
end | ||
|
||
PDK::Generate::Provider.new(module_dir, provider_name, opts).run | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
require 'pdk/generate/puppet_object' | ||
|
||
module PDK | ||
module Generate | ||
class Provider < PuppetObject | ||
OBJECT_TYPE = :provider | ||
|
||
# Prepares the data needed to render the new defined type template. | ||
# | ||
# @return [Hash{Symbol => Object}] a hash of information that will be | ||
# provided to the defined type and defined type spec templates during | ||
# rendering. | ||
def template_data | ||
data = { | ||
name: object_name, | ||
provider_class: Provider.class_name_from_object_name(object_name), | ||
} | ||
|
||
data | ||
end | ||
|
||
# @return [String] the path where the new type will be written. | ||
def target_object_path | ||
@target_object_path ||= File.join(module_dir, 'lib', 'puppet', 'type', object_name) + '.rb' | ||
end | ||
|
||
# @return [String] the path where the new provider will be written. | ||
def target_addon_path | ||
@target_addon_path ||= File.join(module_dir, 'lib', 'puppet', 'provider', object_name, object_name) + '.rb' | ||
end | ||
|
||
# Calculates the path to the file where the tests for the new defined | ||
# type will be written. | ||
# | ||
# @return [String] the path where the tests for the new defined type | ||
# will be written. | ||
def target_spec_path | ||
@target_spec_path ||= File.join(module_dir, 'spec', 'unit', 'puppet', 'provider', object_name, object_name) + '_spec.rb' | ||
end | ||
|
||
# transform a object name into a ruby class name | ||
def self.class_name_from_object_name(object_name) | ||
object_name.to_s.split('_').map(&:capitalize).join | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
require 'spec_helper_acceptance' | ||
|
||
describe 'pdk new provider', module_command: true do | ||
shared_examples 'it creates a provider' do |name| | ||
describe command("pdk new provider #{name}") do | ||
its(:stderr) { is_expected.to match(%r{creating .* from template}i) } | ||
its(:stderr) { is_expected.not_to match(%r{WARN|ERR}) } | ||
its(:stdout) { is_expected.to match(%r{\A\Z}) } | ||
its(:exit_status) { is_expected.to eq(0) } | ||
end | ||
|
||
describe file('lib/puppet/type') do | ||
it { is_expected.to be_directory } | ||
end | ||
|
||
describe file("lib/puppet/provider/#{name}") do | ||
it { is_expected.to be_directory } | ||
end | ||
|
||
describe file("spec/unit/puppet/provider/#{name}") do | ||
it { is_expected.to be_directory } | ||
end | ||
|
||
context 'with the experimental `puppet-resource_api` in the Gemfile' do | ||
describe command('echo "gem \'puppet-resource_api\', git: \'https://github.com/puppetlabs/puppet-resource_api.git\'" >> Gemfile') do | ||
its(:exit_status) { is_expected.to eq(0) } | ||
end | ||
|
||
describe command('echo "RSpec.configure { |c| c.mock_with :rspec }" >> spec/spec_helper.rb') do | ||
its(:exit_status) { is_expected.to eq(0) } | ||
end | ||
|
||
describe command('rm -f Gemfile.lock') do | ||
its(:exit_status) { is_expected.to eq(0) } | ||
end | ||
|
||
context 'when validating the generated code' do | ||
describe command('pdk validate ruby') do | ||
its(:stdout) { is_expected.to be_empty } | ||
its(:stderr) { is_expected.to be_empty } | ||
its(:exit_status) { is_expected.to eq(0) } | ||
end | ||
end | ||
|
||
context 'when running the generated spec tests' do | ||
describe command('pdk test unit') do | ||
its(:stderr) { is_expected.to match(%r{0 failures}) } | ||
its(:stderr) { is_expected.not_to match(%r{no examples found}i) } | ||
its(:exit_status) { is_expected.to eq(0) } | ||
end | ||
end | ||
end | ||
end | ||
|
||
context 'in a fresh module' do | ||
include_context 'in a new module', 'new_provider' | ||
|
||
context 'when creating a provider' do | ||
it_behaves_like 'it creates a provider', 'test_provider' | ||
|
||
describe file('lib/puppet/type/test_provider.rb') do | ||
it { is_expected.to be_file } | ||
its(:content) { is_expected.to match(%r{Puppet::ResourceApi.register_type}) } | ||
its(:content) { is_expected.to match(%r{name: 'test_provider'}) } | ||
end | ||
|
||
describe file('lib/puppet/provider/test_provider/test_provider.rb') do | ||
it { is_expected.to be_file } | ||
its(:content) { is_expected.to match(%r{class Puppet::Provider::TestProvider::TestProvider}) } | ||
end | ||
|
||
describe file('spec/unit/puppet/provider/test_provider/test_provider_spec.rb') do | ||
it { is_expected.to be_file } | ||
its(:content) { is_expected.to match(%r{RSpec.describe Puppet::Provider::TestProvider::TestProvider do}) } | ||
end | ||
end | ||
end | ||
end |