foreman_templates 7.0.6 → 9.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/api/v2/template_controller.rb +8 -2
  3. data/app/controllers/concerns/foreman/controller/parameters/template_params.rb +22 -2
  4. data/app/controllers/ui_template_syncs_controller.rb +2 -2
  5. data/app/models/setting/template_sync.rb +12 -2
  6. data/app/services/foreman_templates/export_result.rb +20 -29
  7. data/app/services/foreman_templates/template_exporter.rb +40 -32
  8. data/app/services/foreman_templates/template_importer.rb +20 -4
  9. data/app/views/template_syncs/index.html.erb +20 -19
  10. data/app/views/ui_template_syncs/template_export_result.rabl +4 -4
  11. data/db/migrate/20180627134929_change_lock_setting.rb +5 -0
  12. data/lib/foreman_templates/engine.rb +6 -0
  13. data/lib/foreman_templates/version.rb +1 -1
  14. data/package.json +15 -21
  15. data/webpack/ForemanTemplates.js +17 -13
  16. data/webpack/__mocks__/foremanReact/components/Layout/LayoutSelectors.js +4 -0
  17. data/webpack/__mocks__/foremanReact/components/common/forms/ForemanForm.js +2 -0
  18. data/webpack/components/NewTemplateSync/__fixtures__/templateSyncSettings.fixtures.js +4 -4
  19. data/webpack/components/NewTemplateSync/__tests__/__snapshots__/NewTemplateSync.test.js.snap +2 -2
  20. data/webpack/components/NewTemplateSync/components/NewTemplateSyncForm/NewTemplateSyncForm.js +89 -53
  21. data/webpack/components/NewTemplateSync/components/NewTemplateSyncForm/NewTemplateSyncFormSelectors.js +10 -13
  22. data/webpack/components/NewTemplateSync/components/NewTemplateSyncForm/__tests__/NewTemplateSyncFormSelectors.test.js +1 -19
  23. data/webpack/components/NewTemplateSync/components/NewTemplateSyncForm/__tests__/__snapshots__/NewTemplateSyncFormSelectors.test.js.snap +7 -36
  24. data/webpack/components/NewTemplateSync/components/NewTemplateSyncForm/index.js +7 -16
  25. data/webpack/components/NewTemplateSync/components/SyncSettingField.js +5 -11
  26. data/webpack/components/NewTemplateSync/components/SyncSettingFields.js +8 -25
  27. data/webpack/components/NewTemplateSync/components/TextButtonField/index.js +27 -20
  28. data/webpack/components/NewTemplateSync/components/__tests__/SyncSettingField.test.js +2 -1
  29. data/webpack/components/NewTemplateSync/components/__tests__/SyncSettingFields.test.js +1 -0
  30. data/webpack/components/NewTemplateSync/components/__tests__/TextButtonField.test.js +4 -4
  31. data/webpack/components/NewTemplateSync/components/__tests__/__snapshots__/SyncSettingField.test.js.snap +18 -27
  32. data/webpack/components/NewTemplateSync/components/__tests__/__snapshots__/SyncSettingFields.test.js.snap +5 -3
  33. data/webpack/components/NewTemplateSync/components/__tests__/__snapshots__/TextButtonField.test.js.snap +8 -91
  34. data/webpack/components/TemplateSyncResult/__fixtures__/templateSyncResult.fixtures.js +2 -2
  35. data/webpack/components/TemplateSyncResult/__tests__/__snapshots__/TemplateSyncResult.test.js.snap +3 -1
  36. data/webpack/components/TemplateSyncResult/__tests__/__snapshots__/TemplateSyncResultReducer.test.js.snap +3 -1
  37. data/webpack/components/TemplateSyncResult/components/SyncResultList.js +2 -2
  38. data/webpack/components/TemplateSyncResult/components/SyncedTemplate/__snapshots__/helpers.test.js.snap +37 -0
  39. data/webpack/components/TemplateSyncResult/components/SyncedTemplate/helpers.js +21 -11
  40. data/webpack/components/TemplateSyncResult/components/SyncedTemplate/helpers.test.js +21 -0
  41. data/webpack/components/TemplateSyncResult/components/__tests__/__snapshots__/SyncResultList.test.js.snap +8 -6
  42. data/webpack/components/TemplateSyncResult/components/__tests__/__snapshots__/SyncedTemplate.test.js.snap +39 -15
  43. data/webpack/testSetup.js +2 -1
  44. metadata +8 -8
  45. data/webpack/__mocks__/foremanReact/components/common/forms/Form.js +0 -2
  46. data/webpack/components/NewTemplateSync/components/NewTemplateSyncForm/NewTemplateSyncFormConstants.js +0 -1
  47. data/webpack/components/NewTemplateSync/components/NewTemplateSyncForm/__tests__/NewTemplateSyncForm.test.js +0 -42
  48. data/webpack/components/NewTemplateSync/components/NewTemplateSyncForm/__tests__/__snapshots__/NewTemplateSyncForm.test.js.snap +0 -186
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 40c303f1207307e582859933360b3e2e9b2deb5793ff7816bf2fe2ccdd17b678
4
- data.tar.gz: e25c5b35b84061c38f7051097e5c184a5369a1ba18974c40ae137e9828da3617
3
+ metadata.gz: f43c3bbc33be62b6f39ce0abfaa0475f9635d2230f4a7f6bb551e66cf7f7fb0b
4
+ data.tar.gz: 3fdf3c192b4e639bc0fdc0a98deef65f344e1be65853dbeebb876bde4592405c
5
5
  SHA512:
6
- metadata.gz: 78989629db36f53ca6aa96c396c3d32c02ed5176c3cf5026ccafb4ddc06aae85538ae8d68cdcd443bd9c8d8355c7bee507115606d7d9077d9c6d853052204286
7
- data.tar.gz: 5ff10abf7e6c88946d008a17f6afe9fd8e5880b141d6b7ac9f833c2e789d40a42b03043aabc8024c02c473e18eb79fbaf0bde2dfd4d31c8adfee9d399dc7222c
6
+ metadata.gz: 20d726f2cb66074badb1779ad8b49613ad0f2e899fd3d8db9559a9590cec9224f98d7b2828072b4dd046cf053b5799a7cfcfeafb99723efa867cb9625ac33aa9
7
+ data.tar.gz: 2a2e07c0374ce30e0ecf900262f41e33d16f0a5a494841e5a00eaf3c5c00cbec90df3cd6d785cc68c99916783245f640af3628b7d47cc365bbf8a659416643ba
@@ -3,6 +3,10 @@ module Api
3
3
  class TemplateController < ::Api::V2::BaseController
4
4
  include ::Foreman::Controller::Parameters::TemplateParams
5
5
 
6
+ resource_description do
7
+ resource_id 'templates'
8
+ end
9
+
6
10
  def_param_group :foreman_template_sync_params do
7
11
  param :branch, String, :required => false, :desc => N_("Branch in Git repo.")
8
12
  param :repo, String, :required => false, :desc => N_("Override the default repo from settings.")
@@ -15,7 +19,7 @@ module Api
15
19
  param :prefix, String, :required => false, :desc => N_("The string all imported templates should begin with.")
16
20
  param :associate, Setting::TemplateSync.associate_types.keys, :required => false, :desc => N_("Associate to OS's, Locations & Organizations. Options are: always, new or never.")
17
21
  param :force, :bool, :required => false, :desc => N_("Update templates that are locked")
18
- param :lock, :bool, :required => false, :desc => N_("Lock imported templates")
22
+ param :lock, Setting::TemplateSync.lock_types.keys + ["true", "false", "0", "1"], :required => false, :desc => N_("Lock imported templates")
19
23
  param :verbose, :bool, :required => false, :desc => N_("Show template diff in response")
20
24
  param_group :foreman_template_sync_params
21
25
  param_group :taxonomies, ::Api::V2::BaseController
@@ -29,11 +33,13 @@ module Api
29
33
 
30
34
  api :POST, "/templates/export", N_("Initiate Export")
31
35
  param :metadata_export_mode, Setting::TemplateSync.metadata_export_mode_types.keys, :required => false, :desc => N_("Specify how to handle metadata")
