diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2017-04-01 19:25:57 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-04-01 19:25:57 -0700 |
commit | 749174ba96d07d24a1ff44fe2023ee5780ceb716 (patch) | |
tree | dc7c64cb289ac4c130474bcdd56c27152e15a439 | |
parent | e5770a6e3f87eac0cccc7a044f00e217ae5ddaac (diff) | |
parent | 22c6c8e760cbfe2a562f6226b4847cffc09f055f (diff) | |
download | chef-749174ba96d07d24a1ff44fe2023ee5780ceb716.tar.gz |
Merge pull request #5984 from chef/lcg/notifying-array-resources
Chef-13: fix notifying array resources
-rw-r--r-- | Gemfile.lock | 20 | ||||
-rw-r--r-- | RELEASE_NOTES.md | 14 | ||||
-rw-r--r-- | lib/chef/deprecated.rb | 10 | ||||
-rw-r--r-- | lib/chef/dsl/declare_resource.rb | 2 | ||||
-rw-r--r-- | lib/chef/resource_collection/resource_set.rb | 46 | ||||
-rw-r--r-- | spec/integration/recipes/notifies_spec.rb | 29 | ||||
-rw-r--r-- | spec/unit/resource_collection/resource_set_spec.rb | 16 | ||||
-rw-r--r-- | spec/unit/resource_collection_spec.rb | 3 |
8 files changed, 111 insertions, 29 deletions
diff --git a/Gemfile.lock b/Gemfile.lock index 5ce91d96ed..d5a6223cd7 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,6 +1,6 @@ GIT remote: https://github.com/chef/chef-server - revision: e530dd27a696ec154394f360411ae874adc4e3af + revision: 867fb0245d5c162195cdf33f9e923c732cf512c3 specs: oc-chef-pedant (2.2.0) activesupport (>= 4.2.7.1, < 6.0) @@ -54,7 +54,7 @@ GIT GIT remote: https://github.com/poise/halite.git - revision: 09c4ef7c6b8188ec2b9a929de8d9f6aa3f620553 + revision: b9b3f61682abe1c656f57b7edbbf43d918c5c16b specs: halite (1.5.1.pre) bundler @@ -64,7 +64,7 @@ GIT GIT remote: https://github.com/poise/poise-boiler.git - revision: d547f77ca7335af7943f681e1d0dd68c825cf429 + revision: 40763eaded9384b1d8d3e3db6f883613b0a3ebe2 specs: poise-boiler (1.14.1.pre) bundler @@ -97,7 +97,7 @@ GIT GIT remote: https://github.com/poise/poise.git - revision: c42a55afe2ff897801b274eb47594135ac856b12 + revision: bf1ae70fda2486964397acd04221c968c30dd092 specs: poise (2.7.3.pre) halite (~> 1.0) @@ -205,13 +205,13 @@ GEM mixlib-cli (~> 1.4) artifactory (2.8.1) ast (2.3.0) - aws-sdk (2.8.14) - aws-sdk-resources (= 2.8.14) - aws-sdk-core (2.8.14) + aws-sdk (2.9.1) + aws-sdk-resources (= 2.9.1) + aws-sdk-core (2.9.1) aws-sigv4 (~> 1.0) jmespath (~> 1.0) - aws-sdk-resources (2.8.14) - aws-sdk-core (= 2.8.14) + aws-sdk-resources (2.9.1) + aws-sdk-core (= 2.9.1) aws-sigv4 (1.0.0) backports (3.7.0) binding_of_caller (0.7.2) @@ -477,7 +477,7 @@ GEM net-ssh (>= 2.7, < 5.0) net-telnet sfl - stove (5.0.0) + stove (5.1.0) chef-api (~> 0.5) logify (~> 0.2) syslog-logger (1.6.8) diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 3f3d8c44bb..81fa6c6e05 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -164,3 +164,17 @@ match when given partial strings (available since Chef 11). The `knife ssh` sea search API matches in both cases. The node search fuzzifier has also been extracted out to a `fuzz` option to Chef::Search::Query for re-use elsewhere. +### Resources which later modify their name during creation will have their name changed on the ResourceCollection and notifications + +```ruby +some_resource "name_one" do + name "name_two" +end +``` + +The fix for sending notifications to multipackage resources involved changing the API which inserts resources into the resource collection slightly +so that it no longer directly takes the string which is typed into the DSL but reads the (possibly coerced) name off of the resource after it is +built. The end result is that the above resource will be named `some_resource[name_two]` instead of `some_resource[name_one]`. Note that setting +the name (*not* the `name_property`, but actually renaming the resource) is very uncommon. The fix is to simply name the resource correctly in +the first place (`some_resource "name_two" do ...`) + diff --git a/lib/chef/deprecated.rb b/lib/chef/deprecated.rb index 461f65225b..d4db0813c7 100644 --- a/lib/chef/deprecated.rb +++ b/lib/chef/deprecated.rb @@ -218,6 +218,16 @@ class Chef end end + class MultiresourceMatch < Base + def id + 16 + end + + def target + "multiresource_match.html" + end + end + # id 3694 was deleted class Generic < Base diff --git a/lib/chef/dsl/declare_resource.rb b/lib/chef/dsl/declare_resource.rb index ac3776c92f..6869e77eca 100644 --- a/lib/chef/dsl/declare_resource.rb +++ b/lib/chef/dsl/declare_resource.rb @@ -241,7 +241,7 @@ class Chef resource = build_resource(type, name, created_at: created_at, &resource_attrs_block) - run_context.resource_collection.insert(resource, resource_type: type, instance_name: name) + run_context.resource_collection.insert(resource, resource_type: resource.declared_type, instance_name: resource.name) resource end diff --git a/lib/chef/resource_collection/resource_set.rb b/lib/chef/resource_collection/resource_set.rb index 111d23dc09..6ff29247a0 100644 --- a/lib/chef/resource_collection/resource_set.rb +++ b/lib/chef/resource_collection/resource_set.rb @@ -161,27 +161,39 @@ class Chef end def find_resource_by_string(arg) - results = Array.new - case arg - when MULTIPLE_RESOURCE_MATCH - resource_type = $1 - arg =~ /^.+\[(.+)\]$/ - resource_list = $1 - resource_list.split(",").each do |instance_name| - results << lookup(create_key(resource_type, instance_name)) - end - when SINGLE_RESOURCE_MATCH + begin + if arg =~ SINGLE_RESOURCE_MATCH resource_type = $1 name = $2 - results << lookup(create_key(resource_type, name)) - when NAMELESS_RESOURCE_MATCH - resource_type = $1 - name = "" - results << lookup(create_key(resource_type, name)) + return [ lookup(create_key(resource_type, name)) ] + end + rescue Chef::Exceptions::ResourceNotFound => e + if arg =~ MULTIPLE_RESOURCE_MATCH + begin + results = Array.new + resource_type = $1 + arg =~ /^.+\[(.+)\]$/ + resource_list = $1 + resource_list.split(",").each do |instance_name| + results << lookup(create_key(resource_type, instance_name)) + end + Chef.deprecated(:multiresource_match, "The resource_collection multi-resource syntax is deprecated") + return results + rescue Chef::Exceptions::ResourceNotFound + raise e + end else - raise ArgumentError, "Bad string format #{arg}, you must have a string like resource_type[name]!" + raise e + end end - results + + if arg =~ NAMELESS_RESOURCE_MATCH + resource_type = $1 + name = "" + return [ lookup(create_key(resource_type, name)) ] + end + + raise ArgumentError, "Bad string format #{arg}, you must have a string like resource_type[name]!" end end end diff --git a/spec/integration/recipes/notifies_spec.rb b/spec/integration/recipes/notifies_spec.rb index 6f6a0f06b0..b008e4ade7 100644 --- a/spec/integration/recipes/notifies_spec.rb +++ b/spec/integration/recipes/notifies_spec.rb @@ -362,4 +362,33 @@ EOM result.error! end end + + when_the_repository "has resources that have arrays as the name" do + before do + directory "cookbooks/x" do + file "recipes/default.rb", <<-EOM + log [ "a", "b" ] do + action :nothing + end + + log "doit" do + notifies :write, "log[a, b]" + end + EOM + end + end + + it "notifying the resource should work" do + file "config/client.rb", <<EOM +local_mode true +cookbook_path "#{path_to('cookbooks')}" +log_level :warn +EOM + + result = shell_out("#{chef_client} -c \"#{path_to('config/client.rb')}\" --no-color -F doc -o 'x::default'", :cwd => chef_dir) + expect(result.stdout).to match /\* log\[a, b\] action write/ + result.error! + end + + end end diff --git a/spec/unit/resource_collection/resource_set_spec.rb b/spec/unit/resource_collection/resource_set_spec.rb index ecf3e2707f..9955bc2bea 100644 --- a/spec/unit/resource_collection/resource_set_spec.rb +++ b/spec/unit/resource_collection/resource_set_spec.rb @@ -27,6 +27,7 @@ describe Chef::ResourceCollection::ResourceSet do let(:zen_master) { Chef::Resource::ZenMaster.new(zen_master_name) } let(:zen_master2) { Chef::Resource::ZenMaster.new(zen_master2_name) } let(:zen_follower) { Chef::Resource::ZenFollower.new(zen_follower_name) } + let(:zen_array) { Chef::Resource::ZenMaster.new( [ zen_master_name, zen_master2_name ]) } describe "initialize" do it "should return a Chef::ResourceSet" do @@ -113,13 +114,27 @@ describe Chef::ResourceCollection::ResourceSet do end it "should find resources by strings of zen_master[a,b]" do + Chef::Config[:treat_deprecation_warnings_as_errors] = false collection.insert_as(zen_master) collection.insert_as(zen_master2) check_by_names(collection.find("zen_master[#{zen_master_name},#{zen_master2_name}]"), zen_master_name, zen_master2_name) end + it "should find array names" do + collection.insert_as(zen_array) + expect(collection.find("zen_master[#{zen_master_name}, #{zen_master2_name}]")).to eql(zen_array) + end + + it "should favor array names over multi resource syntax" do + collection.insert_as(zen_master) + collection.insert_as(zen_master2) + collection.insert_as(zen_array) + expect(collection.find("zen_master[#{zen_master_name}, #{zen_master2_name}]")).to eql(zen_array) + end + it "should find resources by strings of zen_master[a,b] with custom names" do + Chef::Config[:treat_deprecation_warnings_as_errors] = false collection.insert_as(zen_master, :zzz, "name1") collection.insert_as(zen_master2, :zzz, "name2") check_by_names(collection.find("zzz[name1,name2]"), @@ -134,6 +149,7 @@ describe Chef::ResourceCollection::ResourceSet do end it "should find resources of multiple types by strings of zen_master[a] with custom names" do + Chef::Config[:treat_deprecation_warnings_as_errors] = false collection.insert_as(zen_master, :zzz, "name1") collection.insert_as(zen_master2, :zzz, "name2") collection.insert_as(zen_follower, :yyy, "name3") diff --git a/spec/unit/resource_collection_spec.rb b/spec/unit/resource_collection_spec.rb index 76038e51b9..c696572b13 100644 --- a/spec/unit/resource_collection_spec.rb +++ b/spec/unit/resource_collection_spec.rb @@ -1,7 +1,7 @@ # # Author:: Adam Jacob (<adam@chef.io>) # Author:: Christopher Walters (<cw@chef.io>) -# Copyright:: Copyright 2008-2016, Chef Software Inc. +# Copyright:: Copyright 2008-2017, Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -219,6 +219,7 @@ describe Chef::ResourceCollection do end it "should find resources by strings of zen_master[a,b]" do + Chef::Config[:treat_deprecation_warnings_as_errors] = false load_up_resources results = rc.resources("zen_master[monkey,dog]") expect(results.length).to eql(2) |