foreman_ansible 6.1.1 → 6.3.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (139) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/ansible_roles_controller.rb +12 -8
  3. data/app/controllers/ansible_variables_controller.rb +4 -0
  4. data/app/controllers/api/v2/ansible_roles_controller.rb +32 -11
  5. data/app/controllers/api/v2/ansible_variables_controller.rb +6 -4
  6. data/app/controllers/foreman_ansible/concerns/import_controller_helper.rb +3 -3
  7. data/app/helpers/foreman_ansible/ansible_reports_helper.rb +9 -4
  8. data/app/helpers/foreman_ansible/ansible_roles_data_preparations.rb +95 -0
  9. data/app/helpers/foreman_ansible/ansible_roles_helper.rb +20 -1
  10. data/app/jobs/sync_roles_and_variables.rb +20 -0
  11. data/app/models/concerns/foreman_ansible/host_managed_extensions.rb +7 -4
  12. data/app/models/concerns/foreman_ansible/hostgroup_extensions.rb +8 -5
  13. data/app/models/foreman_ansible/ansible_provider.rb +2 -1
  14. data/app/models/host_ansible_role.rb +1 -0
  15. data/app/models/hostgroup_ansible_role.rb +1 -0
  16. data/app/models/setting/ansible.rb +9 -0
  17. data/app/services/foreman_ansible/ansible_report_importer.rb +4 -0
  18. data/app/services/foreman_ansible/ansible_report_scanner.rb +15 -1
  19. data/app/services/foreman_ansible/api_roles_importer.rb +23 -11
  20. data/app/services/foreman_ansible/fact_parser.rb +21 -1
  21. data/app/services/foreman_ansible/import_roles_and_variables_error_notification.rb +31 -0
  22. data/app/services/foreman_ansible/import_roles_and_variables_success_notification.rb +26 -0
  23. data/app/services/foreman_ansible/inventory_creator.rb +3 -7
  24. data/app/services/foreman_ansible/operating_system_parser.rb +1 -1
  25. data/app/services/foreman_ansible/roles_importer.rb +3 -2
  26. data/app/services/foreman_ansible/ui_roles_importer.rb +2 -2
  27. data/app/services/foreman_ansible/variables_importer.rb +36 -1
  28. data/app/views/ansible_roles/import.html.erb +10 -50
  29. data/app/views/ansible_variables/index.html.erb +1 -2
  30. data/app/views/api/v2/ansible_roles/sync.json.rabl +5 -0
  31. data/app/views/foreman_ansible/ansible_roles/_select_tab_content.html.erb +8 -17
  32. data/app/views/foreman_ansible/config_reports/_ansible.html.erb +13 -6
  33. data/app/views/foreman_ansible/job_templates/ansible_collections_-_install_from_galaxy.erb +28 -0
  34. data/app/views/foreman_ansible/job_templates/ansible_roles_-_install_from_galaxy.erb +1 -1
  35. data/app/views/foreman_ansible/job_templates/convert_to_rhel.erb +44 -0
  36. data/config/routes.rb +5 -4
  37. data/db/migrate/20210120150019_add_position_to_ansible_role.rb +27 -0
  38. data/db/seeds.d/100_common_parameters.rb +7 -0
  39. data/db/seeds.d/90_notification_blueprints.rb +19 -1
  40. data/lib/foreman_ansible/engine.rb +1 -0
  41. data/lib/foreman_ansible/register.rb +8 -7
  42. data/lib/foreman_ansible/version.rb +1 -1
  43. data/locale/ca/LC_MESSAGES/foreman_ansible.mo +0 -0
  44. data/locale/ca/foreman_ansible.po +1 -1
  45. data/locale/cs_CZ/LC_MESSAGES/foreman_ansible.mo +0 -0
  46. data/locale/cs_CZ/foreman_ansible.po +1 -1
  47. data/locale/de/LC_MESSAGES/foreman_ansible.mo +0 -0
  48. data/locale/de/foreman_ansible.po +1 -1
  49. data/locale/en/LC_MESSAGES/foreman_ansible.mo +0 -0
  50. data/locale/en/foreman_ansible.po +1 -1
  51. data/locale/en_GB/LC_MESSAGES/foreman_ansible.mo +0 -0
  52. data/locale/en_GB/foreman_ansible.po +1 -1
  53. data/locale/es/LC_MESSAGES/foreman_ansible.mo +0 -0
  54. data/locale/es/foreman_ansible.po +1 -1
  55. data/locale/fr/LC_MESSAGES/foreman_ansible.mo +0 -0
  56. data/locale/fr/foreman_ansible.po +1 -1
  57. data/locale/gl/LC_MESSAGES/foreman_ansible.mo +0 -0
  58. data/locale/gl/foreman_ansible.po +1 -1
  59. data/locale/it/LC_MESSAGES/foreman_ansible.mo +0 -0
  60. data/locale/it/foreman_ansible.po +1 -1
  61. data/locale/ja/LC_MESSAGES/foreman_ansible.mo +0 -0
  62. data/locale/ja/foreman_ansible.po +1 -1
  63. data/locale/ko/LC_MESSAGES/foreman_ansible.mo +0 -0
  64. data/locale/ko/foreman_ansible.po +1 -1
  65. data/locale/nl_NL/LC_MESSAGES/foreman_ansible.mo +0 -0
  66. data/locale/nl_NL/foreman_ansible.po +1 -1
  67. data/locale/pl/LC_MESSAGES/foreman_ansible.mo +0 -0
  68. data/locale/pl/foreman_ansible.po +1 -1
  69. data/locale/pt_BR/LC_MESSAGES/foreman_ansible.mo +0 -0
  70. data/locale/pt_BR/foreman_ansible.po +1 -1
  71. data/locale/ru/LC_MESSAGES/foreman_ansible.mo +0 -0
  72. data/locale/ru/foreman_ansible.po +1 -1
  73. data/locale/sv_SE/LC_MESSAGES/foreman_ansible.mo +0 -0
  74. data/locale/sv_SE/foreman_ansible.po +1 -1
  75. data/locale/zh_CN/LC_MESSAGES/foreman_ansible.mo +0 -0
  76. data/locale/zh_CN/foreman_ansible.po +1 -1
  77. data/locale/zh_TW/LC_MESSAGES/foreman_ansible.mo +0 -0
  78. data/locale/zh_TW/foreman_ansible.po +1 -1
  79. data/package.json +11 -42
  80. data/test/factories/ansible_roles.rb +12 -0
  81. data/test/factories/host_ansible_enhancements.rb +23 -0
  82. data/test/foreman_ansible/helpers/ansible_roles_helper_test.rb +45 -0
  83. data/test/functional/ansible_variables_controller_test.rb +0 -15
  84. data/test/functional/api/v2/ansible_roles_controller_test.rb +23 -0
  85. data/test/functional/api/v2/hostgroups_controller_test.rb +5 -4
  86. data/test/functional/api/v2/hosts_controller_test.rb +9 -8
  87. data/test/functional/hosts_controller_test.rb +10 -6
  88. data/test/unit/concerns/host_managed_extensions_test.rb +17 -3
  89. data/test/unit/concerns/hostgroup_extensions_test.rb +13 -7
  90. data/test/unit/host_ansible_role_test.rb +2 -1
  91. data/test/unit/hostgroup_ansible_role_test.rb +15 -1
  92. data/test/unit/ignore_roles_test.rb +43 -0
  93. data/test/unit/import_roles_and_variables.rb +60 -0
  94. data/test/unit/services/ansible_variables_importer_test.rb +13 -0
  95. data/test/unit/services/api_roles_importer_test.rb +22 -5
  96. data/test/unit/services/fact_parser_test.rb +16 -0
  97. data/test/unit/services/inventory_creator_test.rb +15 -7
  98. data/test/unit/services/ui_roles_importer_test.rb +2 -2
  99. data/webpack/__mocks__/foremanReact/common/helpers.js +13 -0
  100. data/webpack/__mocks__/foremanReact/components/common/forms/OrderableSelect/helpers.js +5 -0
  101. data/webpack/__mocks__/foremanReact/redux/API.js +7 -0
  102. data/webpack/components/AnsibleRolesAndVariables/AnsibleRolesAndVariables.js +178 -0
  103. data/webpack/components/AnsibleRolesAndVariables/AnsibleRolesAndVariables.scss +9 -0
  104. data/webpack/components/AnsibleRolesAndVariables/AnsibleRolesAndVariablesActions.js +39 -0
  105. data/webpack/components/AnsibleRolesAndVariables/AnsibleRolesAndVariablesConstants.js +4 -0
  106. data/webpack/components/AnsibleRolesAndVariables/AnsibleRolesAndVariablesHelpers.js +11 -0
  107. data/webpack/components/AnsibleRolesAndVariables/__test__/AnsibleRolesAndVariablesHelpers.test.js +50 -0
  108. data/webpack/components/AnsibleRolesAndVariables/__test__/AnsibleRolesAndVariablesImport.test.js +39 -0
  109. data/webpack/components/AnsibleRolesAndVariables/__test__/__snapshots__/AnsibleRolesAndVariablesImport.test.js.snap +177 -0
  110. data/webpack/components/AnsibleRolesAndVariables/index.js +31 -0
  111. data/webpack/components/AnsibleRolesSwitcher/AnsibleRolesSwitcher.js +25 -27
  112. data/webpack/components/AnsibleRolesSwitcher/AnsibleRolesSwitcher.scss +1 -1
  113. data/webpack/components/AnsibleRolesSwitcher/AnsibleRolesSwitcherActions.js +20 -14
  114. data/webpack/components/AnsibleRolesSwitcher/AnsibleRolesSwitcherConstants.js +1 -2
  115. data/webpack/components/AnsibleRolesSwitcher/AnsibleRolesSwitcherReducer.js +13 -8
  116. data/webpack/components/AnsibleRolesSwitcher/AnsibleRolesSwitcherSelectors.js +8 -28
  117. data/webpack/components/AnsibleRolesSwitcher/__fixtures__/ansibleRolesSwitcherReducer.fixtures.js +1 -0
  118. data/webpack/components/AnsibleRolesSwitcher/__tests__/AnsibleRolesSwitcher.test.js +6 -4
  119. data/webpack/components/AnsibleRolesSwitcher/__tests__/AnsibleRolesSwitcherReducer.test.js +1 -9
  120. data/webpack/components/AnsibleRolesSwitcher/__tests__/AnsibleRolesSwitcherSelectors.test.js +3 -16
  121. data/webpack/components/AnsibleRolesSwitcher/__tests__/__snapshots__/AnsibleRolesSwitcher.test.js.snap +4 -8
  122. data/webpack/components/AnsibleRolesSwitcher/__tests__/__snapshots__/AnsibleRolesSwitcherReducer.test.js.snap +8 -97
  123. data/webpack/components/AnsibleRolesSwitcher/__tests__/__snapshots__/AnsibleRolesSwitcherSelectors.test.js.snap +5 -30
  124. data/webpack/components/AnsibleRolesSwitcher/components/AnsiblePermissionDenied.test.js +1 -1
  125. data/webpack/components/AnsibleRolesSwitcher/components/AnsibleRole.js +5 -2
  126. data/webpack/components/AnsibleRolesSwitcher/components/AnsibleRole.test.js +1 -1
  127. data/webpack/components/AnsibleRolesSwitcher/components/AnsibleRoleInputs.js +43 -0
  128. data/webpack/components/AnsibleRolesSwitcher/components/AnsibleRoleInputs.test.js +19 -0
  129. data/webpack/components/AnsibleRolesSwitcher/components/AssignedRolesList.js +63 -51
  130. data/webpack/components/AnsibleRolesSwitcher/components/AssignedRolesList.test.js +4 -1
  131. data/webpack/components/AnsibleRolesSwitcher/components/AvailableRolesList.test.js +1 -1
  132. data/webpack/components/AnsibleRolesSwitcher/components/OrderedRolesTooltip.js +23 -0
  133. data/webpack/components/AnsibleRolesSwitcher/components/__snapshots__/AnsibleRoleInputs.test.js.snap +51 -0
  134. data/webpack/components/AnsibleRolesSwitcher/components/__snapshots__/AssignedRolesList.test.js.snap +28 -28
  135. data/webpack/components/AnsibleRolesSwitcher/index.js +4 -8
  136. data/webpack/index.js +6 -13
  137. metadata +63 -18
  138. data/app/views/ansible_variables/import.html.erb +0 -56
  139. data/webpack/test_setup.js +0 -12
