hammer_cli_foreman 2.0.2 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. checksums.yaml +4 -4
  2. data/doc/developer_docs.md +1 -0
  3. data/doc/plugin.md +22 -0
  4. data/doc/release_notes.md +20 -4
  5. data/lib/hammer_cli_foreman.rb +8 -0
  6. data/lib/hammer_cli_foreman/api/oauth/authentication_code_grant.rb +7 -4
  7. data/lib/hammer_cli_foreman/associating_commands.rb +33 -5
  8. data/lib/hammer_cli_foreman/auth.rb +6 -6
  9. data/lib/hammer_cli_foreman/bookmark.rb +50 -0
  10. data/lib/hammer_cli_foreman/command_extensions.rb +2 -0
  11. data/lib/hammer_cli_foreman/command_extensions/puppet_environment.rb +13 -7
  12. data/lib/hammer_cli_foreman/command_extensions/puppet_environments.rb +13 -7
  13. data/lib/hammer_cli_foreman/command_extensions/subnet.rb +25 -0
  14. data/lib/hammer_cli_foreman/command_extensions/user.rb +17 -0
  15. data/lib/hammer_cli_foreman/commands.rb +14 -9
  16. data/lib/hammer_cli_foreman/compute_resource.rb +15 -0
  17. data/lib/hammer_cli_foreman/compute_resource/ec2.rb +11 -0
  18. data/lib/hammer_cli_foreman/compute_resource/gce.rb +9 -0
  19. data/lib/hammer_cli_foreman/compute_resource/libvirt.rb +11 -0
  20. data/lib/hammer_cli_foreman/compute_resource/openstack.rb +7 -0
  21. data/lib/hammer_cli_foreman/compute_resource/ovirt.rb +21 -9
  22. data/lib/hammer_cli_foreman/compute_resource/vmware.rb +15 -2
  23. data/lib/hammer_cli_foreman/hostgroup.rb +11 -19
  24. data/lib/hammer_cli_foreman/hosts/common_update_options.rb +32 -10
  25. data/lib/hammer_cli_foreman/id_resolver.rb +12 -11
  26. data/lib/hammer_cli_foreman/mail_notification.rb +31 -0
  27. data/lib/hammer_cli_foreman/option_builders.rb +70 -48
  28. data/lib/hammer_cli_foreman/option_sources.rb +1 -0
  29. data/lib/hammer_cli_foreman/option_sources/referenced_resource_id_params.rb +47 -0
  30. data/lib/hammer_cli_foreman/subnet.rb +26 -15
  31. data/lib/hammer_cli_foreman/task_helper.rb +180 -0
  32. data/lib/hammer_cli_foreman/user.rb +3 -17
  33. data/lib/hammer_cli_foreman/version.rb +1 -1
  34. data/lib/hammer_cli_foreman/virtual_machine.rb +55 -0
  35. data/locale/ca/LC_MESSAGES/hammer-cli-foreman.mo +0 -0
  36. data/locale/de/LC_MESSAGES/hammer-cli-foreman.mo +0 -0
  37. data/locale/en/LC_MESSAGES/hammer-cli-foreman.mo +0 -0
  38. data/locale/en_GB/LC_MESSAGES/hammer-cli-foreman.mo +0 -0
  39. data/locale/es/LC_MESSAGES/hammer-cli-foreman.mo +0 -0
  40. data/locale/fr/LC_MESSAGES/hammer-cli-foreman.mo +0 -0
  41. data/locale/it/LC_MESSAGES/hammer-cli-foreman.mo +0 -0
  42. data/locale/ja/LC_MESSAGES/hammer-cli-foreman.mo +0 -0
  43. data/locale/ko/LC_MESSAGES/hammer-cli-foreman.mo +0 -0
  44. data/locale/pt_BR/LC_MESSAGES/hammer-cli-foreman.mo +0 -0
  45. data/locale/ru/LC_MESSAGES/hammer-cli-foreman.mo +0 -0
  46. data/locale/zh_CN/LC_MESSAGES/hammer-cli-foreman.mo +0 -0
  47. data/locale/zh_TW/LC_MESSAGES/hammer-cli-foreman.mo +0 -0
  48. data/test/data/2.1/foreman_api.json +1 -0
  49. data/test/functional/associating_commands_test.rb +134 -30
  50. data/test/functional/bookmark_test.rb +193 -0
  51. data/test/functional/host_test.rb +27 -1
  52. data/test/functional/mail_notification_test.rb +57 -0
  53. data/test/functional/subnet/create_test.rb +28 -5
  54. data/test/functional/template_test.rb +2 -2
  55. data/test/functional/virtual_machine_test.rb +129 -0
  56. data/test/test_helper.rb +1 -1
  57. metadata +23 -6
