foreman_remote_execution 4.6.0 → 5.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (143) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby_ci.yml +7 -0
  3. data/.rubocop_todo.yml +1 -0
  4. data/app/controllers/api/v2/job_invocations_controller.rb +16 -1
  5. data/app/controllers/job_invocations_controller.rb +1 -1
  6. data/app/controllers/ui_job_wizard_controller.rb +21 -2
  7. data/app/graphql/mutations/job_invocations/create.rb +43 -0
  8. data/app/graphql/types/job_invocation.rb +16 -0
  9. data/app/graphql/types/job_invocation_input.rb +13 -0
  10. data/app/graphql/types/recurrence_input.rb +8 -0
  11. data/app/graphql/types/scheduling_input.rb +6 -0
  12. data/app/graphql/types/targeting_enum.rb +7 -0
  13. data/app/helpers/concerns/foreman_remote_execution/hosts_helper_extensions.rb +5 -1
  14. data/app/helpers/remote_execution_helper.rb +9 -3
  15. data/app/lib/actions/remote_execution/run_host_job.rb +10 -1
  16. data/app/lib/actions/remote_execution/run_hosts_job.rb +58 -4
  17. data/app/mailers/rex_job_mailer.rb +15 -0
  18. data/app/models/concerns/foreman_remote_execution/host_extensions.rb +10 -0
  19. data/app/models/concerns/foreman_remote_execution/smart_proxy_extensions.rb +6 -0
  20. data/app/models/host_proxy_invocation.rb +4 -0
  21. data/app/models/host_status/execution_status.rb +3 -3
  22. data/app/models/job_invocation.rb +12 -5
  23. data/app/models/job_invocation_composer.rb +25 -17
  24. data/app/models/job_template.rb +1 -1
  25. data/app/models/remote_execution_feature.rb +5 -1
  26. data/app/models/remote_execution_provider.rb +18 -2
  27. data/app/models/rex_mail_notification.rb +13 -0
  28. data/app/models/targeting.rb +7 -3
  29. data/app/services/ui_notifications/remote_execution_jobs/base_job_finish.rb +2 -1
  30. data/app/views/dashboard/_latest-jobs.html.erb +21 -0
  31. data/app/views/job_invocations/index.html.erb +1 -1
  32. data/app/views/job_invocations/refresh.js.erb +1 -0
  33. data/app/views/rex_job_mailer/job_finished.html.erb +24 -0
  34. data/app/views/rex_job_mailer/job_finished.text.erb +9 -0
  35. data/app/views/template_invocations/show.html.erb +2 -1
  36. data/app/views/templates/ssh/module_action.erb +1 -0
  37. data/app/views/templates/ssh/power_action.erb +2 -0
  38. data/app/views/templates/ssh/puppet_run_once.erb +1 -0
  39. data/config/routes.rb +1 -0
  40. data/db/migrate/2021051713291621250977_add_host_proxy_invocations.rb +12 -0
  41. data/db/migrate/20210816100932_rex_setting_category_to_dsl.rb +5 -0
  42. data/db/seeds.d/50-notification_blueprints.rb +14 -0
  43. data/db/seeds.d/95-mail_notifications.rb +24 -0
  44. data/foreman_remote_execution.gemspec +2 -3
  45. data/lib/foreman_remote_execution/engine.rb +114 -8
  46. data/lib/foreman_remote_execution/version.rb +1 -1
  47. data/package.json +9 -7
  48. data/test/functional/api/v2/job_invocations_controller_test.rb +20 -0
  49. data/test/functional/cockpit_controller_test.rb +0 -1
  50. data/test/graphql/mutations/job_invocations/create.rb +58 -0
  51. data/test/graphql/queries/job_invocation_query_test.rb +31 -0
  52. data/test/graphql/queries/job_invocations_query_test.rb +35 -0
  53. data/test/helpers/remote_execution_helper_test.rb +0 -1
  54. data/test/unit/actions/run_host_job_test.rb +21 -0
  55. data/test/unit/actions/run_hosts_job_test.rb +99 -4
  56. data/test/unit/concerns/host_extensions_test.rb +40 -7
  57. data/test/unit/input_template_renderer_test.rb +1 -89
  58. data/test/unit/job_invocation_composer_test.rb +18 -18
  59. data/test/unit/job_invocation_report_template_test.rb +16 -13
  60. data/test/unit/job_invocation_test.rb +1 -1
  61. data/test/unit/job_template_effective_user_test.rb +0 -4
  62. data/test/unit/remote_execution_provider_test.rb +46 -4
  63. data/test/unit/targeting_test.rb +68 -1
  64. data/webpack/JobWizard/JobWizard.js +158 -24
  65. data/webpack/JobWizard/JobWizard.scss +93 -1
  66. data/webpack/JobWizard/JobWizardConstants.js +54 -0
  67. data/webpack/JobWizard/JobWizardSelectors.js +41 -0
  68. data/webpack/JobWizard/__tests__/fixtures.js +188 -3
  69. data/webpack/JobWizard/__tests__/integration.test.js +41 -106
  70. data/webpack/JobWizard/__tests__/validation.test.js +141 -0
  71. data/webpack/JobWizard/autofill.js +38 -0
  72. data/webpack/JobWizard/index.js +7 -0
  73. data/webpack/JobWizard/steps/AdvancedFields/AdvancedFields.js +41 -10
  74. data/webpack/JobWizard/steps/AdvancedFields/DescriptionField.js +90 -0
  75. data/webpack/JobWizard/steps/AdvancedFields/Fields.js +116 -55
  76. data/webpack/JobWizard/steps/AdvancedFields/__tests__/AdvancedFields.test.js +354 -16
  77. data/webpack/JobWizard/steps/AdvancedFields/__tests__/__snapshots__/AdvancedFields.test.js.snap +79 -246
  78. data/webpack/JobWizard/steps/CategoryAndTemplate/CategoryAndTemplate.js +5 -2
  79. data/webpack/JobWizard/steps/CategoryAndTemplate/CategoryAndTemplate.test.js +123 -51
  80. data/webpack/JobWizard/steps/CategoryAndTemplate/index.js +3 -2
  81. data/webpack/JobWizard/steps/HostsAndInputs/HostPreviewModal.js +62 -0
  82. data/webpack/JobWizard/steps/HostsAndInputs/HostSearch.js +54 -0
  83. data/webpack/JobWizard/steps/HostsAndInputs/SelectAPI.js +33 -0
  84. data/webpack/JobWizard/steps/HostsAndInputs/SelectGQL.js +52 -0
  85. data/webpack/JobWizard/steps/HostsAndInputs/SelectedChips.js +100 -0
  86. data/webpack/JobWizard/steps/HostsAndInputs/TemplateInputs.js +23 -0
  87. data/webpack/JobWizard/steps/HostsAndInputs/__tests__/HostsAndInputs.test.js +151 -0
  88. data/webpack/JobWizard/steps/HostsAndInputs/__tests__/TemplateInputs.test.js +53 -0
  89. data/webpack/JobWizard/steps/HostsAndInputs/buildHostQuery.js +18 -0
  90. data/webpack/JobWizard/steps/HostsAndInputs/hostgroups.gql +8 -0
  91. data/webpack/JobWizard/steps/HostsAndInputs/hosts.gql +8 -0
  92. data/webpack/JobWizard/steps/HostsAndInputs/index.js +214 -0
  93. data/webpack/JobWizard/steps/ReviewDetails/index.js +193 -0
  94. data/webpack/JobWizard/steps/Schedule/PurposeField.js +31 -0
  95. data/webpack/JobWizard/steps/Schedule/QueryType.js +51 -0
  96. data/webpack/JobWizard/steps/Schedule/RepeatCron.js +53 -0
  97. data/webpack/JobWizard/steps/Schedule/RepeatDaily.js +37 -0
  98. data/webpack/JobWizard/steps/Schedule/RepeatHour.js +54 -0
  99. data/webpack/JobWizard/steps/Schedule/RepeatMonth.js +46 -0
  100. data/webpack/JobWizard/steps/Schedule/RepeatOn.js +125 -0
  101. data/webpack/JobWizard/steps/Schedule/RepeatWeek.js +70 -0
  102. data/webpack/JobWizard/steps/Schedule/ScheduleType.js +28 -0
  103. data/webpack/JobWizard/steps/Schedule/StartEndDates.js +106 -0
  104. data/webpack/JobWizard/steps/Schedule/__tests__/Schedule.test.js +402 -0
  105. data/webpack/JobWizard/steps/Schedule/__tests__/StartEndDates.test.js +32 -0
  106. data/webpack/JobWizard/steps/Schedule/index.js +178 -0
  107. data/webpack/JobWizard/steps/form/DateTimePicker.js +126 -0
  108. data/webpack/JobWizard/steps/form/FormHelpers.js +5 -0
  109. data/webpack/JobWizard/steps/form/Formatter.js +181 -0
  110. data/webpack/JobWizard/steps/form/NumberInput.js +36 -0
  111. data/webpack/JobWizard/steps/form/ResourceSelect.js +29 -0
  112. data/webpack/JobWizard/steps/form/SearchSelect.js +121 -0
  113. data/webpack/JobWizard/steps/form/SelectField.js +28 -5
  114. data/webpack/JobWizard/steps/form/WizardTitle.js +14 -0
  115. data/webpack/JobWizard/steps/form/__tests__/SelectSearch.test.js +33 -0
  116. data/webpack/JobWizard/submit.js +120 -0
  117. data/webpack/JobWizard/validation.js +53 -0
  118. data/webpack/__mocks__/foremanReact/Root/Context/ForemanContext/index.js +2 -0
  119. data/webpack/__mocks__/foremanReact/common/I18n.js +2 -0
  120. data/webpack/__mocks__/foremanReact/components/AutoComplete/AutoCompleteActions.js +1 -0
  121. data/webpack/__mocks__/foremanReact/components/AutoComplete/AutoCompleteConstants.js +1 -0
  122. data/webpack/__mocks__/foremanReact/components/SearchBar.js +18 -1
  123. data/webpack/__mocks__/foremanReact/routes/RouterSelector.js +1 -0
  124. data/webpack/helpers.js +1 -0
  125. data/webpack/react_app/components/RecentJobsCard/JobStatusIcon.js +43 -0
  126. data/webpack/react_app/components/RecentJobsCard/RecentJobsCard.js +73 -66
  127. data/webpack/react_app/components/RecentJobsCard/RecentJobsTable.js +98 -0
  128. data/webpack/react_app/components/RecentJobsCard/constants.js +11 -0
  129. data/webpack/react_app/components/RecentJobsCard/styles.scss +11 -0
  130. data/webpack/react_app/components/TargetingHosts/__tests__/__snapshots__/TargetingHostsPage.test.js.snap +1 -0
  131. data/webpack/react_app/extend/fillRecentJobsCard.js +1 -1
  132. metadata +71 -16
  133. data/app/models/concerns/foreman_remote_execution/orchestration/ssh.rb +0 -70
  134. data/app/models/setting/remote_execution.rb +0 -88
  135. data/test/models/orchestration/ssh_test.rb +0 -56
  136. data/webpack/JobWizard/__tests__/JobWizard.test.js +0 -13
  137. data/webpack/JobWizard/__tests__/__snapshots__/JobWizard.test.js.snap +0 -32
  138. data/webpack/JobWizard/steps/CategoryAndTemplate/__snapshots__/CategoryAndTemplate.test.js.snap +0 -113
  139. data/webpack/JobWizard/steps/form/__tests__/GroupedSelectField.test.js +0 -38
  140. data/webpack/JobWizard/steps/form/__tests__/SelectField.test.js +0 -23
  141. data/webpack/JobWizard/steps/form/__tests__/__snapshots__/GroupedSelectField.test.js.snap +0 -37
  142. data/webpack/JobWizard/steps/form/__tests__/__snapshots__/SelectField.test.js.snap +0 -23
  143. data/webpack/react_app/components/RecentJobsCard/styles.css +0 -15