@@ -12,7 +12,8 @@ class HostAnsibleRoleTest < ActiveSupport::TestCase
12
12
  describe 'uniqueness' do
13
13
  subject do
14
14
  HostAnsibleRole.new(:host => FactoryBot.build(:host),
15
- :ansible_role => FactoryBot.build(:ansible_role))
15
+ :ansible_role => FactoryBot.build(:ansible_role),
16
+ :position => 0)
16
17
  end
17
18
  should validate_uniqueness_of(:ansible_role_id).scoped_to(:host_id)
18
19
  end
@@ -13,9 +13,23 @@ class HostgroupAnsibleRoleTest < ActiveSupport::TestCase
13
13
  subject do
14
14
  HostgroupAnsibleRole.new(
15
15
  :hostgroup => FactoryBot.build(:hostgroup),
16
- :ansible_role => FactoryBot.build(:ansible_role)
16
+ :ansible_role => FactoryBot.build(:ansible_role),
17
+ :position => 0
17
18
  )
18
19
  end
19
20
  should validate_uniqueness_of(:ansible_role_id).scoped_to(:hostgroup_id)
20
21
  end
22
+
23
+ describe 'ordering' do
24
+ test 'should list roles in correct order' do
25
+ hg = FactoryBot.create(:hostgroup)
26
+ 5.times do |idx|
27
+ HostgroupAnsibleRole.create(:hostgroup => hg, :ansible_role => FactoryBot.create(:ansible_role), :position => idx)
28
+ end
29
+
30
+ hg.hostgroup_ansible_roles.each_with_index do |item, idx|
31
+ assert_equal(item.position, idx + 1)
32
+ end
33
+ end
34
+ end
21
35
  end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_plugin_helper'
