diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2020-04-02 19:52:21 -0700 |
---|---|---|
committer | Lamont Granquist <lamont@scriptkiddie.org> | 2020-04-02 19:52:21 -0700 |
commit | 8dcf5be92642700ee3f2aedb83484bcf0e2c159c (patch) | |
tree | 3f44abd8bef8e75f05f57cd1213f14418aabed7a | |
parent | 333e0695117f8a518b8f2729bc3f97a5bd6560e4 (diff) | |
download | chef-8dcf5be92642700ee3f2aedb83484bcf0e2c159c.tar.gz |
Change name_property to be identity and not desired_state by default
If no other property is an identity property then the name_property is
the default identity property. The name_property is also marked as not
being desired state by default (the 'name' should never change).
Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
-rw-r--r-- | lib/chef/mixin/properties.rb | 14 | ||||
-rw-r--r-- | lib/chef/property.rb | 6 | ||||
-rw-r--r-- | lib/chef/resource.rb | 13 | ||||
-rw-r--r-- | lib/chef/resource_reporter.rb | 2 | ||||
-rw-r--r-- | spec/unit/property/state_spec.rb | 19 | ||||
-rw-r--r-- | spec/unit/resource_reporter_spec.rb | 8 |
6 files changed, 45 insertions, 17 deletions
diff --git a/lib/chef/mixin/properties.rb b/lib/chef/mixin/properties.rb index ecb589e015..94afa4640b 100644 --- a/lib/chef/mixin/properties.rb +++ b/lib/chef/mixin/properties.rb @@ -264,10 +264,22 @@ class Chef end result = properties.values.select(&:identity?) - result = [ properties[:name] ] if result.empty? + # if there are no other identity properites set, then the name_property becomes the identity, or + # failing that we use the actual name. + if result.empty? + result = name_property ? [ properties[name_property] ] : [ properties[:name] ] + end result end + # Returns the name of the name property. Returns nil if there is no name property. + # + # @return [Symbol] the name property for this resource + def name_property + p = properties.find { |n, p| p.name_property? } + p ? p.first : nil + end + def included(other) other.extend ClassMethods end diff --git a/lib/chef/property.rb b/lib/chef/property.rb index 336331b59f..a29286c21b 100644 --- a/lib/chef/property.rb +++ b/lib/chef/property.rb @@ -1,7 +1,7 @@ # # Author:: John Keiser <jkeiser@chef.io> # Copyright:: Copyright 2015-2016, John Keiser -# Copyright:: Copyright 2015-2020, Chef Software, Inc. +# Copyright:: Copyright 2015-2020, Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -141,6 +141,10 @@ class Chef raise ArgumentError, "A property cannot be both a name_property/name_attribute and have a default value. Use one or the other on property #{self}" end + if options[:name_property] + options[:desired_state] = false unless options.key?(:desired_state) + end + # Recursively freeze the default if it isn't a lazy value. unless default.is_a?(DelayedEvaluator) visitor = lambda do |obj| diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb index 5153dd4910..e6a33dc71a 100644 --- a/lib/chef/resource.rb +++ b/lib/chef/resource.rb @@ -497,11 +497,22 @@ class Chef def state_for_resource_reporter state = {} state_properties = self.class.state_properties +# p = state_properties.find { |p| p.name_property? } +# if p +# pp p +# raise "boom" +# end + state_properties.each do |property| - if property.identity? || property.is_set?(self) + if property.is_set?(self) state[property.name] = property.sensitive? ? "*sensitive value suppressed*" : send(property.name) end end +# identity_properties = self.class.identity_properties +# pp identity_properties +# identity_properties.each do |property| +# state[property.name] = property.sensitive? ? "*sensitive value suppressed*" : send(property.name) +# end state end diff --git a/lib/chef/resource_reporter.rb b/lib/chef/resource_reporter.rb index 246b2d5500..c8517dfc83 100644 --- a/lib/chef/resource_reporter.rb +++ b/lib/chef/resource_reporter.rb @@ -3,7 +3,7 @@ # Author:: Prajakta Purohit (prajakta@chef.io>) # Auther:: Tyler Cloke (<tyler@opscode.com>) # -# Copyright:: Copyright 2012-2019, Chef Software Inc. +# Copyright:: Copyright 2012-2020, Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/spec/unit/property/state_spec.rb b/spec/unit/property/state_spec.rb index a6428e8617..f120039f8b 100644 --- a/spec/unit/property/state_spec.rb +++ b/spec/unit/property/state_spec.rb @@ -285,8 +285,8 @@ describe "Chef::Resource#identity and #state" do it "identity when x is not defined returns the value of x" do expect(resource.identity).to eq "blah" end - it "state when x is not defined returns the value of x" do - expect(resource.state_for_resource_reporter).to eq(x: "blah") + it "the name property is not included in the desired state" do + expect(resource.state_for_resource_reporter).to eq({}) end end end @@ -332,12 +332,17 @@ describe "Chef::Resource#identity and #state" do end with_property ":x, name_property: true" do - # it "Unset values with name_property are included in state" do - # expect(resource.state_for_resource_reporter).to eq({ x: 'blah' }) - # end - it "Set values with name_property are included in state" do + it "Is by default the identity property if no other identity property is included" do + expect(resource.identity).to eq("blah") + end + + it "Unset values with name_property are not included in state" do + expect(resource.state_for_resource_reporter).to eq({}) + end + + it "Set values with name_property are not included in state" do resource.x 1 - expect(resource.state_for_resource_reporter).to eq(x: 1) + expect(resource.state_for_resource_reporter).to eq({}) end end diff --git a/spec/unit/resource_reporter_spec.rb b/spec/unit/resource_reporter_spec.rb index 775b14ef19..22bba578eb 100644 --- a/spec/unit/resource_reporter_spec.rb +++ b/spec/unit/resource_reporter_spec.rb @@ -3,7 +3,7 @@ # Author:: Prajakta Purohit (<prajakta@chef.io>) # Author:: Tyler Cloke (<tyler@chef.io>) # -# Copyright:: Copyright 2012-2019, Chef Software Inc. +# Copyright:: Copyright 2012-2020, Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -294,7 +294,7 @@ describe Chef::ResourceReporter do resource_reporter.run_started(run_status) end - context "when the new_resource is sensitive" do + context "when the new_resource is a sensitive execute resource" do before do @execute_resource = Chef::Resource::Execute.new("sensitive-resource") @execute_resource.name("sensitive-resource") @@ -312,10 +312,6 @@ describe Chef::ResourceReporter do it "resource_name in prepared_run_data should be the same" do expect(@first_update_report["name"]).to eq("sensitive-resource") end - - it "resource_command in prepared_run_data should be blank" do - expect(@first_update_report["after"]).to eq({ command: "sensitive-resource" }) - end end context "when the new_resource does not have a string for name and identity" do |