Skip to content

Commit

Permalink
Merge pull request #10 from DavidS/fix-params-and-properties
Browse files Browse the repository at this point in the history
Fix params and properties
  • Loading branch information
DavidS authored Jan 23, 2018
2 parents 4721e31 + cbac405 commit e28eb78
Show file tree
Hide file tree
Showing 5 changed files with 301 additions and 34 deletions.
3 changes: 3 additions & 0 deletions .codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
# disable comments, info can be gotten from the pr status updates, and the comment editing spams the hipchat notifications
comment: false
38 changes: 24 additions & 14 deletions lib/puppet/resource_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,14 @@ def my_provider
case options[:type]
when 'String'
# require any string value
newvalues %r{} do
end
Puppet::ResourceApi.def_newvalues(self, param_or_property, %r{})
# rubocop:disable Lint/BooleanSymbol
when 'Boolean'
newvalues 'true', 'false', :true, :false, true, false
Puppet::ResourceApi.def_newvalues(self, param_or_property, 'true', 'false')
aliasvalue true, 'true'
aliasvalue false, 'false'
aliasvalue :true, 'true'
aliasvalue :false, 'false'

munge do |v|
case v
Expand All @@ -119,31 +122,27 @@ def my_provider
end
# rubocop:enable Lint/BooleanSymbol
when 'Integer'
newvalues %r{^-?\d+$}
Puppet::ResourceApi.def_newvalues(self, param_or_property, %r{^-?\d+$})
munge do |v|
Puppet::Pops::Utils.to_n(v)
end
when 'Float', 'Numeric'
newvalues Puppet::Pops::Patterns::NUMERIC
Puppet::ResourceApi.def_newvalues(self, param_or_property, Puppet::Pops::Patterns::NUMERIC)
munge do |v|
Puppet::Pops::Utils.to_n(v)
end
when 'Enum[present, absent]'
newvalues :absent, :present
Puppet::ResourceApi.def_newvalues(self, param_or_property, :absent, :present)
when 'Variant[Pattern[/\A(0x)?[0-9a-fA-F]{8}\Z/], Pattern[/\A(0x)?[0-9a-fA-F]{16}\Z/], Pattern[/\A(0x)?[0-9a-fA-F]{40}\Z/]]'
# the namevar needs to be a Parameter, which only has newvalue*s*
newvalues(%r{\A(0x)?[0-9a-fA-F]{8}\Z}, %r{\A(0x)?[0-9a-fA-F]{16}\Z}, %r{\A(0x)?[0-9a-fA-F]{40}\Z})
Puppet::ResourceApi.def_newvalues(self, param_or_property, %r{\A(0x)?[0-9a-fA-F]{8}\Z}, %r{\A(0x)?[0-9a-fA-F]{16}\Z}, %r{\A(0x)?[0-9a-fA-F]{40}\Z})
when 'Optional[String]'
newvalues(%r{}, :undef) do
end
Puppet::ResourceApi.def_newvalues(self, param_or_property, %r{}, :undef)
when 'Variant[Stdlib::Absolutepath, Pattern[/\A(https?|ftp):\/\//]]'
# TODO: this is wrong, but matches original implementation
[/^\//, /\A(https?|ftp):\/\//].each do |v| # rubocop:disable Style/RegexpLiteral
newvalues v do
end
end
Puppet::ResourceApi.def_newvalues(self, param_or_property, /^\//, /\A(https?|ftp):\/\//) # rubocop:disable Style/RegexpLiteral
when 'Pattern[/\A((hkp|http|https):\/\/)?([a-z\d])([a-z\d-]{0,61}\.)+[a-z\d]+(:\d{2,5})?$/]'
newvalues(/\A((hkp|http|https):\/\/)?([a-z\d])([a-z\d-]{0,61}\.)+[a-z\d]+(:\d{2,5})?$/) # rubocop:disable Style/RegexpLiteral
Puppet::ResourceApi.def_newvalues(self, param_or_property, /\A((hkp|http|https):\/\/)?([a-z\d])([a-z\d-]{0,61}\.)+[a-z\d]+(:\d{2,5})?$/) # rubocop:disable Style/RegexpLiteral
else
raise Puppet::DevError, "Datatype #{options[:type]} is not yet supported in this prototype"
end
Expand Down Expand Up @@ -228,4 +227,15 @@ def load_provider(type_name)
def self.class_name_from_type_name(type_name)
type_name.to_s.split('_').map(&:capitalize).join
end

# Add the value to `this` property or param, depending on whether param_or_property is `:newparam`, or `:newproperty`
def self.def_newvalues(this, param_or_property, *values)
if param_or_property == :newparam
this.newvalues(*values)
else
values.each do |v|
this.newvalue(v) {}
end
end
end
end
21 changes: 17 additions & 4 deletions spec/acceptance/device_spec.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
require 'puppet/application/device'
require 'spec_helper'
require 'tempfile'

RSpec.describe 'exercising a device provider' do
let(:common_args) { '--verbose --trace --modulepath spec/fixtures' }

before(:each) { skip 'No device --apply in the puppet gems yet' if ENV['PUPPET_GEM_VERSION'] }

describe 'using `puppet resource`' do
it 'reads resources from the target system'
it 'manages resources on the target system'
it 'reads resources from the target system' do
stdout_str, status = Open3.capture2e("puppet resource #{common_args} device_provider")
if Gem::Version.new(RUBY_VERSION.dup) < Gem::Version.new('2.4')
expect(stdout_str.strip).to eq 'DL is deprecated, please use Fiddle'
else
expect(stdout_str.strip).to be_empty
end
expect(status).to eq 0
end
it 'manages resources on the target system' do
stdout_str, status = Open3.capture2e("puppet resource #{common_args} device_provider foo ensure=present")
expect(stdout_str).to match %r{Notice: /Device_provider\[foo\]/ensure: defined 'ensure' as 'present'}
expect(status).to eq 0
end
end

describe 'using `puppet device`' do
let(:common_args) { '--verbose --trace --target the_node --modulepath spec/fixtures' }
let(:common_args) { super() + ' --target the_node' }
let(:device_conf) { Tempfile.new('device.conf') }
let(:device_conf_content) do
<<DEVICE_CONF
Expand Down
144 changes: 129 additions & 15 deletions spec/fixtures/test_device/lib/puppet/type/device_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,140 @@
Puppet::ResourceApi.register_type(
name: 'device_provider',
docs: 'A example/test provider for device support',
attributes: {
ensure: {
type: 'Enum[present, absent]',
desc: 'Whether this apt key should be present or absent on the target system.',
default: 'present',
},
name: {
type: 'String',
desc: 'The name of the resource.',
attributes: {
name: {
type: 'String',
behaviour: :namevar,
desc: 'the title',
},
string: {
type: 'String',
desc: 'a string parameter',
default: 'default value',
},
boolean: {
type: 'Boolean',
desc: 'a boolean parameter',
},
integer: {
type: 'Integer',
desc: 'an integer parameter',
},
float: {
type: 'Float',
desc: 'a floating point parameter',
},
ensure: {
type: 'Enum[present, absent]',
desc: 'a ensure parameter',
},
variant_pattern: {
type: 'Variant[Pattern[/\A(0x)?[0-9a-fA-F]{8}\Z/], Pattern[/\A(0x)?[0-9a-fA-F]{16}\Z/], Pattern[/\A(0x)?[0-9a-fA-F]{40}\Z/]]',
desc: 'a pattern parameter',
},
path: {
type: 'Variant[Stdlib::Absolutepath, Pattern[/\A(https?|ftp):\/\//]]',
desc: 'a path or URL parameter',
},
attribute: {
type: 'String',
desc: 'An attribute.',
url: {
type: 'Pattern[/\A((hkp|http|https):\/\/)?([a-z\d])([a-z\d-]{0,61}\.)+[a-z\d]+(:\d{2,5})?$/]',
desc: 'a hkp or http(s) url parameter',
},
parameter: {
optional_string: {
type: 'Optional[String]',
desc: 'a optional string parameter',
},
string_param: {
type: 'String',
desc: 'A parameter',
desc: 'a string parameter',
default: 'default value',
behaviour: :parameter,
},
boolean_param: {
type: 'Boolean',
desc: 'a boolean parameter',
behaviour: :parameter,
},
integer_param: {
type: 'Integer',
desc: 'an integer parameter',
behaviour: :parameter,
},
float_param: {
type: 'Float',
desc: 'a floating point parameter',
behaviour: :parameter,
},
ensure_param: {
type: 'Enum[present, absent]',
desc: 'a ensure parameter',
behaviour: :parameter,
},
variant_pattern_param: {
type: 'Variant[Pattern[/\A(0x)?[0-9a-fA-F]{8}\Z/], Pattern[/\A(0x)?[0-9a-fA-F]{16}\Z/], Pattern[/\A(0x)?[0-9a-fA-F]{40}\Z/]]',
desc: 'a pattern parameter',
behaviour: :parameter,
},
path_param: {
type: 'Variant[Stdlib::Absolutepath, Pattern[/\A(https?|ftp):\/\//]]',
desc: 'a path or URL parameter',
behaviour: :parameter,
},
url_param: {
type: 'Pattern[/\A((hkp|http|https):\/\/)?([a-z\d])([a-z\d-]{0,61}\.)+[a-z\d]+(:\d{2,5})?$/]',
desc: 'a hkp or http(s) url parameter',
behaviour: :parameter,
},
optional_string_param: {
type: 'Optional[String]',
desc: 'a optional string parameter',
behaviour: :parameter,
},
string_ro: {
type: 'String',
desc: 'a string readonly',
default: 'default value',
behaviour: :readonly,
},
boolean_ro: {
type: 'Boolean',
desc: 'a boolean readonly',
behaviour: :readonly,
},
integer_ro: {
type: 'Integer',
desc: 'an integer readonly',
behaviour: :readonly,
},
float_ro: {
type: 'Float',
desc: 'a floating point readonly',
behaviour: :readonly,
},
ensure_ro: {
type: 'Enum[present, absent]',
desc: 'a ensure readonly',
behaviour: :readonly,
},
variant_pattern_ro: {
type: 'Variant[Pattern[/\A(0x)?[0-9a-fA-F]{8}\Z/], Pattern[/\A(0x)?[0-9a-fA-F]{16}\Z/], Pattern[/\A(0x)?[0-9a-fA-F]{40}\Z/]]',
desc: 'a pattern readonly',
behaviour: :readonly,
},
path_ro: {
type: 'Variant[Stdlib::Absolutepath, Pattern[/\A(https?|ftp):\/\//]]',
desc: 'a path or URL readonly',
behaviour: :readonly,
},
url_ro: {
type: 'Pattern[/\A((hkp|http|https):\/\/)?([a-z\d])([a-z\d-]{0,61}\.)+[a-z\d]+(:\d{2,5})?$/]',
desc: 'a hkp or http(s) url readonly',
behaviour: :readonly,
},
optional_string_ro: {
type: 'Optional[String]',
desc: 'a optional string readonly',
behaviour: :readonly,
},
},
features: ['remote_resource'],
)
Loading

0 comments on commit e28eb78

Please sign in to comment.