knife-solo_data_bag 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,2 +1,8 @@
1
1
  ## head
2
2
 
3
+ ## 0.2.0 (07/01/2012)
4
+ * Add support for specifying JSON content on the command line (props to BK Box)
5
+
6
+ ## 0.1.0 (05/17/2012)
7
+ * Initial release
8
+
data/README.md CHANGED
@@ -20,6 +20,10 @@ Create an encrypted data bag with the provided file content as the secret
20
20
 
21
21
  $ knife solo data bag create apps app_1 --secret-file 'SECRET_FILE'
22
22
 
23
+ Create a data bag item with JSON from the command line (works with encryption)
24
+
25
+ $ knife solo data bag create apps app_1 --json '{"id": "app_1", "username": "bob"}'
26
+
23
27
  ### Edit
24
28
  Edit a plain text data bag
25
29
 
@@ -1,6 +1,8 @@
1
1
  module KnifeSoloDataBag
2
2
  module Helpers
3
3
 
4
+ require 'json'
5
+
4
6
  def bag_item_path
5
7
  File.expand_path File.join(bag_path, "#{item_name}.json")
6
8
  end
@@ -22,6 +24,10 @@ module KnifeSoloDataBag
22
24
  config[:secret] || config[:secret_file]
23
25
  end
24
26
 
27
+ def convert_json_string
28
+ JSON.parse config[:json_string]
29
+ end
30
+
25
31
  def validate_bag_name_provided
26
32
  unless bag_name
27
33
  show_usage
@@ -37,6 +43,14 @@ module KnifeSoloDataBag
37
43
  end
38
44
  end
39
45
 
46
+ def validate_json_string
47
+ begin
48
+ JSON.parse config[:json_string], :create_additions => false
49
+ rescue => error
50
+ raise "Syntax error in #{config[:json_string]}: #{error.message}"
51
+ end
52
+ end
53
+
40
54
  def validate_multiple_secrets_were_not_provided
41
55
  if config[:secret] && config[:secret_file]
42
56
  show_usage
@@ -21,6 +21,11 @@ module KnifeSoloDataBag
21
21
  :long => '--secret-file SECRET_FILE',
22
22
  :description => 'A file containing the secret key to use to encrypt data bag item values'
23
23
 
24
+ option :json_string,
25
+ :short => '-j JSON_STRING',
26
+ :long => '--json JSON_STRING',
27
+ :description => 'The data bag json string that can be passed at the CLI'
28
+
24
29
  def run
25
30
  @bag_name, @item_name = @name_args
26
31
  ensure_valid_arguments
@@ -38,18 +43,27 @@ module KnifeSoloDataBag
38
43
  FileUtils.mkdir_p bag_path unless File.exists? bag_path
39
44
  end
40
45
 
41
- def create_bag_item
42
- create_object({'id' => item_name}, "data_bag_item[#{item_name}]") do |output|
43
- item = Chef::DataBagItem.from_hash bag_item_content(output)
44
- item.data_bag bag_name
45
- persist_bag_item item
46
+ def create_item_object
47
+ if config[:json_string].nil?
48
+ create_object({'id' => item_name}, "data_bag_item[#{item_name}]") do |output|
49
+ item = Chef::DataBagItem.from_hash bag_item_content(output)
50
+ end
51
+ else
52
+ item = Chef::DataBagItem.from_hash bag_item_content(convert_json_string)
46
53
  end
47
54
  end
48
55
 
56
+ def create_bag_item
57
+ item = create_item_object
58
+ item.data_bag bag_name
59
+ persist_bag_item item
60
+ end
61
+
49
62
  def ensure_valid_arguments
50
63
  validate_bag_name_provided
51
64
  validate_bags_path_exists
52
65
  validate_multiple_secrets_were_not_provided
66
+ validate_json_string unless config[:json_string].nil?
53
67
  end
54
68
 
55
69
  def persist_bag_item(item)
@@ -1,5 +1,5 @@
1
1
  module Knife
2
2
  module SoloDataBag
3
- VERSION = '0.1.0'
3
+ VERSION = '0.2.0'
4
4
  end
5
5
  end
@@ -87,6 +87,57 @@ describe KnifeSoloDataBag::SoloDataBagCreate do
87
87
 
88
88
  end
89
89
 
90
+ context 'when also specifying a json string' do
91
+ before do
92
+ @knife.name_args << 'bar'
93
+ @knife.config[:json_string] = '{"id": "foo", "sub": {"key_1": "value_1", "key_2": "value_2"}}'
94
+ @input_data = {'id' => 'foo', 'sub' => {'key_1' => 'value_1', 'key_2' => 'value_2'}}
95
+ @item_path = "#{@bag_path}/bar.json"
96
+ end
97
+
98
+ it 'should create the data bag item' do
99
+ @knife.run
100
+ JSON.parse(File.read(@item_path)).raw_data.should == @input_data
101
+ end
102
+
103
+ context 'when encrypting with -s or --secret' do
104
+ before do
105
+ @knife.name_args << 'bar'
106
+ @knife.config[:secret] = 'secret_key'
107
+ end
108
+
109
+ it 'should create the encrypted data bag item' do
110
+ @knife.run
111
+ content = JSON.parse(File.read(@item_path)).raw_data
112
+ @input_data.keys.reject{|i| i == 'id'}.each do |k|
113
+ content.should have_key k
114
+ content[k].should_not == @input_data[k]
115
+ end
116
+ end
117
+ end
118
+
119
+ context 'when encrypting with --secret-file' do
120
+ before do
121
+ @knife.name_args << 'bar'
122
+ @secret_path = '/var/chef/secret.txt'
123
+ @knife.config[:secret_file] = @secret_path
124
+ Chef::EncryptedDataBagItem.should_receive(:load_secret).
125
+ with(@secret_path).
126
+ and_return('psst')
127
+ end
128
+
129
+ it 'should create the encrypted data bag item' do
130
+ @knife.run
131
+ content = JSON.parse(File.read(@item_path)).raw_data
132
+ @input_data.keys.reject{|i| i == 'id'}.each do |k|
133
+ content.should have_key k
134
+ content[k].should_not == @input_data[k]
135
+ end
136
+ end
137
+ end
138
+
139
+ end
140
+
90
141
  end
91
142
 
92
143
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: knife-solo_data_bag
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-17 00:00:00.000000000Z
12
+ date: 2012-07-01 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: chef
16
- requirement: &70125975538240 !ruby/object:Gem::Requirement
16
+ requirement: &70100793125240 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 0.10.10
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70125975538240
24
+ version_requirements: *70100793125240
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &70125975537740 !ruby/object:Gem::Requirement
27
+ requirement: &70100793124740 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 2.10.0
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70125975537740
35
+ version_requirements: *70100793124740
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: fakefs
38
- requirement: &70125975537280 !ruby/object:Gem::Requirement
38
+ requirement: &70100793124280 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: 0.4.0
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70125975537280
46
+ version_requirements: *70100793124280
47
47
  description: A knife plugin for working with data bags and chef solo
48
48
  email:
49
49
  - bishop.thomas@gmail.com