4
+
5
+ # Tests for testing the ignore list
6
+ class IgnoreRolesTest < ActiveSupport::TestCase
7
+ include ForemanAnsible::AnsibleRolesDataPreparations
8
+
9
+ role1 = AnsibleRole.new(:name => 'foo')
10
+ role2 = AnsibleRole.new(:name => 'bar')
11
+ role3 = AnsibleRole.new(:name => 'hello.txt')
12
+ role4 = AnsibleRole.new(:name => 'hello.yml')
13
+ role5 = AnsibleRole.new(:name => 'foo.yml')
14
+
15
+ describe 'It should Ignore Role' do
16
+ test 'If role starts with h' do
17
+ Setting.stubs(:[]).with(:ansible_roles_to_ignore).returns(['h*'])
18
+ assert_not role_match_excluded_roles(role1.name)
19
+ assert_not role_match_excluded_roles(role2.name)
20
+ assert role_match_excluded_roles(role3.name)
21
+ assert role_match_excluded_roles(role4.name)
22
+ assert_not role_match_excluded_roles(role5.name)
23
+ end
24
+
25
+ test 'If role ends with .yml' do
26
+ Setting.stubs(:[]).with(:ansible_roles_to_ignore).returns(['*.yml'])
27
+ assert_not role_match_excluded_roles(role1.name)
28
+ assert_not role_match_excluded_roles(role2.name)
29
+ assert_not role_match_excluded_roles(role3.name)
30
+ assert role_match_excluded_roles(role4.name)
31
+ assert role_match_excluded_roles(role5.name)
32
+ end
33
+
34
+ test 'If role have o char in the middle' do
35
+ Setting.stubs(:[]).with(:ansible_roles_to_ignore).returns(['*o*'])
36
+ assert role_match_excluded_roles(role1.name)
37
+ assert_not role_match_excluded_roles(role2.name)
38
+ assert role_match_excluded_roles(role3.name)
39
+ assert role_match_excluded_roles(role4.name)
40
+ assert role_match_excluded_roles(role5.name)
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_plugin_helper'
4
+
5
+ # Tests for testing the ignore list
6
+ class ImportRolesAndVariables < ActiveSupport::TestCase
7
+ include ForemanAnsible::AnsibleRolesDataPreparations
8
+
9
+ # existing Roles -> should be updated
10
+ role1 = AnsibleRole.new(:name => 'ntp', :id => 10)
11
+ role2 = AnsibleRole.new(:name => 'bar', :id => 11)
12
+
13
+ # New Roles -> Should be imported
14
+ role3 = AnsibleRole.new(:name => 'foo', :id => nil)
15
+ role4 = AnsibleRole.new(:name => 'world', :id => nil)
16
+
17
+ # Existing Role -> should be removed
18
+ role5 = AnsibleRole.new(:name => 'h', :id => 12)
19
+
20
+ # role1 variables -> add
21
+ variable1 = AnsibleVariable.new(:key => 'ntp_statistics', :ansible_role_id => 10, :id => nil)
22
+ variable2 = AnsibleVariable.new(:key => 'ntp_broadcast', :ansible_role_id => 10, :id => nil)
23
+
24
+ # role1 variables -> update
25
+ variable3 = AnsibleVariable.new(:key => 'ntp_statistics', :ansible_role_id => 10, :id => 4)
26
+ variable4 = AnsibleVariable.new(:key => 'ntp_crypto', :ansible_role_id => 10, :id => 5)
27
+
28
+ # role1 variables -> obsolete
29
+ variable5 = AnsibleVariable.new(:key => 'ntp_controlkey', :ansible_role_id => 10, :id => 6)
30
+ variable6 = AnsibleVariable.new(:key => 'ntp_key', :ansible_role_id => 10, :id => 7)
31
+
32
+ # role2 variable -> Update
33
+ variable7 = AnsibleVariable.new(:key => 'bar_key', :ansible_role_id => 11, :id => 7, :override => false, :hidden_value => true)
34
+ changed = { 'old' => [role1, role2], 'new' => [role3, role4], 'obsolete' => [role5] }
35
+ old_roles_variables = { 'new' => [variable1, variable2], 'update' => [variable3, variable4, variable7], 'obsolete' => [variable5, variable6] }
36
+
37
+ describe 'It should prepare the rows correctly' do
38
+ setup do
39
+ @variables_importer = mock('mock_variables_importer')
40
+ end
41
+
42
+ test 'prepare 5 rows' do
43
+ expected = [{ :cells => [role1.name, 'Update Role Variables', 'Add: 2, Remove: 2, Update: 2', '', ''],
44
+ :role => role1, :kind => 'old', :id => role1.name },
45
+ { :cells => [role2.name, 'Update Role Variables', 'Update: 1', '', ''], :role => role2, :kind => 'old', :id => role2.name },
46
+ { :cells => [role3.name, 'Import Role', 'Add: 3', '', ''], :role => role3, :kind => 'new', :id => role3.name },
47
+ { :cells => [role4.name, 'Import Role', 'Add: 2', '', ''], :role => role4, :kind => 'new', :id => role4.name },
48
+ { :cells => [role5.name, 'Remove Role', '', 0, 0], :role => role5, :kind => 'obsolete', :id => role5.name }]
49
+ # role3 and role4 importing new variables
50
+ @variables_importer.expects(:get_variables_names).with(role3.name).returns({ 'a' => true, 'b' => false, 'c' => [] })
51
+ @variables_importer.expects(:get_variables_names).with(role4.name).returns({ 'd' => true, 'e' => false })
52
+
53
+ @variables_importer.expects(:import_variable_names).with([role3, role4]).returns(old_roles_variables)
54
+ @variables_importer.expects(:import_variable_names).with([role1, role2]).returns(old_roles_variables)
55
+ @variables_importer.expects(:import_variable_names).with([role5]).returns(old_roles_variables)
56
+ result = prepare_ansible_import_rows(changed, @variables_importer)
57
+ assert_equal result, expected
58
+ end
59
+ end
60
+ end
@@ -59,6 +59,19 @@ class AnsibleVariablesImporterTest < ActiveSupport::TestCase
59
59
  assert_empty changes['update']