@@ -0,0 +1,193 @@
1
+ # frozen_string_literal: true
2
+
3
+ require File.join(File.dirname(__FILE__), 'test_helper')
4
+
5
+ describe 'bookmark' do
6
+ describe 'list' do
7
+ before do
8
+ @cmd = %w[bookmark list]
9
+ @bookmarks = [{
10
+ id: 1,
11
+ name: 'active',
12
+ controller: 'hosts',
13
+ owner_id: 1,
14
+ owner_type: 'User'
15
+ }]
16
+ end
17
+
18
+ it 'should return a list of bookmarks' do
19
+ api_expects(:bookmarks, :index, 'List bookmarks').returns(@bookmarks)
20
+
21
+ output = IndexMatcher.new([
22
+ %w[ID NAME CONTROLLER OWNER\ ID OWNER\ TYPE],
23
+ %w[1 active hosts 1 User]
24
+ ])
25
+ expected_result = success_result(output)
26
+
27
+ result = run_cmd(@cmd)
28
+ assert_cmd(expected_result, result)
29
+ end
30
+ end
31
+
32
+ describe 'info' do
33
+ before do
34
+ @cmd = %w[bookmark info]
35
+ @bookmark = {
36
+ id: 1,
37
+ name: 'active',
38
+ controller: 'hosts',
39
+ owner_id: 1,
40
+ owner_type: 'User'
41
+ }
42
+ end
43
+
44
+ it 'should return the info of a bookmark' do
45
+ params = ['--id=1']
46
+ api_expects(:bookmarks, :show, 'Info bookmark').returns(@bookmark)
47
+ output = OutputMatcher.new([
48
+ 'Id: 1',
49
+ 'Name: active',
50
+ 'Controller: hosts',
51
+ 'Owner Id: 1',
52
+ 'Owner Type: User'
53
+ ])
54
+
55
+ expected_result = success_result(output)
56
+ result = run_cmd(@cmd + params)
57
+ assert_cmd(expected_result, result)
58
+ end
59
+
60
+ describe 'create bookmark' do
61
+ before do
62
+ @cmd = %w[bookmark create]
63
+ end
64
+
65
+ it 'should create a bookmark' do
66
+ params = ['--name=newBookmark', '--controller=hosts', '--query=status = failed']
67
+ api_expects(:bookmarks, :create, 'Create bookmark') do |params|
68
+ (params['bookmark']['name'] == 'newBookmark')
69
+ (params['bookmark']['controller'] == 'hosts')
70
+ (params['bookmark']['query'] == 'status = failed')
71
+ end
72
+ result = run_cmd(@cmd + params)
73
+ assert_cmd(success_result("Bookmark %<name>s created.\n"), result)
74
+ end
75
+
76
+ it 'should print error on missing --name' do
77
+ params = ['--controller=hosts', '--query=status = failed']
78
+ expected_result = CommandExpectation.new
79
+ expected_result.expected_err =
80
+ ['Failed to create bookmark:',
81
+ " Missing arguments for '--name'.",
82
+ ''].join("\n")
83
+ expected_result.expected_exit_code = HammerCLI::EX_USAGE
84
+
85
+ api_expects_no_call
86
+
87
+ result = run_cmd(@cmd + params)
88
+ assert_cmd(expected_result, result)
89
+ end
90
+
91
+ it 'should print error on missing --controller' do
92
+ params = ['--name=newBookmark', '--query=status = failed']
93
+ expected_result = CommandExpectation.new
94
+ expected_result.expected_err =
95
+ ['Failed to create bookmark:',
96
+ " Missing arguments for '--controller'.",
97
+ ''].join("\n")
98
+ expected_result.expected_exit_code = HammerCLI::EX_USAGE
99
+ api_expects_no_call
100
+
101
+ result = run_cmd(@cmd + params)
102
+
103
+ assert_cmd(expected_result, result)
104
+ end
105
+
106
+ it 'should print error on missing --query' do
107
+ params = ['--name=newBookmark', '--controller=hosts']
108
+
109
+ expected_result = CommandExpectation.new
110
+ expected_result.expected_err =
111
+ ['Failed to create bookmark:',
112
+ " Missing arguments for '--query'.",
113
+ ''].join("\n")
114
+ expected_result.expected_exit_code = HammerCLI::EX_USAGE
115
+
116
+ api_expects_no_call
117
+
118
+ result = run_cmd(@cmd + params)
119
+
120
+ assert_cmd(expected_result, result)
121
+ end
122
+ end
123
+
124
+ describe 'update bookmark' do
125
+ before do
126
+ @cmd = %w[bookmark update]
127
+ @bookmark = {
128
+ "id": 1,
129
+ 'name': 'bookmark-2',
130
+ 'controller': 'hosts',
131
+ 'query': 'status = failed',
132
+ 'owner_id': 1,
133
+ 'owner_type': 'User'
134
+ }
135
+ end
136
+
137
+ it 'allows minimal options' do
138
+ api_expects(:bookmarks, :update) do |par|
139
+ par['id'] == '1'
140
+ end
141
+ run_cmd(%w[bookmark update --id 1])
142
+ end
143
+
144
+ it 'update bookmark' do
145
+ params = ['--id=1', '--new-name=bookmark-2', '--controller=hosts', '--query=status != failed']
146
+ api_expects(:bookmarks, :update, 'Update the bookmark') do |params|
147
+ (params['bookmark']['id'] = 1)
148
+ (params['bookmark']['name'] = 'bookmark-2')
149
+ (params['bookmark']['controller'] = 'hosts')
150
+ (params['bookmark']['query'] = 'status != failed')
151
+ end
152
+ result = run_cmd(@cmd + params)
153
+ assert_cmd(success_result("Bookmark %<name>s updated successfully.\n"), result)
154
+ end
155
+ end
156
+
157
+ describe 'delete bookmark' do
158
+ before do
159
+ @cmd = %w[bookmark delete]
160
+
161
+ @bookmark = {
162
+ "id": 1,
163
+ 'name': 'bookmark-2',
164
+ 'controller': 'hosts',
165
+ 'query': 'status = failed',
166
+ 'owner_id': 1,
167
+ 'owner_type': 'User'
168
+ }
169
+ end
170
+
171
+ it 'delete bookmark' do
172
+ params = ['--id=1']
173
+ api_expects(:bookmarks, :destroy, id: 1).returns({})
174
+ result = run_cmd(@cmd + params)
175
+ assert_cmd(success_result("Bookmark deleted successfully.\n"), result)
176
+ end
177
+
178
+ it 'should print error on missing --id' do
179
+ params = []
180
+
181
+ expected_result = CommandExpectation.new
182
+ expected_result.expected_err =
183
+ ['Failed to delete bookmark:',
184
+ " Missing arguments for '--id'.",
185
+ ''].join("\n")
186
+ expected_result.expected_exit_code = HammerCLI::EX_USAGE
187
+ api_expects_no_call
188
+ result = run_cmd(@cmd + params)
189
+ assert_cmd(expected_result, result)
190
+ end
191
+ end
192
+ end
193
+ end
@@ -308,7 +308,10 @@ describe 'host update' do
308
308
  'organization_id' => '5',
