Class: Chef::Knife::VaultRemove

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

Instance Method Summary collapse

Methods included from VaultBase

#configure_chef, included, #show_usage

Instance Method Details

#runObject



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
93
94
95
# File 'lib/chef/knife/vault_remove.rb', line 49

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

  set_mode(config[:vault_mode])

  if vault && item && ((values || json_file) || (search || clients || admins))
    begin
      vault_item = ChefVault::Item.load(vault, item)
      remove_items = []

      if values || json_file
        begin
          json = JSON.parse(values)
          json.each do |key, _|
            remove_items << key
          end
        rescue JSON::ParserError
          remove_items = values.split(",")
        end

        remove_items.each do |key|
          key = key.dup
          vault_item.remove(key.strip)
        end
      end

      vault_item.clients(search, :delete) if search
      vault_item.clients(clients, :delete) if clients
      vault_item.admins(admins, :delete) if admins

      vault_item.rotate_keys!(clean_unknown_clients)
    rescue ChefVault::Exceptions::KeysNotFound,
           ChefVault::Exceptions::ItemNotFound
      raise ChefVault::Exceptions::ItemNotFound,
        "#{vault}/#{item} does not exist, "\
        "use 'knife vault create' to create."
    end
  else
    show_usage
  end
end