36
+ param :commit_msg, String, :desc => N_("Custom commit message for templates export")
32
37
  param_group :foreman_template_sync_params
33
38
  param_group :taxonomies, ::Api::V2::BaseController
34
39
  def export
35
40
  @result = ForemanTemplates::TemplateExporter.new(template_export_params).export!
36
- render :json => { :message => @result.to_h }, :status => @result.exported ? 200 : 500
41
+ @result[:templates] = @result[:templates].map(&:to_h)
42
+ render :json => { :message => @result }, :status => @result[:error] ? 500 : 200
37
43
  end
38
44
  end
39
45
  end
@@ -15,7 +15,7 @@ module Foreman
15
15
  end
16
16
 
17
17
  def extra_export_params
18
- [:metadata_export_mode]
18
+ %i(metadata_export_mode commit_msg)
19
19
  end
20
20
 
21
21
  def template_params_filter(extra_params = [])
@@ -34,7 +34,27 @@ module Foreman
34
34
  end
35
35
 
36
36
  def template_import_params
37
- add_taxonomy_params(base_import_params(:none))
37
+ transform_lock_param add_taxonomy_params(base_import_params(:none))
38
+ end
39
+
40
+ def transform_lock_param(params)
41
+ lock = params[:lock]
42
+ return params if lock.nil?
43
+
44
+ if lock == "true" || lock.is_a?(TrueClass) || lock.to_s == "1"
45
+ log_deprecated_param(lock)
46
+ params[:lock] = "lock"
47
+ end
48
+
49
+ if lock == "false" || lock.is_a?(FalseClass) || lock.to_s == "0"
50
+ log_deprecated_param(lock)
51
+ params[:lock] = "unlock"
52
+ end
53
+ params
54
+ end
55
+
56
+ def log_deprecated_param(value)
57
+ Logging.logger('app').warn "Using '#{value}' as a value for lock when syncing templates is deprecated and will be removed in the future."
38
58
  end
39
59
 
40
60
  def template_export_params
@@ -18,7 +18,7 @@ class UiTemplateSyncsController < ApplicationController
18
18
  end
19
19
 
20
20
  def export
21
- @result = ForemanTemplates::TemplateExporter.new(ui_template_export_params).export!
21
+ @result = OpenStruct.new ForemanTemplates::TemplateExporter.new(ui_template_export_params).export!
22
22
 
23
23
  if @result.error
24
24
  render_errors [@result.error]
@@ -43,6 +43,6 @@ class UiTemplateSyncsController < ApplicationController
43
43
  end
44
44
 
45
45
  def render_errors(messages, severity = 'danger')
46
- render :json => { :error => { :errors => { :base => messages }, :severity => severity } }, :status => :unprocessable_entity
46
+ render :json => { :error => { :errors => { :base => messages }, full_messages: messages, :severity => severity } }, :status => :unprocessable_entity
47
47
  end
48
48
  end
@@ -11,7 +11,7 @@ class Setting
11
11
  end
12
12
 
13
13
  def self.export_stripped_names
14
- %w(metadata_export_mode)
14
+ %w(metadata_export_mode commit_msg)
15
15
  end
16
16
 
17
17
  def self.import_setting_names(except = [])
@@ -38,6 +38,15 @@ class Setting
38
38
  }
39
39
  end
40
40
 
41
+ def self.lock_types
42
+ {
43
+ 'lock' => _('Lock'),
44
+ 'keep_lock_new' => _('Keep, lock new'),
45
+ 'keep' => _('Keep, do not lock new'),
46
+ 'unlock' => _('Unlock')
47
+ }
48
+ end
49
+
41
50
  def self.metadata_export_mode_types
