foreman_templates 7.0.6 → 7.0.7

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 40c303f1207307e582859933360b3e2e9b2deb5793ff7816bf2fe2ccdd17b678
4
- data.tar.gz: e25c5b35b84061c38f7051097e5c184a5369a1ba18974c40ae137e9828da3617
3
+ metadata.gz: 9583ee6b0f88f013c5a26f23dd90a22cf7bdcdeebbb8052c3fdffaf11960fb3d
4
+ data.tar.gz: 5b54607bfe06ca7cdd359d371afbb497e343310ae48d29c2f359bb4d43a6a38d
5
5
  SHA512:
6
- metadata.gz: 78989629db36f53ca6aa96c396c3d32c02ed5176c3cf5026ccafb4ddc06aae85538ae8d68cdcd443bd9c8d8355c7bee507115606d7d9077d9c6d853052204286
7
- data.tar.gz: 5ff10abf7e6c88946d008a17f6afe9fd8e5880b141d6b7ac9f833c2e789d40a42b03043aabc8024c02c473e18eb79fbaf0bde2dfd4d31c8adfee9d399dc7222c
6
+ metadata.gz: d6826d8bdd2c9bbc47d09cfe8a9cfeddb169a6aa65933fd670144921091267d20814b5cf4cf37e93629a81fa2b1b81120ce796dd53194d4740b02b7e10c04720
7
+ data.tar.gz: 2e019ce44797e7c28648f8b8913d224441a514fad0e75c977502c1c79c4bab09dbcb4b538a584c28fc8049c60df5ff3c270dfe2de2df97564c5a5af3125ebce5
@@ -33,7 +33,8 @@ module Api
33
33
  param_group :taxonomies, ::Api::V2::BaseController
34
34
  def export
35
35
  @result = ForemanTemplates::TemplateExporter.new(template_export_params).export!
36
- render :json => { :message => @result.to_h }, :status => @result.exported ? 200 : 500
36
+ @result[:templates] = @result[:templates].map(&:to_h)
37
+ render :json => { :message => @result }, :status => @result[:error] ? 500 : 200
37
38
  end
38
39
  end
39
40
  end
@@ -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]
@@ -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
@@ -4,27 +4,29 @@ module ForemanTemplates
4
4
  super + %i(metadata_export_mode)
5
5
  end
6
6
 
7
+ def initialize(args = {})
8
+ super args
9
+ @result_lines = []
10
+ end
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}'"
@@ -42,15 +44,13 @@ module ForemanTemplates
42
44
  if new_repo || status.added.any? || status.changed.any? || status.deleted.any? || status.untracked.any?
43
45
  git_repo.commit "Templates export made by Foreman user #{foreman_git_user}"
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
@@ -1,5 +1,9 @@
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
8
  <div id="foreman-templates"/>
5
9
 
@@ -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
@@ -1,3 +1,3 @@
1
1
  module ForemanTemplates
2
- VERSION = '7.0.6'.freeze
2
+ VERSION = '7.0.7'.freeze
3
3
  end
@@ -0,0 +1,4 @@
1
+ export const selectLayout = jest.fn(() => ({
2
+ currentOrganization: {},
3
+ currentLocation: {},
4
+ }));
@@ -8,8 +8,26 @@ import SyncSettingsFields from '../SyncSettingFields';
8
8
  import SyncTypeRadios from '../SyncTypeRadios';
9
9
  import { NEW_TEMPLATE_SYNC_FORM_NAME } from './NewTemplateSyncFormConstants';
10
10
 
11
+ const addTaxParams = key => (params, currentTax) => {
12
+ if (currentTax.id) {
13
+ params[key] = [currentTax.id];
14
+ }
15
+ return params;
16
+ };
17
+
18
+ const addOrgParams = addTaxParams('organization_ids');
19
+ const addLocParams = addTaxParams('location_ids');
20
+
11
21
  const submit = syncType => (formValues, dispatch, props) => {
12
- const { submitForm, importUrl, exportUrl, history, currentFields } = props;
22
+ const {
23
+ submitForm,
24
+ importUrl,
25
+ exportUrl,
26
+ history,
27
+ currentFields,
28
+ currentLocation,
29
+ currentOrganization,
30
+ } = props;
13
31
  const url = syncType === 'import' ? importUrl : exportUrl;
14
32
  const currentFieldNames = Object.keys(currentFields);
15
33
  const postValues = Object.keys(formValues).reduce((memo, key) => {
@@ -21,7 +39,10 @@ const submit = syncType => (formValues, dispatch, props) => {
21
39
 
22
40
  return submitForm({
23
41
  url,
24
- values: postValues,
42
+ values: addOrgParams(
43
+ addLocParams(postValues, currentLocation),
44
+ currentOrganization
45
+ ),
25
46
  message: `Templates were ${syncType}ed.`,
26
47
  item: 'TemplateSync',
27
48
  }).then(args => {
@@ -3,6 +3,8 @@ import { reduxForm } from 'redux-form';
3
3
 
4
4
  import * as FormActions from 'foremanReact/redux/actions/common/forms';
5
5
 
6
+ import { selectLayout } from 'foremanReact/components/Layout/LayoutSelectors';
7
+
6
8
  import { NEW_TEMPLATE_SYNC_FORM_NAME } from './NewTemplateSyncFormConstants';
7
9
  import NewTemplateSyncForm from './NewTemplateSyncForm';
8
10
 
@@ -32,6 +34,8 @@ const mapStateToProps = (state, ownProps) => {
32
34
  importSettings,
33
35
  exportSettings,
34
36
  currentFields,
37
+ currentOrganization: selectLayout(state).currentOrganization,
38
+ currentLocation: selectLayout(state).currentLocation,
35
39
  };
36
40
  };
37
41
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foreman_templates
3
3
  version: !ruby/object:Gem::Version
4
- version: 7.0.6
4
+ version: 7.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Greg Sutcliffe
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-01-14 00:00:00.000000000 Z
11
+ date: 2020-03-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: diffy
@@ -96,6 +96,7 @@ files:
96
96
  - webpack/ForemanTemplates.js
97
97
  - webpack/Routes.js
98
98
  - webpack/__mocks__/foremanReact/common/helpers.js
99
+ - webpack/__mocks__/foremanReact/components/Layout/LayoutSelectors.js
99
100
  - webpack/__mocks__/foremanReact/components/Pagination/PaginationWrapper.js
100
101
  - webpack/__mocks__/foremanReact/components/common/forms/CommonForm.js
101
102
  - webpack/__mocks__/foremanReact/components/common/forms/Form.js