60
60
  end
61
61
 
62
+ test 'sets hidden value to false by default' do
63
+ role = FactoryBot.create(:ansible_role)
64
+ variable = FactoryBot.create(:ansible_variable)
65
+ api_response = {
66
+ role.name => { variable.key => 'new value' }
67
+ }
68
+ changes = importer.import_variables(api_response, [role.name])
69
+ assert_empty changes['update']
70
+ assert_not_empty changes['new']
71
+ assert_equal variable.key, changes['new'].first.key
72
+ assert_not changes['new'].first.hidden_value?
73
+ end
74
+
62
75
  test 'reimports variable with same key for different role' do
63
76
  role = FactoryBot.create(:ansible_role)
64
77
  variable = FactoryBot.create(:ansible_variable)
@@ -12,11 +12,28 @@ class ApiRolesImporterTest < ActiveSupport::TestCase
12
12
  end
13
13
 
14
14
  test 'should import roles' do
15
- @importer.stubs(:import_role_names).returns(:new => @test_roles)
16
- res = @importer.import!
17
- assert_equal 2, res.count
18
- assert AnsibleRole.find_by :name => @test_roles.first.name
19
- assert AnsibleRole.find_by :name => @test_roles.last.name
15
+ @importer.stubs(:prepare_ansible_import_rows).returns(
16
+ [{
17
+ :name => 'test_user.test_name',
18
+ :id => nil,
19
+ :role_action => 'Import Role',
20
+ :variables => 'Add: 7',
21
+ :hosts_count => '',
22
+ :hostgroup_count => '',
23
+ :kind => 'new'
24
+ },
25
+ {
26
+ :name => 'some_user.some_role',
27
+ :id => nil,
28
+ :role_action => 'Import Role',
29
+ :variables => 'Add: 3',
30
+ :hosts_count => '',
31
+ :hostgroup_count => '',
32
+ :kind => 'new'
33
+ }]
34
+ )
35
+ res = @importer.import!([@test_roles.first.name, @test_roles.last.name])
36
+ assert_equal 2, res['changed']['new'].count
20
37
  end
