Class: Chef::Knife::DataBagEdit

Inherits:
Chef::Knife show all
Includes:
DataBagSecretOptions
Defined in:
lib/chef/knife/data_bag_edit.rb

Constant Summary

Constants inherited from Chef::Knife

CHEF_ORGANIZATION_MANAGEMENT, KNIFE_ROOT, OFFICIAL_PLUGINS, OPSCODE_HOSTED_CHEF_ACCESS_CONTROL, VERSION

Instance Attribute Summary

Attributes inherited from Chef::Knife

#name_args, #ui

Instance Method Summary collapse

Methods included from DataBagSecretOptions

#encryption_secret_provided?, #encryption_secret_provided_ignore_encrypt_flag?, included, #read_secret, #validate_secrets

Methods inherited from Chef::Knife

#api_key, #apply_computed_config, category, chef_config_dir, common_name, #config_file_defaults, #config_file_settings, config_loader, #config_source, #configure_chef, #create_object, #delete_object, dependency_loaders, deps, #format_rest_error, guess_category, #humanize_exception, #humanize_http_exception, inherited, #initialize, list_commands, load_commands, load_config, load_deps, #maybe_setup_fips, #merge_configs, msg, #noauth_rest, #parse_options, reset_config_loader!, reset_subcommands!, #rest, #root_rest, run, #run_with_pretty_exceptions, #server_url, #show_usage, snake_case_name, subcommand_category, subcommand_class_from, subcommand_files, subcommand_loader, subcommands, subcommands_by_category, #test_mandatory_field, ui, unnamed?, use_separate_defaults?, #username

Constructor Details

This class inherits a constructor from Chef::Knife

Instance Method Details

#load_item(bag, item_name) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/chef/knife/data_bag_edit.rb', line 36

def load_item(bag, item_name)
  item = Chef::DataBagItem.load(bag, item_name)
  if encrypted?(item.raw_data)
    if encryption_secret_provided_ignore_encrypt_flag?
      [Chef::EncryptedDataBagItem.new(item, read_secret).to_hash, true]
    else
      ui.fatal("You cannot edit an encrypted data bag without providing the secret.")
      exit(1)
    end
  else
    [item.raw_data, false]
  end
end

#runObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/chef/knife/data_bag_edit.rb', line 50

def run
  if @name_args.length != 2
    stdout.puts "You must supply the data bag and an item to edit"
    stdout.puts opt_parser
    exit 1
  end

  item, was_encrypted = load_item(@name_args[0], @name_args[1])
  edited_item = edit_hash(item)

  if was_encrypted || encryption_secret_provided?
    ui.info("Encrypting data bag using provided secret.")
    item_to_save = Chef::EncryptedDataBagItem.encrypt_data_bag_item(edited_item, read_secret)
  else
    ui.info("Saving data bag unencrypted. To encrypt it, provide an appropriate secret.")
    item_to_save = edited_item
  end

  rest.put("data/#{@name_args[0]}/#{@name_args[1]}", item_to_save)
  stdout.puts("Saved data_bag_item[#{@name_args[1]}]")
  ui.output(edited_item) if config[:print_after]
end