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 /lib/chef | |
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
Diffstat (limited to 'lib/chef')
-rw-r--r-- | lib/chef/knife.rb | 4 | ||||
-rw-r--r-- | lib/chef/knife/client_create.rb | 2 | ||||
-rw-r--r-- | lib/chef/knife/client_edit.rb | 2 | ||||
-rw-r--r-- | lib/chef/knife/core/node_editor.rb | 2 | ||||
-rw-r--r-- | lib/chef/knife/core/ui.rb | 17 | ||||
-rw-r--r-- | lib/chef/knife/data_bag_edit.rb | 2 | ||||
-rw-r--r-- | lib/chef/knife/environment_create.rb | 2 | ||||
-rw-r--r-- | lib/chef/knife/key_create.rb | 6 | ||||
-rw-r--r-- | lib/chef/knife/key_edit.rb | 6 | ||||
-rw-r--r-- | lib/chef/knife/node_create.rb | 2 | ||||
-rw-r--r-- | lib/chef/knife/osc_user_create.rb | 2 | ||||
-rw-r--r-- | lib/chef/knife/osc_user_edit.rb | 2 | ||||
-rw-r--r-- | lib/chef/knife/role_create.rb | 2 | ||||
-rw-r--r-- | lib/chef/knife/user_create.rb | 2 | ||||
-rw-r--r-- | lib/chef/knife/user_edit.rb | 2 |
15 files changed, 36 insertions, 19 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 |