21
38
 
22
39
  test 'should obsolete roles' do
@@ -29,6 +29,22 @@ module ForemanAnsible
29
29
  refute @facts_parser.environment
30
30
  end
31
31
 
32
+ test 'calculates virtual reported data' do
33
+ refute @facts_parser.virtual
34
+ end
35
+
36
+ test 'calculates ram reported data' do
37
+ assert_equal 7899, @facts_parser.ram
38
+ end
39
+
40
+ test 'calculates sockets reported data' do
41
+ assert_equal 1, @facts_parser.sockets
42
+ end
43
+
44
+ test 'calculates cores reported data' do
45
+ assert_equal 2, @facts_parser.cores
46
+ end
47
+
32
48
  test 'creates operatingsystem from operating system options' do
33
49
  sample_mock = mock
34
50
  major_fact = @facts_parser.facts['ansible_distribution_major_version']
@@ -61,8 +61,7 @@ module ForemanAnsible
61
61
  connection_params['ansible_user']
62
62
  refute_equal Setting['remote_execution_ssh_port'],
63
63
  connection_params['ansible_port']
64
- assert_equal ForemanRemoteExecutionCore.settings[:ssh_identity_key_file],
65
- connection_params['ansible_ssh_private_key_file']
64
+ assert_nil connection_params['ansible_ssh_private_key_file']
66
65
  assert_equal extra_options['ansible_port'],