309
309
  'location_id' => '5',
310
310
  'compute_attributes' => {},
311
- 'puppetclass_ids' => []
311
+ 'puppetclass_ids' => [],
312
+ 'owner_id' => '1',
313
+ 'owner_name' => 'adminGroup',
314
+ 'owner_type' => 'Usergroup'
312
315
  }
313
316
  end
314
317
 
@@ -384,6 +387,29 @@ describe 'host update' do
384
387
 
385
388
  assert_cmd(expected_result, result)
386
389
  end
390
+
391
+ it 'should update the host owner' do
392
+ params = ['--owner-type=Usergroup', '--owner=adminGroup']
393
+
394
+ api_expects_search(:usergroups, name: 'adminGroup').returns(
395
+ index_response([{ 'id' => '1' }])
396
+ )
397
+ api_expects(:hosts, :update, 'Update host with new owner').with_params(
398
+ 'id' => '1', 'location_id' => 1, 'organization_id' => 1, 'host' => {
399
+ 'owner_id' => '1', 'compute_attributes' => {}
400
+ }
401
+ ) do |par|
402
+ par['id'] == '1' &&
403
+ par['host']['owner_id'] == '1' &&
404
+ par['host']['compute_attributes'] == {}
405
+ end.returns(updated_host)
406
+
407
+ expected_result = success_result("Host updated.\n")
408
+
409
+ result = run_cmd(cmd + minimal_params + params)
410
+
411
+ assert_cmd(expected_result, result)
412
+ end
387
413
  end