42
51
  {
43
52
  'refresh' => _('Refresh'),
@@ -75,7 +84,8 @@ class Setting
75
84
  self.set('template_sync_branch', N_('Default branch in Git repo'), '', N_('Branch')),
76
85
  self.set('template_sync_metadata_export_mode', N_('Default metadata export mode, refresh re-renders metadata, keep will keep existing metadata, remove exports template without metadata'), 'refresh', N_('Metadata export mode'), nil, { :collection => Proc.new { self.metadata_export_mode_types } }),
77
86
  self.set('template_sync_force', N_('Should importing overwrite locked templates?'), false, N_('Force import')),
78
- self.set('template_sync_lock', N_('Should importing lock templates?'), false, N_('Lock templates')),
87
+ self.set('template_sync_lock', N_('How to handle lock for imported templates?'), 'keep', N_('Lock templates'), nil, { :collection => Proc.new { self.lock_types } }),
88
+ self.set('template_sync_commit_msg', N_('Custom commit message for templates export'), 'Templates export made by a Foreman user', N_('Commit message'))
79
89
  ].compact.each { |s| self.create! s.update(:category => "Setting::TemplateSync") }
80
90
  end
81
91
 
@@ -1,42 +1,33 @@
1
1
  module ForemanTemplates
2
2
  class ExportResult
3
- attr_accessor :exported, :error, :warning
4
- attr_reader :templates, :git_user, :branch, :repo
3
+ attr_reader :template, :name, :template_file, :exported, :additional_info
5
4
 
6
- def initialize(repo, branch, git_user)
7
- @repo = repo
8
- @branch = branch
9
- @git_user = git_user
10
- @error = nil
11
- @warning = nil
12
- @templates = []
13
- @exported = false
14
- end
15
-
16
- def add_exported_templates(templates)
17
- @templates.concat templates
5
+ def initialize(template, exported = true)
6
+ @template = template
7
+ @exported = exported
8
+ @name = template.name
9
+ @template_file = template.template_file
18
10
  end
19
11
 
20
12
  def to_h
21
- { :error => @error,
22
- :warning => @warning,
23
- :repo => @repo,
24
- :branch => @branch,
25
- :git_user => @git_user,
26
- :templates => dumped_files_result }
13
+ {
14
+ :id => template.id,
15
+ :name => @name,
16
+ :exported => @exported,
17
+ :type => template.class.name.underscore,
18
+ :additional_info => @additional_info
19
+ }
27
20
  end
28
21
 
29
- private
30
-
31
- def dumped_files_result
32
- @templates.map { |template| to_template_h template }
22
+ def matching_filter
23
+ generic_info "Skipping, 'name' filtered out based on 'filter' and 'negate' settings"
33
24
  end
34
25
 
35
- def to_template_h(template)
36
- { :id => template.id,
37
- :name => template.name,
38
- :exported => @exported,
39
- :type => template.class.name.underscore }
26
+ def generic_info(additional_msg)
27
+ @exported = false
28
+ @additional_info = additional_msg
29
+ Logging.logger('app').debug "Not exporting #{@template.name}: #{additional_msg}"
30
+ self
40
31
  end
41
32
  end
42
33
  end
@@ -1,30 +1,32 @@
1
1
  module ForemanTemplates
2
2
  class TemplateExporter < Action
3
3
  def self.setting_overrides
4
- super + %i(metadata_export_mode)
4
+ super + %i(metadata_export_mode commit_msg)
5
+ end
6
+
7
+ def initialize(args = {})
8
+ super args
9
+ @result_lines = []
5
10
  end
6
11
 
7
12
  def export!
8
- @export_result = ExportResult.new(@repo, @branch, foreman_git_user)
9
13
  if git_repo?
10
14
  export_to_git
11
15
  else
12
16
  export_to_files
13
17
  end
14
-
15
- return @export_result
18
+ export_result
16
19
  end
17
20
 
18
21
  def export_to_files
19
22
  @dir = get_absolute_repo_path
20
23
  verify_path!(@dir)
21
24
  dump_files!
22
- @export_result.exported = true
23
25
  end
24
26
 
25
27
  def export_to_git
26
28
  @dir = Dir.mktmpdir
27
- return @export_result if branch_missing?
29
+ return if branch_missing?
28
30
 
29
31
  git_repo = Git.clone(@repo, @dir)
30
32
  logger.debug "cloned '#{@repo}' to '#{@dir}'"
@@ -40,17 +42,15 @@ module ForemanTemplates
40
42
  new_repo = true
41
43
  end
42
44
  if new_repo || status.added.any? || status.changed.any? || status.deleted.any? || status.untracked.any?