67
66
  connection_params['ansible_port']
68
67
  assert_equal Setting['ansible_winrm_server_cert_validation'],
@@ -128,11 +127,15 @@ module ForemanAnsible
128
127
  end
129
128
 
130
129
  test 'ansible_roles are passed as top-level "hostvars"' do
130
+ host = FactoryBot.create(:host)
131
131
  roles = [].tap do |array|
132
- 2.times { array << FactoryBot.create(:ansible_role) }
132
+ 2.times do |idx|
133
+ role = FactoryBot.create(:ansible_role)
134
+ FactoryBot.create(:host_ansible_role, :host_id => host.id, :ansible_role_id => role.id, :position => idx)
135
+ array << role
136
+ end
133
137
  end
134
138
 
135
- host = FactoryBot.create(:host, :ansible_roles => roles)
136
139
  inventory = ForemanAnsible::InventoryCreator.new([host]).to_hash
137
140
  inventory_roles = inventory['_meta']['hostvars'][host.name]['foreman_ansible_roles']
138
141
  assert_equal 2, inventory_roles.count
@@ -156,7 +159,9 @@ module ForemanAnsible
156
159
  end
157
160
  end
158
161
 
159
- host = FactoryBot.create(:host, :ansible_roles => [role])
162
+ host = FactoryBot.create(:host)
163
+ FactoryBot.create(:host_ansible_role, :host_id => host.id, :ansible_role_id => role.id, :position => 0)
164
+
160
165
  inventory = ForemanAnsible::InventoryCreator.new([host]).to_hash
161
166
  host_inventory = inventory['_meta']['hostvars'][host.name]
162
167
  assert host_inventory[variables.first.key]
@@ -174,7 +179,9 @@ module ForemanAnsible
174
179
  end
175
180
  end
176
181
 
177
- host = FactoryBot.create(:host, :ansible_roles => [role])
182
+ host = FactoryBot.create(:host)
183
+ FactoryBot.create(:host_ansible_role, :host_id => host.id, :ansible_role_id => role.id, :position => 0)
184
+
178
185
  inventory = ForemanAnsible::InventoryCreator.new([host]).to_hash
179
186
  inventory_roles = inventory['_meta']['hostvars'][host.name]['foreman']