388
414
 
389
415
  describe 'host config reports' do
@@ -0,0 +1,57 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper')
2
+
3
+ describe 'mail_notification' do
4
+ describe 'list' do
5
+ before do
6
+ @cmd = %w[mail-notification list]
7
+ @mail_notifications = [{
8
+ id: 1,
9
+ name: 'config_summary',
10
+ description: 'A summary of eventful configuration management reports',
11
+ subscription_type: 'report'
12
+ }]
13
+ end
14
+
15
+ it 'should return a list of mail notifications' do
16
+ api_expects(:mail_notifications, :index, 'List mail notifications').returns(@mail_notifications)
17
+
18
+ output = IndexMatcher.new([
19
+ %w[ID NAME],
20
+ %w[1 config_summary]
21
+ ])
22
+ expected_result = success_result(output)
23
+
24
+ result = run_cmd(@cmd)
25
+ assert_cmd(expected_result, result)
26
+ end
27
+ end
28
+
29
+ describe 'info' do
30
+ before do
31
+ @cmd = %w[mail-notification info]
32
+ @mail_notification = {
33
+ id: 1,
34
+ name: 'config_summary',
35
+ description: 'A summary of eventful configuration management reports',
36
+ subscription_type: 'report'
37
+ }
38
+ end
39
+
40
+ it 'should return the info of a mail notification' do
41
+ params = ['--id=1']
42
+ api_expects(:mail_notifications, :show, 'Info mail notification').returns(@mail_notification)
43
+
44
+ output = OutputMatcher.new([
45
+ 'Id: 1',
46
+ 'Name: config_summary',
47
+ 'Description: A summary of eventful configuration management reports',
48
+ 'Subscription type: report'
49
+ ])
50
+
51
+ expected_result = success_result(output)
52
+
53
+ result = run_cmd(@cmd + params)
54
+ assert_cmd(expected_result, result)
55
+ end
56
+ end
57
+ end
@@ -14,13 +14,36 @@ module HammerCLIForeman
14
14
  params
15
15
  end
16
16
 
17
- it 'allows minimal options' do
18
- api_expects(:subnets, :create) do |par|
19
- par['subnet']['name'] == 'net1'
20
- end
17
+ it 'should print error on missing --mask or --prefix' do
18
+ params = ['--name=net1']
19
+ cmd = %w[subnet create]
20
+
21
+ expected_result = usage_error_result(
22
+ cmd,
23
+ 'At least one of options --mask, --prefix is required.',
24
+ 'Could not create the subnet'
25
+ )
26
+
27
+ api_expects_no_call
28
+ result = run_cmd(cmd + params)
29
+ assert_cmd(expected_result, result)
30
+ end
31
+
32
+ it 'allows minimal options with mask for IPv4' do
33
+ api_expects(:subnets, :create).with_params(subnet_params(mask: '255.255.255.0'))
21
34
  run_cmd(%w(subnet create --name net1 --network=192.168.122.0 --mask=255.255.255.0))
22
35
  end
