diff options
author | Thom May <thom@chef.io> | 2016-02-09 11:20:56 -0800 |
---|---|---|
committer | Thom May <thom@chef.io> | 2016-02-15 12:32:06 +0000 |
commit | 8eae0cc429b1ad2f569e611202051a1bddbe142a (patch) | |
tree | 9f3ec8fc4331a69825e8acb8d298a3cd74fdea65 | |
parent | a030711a57e126f58b2e897c915d430e67cb7d13 (diff) | |
download | chef-8eae0cc429b1ad2f569e611202051a1bddbe142a.tar.gz |
Be way more explicit about how we're handling datatm/missed_migration
This just codifies the behaviour we're actually using - that we're
passing a json string and expecting a hash back.
Also adds a deprecation warning to the use of Chef::JSONCompat.from_json
30 files changed, 64 insertions, 40 deletions
diff --git a/lib/chef/knife.rb b/lib/chef/knife.rb index fb43f8721b..657216bcf0 100644 --- a/lib/chef/knife.rb +++ b/lib/chef/knife.rb @@ -510,8 +510,8 @@ class Chef response.body end - def create_object(object, pretty_name = nil, &block) - output = edit_data(object) + def create_object(object, pretty_name = nil, object_class: nil, &block) + output = edit_data(object, object_class: object_class) if Kernel.block_given? output = block.call(output) diff --git a/lib/chef/knife/client_create.rb b/lib/chef/knife/client_create.rb index 22c7ce907d..e296e416ce 100644 --- a/lib/chef/knife/client_create.rb +++ b/lib/chef/knife/client_create.rb @@ -91,7 +91,7 @@ class Chef client.public_key File.read(File.expand_path(config[:public_key])) end - output = edit_data(client) + output = edit_hash(client) final_client = create_client(output) ui.info("Created #{final_client}") diff --git a/lib/chef/knife/client_edit.rb b/lib/chef/knife/client_edit.rb index c05b677a28..948d43cc67 100644 --- a/lib/chef/knife/client_edit.rb +++ b/lib/chef/knife/client_edit.rb @@ -39,7 +39,7 @@ class Chef end original_data = Chef::ApiClientV1.load(@client_name).to_hash - edited_client = edit_data(original_data) + edited_client = edit_hash(original_data) if original_data != edited_client client = Chef::ApiClientV1.from_hash(edited_client) client.save diff --git a/lib/chef/knife/core/node_editor.rb b/lib/chef/knife/core/node_editor.rb index 9b5f0a435f..0f7e51cdf3 100644 --- a/lib/chef/knife/core/node_editor.rb +++ b/lib/chef/knife/core/node_editor.rb @@ -41,7 +41,7 @@ class Chef abort "You specified the --disable_editing option, nothing to edit" if config[:disable_editing] assert_editor_set! - updated_node_data = ui.edit_data(view) + updated_node_data = ui.edit_hash(view) apply_updates(updated_node_data) @updated_node end diff --git a/lib/chef/knife/core/ui.rb b/lib/chef/knife/core/ui.rb index d6c21ee411..938942c173 100644 --- a/lib/chef/knife/core/ui.rb +++ b/lib/chef/knife/core/ui.rb @@ -165,13 +165,13 @@ class Chef # Hash -> Hash # Works the same as edit_data but - # returns a hash rather than a JSON string/Fully infated object + # returns a hash rather than a JSON string/Fully inflated object def edit_hash(hash) raw = edit_data(hash, false) Chef::JSONCompat.parse(raw) end - def edit_data(data, parse_output = true) + def edit_data(data, parse_output = true, object_class: nil) output = Chef::JSONCompat.to_json_pretty(data) if !config[:disable_editing] Tempfile.open([ "knife-edit-", ".json" ]) do |tf| @@ -184,13 +184,22 @@ class Chef end end - parse_output ? Chef::JSONCompat.from_json(output) : output + if parse_output + if object_class.nil? + Chef.log_deprecation("Auto inflation of JSON data is deprecated. Please pass in the class to inflate or use #edit_hash") + Chef::JSONCompat.from_json(output) + else + object_class.from_hash(Chef::JSONCompat.parse(output)) + end + else + output + end end def edit_object(klass, name) object = klass.load(name) - output = edit_data(object) + output = edit_data(object, object_class: klass) # Only make the save if the user changed the object. # diff --git a/lib/chef/knife/data_bag_edit.rb b/lib/chef/knife/data_bag_edit.rb index ba39207db1..e059df5828 100644 --- a/lib/chef/knife/data_bag_edit.rb +++ b/lib/chef/knife/data_bag_edit.rb @@ -55,7 +55,7 @@ class Chef end item, was_encrypted = load_item(@name_args[0], @name_args[1]) - edited_item = edit_data(item) + edited_item = edit_hash(item) if was_encrypted || encryption_secret_provided? ui.info("Encrypting data bag using provided secret.") diff --git a/lib/chef/knife/environment_create.rb b/lib/chef/knife/environment_create.rb index 9f022bb49a..cfc1bc268c 100644 --- a/lib/chef/knife/environment_create.rb +++ b/lib/chef/knife/environment_create.rb @@ -46,7 +46,7 @@ class Chef env = Chef::Environment.new env.name(env_name) env.description(config[:description]) if config[:description] - create_object(env) + create_object(env, object_class: Chef::Environment) end end end diff --git a/lib/chef/knife/key_create.rb b/lib/chef/knife/key_create.rb index 55c4f4ef36..a9f9da97a7 100644 --- a/lib/chef/knife/key_create.rb +++ b/lib/chef/knife/key_create.rb @@ -51,6 +51,10 @@ EOS @ui.edit_data(key) end + def edit_hash(key) + @ui.edit_hash(key) + end + def display_info(input) @ui.info(input) end @@ -91,7 +95,7 @@ EOS key.expiration_date("infinity") end - output = edit_data(key) + output = edit_hash(key) key = create_key_from_hash(output) display_info("Created key: #{key.name}") diff --git a/lib/chef/knife/key_edit.rb b/lib/chef/knife/key_edit.rb index cd54e61cb6..8490d10fa5 100644 --- a/lib/chef/knife/key_edit.rb +++ b/lib/chef/knife/key_edit.rb @@ -53,6 +53,10 @@ EOS @ui.edit_data(key) end + def edit_hash(key) + @ui.edit_hash(key) + end + def display_info(input) @ui.info(input) end @@ -95,7 +99,7 @@ EOS key.expiration_date(@config[:expiration_date]) end - output = edit_data(key) + output = edit_hash(key) key = update_key_from_hash(output) to_display = "Updated key: #{key.name}" diff --git a/lib/chef/knife/node_create.rb b/lib/chef/knife/node_create.rb index 21d67f1355..f80ac9e87c 100644 --- a/lib/chef/knife/node_create.rb +++ b/lib/chef/knife/node_create.rb @@ -40,7 +40,7 @@ class Chef node = Chef::Node.new node.name(@node_name) - create_object(node) + create_object(node, object_class: Chef::Node) end end end diff --git a/lib/chef/knife/osc_user_create.rb b/lib/chef/knife/osc_user_create.rb index 3d879fd4f6..2e971365ec 100644 --- a/lib/chef/knife/osc_user_create.rb +++ b/lib/chef/knife/osc_user_create.rb @@ -78,7 +78,7 @@ class Chef user.public_key File.read(File.expand_path(config[:user_key])) end - output = edit_data(user) + output = edit_hash(user) user = Chef::User.from_hash(output).create ui.info("Created #{user}") diff --git a/lib/chef/knife/osc_user_edit.rb b/lib/chef/knife/osc_user_edit.rb index b0c691d7c3..c73a80917c 100644 --- a/lib/chef/knife/osc_user_edit.rb +++ b/lib/chef/knife/osc_user_edit.rb @@ -44,7 +44,7 @@ class Chef end original_user = Chef::User.load(@user_name).to_hash - edited_user = edit_data(original_user) + edited_user = edit_hash(original_user) if original_user != edited_user user = Chef::User.from_hash(edited_user) user.update diff --git a/lib/chef/knife/role_create.rb b/lib/chef/knife/role_create.rb index 7e581f42d6..a389d849f7 100644 --- a/lib/chef/knife/role_create.rb +++ b/lib/chef/knife/role_create.rb @@ -46,7 +46,7 @@ class Chef role = Chef::Role.new role.name(@role_name) role.description(config[:description]) if config[:description] - create_object(role) + create_object(role, object_class: Chef::Role) end end end diff --git a/lib/chef/knife/user_create.rb b/lib/chef/knife/user_create.rb index b1782a893b..ac81f29e82 100644 --- a/lib/chef/knife/user_create.rb +++ b/lib/chef/knife/user_create.rb @@ -130,7 +130,7 @@ EOF user.public_key File.read(File.expand_path(config[:user_key])) end - output = edit_data(user) + output = edit_hash(user) final_user = create_user_from_hash(output) ui.info("Created #{user}") diff --git a/lib/chef/knife/user_edit.rb b/lib/chef/knife/user_edit.rb index 9564a31b15..bb80ede267 100644 --- a/lib/chef/knife/user_edit.rb +++ b/lib/chef/knife/user_edit.rb @@ -66,7 +66,7 @@ EOF ui.warn(osc_11_warning) run_osc_11_user_edit else # EC / CS 12 user create - edited_user = edit_data(original_user) + edited_user = edit_hash(original_user) if original_user != edited_user user = Chef::UserV1.from_hash(edited_user) user.update diff --git a/spec/unit/knife/client_create_spec.rb b/spec/unit/knife/client_create_spec.rb index bd9d4a1d63..17d18d084e 100644 --- a/spec/unit/knife/client_create_spec.rb +++ b/spec/unit/knife/client_create_spec.rb @@ -40,7 +40,7 @@ describe Chef::Knife::ClientCreate do k = Chef::Knife::ClientCreate.new k.name_args = [] allow(k).to receive(:client).and_return(client) - allow(k).to receive(:edit_data).with(client).and_return(client) + allow(k).to receive(:edit_hash).with(client).and_return(client) allow(k.ui).to receive(:stderr).and_return(stderr) allow(k.ui).to receive(:stdout).and_return(stdout) k @@ -118,7 +118,7 @@ describe Chef::Knife::ClientCreate do end it "should allow you to edit the data" do - expect(knife).to receive(:edit_data).with(client).and_return(client) + expect(knife).to receive(:edit_hash).with(client).and_return(client) knife.run end diff --git a/spec/unit/knife/client_edit_spec.rb b/spec/unit/knife/client_edit_spec.rb index bdfd7e3331..584a94014c 100644 --- a/spec/unit/knife/client_edit_spec.rb +++ b/spec/unit/knife/client_edit_spec.rb @@ -39,7 +39,7 @@ describe Chef::Knife::ClientEdit do it "should edit the client" do allow(Chef::ApiClientV1).to receive(:load).with("adam").and_return(data) - expect(@knife).to receive(:edit_data).with(data).and_return(data) + expect(@knife).to receive(:edit_hash).with(data).and_return(data) @knife.run end diff --git a/spec/unit/knife/core/node_editor_spec.rb b/spec/unit/knife/core/node_editor_spec.rb index 770e2e84aa..6665616027 100644 --- a/spec/unit/knife/core/node_editor_spec.rb +++ b/spec/unit/knife/core/node_editor_spec.rb @@ -165,7 +165,7 @@ describe Chef::Knife::NodeEditor do context "and changes affect only editable properties" do before(:each) do - allow(ui).to receive(:edit_data) + allow(ui).to receive(:edit_hash) .with(subject.view) .and_return(updated_data) @@ -181,7 +181,7 @@ describe Chef::Knife::NodeEditor do before(:each) do data = updated_data.merge("bad_property" => "bad_value") - allow(ui).to receive(:edit_data) + allow(ui).to receive(:edit_hash) .with(subject.view) .and_return(data) @@ -197,7 +197,7 @@ describe Chef::Knife::NodeEditor do context "and changes were not made" do before(:each) do - allow(ui).to receive(:edit_data) + allow(ui).to receive(:edit_hash) .with(subject.view) .and_return(subject.view.dup) diff --git a/spec/unit/knife/core/ui_spec.rb b/spec/unit/knife/core/ui_spec.rb index eacca2af2e..58f13ef4a0 100644 --- a/spec/unit/knife/core/ui_spec.rb +++ b/spec/unit/knife/core/ui_spec.rb @@ -30,6 +30,7 @@ describe Chef::Knife::UI do :format => "summary", } @ui = Chef::Knife::UI.new(@out, @err, @in, @config) + Chef::Config[:treat_deprecation_warnings_as_errors] = false end describe "edit" do @@ -58,6 +59,12 @@ describe Chef::Knife::UI do it "returns a ruby object" do expect(subject).to eql(ruby_for_json) end + + it "gives a deprecation error" do + Chef::Config[:treat_deprecation_warnings_as_errors] = true + expect { subject }.to raise_error Chef::Exceptions::DeprecatedFeatureError, + /Auto inflation of JSON data is deprecated./ + end end end diff --git a/spec/unit/knife/data_bag_edit_spec.rb b/spec/unit/knife/data_bag_edit_spec.rb index 1001e68ce4..635dc63489 100644 --- a/spec/unit/knife/data_bag_edit_spec.rb +++ b/spec/unit/knife/data_bag_edit_spec.rb @@ -55,7 +55,7 @@ describe Chef::Knife::DataBagEdit do it "correctly edits then uploads the data bag" do expect(Chef::DataBagItem).to receive(:load).with(bag_name, item_name).and_return(db) expect(knife).to receive(:encrypted?).with(db.raw_data).and_return(is_encrypted?) - expect(knife).to receive(:edit_data).with(data_to_edit).and_return(raw_edited_hash) + expect(knife).to receive(:edit_hash).with(data_to_edit).and_return(raw_edited_hash) expect(rest).to receive(:put).with("data/#{bag_name}/#{item_name}", transmitted_hash).ordered knife.run diff --git a/spec/unit/knife/environment_create_spec.rb b/spec/unit/knife/environment_create_spec.rb index 6e6e085f6f..a80626657c 100644 --- a/spec/unit/knife/environment_create_spec.rb +++ b/spec/unit/knife/environment_create_spec.rb @@ -50,7 +50,7 @@ describe Chef::Knife::EnvironmentCreate do end it "should prompt you to edit the data" do - expect(@knife).to receive(:edit_data).with(@environment) + expect(@knife).to receive(:edit_data).with(@environment, object_class: Chef::Environment) @knife.run end diff --git a/spec/unit/knife/environment_edit_spec.rb b/spec/unit/knife/environment_edit_spec.rb index 6ec4965b8d..1f2ff27ce8 100644 --- a/spec/unit/knife/environment_edit_spec.rb +++ b/spec/unit/knife/environment_edit_spec.rb @@ -40,7 +40,7 @@ describe Chef::Knife::EnvironmentEdit do end it "should let you edit the environment" do - expect(@knife.ui).to receive(:edit_data).with(@environment) + expect(@knife.ui).to receive(:edit_data).with(@environment, object_class: Chef::Environment) @knife.run end @@ -48,7 +48,7 @@ describe Chef::Knife::EnvironmentEdit do pansy = Chef::Environment.new @environment.name("new_environment_name") - expect(@knife.ui).to receive(:edit_data).with(@environment).and_return(pansy) + expect(@knife.ui).to receive(:edit_data).with(@environment, object_class: Chef::Environment).and_return(pansy) expect(pansy).to receive(:save) @knife.run end diff --git a/spec/unit/knife/key_create_spec.rb b/spec/unit/knife/key_create_spec.rb index 1647b14dc2..87315bd5df 100644 --- a/spec/unit/knife/key_create_spec.rb +++ b/spec/unit/knife/key_create_spec.rb @@ -112,7 +112,7 @@ Tfuc9dUYsFjptWYrV6pfEQ+bgo1OGBXORBFcFL+2D7u9JYquKrMgosznHoEkQNLo allow(key_create_object).to receive(:output_private_key_to_file) allow(key_create_object).to receive(:display_private_key) - allow(key_create_object).to receive(:edit_data).and_return(expected_hash) + allow(key_create_object).to receive(:edit_hash).and_return(expected_hash) allow(key_create_object).to receive(:create_key_from_hash).and_return(Chef::Key.from_hash(expected_hash)) allow(key_create_object).to receive(:display_info) end diff --git a/spec/unit/knife/key_edit_spec.rb b/spec/unit/knife/key_edit_spec.rb index 0fc7208139..44300b3e0f 100644 --- a/spec/unit/knife/key_edit_spec.rb +++ b/spec/unit/knife/key_edit_spec.rb @@ -110,7 +110,7 @@ Tfuc9dUYsFjptWYrV6pfEQ+bgo1OGBXORBFcFL+2D7u9JYquKrMgosznHoEkQNLo allow(key_edit_object).to receive(:output_private_key_to_file) allow(key_edit_object).to receive(:display_private_key) - allow(key_edit_object).to receive(:edit_data).and_return(expected_hash) + allow(key_edit_object).to receive(:edit_hash).and_return(expected_hash) allow(key_edit_object).to receive(:display_info) end diff --git a/spec/unit/knife/osc_user_create_spec.rb b/spec/unit/knife/osc_user_create_spec.rb index a52ba54199..0413d46f78 100644 --- a/spec/unit/knife/osc_user_create_spec.rb +++ b/spec/unit/knife/osc_user_create_spec.rb @@ -44,7 +44,7 @@ describe Chef::Knife::OscUserCreate do allow(@user).to receive(:create).and_return(@user_with_private_key) allow(Chef::User).to receive(:new).and_return(@user) allow(Chef::User).to receive(:from_hash).and_return(@user) - allow(@knife).to receive(:edit_data).and_return(@user.to_hash) + allow(@knife).to receive(:edit_hash).and_return(@user.to_hash) end it "creates a new user" do @@ -79,7 +79,7 @@ describe Chef::Knife::OscUserCreate do end it "allows you to edit the data" do - expect(@knife).to receive(:edit_data).with(@user) + expect(@knife).to receive(:edit_hash).with(@user) @knife.run end diff --git a/spec/unit/knife/osc_user_edit_spec.rb b/spec/unit/knife/osc_user_edit_spec.rb index 4dc92d61c8..59fb152b5b 100644 --- a/spec/unit/knife/osc_user_edit_spec.rb +++ b/spec/unit/knife/osc_user_edit_spec.rb @@ -39,7 +39,7 @@ describe Chef::Knife::OscUserEdit do it "loads and edits the user" do data = { :name => "my_user" } allow(Chef::User).to receive(:load).with("my_user").and_return(data) - expect(@knife).to receive(:edit_data).with(data).and_return(data) + expect(@knife).to receive(:edit_hash).with(data).and_return(data) @knife.run end diff --git a/spec/unit/knife/role_create_spec.rb b/spec/unit/knife/role_create_spec.rb index d414e5a18d..3d06a871e2 100644 --- a/spec/unit/knife/role_create_spec.rb +++ b/spec/unit/knife/role_create_spec.rb @@ -52,7 +52,7 @@ describe Chef::Knife::RoleCreate do end it "should allow you to edit the data" do - expect(@knife).to receive(:edit_data).with(@role) + expect(@knife).to receive(:edit_data).with(@role, object_class: Chef::Role) @knife.run end diff --git a/spec/unit/knife/role_edit_spec.rb b/spec/unit/knife/role_edit_spec.rb index 71d6980670..5e03b7aef3 100644 --- a/spec/unit/knife/role_edit_spec.rb +++ b/spec/unit/knife/role_edit_spec.rb @@ -39,7 +39,7 @@ describe Chef::Knife::RoleEdit do end it "should edit the role data" do - expect(@knife.ui).to receive(:edit_data).with(@role) + expect(@knife.ui).to receive(:edit_data).with(@role, object_class: Chef::Role) @knife.run end @@ -47,7 +47,7 @@ describe Chef::Knife::RoleEdit do pansy = Chef::Role.new @role.name("new_role_name") - expect(@knife.ui).to receive(:edit_data).with(@role).and_return(pansy) + expect(@knife.ui).to receive(:edit_data).with(@role, object_class: Chef::Role).and_return(pansy) expect(pansy).to receive(:save) @knife.run end @@ -55,7 +55,7 @@ describe Chef::Knife::RoleEdit do it "should not save the unedited role data" do pansy = Chef::Role.new - expect(@knife.ui).to receive(:edit_data).with(@role).and_return(pansy) + expect(@knife.ui).to receive(:edit_data).with(@role, object_class: Chef::Role).and_return(pansy) expect(pansy).not_to receive(:save) @knife.run diff --git a/spec/unit/knife/user_create_spec.rb b/spec/unit/knife/user_create_spec.rb index 806e8cf400..e708d2d1ad 100644 --- a/spec/unit/knife/user_create_spec.rb +++ b/spec/unit/knife/user_create_spec.rb @@ -111,7 +111,7 @@ describe Chef::Knife::UserCreate do context "when all mandatory fields are validly specified" do before do knife.name_args = %w{some_user some_display_name some_first_name some_last_name some_email some_password} - allow(knife).to receive(:edit_data).and_return(knife.user.to_hash) + allow(knife).to receive(:edit_hash).and_return(knife.user.to_hash) allow(knife).to receive(:create_user_from_hash).and_return(knife.user) end diff --git a/spec/unit/knife/user_edit_spec.rb b/spec/unit/knife/user_edit_spec.rb index ec118ed181..18ade54068 100644 --- a/spec/unit/knife/user_edit_spec.rb +++ b/spec/unit/knife/user_edit_spec.rb @@ -53,7 +53,7 @@ describe Chef::Knife::UserEdit do it "loads and edits the user" do data = { "username" => "my_user" } allow(Chef::UserV1).to receive(:load).with("my_user").and_return(data) - expect(knife).to receive(:edit_data).with(data).and_return(data) + expect(knife).to receive(:edit_hash).with(data).and_return(data) knife.run end |