180
187
  refute inventory_roles[variables.first.key]
@@ -188,7 +195,8 @@ module ForemanAnsible
188
195
  :ansible_role_id => role.id,
189
196
  :default_value => "variable value",
190
197
  :override => true)
191
- host = FactoryBot.create(:host, :ansible_roles => [role])
198
+ host = FactoryBot.create(:host)
199
+ FactoryBot.create(:host_ansible_role, :host_id => host.id, :ansible_role_id => role.id, :position => 0)
192
200
  host.expects(:host_params).returns('test_var' => 'param value').at_least_once
193
201
  inventory = ForemanAnsible::InventoryCreator.new([host]).to_hash
194
202
  assert_equal 'variable value', inventory['_meta']['hostvars'][host.name]['test_var']
@@ -26,7 +26,7 @@ class UiRolesImporterTest < ActiveSupport::TestCase
26
26
  @role = FactoryBot.create(:ansible_role)
27
27
  new_role_name = 'test_role.foreman'
28
28
  @new_role = { :id => nil, :name => new_role_name }
29
- @changes = { 'new' => { 'test_role.foreman' => @new_role.to_json },
30
- 'obsolete' => { @role.name => @role.to_json } }
29
+ @changes = { 'new' => { 'test_role.foreman' => @new_role },
30
+ 'obsolete' => { @role.name => @role } }
31
31
  end
32
32
  end