43
- git_repo.commit "Templates export made by Foreman user #{foreman_git_user}"
45
+ git_repo.commit commit_msg
44
46
  git_repo.push 'origin', branch
45
- @export_result.exported = true
46
47
  else
47
- @export_result.warning = 'No change detected, skipping the commit and push'
48
+ @warning = 'No change detected, skipping the commit and push'
48
49
  end
49
50
  rescue StandardError => e
50
- @export_result.error = e.message
51
+ @error = e.message
51
52
  ensure
52
53
  FileUtils.remove_entry_secure(@dir) if File.exist?(@dir)
53
- @export_result
54
54
  end
55
55
 
56
56
  def setup_git_branch(git_repo)
@@ -70,22 +70,18 @@ module ForemanTemplates
70
70
  end
71
71
 
72
72
  def dump_files!
73
- templates = templates_to_dump
74
- begin
75
- templates.map do |template|
76
- current_dir = get_dump_dir(template)
77
- FileUtils.mkdir_p current_dir
78
- filename = File.join(current_dir, template.template_file)
79
- File.open(filename, 'w+') do |file|
80
- logger.debug "Writing to file #{filename}"
81
- bytes = file.write template.public_send(export_method)
82
- logger.debug "finished writing #{bytes}"
83
- end
73
+ templates_to_dump.map do |template|
74
+ current_dir = get_dump_dir(template)
75
+ FileUtils.mkdir_p current_dir
76
+ filename = File.join(current_dir, template.template_file)
77
+ File.open(filename, 'w+') do |file|
78
+ logger.debug "Writing to file #{filename}"
79
+ bytes = file.write template.public_send(export_method)
80
+ logger.debug "finished writing #{bytes}"
84
81
  end
85
- rescue StandardError => e
86
- raise PathAccessException, e.message
87
82
  end
88
- @export_result.add_exported_templates templates
83
+ rescue StandardError => e
84
+ raise PathAccessException, e.message
89
85
  end
90
86
 
91
87
  def get_dump_dir(template)
@@ -96,18 +92,23 @@ module ForemanTemplates
96
92
  end
97
93
 
98
94
  def templates_to_dump
