Class: Chef::Knife::DataBagCreate

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

Constant Summary

Constants inherited from Chef::Knife

CHEF_ORGANIZATION_MANAGEMENT, OFFICIAL_PLUGINS, OPSCODE_HOSTED_CHEF_ACCESS_CONTROL

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 included from EncryptedDataBagItem::CheckEncrypted

#encrypted?

Methods inherited from Chef::Knife

#api_key, #apply_computed_config, category, chef_config_dir, #cli_keys, common_name, #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, 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

Methods included from Mixin::ConvertToClassName

#constantize, #convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #normalize_snake_case_name, #snake_case_basename

Constructor Details

This class inherits a constructor from Chef::Knife

Instance Method Details

#runObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/chef/knife/data_bag_create.rb', line 36

def run
  @data_bag_name, @data_bag_item_name = @name_args

  if @data_bag_name.nil?
    show_usage
    ui.fatal("You must specify a data bag name")
    exit 1
  end

  begin
    Chef::DataBag.validate_name!(@data_bag_name)
  rescue Chef::Exceptions::InvalidDataBagName => e
    ui.fatal(e.message)
    exit(1)
  end

  # Verify if the data bag exists
  begin
    rest.get("data/#{@data_bag_name}")
    ui.info("Data bag #{@data_bag_name} already exists")
  rescue Net::HTTPClientException => e
    raise unless e.to_s =~ /^404/

    # if it doesn't exists, try to create it
    rest.post("data", { "name" => @data_bag_name })
    ui.info("Created data_bag[#{@data_bag_name}]")
  end

  # if an item is specified, create it, as well
  if @data_bag_item_name
    create_object({ "id" => @data_bag_item_name }, "data_bag_item[#{@data_bag_item_name}]") do |output|
      item = Chef::DataBagItem.from_hash(
        if encryption_secret_provided?
          Chef::EncryptedDataBagItem.encrypt_data_bag_item(output, read_secret)
        else
          output
        end
      )
      item.data_bag(@data_bag_name)
      rest.post("data/#{@data_bag_name}", item)
    end
  end
end