@@ -0,0 +1,13 @@
1
+ import { camelCase } from 'lodash';
2
+
3
+ export const propsToCamelCase = ob =>
4
+ propsToCase(camelCase, 'propsToCamelCase only takes objects', ob);
5
+
6
+ const propsToCase = (casingFn, errorMsg, ob) => {
7
+ if (typeof ob !== 'object') throw Error(errorMsg);
8
+
9
+ return Object.keys(ob).reduce((memo, key) => {
10
+ memo[casingFn(key)] = ob[key];
11
+ return memo;
12
+ }, {});
13
+ };
@@ -0,0 +1,5 @@
1
+ import React from 'react';
2
+
3
+ export const orderable = (Component, orderableProps) => props => (
4
+ <Component {...orderableProps} {...props} />
5
+ );
@@ -0,0 +1,7 @@
1
+ export const API = {
2
+ get: jest.fn(),
3
+ put: jest.fn(),
4
+ post: jest.fn(),
5
+ delete: jest.fn(),
6
+ patch: jest.fn(),
7
+ };
@@ -0,0 +1,178 @@
1
+ import React, { useState } from 'react';
2
+ import {
3
+ Table,
4
+ TableHeader,
5
+ TableBody,
6
+ cellWidth,
7
+ } from '@patternfly/react-table';
8
+ import {
9
+ Button,
10
+ Toolbar,
11
+ ToolbarGroup,
12
+ ToolbarContent,
13
+ ToolbarItem,
14
+ Checkbox,
15
+ Pagination,
16
+ } from '@patternfly/react-core';
17
+
18
+ import PropTypes from 'prop-types';
19
+ import { DEFAULT_PER_PAGE } from './AnsibleRolesAndVariablesConstants';
20
+ import './AnsibleRolesAndVariables.scss';
21
+
22
+ const ImportRolesAndVariablesTable = ({
23
+ columnsData,
24
+ rowsData,
25
+ proxy,
26
+ onSubmit,
27
+ onCancel,
28
+ }) => {
29
+ const columns = columnsData.map(col => ({
30
+ ...col,
31
+ transforms: [cellWidth(10)],
32
+ }));
33
+ const [rows, setRows] = useState(rowsData);
34
+
35
+ const [isChecked, setIsChecked] = useState(false);
36
+ const [selectedRowsCount, setSelectRowsCount] = useState(0);
37
+
38
+ const [page, setPage] = useState();
39
+ const [perPage, setPerPage] = useState(DEFAULT_PER_PAGE);
40
+ const [paginatedRows, setPaginatedRows] = useState(rows.slice(0, perPage));
41
+
42
+ const onSelect = (event, isSelected, rowId, row) => {
43
+ const selectableRowLength = rows.filter(
44
+ tempRow => tempRow.parent === undefined
45
+ ).length;
46
+ let rowsCount = selectedRowsCount;
47
+ const tempRows = rows.map(tempRow => {
48
+ if (rowId === -1) {
49
+ tempRow.selected = isSelected;
50
+ setIsChecked(isSelected);
51
+ setSelectRowsCount(isSelected ? selectableRowLength : 0);
52
+ } else if (tempRow.id === row.id) {
53
+ tempRow.selected = isSelected;
54
+ rowsCount = isSelected ? selectedRowsCount + 1 : selectedRowsCount - 1;
55
+ if (rowsCount > 0 && rowsCount === selectableRowLength) {
56
+ setIsChecked(true);
57
+ } else {
58
+ setIsChecked(false);
59
+ }
60
+ setSelectRowsCount(rowsCount);
61
+ }
62
+ return tempRow;
63
+ });
64
+ setRows(tempRows);
65
+ };
66
+
67
+ const SelectAll = checked => {
68
+ onSelect(null, checked, -1);
69
+ };
70
+
71
+ const handleSetPage = (event, newPage) => {
72
+ const startIdx = (newPage - 1) * perPage;
73
+ const endIdx =
74
+ rows.length < newPage * perPage ? rows.length : newPage * perPage;
75
+ setPage(newPage);
76
+ setPaginatedRows(rows.slice(startIdx, endIdx));
77
+ };
78
+
79
+ const handlePerPageSelect = (
80
+ event,
81
+ newPerPage,
82
+ newPage,
83
+ startIdx,
84
+ endIdx
85
+ ) => {
86
+ setPerPage(newPerPage);
87
+ setPage(newPage);
88
+ setPaginatedRows(rows.slice(startIdx, endIdx));
89
+ };
90
+
91
+ const renderPagination = (variant = 'top') => (
92
+ <Pagination
93
+ isCompact
94
+ itemCount={rows.length}
95
+ page={page}
96
+ perPage={perPage}
97
+ defaultToFullPage
98
+ onSetPage={handleSetPage}
99
+ onPerPageSelect={handlePerPageSelect}
100
+ perPageOptions={[
101
+ { title: '3', value: 3 },
102
+ { title: '5', value: 5 },
103
+ { title: '10', value: 10 },
104
+ ]}
105
+ titles={{
106
+ paginationTitle: `${variant} pagination`,
107
+ }}
108
+ />
109
+ );
110
+
111
+ const renderSelectAll = () => (
112
+ <Toolbar>
113
+ <ToolbarContent>
114
+ <ToolbarGroup variant="icon-button-group">
115
+ <ToolbarItem>
116
+ <Checkbox
117
+ isChecked={isChecked}
118
+ onChange={SelectAll}
119
+ aria-label="select all checkbox"
120
+ id="select-all"
121
+ name="select-all"
122
+ label={isChecked ? 'Deselect all' : 'Select all'}
123
+ />
124
+ </ToolbarItem>
125
+ </ToolbarGroup>
126
+ </ToolbarContent>
127
+ </Toolbar>
128
+ );
129
+
130
+ const renderSubmitAndCancel = () => (
131
+ <div className="submit-cancel-btns">
132
+ <br />
133
+ <br />
134
+ <Button variant="primary" onClick={() => onSubmit(rows, proxy)}>
135
+ Submit
136
+ </Button>
137
+ <Button variant="secondary" onClick={onCancel}>
138
+ Cancel
139
+ </Button>
140
+ </div>
141
+ );
142
+
143
+ return (
144
+ <div id="import-ansible-roles-variables">
145
+ {renderSelectAll()}
146
+ <Table
147
+ aria-label="import roles and variables"
148
+ onSelect={onSelect}
149
+ rows={paginatedRows}
150
+ cells={columns}
151
+ canSelectAll={false}
152
+ >
153
+ <TableHeader />
154
+ <TableBody />
155
+ </Table>
156
+ {renderPagination()}
157
+ {renderSubmitAndCancel()}
158
+ </div>
159
+ );
160
+ };
161
+
162
+ ImportRolesAndVariablesTable.defaultProps = {
163
+ columnsData: undefined,
164
+ rowsData: [],
165
+ proxy: undefined,
166
+ onSubmit: undefined,
167
+ onCancel: undefined,
168
+ };
169
+
170
+ ImportRolesAndVariablesTable.propTypes = {
171
+ columnsData: PropTypes.array,
172
+ rowsData: PropTypes.array,
173
+ proxy: PropTypes.number,
174
+ onSubmit: PropTypes.func,
175
+ onCancel: PropTypes.func,
176
+ };
177
+
178
+ export default ImportRolesAndVariablesTable;