@@ -0,0 +1,178 @@
1
+ import React, { useEffect, useState } from 'react';
2
+ import PropTypes from 'prop-types';
3
+ import { Form } from '@patternfly/react-core';
4
+ import { ScheduleType } from './ScheduleType';
5
+ import { RepeatOn } from './RepeatOn';
6
+ import { QueryType } from './QueryType';
7
+ import { StartEndDates } from './StartEndDates';
8
+ import { WIZARD_TITLES, repeatTypes } from '../../JobWizardConstants';
9
+ import { PurposeField } from './PurposeField';
10
+ import { WizardTitle } from '../form/WizardTitle';
11
+
12
+ const Schedule = ({ scheduleValue, setScheduleValue, setValid }) => {
13
+ const {
14
+ repeatType,
15
+ repeatAmount,
16
+ repeatData,
17
+ startsAt,
18
+ startsBefore,
19
+ ends,
20
+ isNeverEnds,
21
+ isFuture,
22
+ isTypeStatic,
23
+ purpose,
24
+ } = scheduleValue;
25
+ const [validEnd, setValidEnd] = useState(true);
26
+ const [repeatValid, setRepeatValid] = useState(true);
27
+
28
+ useEffect(() => {
29
+ if (!validEnd || !repeatValid) {
30
+ setValid(false);
31
+ } else if (isFuture && (startsAt.length || startsBefore.length)) {
32
+ setValid(true);
33
+ } else if (!isFuture) {
34
+ setValid(true);
35
+ } else {
36
+ setValid(false);
37
+ }
38
+ // eslint-disable-next-line react-hooks/exhaustive-deps
39
+ }, [startsAt, startsBefore, isFuture, validEnd, repeatValid]);
40
+ return (
41
+ <>
42
+ <WizardTitle title={WIZARD_TITLES.schedule} />
43
+ <Form className="schedule-tab">
44
+ <ScheduleType
45
+ isFuture={isFuture}
46
+ setIsFuture={newValue => {
47
+ if (!newValue) {
48
+ // if schedule type is execute now
49
+ setScheduleValue(current => ({
50
+ ...current,
51
+ startsAt: '',
52
+ startsBefore: '',
53
+ isFuture: newValue,
54
+ }));
55
+ } else {
56
+ setScheduleValue(current => ({
57
+ ...current,
58
+ startsAt: new Date().toISOString(),
59
+ isFuture: newValue,
60
+ }));
61
+ }
62
+ }}
63
+ />
64
+
65
+ <RepeatOn
66
+ repeatType={repeatType}
67
+ repeatData={repeatData}
68
+ setRepeatType={newValue => {
69
+ setScheduleValue(current => ({
70
+ ...current,
71
+ repeatType: newValue,
72
+ startsBefore: '',
73
+ }));
74
+ }}
75
+ setRepeatData={newValue => {
76
+ setScheduleValue(current => ({
77
+ ...current,
78
+ repeatData: newValue,
79
+ }));
80
+ }}
81
+ repeatAmount={repeatAmount}
82
+ setRepeatAmount={newValue => {
83
+ setScheduleValue(current => ({
84
+ ...current,
85
+ repeatAmount: newValue,
86
+ }));
87
+ }}
88
+ setValid={setRepeatValid}
89
+ />
90
+ <StartEndDates
91
+ startsAt={startsAt}
92
+ setStartsAt={newValue => {
93
+ if (!isFuture) {
94
+ setScheduleValue(current => ({
95
+ ...current,
96
+ isFuture: true,
97
+ }));
98
+ }
99
+ setScheduleValue(current => ({
100
+ ...current,
101
+ startsAt: newValue,
102
+ }));
103
+ }}
104
+ startsBefore={startsBefore}
105
+ setStartsBefore={newValue => {
106
+ if (!isFuture) {
107
+ setScheduleValue(current => ({
108
+ ...current,
109
+ isFuture: true,
110
+ }));
111
+ }
112
+ setScheduleValue(current => ({
113
+ ...current,
114
+ startsBefore: newValue,
115
+ }));
116
+ }}
117
+ ends={ends}
118
+ setEnds={newValue => {
119
+ setScheduleValue(current => ({
120
+ ...current,
121
+ ends: newValue,
122
+ }));
123
+ }}
124
+ isNeverEnds={isNeverEnds}
125
+ setIsNeverEnds={newValue => {
126
+ setScheduleValue(current => ({
127
+ ...current,
128
+ isNeverEnds: newValue,
129
+ }));
130
+ }}
131
+ validEnd={validEnd}
132
+ setValidEnd={setValidEnd}
133
+ isFuture={isFuture}
134
+ isStartBeforeDisabled={repeatType !== repeatTypes.noRepeat}
135
+ isEndDisabled={repeatType === repeatTypes.noRepeat}
136
+ />
137
+ <QueryType
138
+ isTypeStatic={isTypeStatic}
139
+ setIsTypeStatic={newValue => {
140
+ setScheduleValue(current => ({
141
+ ...current,
142
+ isTypeStatic: newValue,
143
+ }));
144
+ }}
145
+ />
146
+ <PurposeField
147
+ isDisabled={repeatType === repeatTypes.noRepeat}
148
+ purpose={purpose}
149
+ setPurpose={newValue => {
150
+ setScheduleValue(current => ({
151
+ ...current,
152
+ purpose: newValue,
153
+ }));
154
+ }}
155
+ />
156
+ </Form>
157
+ </>
158
+ );
159
+ };
160
+
161
+ Schedule.propTypes = {
162
+ scheduleValue: PropTypes.shape({
163
+ repeatType: PropTypes.string.isRequired,
164
+ repeatAmount: PropTypes.string,
165
+ repeatData: PropTypes.object,
166
+ startsAt: PropTypes.string,
167
+ startsBefore: PropTypes.string,
168
+ ends: PropTypes.string,
169
+ isFuture: PropTypes.bool,
170
+ isNeverEnds: PropTypes.bool,
171
+ isTypeStatic: PropTypes.bool,
172
+ purpose: PropTypes.string,
173
+ }).isRequired,
174
+ setScheduleValue: PropTypes.func.isRequired,
175
+ setValid: PropTypes.func.isRequired,
176
+ };
177
+
178
+ export default Schedule;
@@ -0,0 +1,126 @@
1
+ import React, { useState } from 'react';
2
+ import PropTypes from 'prop-types';
3
+ import {
4
+ DatePicker,
5
+ TimePicker,
6
+ ValidatedOptions,
7
+ } from '@patternfly/react-core';
8
+ import { debounce } from 'lodash';
9
+ import { translate as __, documentLocale } from 'foremanReact/common/I18n';
10
+
11
+ export const DateTimePicker = ({
12
+ dateTime,
13
+ setDateTime,
14
+ isDisabled,
15
+ ariaLabel,
16
+ allowEmpty,
17
+ }) => {
18
+ const [validated, setValidated] = useState();
19
+ const dateFormat = date =>
20
+ `${date.getFullYear()}/${(date.getMonth() + 1)
21
+ .toString()
22
+ .padStart(2, '0')}/${date
23
+ .getDate()
24
+ .toString()
25
+ .padStart(2, '0')}`;
26
+
27
+ const dateObject = dateTime ? new Date(dateTime) : new Date();
28
+ const formattedDate = dateTime ? dateFormat(dateObject) : '';
29
+ const dateParse = date =>
30
+ new Date(`${date} ${dateObject.getHours()}:${dateObject.getMinutes()}`);
31
+
32
+ const isValidDate = date => date && !Number.isNaN(date.getTime());
33
+
34
+ const isValidTime = time => {
35
+ if (!time) return false;
36
+ const split = time.split(':');
37
+ if (!(split[0].length === 2 && split[1].length === 2)) return false;
38
+ if (isValidDate(new Date(`${formattedDate} ${time}`))) return true;
39
+ if (!formattedDate.length && isValidDate(new Date(`01/01/2020 ${time}`))) {
40
+ const today = new Date();
41
+ today.setHours(split[0]);
42
+ today.setMinutes(split[1]);
43
+ setDateTime(today.toString());
44
+ }
45
+ return false;
46
+ };
47
+
48
+ const onDateChange = newDate => {
49
+ const parsedNewDate = new Date(newDate);
50
+ if (!newDate.length && allowEmpty) {
51
+ setDateTime('');
52
+ setValidated(ValidatedOptions.noval);
53
+ } else if (isValidDate(parsedNewDate)) {
54
+ parsedNewDate.setHours(dateObject.getHours());
55
+ parsedNewDate.setMinutes(dateObject.getMinutes());
56
+ setDateTime(parsedNewDate.toString());
57
+ setValidated(ValidatedOptions.noval);
58
+ } else {
59
+ setValidated(ValidatedOptions.error);
60
+ }
61
+ };
62
+
63
+ const onTimeChange = newTime => {
64
+ if (!newTime.length && allowEmpty) {
65
+ const parsedNewTime = new Date(`${formattedDate} 00:00`);
66
+ setDateTime(parsedNewTime.toString());
67
+ setValidated(ValidatedOptions.noval);
68
+ } else if (isValidTime(newTime)) {
69
+ const parsedNewTime = new Date(`${formattedDate} ${newTime}`);
70
+ setDateTime(parsedNewTime.toString());
71
+ setValidated(ValidatedOptions.noval);
72
+ } else {
73
+ setValidated(ValidatedOptions.error);
74
+ }
75
+ };
76
+ return (
77
+ <>
78
+ <DatePicker
79
+ aria-label={`${ariaLabel} datepicker`}
80
+ value={formattedDate}
81
+ placeholder="yyyy/mm/dd"
82
+ onChange={debounce(onDateChange, 1000, {
83
+ leading: false,
84
+ trailing: true,
85
+ })}
86
+ dateFormat={dateFormat}
87
+ dateParse={dateParse}
88
+ isDisabled={isDisabled}
89
+ locale={documentLocale()}
90
+ invalidFormatText={
91
+ validated === ValidatedOptions.error ? __('Invalid date') : ''
92
+ }
93
+ inputProps={{ validated }}
94
+ />
95
+ <TimePicker
96
+ aria-label={`${ariaLabel} timepicker`}
97
+ className="time-picker"
98
+ time={dateTime ? dateObject.toString() : ''}
99
+ inputProps={dateTime ? {} : { value: '' }}
100
+ placeholder="hh:mm"
101
+ onChange={debounce(onTimeChange, 1000, {
102
+ leading: false,
103
+ trailing: true,
104
+ })}
105
+ is24Hour
106
+ isDisabled={isDisabled}
107
+ invalidFormatErrorMessage={__('Invalid time format')}
108
+ menuAppendTo={() => document.body}
109
+ />
110
+ </>
111
+ );
112
+ };
113
+
114
+ DateTimePicker.propTypes = {
115
+ dateTime: PropTypes.string,
116
+ setDateTime: PropTypes.func.isRequired,
117
+ isDisabled: PropTypes.bool,
118
+ ariaLabel: PropTypes.string,
119
+ allowEmpty: PropTypes.bool,
120
+ };
121
+ DateTimePicker.defaultProps = {
122
+ dateTime: null,
123
+ isDisabled: false,
124
+ ariaLabel: '',
125
+ allowEmpty: true,
126
+ };
@@ -8,6 +8,7 @@ export const helpLabel = (text, id) => {
8
8
  return (
9
9
  <Popover id={`${id}-help`} bodyContent={text} aria-label="help-text">
10
10
  <button
11
+ type="button"
11
12
  aria-label={__('open-help-tooltip-button')}
12
13
  onClick={e => e.preventDefault()}
13
14
  className="pf-c-form__group-label-help"
@@ -17,3 +18,7 @@ export const helpLabel = (text, id) => {
17
18
  </Popover>
18
19
  );
19
20
  };
21
+
22
+ export const isPositiveNumber = text => parseInt(text, 10) > 0;
23
+
24
+ export const isValidDate = d => d instanceof Date && !Number.isNaN(d);
@@ -0,0 +1,181 @@
1
+ import React, { useEffect } from 'react';
2
+ import { useSelector } from 'react-redux';
3
+ import { FormGroup, TextInput, TextArea } from '@patternfly/react-core';
4
+ import PropTypes from 'prop-types';
5
+ import SearchBar from 'foremanReact/components/SearchBar';
6
+ import { getControllerSearchProps } from 'foremanReact/constants';
7
+ import { helpLabel } from './FormHelpers';
8
+ import { SelectField } from './SelectField';
9
+ import { ResourceSelectAPI } from './ResourceSelect';
10
+ import { noop } from '../../../helpers';
11
+
12
+ const TemplateSearchField = ({
13
+ name,
14
+ controller,
15
+ url,
16
+ labelText,
17
+ required,
18
+ defaultValue,
19
+ setValue,
20
+ values,
21
+ }) => {
22
+ const searchQuery = useSelector(
23
+ state => state.autocomplete?.[name]?.searchQuery
24
+ );
25
+ useEffect(() => {
26
+ setValue({ ...values, [name]: searchQuery });
27
+ // eslint-disable-next-line react-hooks/exhaustive-deps
28
+ }, [searchQuery]);
29
+ const id = name.replace(/ /g, '-');
30
+ const props = getControllerSearchProps(controller.replace('/', '_'), name);
31
+ return (
32
+ <FormGroup
33
+ label={name}
34
+ labelIcon={helpLabel(labelText, name)}
35
+ fieldId={id}
36
+ isRequired={required}
37
+ className="foreman-search-field"
38
+ >
39
+ <SearchBar
40
+ initialQuery={defaultValue}
41
+ data={{
42
+ ...props,
43
+ autocomplete: {
44
+ id: name,
45
+ url,
46
+ useKeyShortcuts: true,
47
+ },
48
+ bookmarks: null,
49
+ }}
50
+ onSearch={noop}
51
+ />
52
+ </FormGroup>
53
+ );
54
+ };
55
+
56
+ export const formatter = (input, values, setValue) => {
57
+ const isSelectType = !!input?.options;
58
+ const inputType = input.value_type;
59
+ const isTextType = inputType === 'plain' || !inputType; // null defaults to plain
60
+
61
+ const {
62
+ name,
63
+ required,
64
+ hidden_value: hidden,
65
+ resource_type: resourceType,
66
+ value_type: valueType,
67
+ } = input;
68
+ const labelText = input.description;
69
+ const value = values[name];
70
+ const id = name.replace(/ /g, '-');
71
+ if (valueType === 'resource') {
72
+ return (
73
+ <FormGroup
74
+ label={name}
75
+ fieldId={id}
76
+ labelIcon={helpLabel(labelText, name)}
77
+ isRequired={required}
78
+ key={id}
79
+ >
80
+ <ResourceSelectAPI
81
+ name={name}
82
+ apiKey={resourceType.replace('::', '')}
83
+ url={`/ui_job_wizard/resources?resource=${resourceType}`}
84
+ selected={value || {}}
85
+ setSelected={newValue => setValue({ ...values, [name]: newValue })}
86
+ />
87
+ </FormGroup>
88
+ );
89
+ }
90
+ if (isSelectType) {
91
+ const options = input.options.split(/\r?\n/).map(option => option.trim());
92
+ return (
93
+ <SelectField
94
+ key={id}
95
+ isRequired={required}
96
+ label={name}
97
+ fieldId={id}
98
+ options={options}
99
+ labelIcon={helpLabel(labelText, name)}
100
+ value={value}
101
+ setValue={newValue => setValue({ ...values, [name]: newValue })}
102
+ />
103
+ );
104
+ }
105
+ if (isTextType) {
106
+ return (
107
+ <FormGroup
108
+ key={name}
109
+ label={name}
110
+ labelIcon={helpLabel(labelText, name)}
111
+ fieldId={id}
112
+ isRequired={required}
113
+ >
114
+ <TextArea
115
+ aria-label={name}
116
+ className={hidden ? 'masked-input' : null}
117
+ required={required}
118
+ rows={2}
119
+ id={id}
120
+ value={value}
121
+ onChange={newValue => setValue({ ...values, [name]: newValue })}
122
+ />
123
+ </FormGroup>
124
+ );
125
+ }
126
+ if (inputType === 'date') {
127
+ return (
128
+ <FormGroup
129
+ key={name}
130
+ label={name}
131
+ labelIcon={helpLabel(labelText, name)}
132
+ fieldId={id}
133
+ isRequired={required}
134
+ >
135
+ <TextInput
136
+ aria-label={name}
137
+ placeholder="YYYY-mm-dd HH:MM"
138
+ className={hidden ? 'masked-input' : null}
139
+ required={required}
140
+ id={id}
141
+ type="text"
142
+ value={value}
143
+ onChange={newValue => setValue({ ...values, [name]: newValue })}
144
+ />
145
+ </FormGroup>
146
+ );
147
+ }
148
+ if (inputType === 'search') {
149
+ const controller = input.resource_type_tableize;
150
+ return (
151
+ <TemplateSearchField
152
+ key={id}
153
+ name={name}
154
+ defaultValue={value}
155
+ controller={resourceType}
156
+ url={`/${controller}/auto_complete_search`}
157
+ labelText={labelText}
158
+ required={required}
159
+ setValue={setValue}
160
+ values={values}
161
+ />
162
+ );
163
+ }
164
+
165
+ return null;
166
+ };
167
+
168
+ TemplateSearchField.propTypes = {
169
+ name: PropTypes.string.isRequired,
170
+ controller: PropTypes.string.isRequired,
171
+ url: PropTypes.string.isRequired,
172
+ labelText: PropTypes.string,
173
+ required: PropTypes.bool.isRequired,
174
+ defaultValue: PropTypes.string,
175
+ setValue: PropTypes.func.isRequired,
176
+ values: PropTypes.object.isRequired,
177
+ };
178
+ TemplateSearchField.defaultProps = {
179
+ labelText: null,
180
+ defaultValue: '',
181
+ };
@@ -0,0 +1,36 @@
1
+ import React, { useState } from 'react';
2
+ import PropTypes from 'prop-types';
3
+ import { FormGroup, TextInput, ValidatedOptions } from '@patternfly/react-core';
4
+ import { translate as __ } from 'foremanReact/common/I18n';
5
+ import { isPositiveNumber } from './FormHelpers';
6
+
7
+ export const NumberInput = ({ formProps, inputProps }) => {
8
+ const [validated, setValidated] = useState();
9
+ const name = inputProps.id.replace(/-/g, ' ');
10
+ return (
11
+ <FormGroup
12
+ {...formProps}
13
+ helperTextInvalid={__('Has to be a positive number')}
14
+ validated={validated}
15
+ >
16
+ <TextInput
17
+ aria-label={name}
18
+ type="text"
19
+ {...inputProps}
20
+ onChange={newValue => {
21
+ setValidated(
22
+ isPositiveNumber(newValue) || newValue === ''
23
+ ? ValidatedOptions.noval
24
+ : ValidatedOptions.error
25
+ );
26
+ inputProps.onChange(newValue);
27
+ }}
28
+ />
29
+ </FormGroup>
30
+ );
31
+ };
32
+
33
+ NumberInput.propTypes = {
34
+ formProps: PropTypes.object.isRequired,
35
+ inputProps: PropTypes.object.isRequired,
36
+ };
@@ -0,0 +1,29 @@
1
+ import React from 'react';
2
+ import { useSelector, useDispatch } from 'react-redux';
3
+ import URI from 'urijs';
4
+ import { get } from 'foremanReact/redux/API';
5
+ import { selectResponse, selectIsLoading } from '../../JobWizardSelectors';
6
+ import { SearchSelect } from '../form/SearchSelect';
7
+
8
+ export const useNameSearchAPI = (apiKey, url) => {
9
+ const dispatch = useDispatch();
10
+ const uri = new URI(url);
11
+ const onSearch = search => {
12
+ dispatch(
13
+ get({
14
+ key: apiKey,
15
+ url: uri.addSearch({
16
+ name: search,
17
+ }),
18
+ })
19
+ );
20
+ };
21
+
22
+ const response = useSelector(state => selectResponse(state, apiKey));
23
+ const isLoading = useSelector(state => selectIsLoading(state, apiKey));
24
+ return [onSearch, response, isLoading];
25
+ };
26
+
27
+ export const ResourceSelectAPI = props => (
28
+ <SearchSelect {...props} useNameSearch={useNameSearchAPI} />
29
+ );
@@ -0,0 +1,121 @@
1
+ import React, { useState, useEffect } from 'react';
2
+ import PropTypes from 'prop-types';
3
+ import { Select, SelectOption, SelectVariant } from '@patternfly/react-core';
4
+ import Immutable from 'seamless-immutable';
5
+ import { sprintf, translate as __ } from 'foremanReact/common/I18n';
6
+
7
+ export const maxResults = 100;
8
+
9
+ export const SearchSelect = ({
10
+ name,
11
+ selected,
12
+ setSelected,
13
+ placeholderText,
14
+ useNameSearch,
15
+ apiKey,
16
+ url,
17
+ variant,
18
+ }) => {
19
+ const [onSearch, response, isLoading] = useNameSearch(apiKey, url);
20
+ const [isOpen, setIsOpen] = useState(false);
21
+ const [typingTimeout, setTypingTimeout] = useState(null);
22
+ const autoSearchDelay = 500;
23
+ useEffect(() => {
24
+ onSearch(selected.name || '');
25
+ if (typingTimeout) {
26
+ return () => clearTimeout(typingTimeout);
27
+ }
28
+ return undefined;
29
+ // eslint-disable-next-line react-hooks/exhaustive-deps
30
+ }, []);
31
+ let selectOptions = [];
32
+ if (response.subtotal > maxResults) {
33
+ selectOptions = [
34
+ <SelectOption
35
+ isDisabled
36
+ key={0}
37
+ description={__('Please refine your search.')}
38
+ >
39
+ {sprintf(
40
+ __('You have %s results to display. Showing first %s results'),
41
+ response.subtotal,
42
+ maxResults
43
+ )}
44
+ </SelectOption>,
45
+ ];
46
+ }
47
+ selectOptions = [
48
+ ...selectOptions,
49
+ ...Immutable.asMutable(response?.results || [])?.map((result, index) => (
50
+ <SelectOption key={index + 1} value={result.id}>
51
+ {result.name}
52
+ </SelectOption>
53
+ )),
54
+ ];
55
+
56
+ const onSelect = (event, selection) => {
57
+ if (variant === SelectVariant.typeahead) {
58
+ setSelected(response.results.find(r => r.id === selection)); // saving the name and id so we will have access to the name between steps
59
+ } else if (variant === SelectVariant.typeaheadMulti) {
60
+ if (selected.map(({ id }) => id).includes(selection)) {
61
+ setSelected(currentSelected =>
62
+ currentSelected.filter(({ id }) => id !== selection)
63
+ );
64
+ } else {
65
+ setSelected(currentSelected => [
66
+ ...currentSelected,
67
+ response.results.find(r => r.id === selection),
68
+ ]);
69
+ }
70
+ }
71
+ };
72
+ const autoSearch = searchTerm => {
73
+ if (typingTimeout) clearTimeout(typingTimeout);
74
+ setTypingTimeout(setTimeout(() => onSearch(searchTerm), autoSearchDelay));
75
+ };
76
+ return (
77
+ <Select
78
+ toggleAriaLabel={`${name} toggle`}
79
+ chipGroupComponent={<></>}
80
+ variant={variant}
81
+ selections={
82
+ variant === SelectVariant.typeahead
83
+ ? selected.id
84
+ : selected.map(({ id }) => id)
85
+ }
86
+ loadingVariant={isLoading ? 'spinner' : null}
87
+ onSelect={onSelect}
88
+ onToggle={setIsOpen}
89
+ isOpen={isOpen}
90
+ className="without_select2"
91
+ maxHeight="45vh"
92
+ onTypeaheadInputChanged={value => {
93
+ autoSearch(value || '');
94
+ }}
95
+ placeholderText={placeholderText}
96
+ onFilter={() => null} // https://github.com/patternfly/patternfly-react/issues/6321
97
+ typeAheadAriaLabel={`${name} typeahead input`}
98
+ >
99
+ {selectOptions}
100
+ </Select>
101
+ );
102
+ };
103
+
104
+ SearchSelect.propTypes = {
105
+ name: PropTypes.string,
106
+ selected: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
107
+ setSelected: PropTypes.func.isRequired,
108
+ placeholderText: PropTypes.string,
109
+ apiKey: PropTypes.string.isRequired,
110
+ url: PropTypes.string,
111
+ useNameSearch: PropTypes.func.isRequired,
112
+ variant: PropTypes.string,
113
+ };
114
+
115
+ SearchSelect.defaultProps = {
116
+ name: 'typeahead select',
117
+ selected: {},
118
+ placeholderText: '',
119
+ url: '',
120
+ variant: SelectVariant.typeahead,
121
+ };