23
36
 
37
+ it 'allows minimal options with prefix for IPv4' do
38
+ api_expects(:subnets, :create).with_params(subnet_params(mask: '255.255.255.0'))
39
+ run_cmd(%w(subnet create --name net1 --network=192.168.122.0 --prefix=24))
40
+ end
41
+
42
+ it 'allows minimal options with prefix for IPv6' do
43
+ api_expects(:subnets, :create).with_params(subnet_params(cidr: '96'))
44
+ run_cmd(%w(subnet create --name net1 --network-type=IPv6 --network='::ffff:c0a8:7a00' --prefix=96))
45
+ end
46
+
24
47
  it 'allows dhcp id' do
25
48
  api_expects(:subnets, :create).with_params(subnet_params(:dhcp_id => 1))
26
49
  run_cmd(%w(subnet create --name net1 --network=192.168.122.0 --mask=255.255.255.0 --dhcp-id 1))
@@ -91,7 +114,7 @@ module HammerCLIForeman
91
114
  it 'allows organization names' do
92
115
  api_expects(:organizations, :index) do |p|
93
116
  p[:search] == "name = \"org1\" or name = \"org2\""
94
- end.returns(index_response([{'id' => 1}, {'id' => 2}]))
117
+ end.returns(index_response([{'id' => 1}, {'id' => 2}]))
95
118
  api_expects(:subnets, :create).with_params(subnet_params(:organization_ids => [1, 2]))
96
119
  run_cmd(%w(subnet create --name net1 --network=192.168.122.0 --mask=255.255.255.0 --organizations org1,org2))
97
120
  end
@@ -131,7 +131,7 @@ describe 'template' do
131
131
  it 'should create new combination with warning' do
132
132
  params = ['create','--provisioning-template-id=10', '--hostgroup-id=1', '--environment-id=1']
133
133
  expected_result = success_result("Template combination created.\n")
134
- expected_result.expected_err = "Warning: Option --environment-id is deprecated. Use --puppet-environment-id instead\n"
134
+ expected_result.expected_err = "Warning: Option --environment-id is deprecated. Use --puppet-environment[-id] instead\n"
135
135
  api_expects(:template_combinations, :create, 'Create template combination') do |params|
136
136
  params['provisioning_template_id'] == 10 &&
137
137
  params['hostgroup_id'] == 1 &&
@@ -160,7 +160,7 @@ describe 'template' do
160
160
  it 'should update combination with warning' do
161
161
  params = ['update', '--id=3', '--provisioning-template-id=10', '--hostgroup-id=1', '--environment-id=1']
162
162
  expected_result = success_result("Template combination updated.\n")
163
- expected_result.expected_err = "Warning: Option --environment-id is deprecated. Use --puppet-environment-id instead\n"
163
+ expected_result.expected_err = "Warning: Option --environment-id is deprecated. Use --puppet-environment[-id] instead\n"
164
164
  api_expects(:template_combinations, :update, 'Update template combination') do |params|
165
165
  params['id'] == '3' &&
166
166
  params['provisioning_template_id'] == 10 &&
