Class: Chef::Knife::VaultShow

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

Instance Method Summary collapse

Methods included from VaultBase

included, #show_usage

Instance Method Details



90
91
92
93
94
95
96
97
98
99
# File 'lib/chef/knife/vault_show.rb', line 90

def print_keys(vault)
  if bag_is_vault?(vault)
    bag = Chef::DataBag.load(vault)
    split_vault_keys(bag)[1].each do |item|
      output item
    end
  else
    output "data bag #{vault} is not a chef-vault"
  end
end


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

def print_values(vault, item, values)
  vault_item = ChefVault::Item.load(vault, item)

  extra_data = {}

  if config[:print]
    case config[:print]
    when 'search'
      extra_data["search_query"] = vault_item.search
    when 'admins'
      extra_data["admins"] = vault_item.admins
    when 'clients'
      extra_data["clients"] = vault_item.clients
    when 'all'
      extra_data["search_query"] = vault_item.search
      extra_data["admins"] = vault_item.admins
      extra_data["clients"] = vault_item.clients
    end
  end

  if values
    included_values = %w(id)

    values.split(",").each do |value|
      value.strip! # remove white space
      included_values << value
    end

    filtered_data = Hash[vault_item.raw_data.find_all{|k, _| included_values.include?(k)}]

    output_data = filtered_data.merge(extra_data)
  else
    all_data = vault_item.raw_data

    output_data = all_data.merge(extra_data)
  end
  output(output_data)
end

#runObject



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

def run
  vault = @name_args[0]
  item = @name_args[1]
  values = @name_args[2]

  if vault && item
    set_mode(config[:vault_mode])
    print_values(vault, item, values)
  elsif vault
    set_mode(config[:vault_mode])
    print_keys(vault)
  else
    show_usage
  end
end