-
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.
(FM-8081) Implement
pdk new transport
This change implements a `pdk new transport` command for using the new Resource API transports. It is closely modelled on the `pdk new provider` command.
- Loading branch information
Showing
9 changed files
with
265 additions
and
6 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,25 @@ | ||
module PDK::CLI | ||
@new_transport_cmd = @new_cmd.define_command do | ||
name 'transport' | ||
usage _('transport [options] <name>') | ||
summary _('[experimental] Create a new ruby transport named <name> using given options') | ||
|
||
run do |opts, args, _cmd| | ||
PDK::CLI::Util.ensure_in_module! | ||
|
||
transport_name = args[0] | ||
module_dir = Dir.pwd | ||
|
||
if transport_name.nil? || transport_name.empty? | ||
puts command.help | ||
exit 1 | ||
end | ||
|
||
unless Util::OptionValidator.valid_transport_name?(transport_name) | ||
raise PDK::CLI::ExitWithError, _("'%{name}' is not a valid transport name") % { name: transport_name } | ||
end | ||
|
||
PDK::Generate::Transport.new(module_dir, transport_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
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,87 @@ | ||
require 'pdk/generate/puppet_object' | ||
|
||
module PDK | ||
module Generate | ||
class Transport < PuppetObject | ||
OBJECT_TYPE = :transport | ||
|
||
# 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, | ||
transport_class: Transport.class_name_from_object_name(object_name), | ||
} | ||
|
||
data | ||
end | ||
|
||
def raise_precondition_error(error) | ||
raise PDK::CLI::ExitWithError, _('%{error}: Creating a transport needs some local configuration in your module.' \ | ||
' Please follow the docs at https://github.com/puppetlabs/puppet-resource_api#getting-started.') % { error: error } | ||
end | ||
|
||
def check_preconditions | ||
super | ||
# These preconditions can be removed once the pdk-templates are carrying the puppet-resource_api gem by default, and have switched | ||
# the default mock_with value. | ||
sync_path = PDK::Util.find_upwards('.sync.yml') | ||
if sync_path.nil? | ||
raise_precondition_error(_('.sync.yml not found')) | ||
end | ||
sync = YAML.load_file(sync_path) | ||
if !sync.is_a? Hash | ||
raise_precondition_error(_('.sync.yml contents is not a Hash')) | ||
elsif !sync.key? 'Gemfile' | ||
raise_precondition_error(_('Gemfile configuration not found')) | ||
elsif !sync['Gemfile'].key? 'optional' | ||
raise_precondition_error(_('Gemfile.optional configuration not found')) | ||
elsif !sync['Gemfile']['optional'].key? ':development' | ||
raise_precondition_error(_('Gemfile.optional.:development configuration not found')) | ||
elsif sync['Gemfile']['optional'][':development'].none? { |g| g['gem'] == 'puppet-resource_api' } | ||
raise_precondition_error(_('puppet-resource_api not found in the Gemfile config')) | ||
elsif !sync.key? 'spec/spec_helper.rb' | ||
raise_precondition_error(_('spec/spec_helper.rb configuration not found')) | ||
elsif !sync['spec/spec_helper.rb'].key? 'mock_with' | ||
raise_precondition_error(_('spec/spec_helper.rb.mock_with configuration not found')) | ||
elsif !sync['spec/spec_helper.rb']['mock_with'] == ':rspec' | ||
raise_precondition_error(_('spec/spec_helper.rb.mock_with not set to \':rspec\'')) | ||
end | ||
end | ||
|
||
# @return [String] the path where the new transport will be written. | ||
def target_object_path | ||
@target_object_path ||= File.join(module_dir, 'lib', 'puppet', 'transport', object_name) + '.rb' | ||
end | ||
|
||
# @return [String] the path where the new schema will be written. | ||
def target_type_path | ||
@target_type_path ||= File.join(module_dir, 'lib', 'puppet', 'transport', 'schema', object_name) + '.rb' | ||
end | ||
|
||
# @return [String] the path where the deviceshim for the transport will be written. | ||
def target_device_path | ||
@target_device_path ||= File.join(module_dir, 'lib', 'puppet', 'util', 'network_device', object_name, 'device.rb') | ||
end | ||
|
||
# @return [String] the path where the tests for the new transport | ||
# will be written. | ||
def target_spec_path | ||
@target_spec_path ||= File.join(module_dir, 'spec', 'unit', 'puppet', 'transport', object_name) + '_spec.rb' | ||
end | ||
|
||
# @return [String] the path where the tests for the new schema will be written. | ||
def target_type_spec_path | ||
@target_type_spec_path ||= File.join(module_dir, 'spec', 'unit', 'puppet', 'transport', 'schema', 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
require 'spec_helper_acceptance' | ||
|
||
describe 'pdk new transport', module_command: true do | ||
def update_module! | ||
command('pdk update --force').exit_status | ||
end | ||
|
||
context 'when run inside of a module' do | ||
include_context 'in a new module', 'new_transport' | ||
|
||
context 'with the Resource API configured in .sync.yml' do | ||
before(:all) do | ||
File.open('.sync.yml', 'w') do |f| | ||
f.write(<<SYNC) | ||
--- | ||
Gemfile: | ||
optional: | ||
':development': | ||
- gem: 'puppet-resource_api' | ||
spec/spec_helper.rb: | ||
mock_with: ':rspec' | ||
SYNC | ||
end | ||
update_module! | ||
end | ||
|
||
describe command('pdk new transport test_transport') 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 have_no_output } | ||
its(:exit_status) { is_expected.to eq(0) } | ||
|
||
describe file(File.join('lib', 'puppet', 'transport')) do | ||
it { is_expected.to be_directory } | ||
end | ||
|
||
describe file(File.join('lib', 'puppet', 'transport', 'test_transport.rb')) do | ||
it { is_expected.to be_file } | ||
its(:content) { is_expected.to match(%r{class TestTransport}) } | ||
end | ||
|
||
describe file(File.join('lib', 'puppet', 'transport', 'schema')) do | ||
it { is_expected.to be_directory } | ||
end | ||
|
||
describe file(File.join('lib', 'puppet', 'transport', 'schema', 'test_transport.rb')) do | ||
it { is_expected.to be_file } | ||
its(:content) { is_expected.to match(%r{Puppet::ResourceApi.register_transport}) } | ||
its(:content) { is_expected.to match(%r{name: 'test_transport'}) } | ||
end | ||
|
||
describe file(File.join('lib', 'puppet', 'util', 'network_device', 'test_transport')) do | ||
it { is_expected.to be_directory } | ||
end | ||
|
||
describe file(File.join('lib', 'puppet', 'util', 'network_device', 'test_transport', 'device.rb')) do | ||
it { is_expected.to be_file } | ||
its(:content) { is_expected.to match(%r{Puppet::Util::NetworkDevice::Test_transport}) } # puppet defaults to `capitalize`ing instead of snake case like ruby would prefer | ||
its(:content) { is_expected.to match(%r{super\('test_transport', url_or_config\)}) } | ||
end | ||
|
||
describe file(File.join('spec', 'unit', 'puppet', 'transport')) do | ||
it { is_expected.to be_directory } | ||
end | ||
|
||
describe file(File.join('spec', 'unit', 'puppet', 'transport', 'test_transport_spec.rb')) do | ||
it { is_expected.to be_file } | ||
its(:content) { is_expected.to match(%r{RSpec.describe Puppet::Transport::TestTransport do}) } | ||
end | ||
|
||
describe file(File.join('spec', 'unit', 'puppet', 'transport', 'schema')) do | ||
it { is_expected.to be_directory } | ||
end | ||
|
||
describe file(File.join('spec', 'unit', 'puppet', 'transport', 'schema', 'test_transport_spec.rb')) do | ||
it { is_expected.to be_file } | ||
its(:content) { is_expected.to match(%r{RSpec.describe 'the test_transport transport' do}) } | ||
end | ||
|
||
describe command('pdk validate ruby') do | ||
its(:stdout) { is_expected.to have_no_output } | ||
its(:stderr) { is_expected.to match(%r{using ruby \d+\.\d+\.\d+}i) } | ||
its(:stderr) { is_expected.to match(%r{using puppet \d+\.\d+\.\d+}i) } | ||
its(:exit_status) { is_expected.to eq(0) } | ||
end | ||
|
||
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 | ||
|
||
context 'without a .sync.yml' do | ||
before(:all) do | ||
FileUtils.rm_f('.sync.yml') | ||
update_module! | ||
end | ||
|
||
describe command('pdk new transport test_transport2') do | ||
its(:stderr) { is_expected.to match(%r{pdk \(ERROR\): .sync.yml not found}i) } | ||
its(:stdout) { is_expected.to have_no_output } | ||
its(:exit_status) { is_expected.not_to eq(0) } | ||
end | ||
end | ||
|
||
context 'with invalid .sync.yml' do | ||
before(:all) do | ||
File.open('.sync.yml', 'w') do |f| | ||
f.write(<<SYNC) | ||
--- | ||
Gemfile: | ||
optional: | ||
':test': | ||
- gem: 'puppet-resource_api' | ||
spec/spec_helper.rb: | ||
mock_with: ':rspec' | ||
SYNC | ||
end | ||
update_module! | ||
end | ||
|
||
describe command('pdk new transport test_transport2') do | ||
its(:stderr) { is_expected.to match(%r{pdk \(ERROR\): Gemfile.optional.:development configuration not found}i) } | ||
its(:stdout) { is_expected.to have_no_output } | ||
its(:exit_status) { is_expected.not_to eq(0) } | ||
end | ||
end | ||
end | ||
end |