Class: Chef::Knife::VaultCreate

Inherits:
Chef::Knife show all
Includes:
VaultAdmins, VaultBase
Defined in:
lib/chef/knife/vault_create.rb

Direct Known Subclasses

EncryptCreate

Instance Method Summary collapse

Methods included from VaultBase

included, #show_usage

Instance Method Details

#runObject



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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/chef/knife/vault_create.rb', line 46

def run
  vault = @name_args[0]
  item = @name_args[1]
  values = @name_args[2]
  search = config[:search]
  json_file = config[:json]
  file = config[:file]

  set_mode(config[:vault_mode])

  if vault && item && (search || admins)
    begin
      vault_item = ChefVault::Item.load(vault, item)
      raise ChefVault::Exceptions::ItemAlreadyExists,
        "#{vault_item.data_bag}/#{vault_item.id} already exists, "\
        "use 'knife vault remove' 'knife vault update' "\
        "or 'knife vault edit' to make changes."
    rescue ChefVault::Exceptions::KeysNotFound,
           ChefVault::Exceptions::ItemNotFound,
           Chef::Exceptions::InvalidDataBagItemID
      vault_item = ChefVault::Item.new(vault, item)
      if values || json_file || file
        merge_values(values, json_file).each do |key, value|
          vault_item[key] = value
        end

        if file
          vault_item["file-name"] = File.basename(file)
          vault_item["file-content"] = File.open(file) { |f| f.read() }
        end
      else
        vault_json = edit_data({})
        vault_json.each do |key, value|
          vault_item[key] = value
        end
      end

      vault_item.search(search) if search
      vault_item.clients(search) if search
      vault_item.admins(admins) if admins

      vault_item.save
    end
  else
    show_usage
  end
end