99
- base = find_templates
100
- if filter.present?
101
- method = negate ? :reject : :select
102
- base.public_send(method) { |template| template.name.match(/#{filter}/i) }
103
- else
104
- base
95
+ find_templates.each do |template|
96
+ if filter.present?
97
+ exportable = template.name =~ /#{filter}/i ? !negate : negate
98
+ result = ExportResult.new(template, exportable)
99
+ next @result_lines << result.matching_filter unless exportable
100
+
101
+ @result_lines << result
102
+ else
103
+ @result_lines << ExportResult.new(template)
104
+ end
105
105
  end
106
+ @result_lines.select(&:exported).map(&:template)
106
107
  end
107
108
 
108
109
  def branch_missing?
109
110
  if @branch.blank?
110
- @export_result.error = "Please specify a branch when exporting into a git repo"
111
+ @error = "Please specify a branch when exporting into a git repo"
111
112
  return true
112
113
  end
113
114
  false
@@ -129,6 +130,13 @@ module ForemanTemplates
129
130
  end
130
131
  end
131
132
 
133
+ def export_result
134
+ {
135
+ :templates => @result_lines, :repo => @repo, :branch => @branch,
136
+ :git_user => foreman_git_user, :error => @error, :warning => @warning
137
+ }
138
+ end
139
+
132
140
  private
133
141
 
134
142
  def find_templates
@@ -10,7 +10,6 @@ module ForemanTemplates
10
10
  super args
11
11
  @verbose = parse_bool(@verbose)
12
12
  @force = parse_bool(@force)
13
- @lock = parse_bool(@lock)
14
13
  @result_lines = []
15
14
  end
16
15
 
@@ -79,11 +78,28 @@ module ForemanTemplates
79
78
  end
80
79
 
81
80
  def import_options
82
- { :force => @force,
81
+ lock_predicate = lambda do |template|
82
+ case @lock
83
+ when 'lock'
84
+ return true
85
+ when 'unlock'
86
+ return false
87
+ when 'keep'
88
+ return template.new_record? ? false : template.locked
89
+ when 'keep_lock_new'
90
+ return template.new_record? ? true : template.locked
91
+ else
92
+ raise ::Foreman::Exception.new("Unknown lock option type, expected one of #{::Setting::TemplateSync.lock_types.keys}, got #{@lock}")
93
+ end
94
+ end
95
+
96
+ {
97
+ :force => @force,
83
98
  :associate => @associate,
84
- :lock => @lock,
99
+ :lock => lock_predicate,
85
100
  :organization_params => @taxonomies[:organizations],
86
- :location_params => @taxonomies[:locations] }
101
+ :location_params => @taxonomies[:locations]
102
+ }
87
103
  end
88
104
 
89
105
  def template_model(metadata, parse_result)
@@ -1,20 +1,21 @@
1
- <%= webpacked_plugins_js_for :foreman_templates %>
2
- <%= webpacked_plugins_css_for :foreman_templates %>
1
+ <% content_for(:javascripts) do %>
2
+ <%= webpacked_plugins_js_for :foreman_templates %>
3
+ <% end %>
4
+ <% content_for(:stylesheets) do %>
5
+ <%= webpacked_plugins_css_for :foreman_templates %>
6
+ <% end %>
3
7
 
4
- <div id="foreman-templates"/>
5
-
6
- <%= mount_react_component('ForemanTemplates',
7
- '#foreman-templates',
8
- { :apiUrls => {
9
- :exportUrl => export_ui_template_syncs_path,
10
- :syncSettingsUrl => sync_settings_ui_template_syncs_path,
11
- :importUrl => import_ui_template_syncs_path
12
- },
13
- :validationData => { :repo => ForemanTemplates::Action.repo_start_with },
14
- :editPaths => edit_paths,
15
- :fileRepoStartWith => ForemanTemplates::Action.file_repo_start_with,
16
- :userPermissions => {
17
- :import => authorized_for(:controller => :ui_template_syncs, :action => :import),
18
- :export => authorized_for(:controller => :ui_template_syncs, :action => :export)
19
- }
20
- }.to_json )%>
8
+ <%= react_component('ForemanTemplates',
9
+ { :apiUrls => {
10
+ :exportUrl => export_ui_template_syncs_path,
11
+ :syncSettingsUrl => sync_settings_ui_template_syncs_path,
12
+ :importUrl => import_ui_template_syncs_path
13
+ },
14
+ :validationData => { :repo => ForemanTemplates::Action.repo_start_with },
15
+ :editPaths => edit_paths,
16
+ :fileRepoStartWith => ForemanTemplates::Action.file_repo_start_with,
17
+ :userPermissions => {
18
+ :import => authorized_for(:controller => :ui_template_syncs, :action => :import),
19
+ :export => authorized_for(:controller => :ui_template_syncs, :action => :export)
20
+ }
21
+ })%>
@@ -1,7 +1,7 @@
1
- object @template
1
+ object @template_result
2
2
 
3
- attributes :name, :template_file
3
+ attributes :name, :template_file, :additional_info
4
4
 
5
- node(false) do |template|
6
- partial "ui_template_syncs/template_attrs", :object => template
5
+ node(false) do |result|
6
+ partial "ui_template_syncs/template_attrs", :object => result.template
7
7
  end
@@ -0,0 +1,5 @@
1
+ class ChangeLockSetting < ActiveRecord::Migration[5.1]
2
+ def up
3
+ Setting.find_by(:name => 'template_sync_lock')&.destroy
4
+ end
5
+ end
@@ -20,6 +20,12 @@ module ForemanTemplates
20
20
  end
21
21
  end
22
22
 
23
+ initializer "foreman_templates.load_app_instance_data" do |app|
24
+ ForemanTemplates::Engine.paths['db/migrate'].existent.each do |path|
25
+ app.config.paths['db/migrate'] << path
26
+ end
27
+ end
28
+
23
29
  initializer 'foreman_templates.register_plugin', :before => :finisher_hook do
24
30
  Foreman::Plugin.register :foreman_templates do
25
31
  requires_foreman '>= 1.24'