Class: Chef::Knife::SecureBagCreate

Inherits:
Knife::DataBagCreate
  • Object
show all
Includes:
SecureBagBase
Defined in:
lib/chef/knife/secure_bag_create.rb

Instance Method Summary collapse

Methods included from SecureBagBase

#data_for_create, #data_for_edit, #data_for_save, #encoded_fields, included, #read_secret, #require_secret, #secret_file, #use_encryption

Instance Method Details

#create_databagObject



13
14
15
16
17
18
19
20
21
# File 'lib/chef/knife/secure_bag_create.rb', line 13

def create_databag
  begin
    rest.post_rest("data", { name: @data_bag_name })
    ui.info("Created data_bag[#{@data_bag_name}]")
  rescue Net::HTTPServerException => e
    raise unless e.to_s =~ /^409/
    ui.info("Data bag #{@data_bag_name} already exists")
  end
end

#create_databag_itemObject



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/chef/knife/secure_bag_create.rb', line 23

def create_databag_item
  create_object(initial_data, 
                "data_bag_item[#{@data_bag_item_name}]") do |output|

    @raw_data = data_for_save(output)

    item = SecureDataBag::Item.from_hash(@raw_data, read_secret)
    item.encoded_fields(encoded_fields)
    item.data_bag(@data_bag_name)

    rest.post_rest("data/#{@data_bag_name}", item.to_hash)
  end
end

#runObject



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
# File 'lib/chef/knife/secure_bag_create.rb', line 37

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

  require_secret

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

  # create the data bag
  create_databag

  if @data_bag_item_name
    create_databag_item
  end
end