@@ -0,0 +1,129 @@
1
+
2
+ require File.join(File.dirname(__FILE__), 'test_helper')
3
+
4
+ require 'hammer_cli_foreman/compute_resource'
5
+
6
+ describe HammerCLIForeman::VirtualMachine do
7
+
8
+ context "InfoCommand" do
9
+ before do
10
+ @cmd = ["compute-resource", "virtual-machine", "info"]
11
+ @vm = {
12
+ 'id' => 1,
13
+ 'name' => 'vm1',
14
+ 'provider' => 'Ovirt',
15
+ 'cpu' => 1,
16
+ 'memory' => 1,
17
+ 'status' => 'down'
18
+ }
19
+ end
20
+
21
+ it "should print error on missing --id and --vm-id" do
22
+ api_expects_no_call
23
+ result = run_cmd(@cmd)
24
+ assert_match("Missing arguments for '--id', '--vm-id'.\n", result.err)
25
+ end
26
+
27
+ it "should print error on missing --vm-id" do
28
+ params = ['--id=1']
29
+
30
+ api_expects_no_call
31
+ result = run_cmd(@cmd + params)
32
+ assert_match("Missing arguments for '--vm-id'.\n", result.err)
33
+ end
34
+
35
+ it "should return vm info" do
36
+ params = ['--id=1', '--vm-id=1']
37
+ api_expects(:compute_resources, :show_vm) do |par|
38
+ par['id'] == '1' && par['vm_id'] == '1'
39
+ end.returns(@vm)
40
+ result = run_cmd(@cmd + params)
41
+ assert_match("Id: 1\nName: vm1\nCPUs: 1\nMemory: 1\nStatus: down\n\n", result.out)
42
+ end
43
+
44
+ end
45
+
46
+ context "ListCommand" do
47
+ before do
48
+ @cmd = ["compute-resource", "virtual-machines"]
49
+ @available_virtual_machines = [
50
+ { id: 1, name: 'vm1' },
51
+ { id: 2, name: 'vm2' }
52
+ ]
53
+ end
54
+
55
+ it "should print error on missing --id" do
56
+ api_expects_no_call
57
+ result = run_cmd(@cmd)
58
+ assert_match("Missing arguments for '--id'.\n", result.err)
59
+ end
60
+
61
+ it "should return vms list" do
62
+ params = ['--id=1']
63
+ api_expects(:compute_resources, :available_virtual_machines) do |par|
64
+ par['id'] == '1'
65
+ end.returns(@available_virtual_machines)
66
+ result = run_cmd(@cmd + params)
67
+ assert_match(/-----|---\nNAME | ID\n-----|---\nvm1 | 1 \nvm2 | 2 \n-----|---\n/, result.out)
68
+ end
69
+ end
70
+
71
+ context "PowerCommand" do
72
+ before do
73
+ @cmd = ["compute-resource", "virtual-machine", "power"]
74
+ end
75
+
76
+ it "should print error on missing --id and --vm-id" do
77
+ api_expects_no_call
78
+ result = run_cmd(@cmd)
79
+ assert_match("Could not power the virtual machine:\n Missing arguments for '--id', '--vm-id'.\n", result.err)
80
+ end
81
+
82
+ it "should print error on missing --vm-id" do
83
+ params = ['--id=1']
84
+
85
+ api_expects_no_call
86
+ result = run_cmd(@cmd + params)
87
+ assert_match("Could not power the virtual machine:\n Missing arguments for '--vm-id'.\n", result.err)
88
+ end
89
+
90
+ it 'should power a virtual machine' do
91
+ params = ['--id=1', '--vm-id=1']
92
+ api_expects(:compute_resources, :power_vm) do |par|
93
+ par['id'] == '1' && par['vm_id'] == '1'
94
+ end.returns({})
95
+ result = run_cmd(@cmd + params)
96
+ assert_cmd(success_result("Virtual machine is powering.\n"), result)
97
+ end
98
+ end
99
+
100
+ context "DeleteCommand" do
101
+ before do
102
+ @cmd = ["compute-resource", "virtual-machine", "delete"]
103
+ end
104
+
105
+ it "should print error on missing --id and --vm-id" do
106
+ api_expects_no_call
107
+ result = run_cmd(@cmd)
108
+ assert_match("Could not delete the virtual machine:\n Missing arguments for '--id', '--vm-id'.\n", result.err)
109
+ end
110
+
111
+ it "should print error on missing --vm-id" do
112
+ params = ['--id=1']
113
+
114
+ api_expects_no_call
115
+ result = run_cmd(@cmd + params)
116
+ assert_match("Could not delete the virtual machine:\n Missing arguments for '--vm-id'.\n", result.err)
117
+ end
118
+
119
+ it 'delete virtual machine' do
120
+ params = ['--id=1', '--vm-id=1']
121
+ api_expects(:compute_resources, :destroy_vm) do |par|
122
+ par['id'] == '1' && par['vm_id'] == '1'
123
+ end.returns({})
124
+ result = run_cmd(@cmd + params)
125
+ assert_cmd(success_result("Virtual machine deleted.\n"), result)
126
+ end
127
+ end
128
+
129
+ end