forked from ManageIQ/manageiq
-
Notifications
You must be signed in to change notification settings - Fork 1
/
resource_action_workflow.rb
161 lines (129 loc) · 4.08 KB
/
resource_action_workflow.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
class ResourceActionWorkflow < MiqRequestWorkflow
attr_accessor :dialog, :request_options
attr_reader :target
def self.base_model
ResourceActionWorkflow
end
def initialize(values, requester, resource_action, options = {})
@settings = {}
@requester = requester
@target = options[:target]
@initiator = options[:initiator]
@dialog = load_dialog(resource_action, values, options)
@settings[:resource_action_id] = resource_action.id if resource_action
@settings[:dialog_id] = @dialog.id if @dialog
end
Vmdb::Deprecation.deprecate_methods(self, :dialogs => :dialog)
def submit_request(data = {})
update_dialog_field_values(data) if data.present?
process_request(ServiceOrder::STATE_ORDERED)
end
def add_request_to_cart
process_request(ServiceOrder::STATE_CART)
end
def update_dialog_field_values(data)
@dialog.load_values_into_fields(data)
end
def process_request(state)
result = {:errors => validate_dialog}
return result if result[:errors].present?
values = create_values
if create_request?(values)
result[:request] = generate_request(state, values)
else
ra = load_resource_action(values)
if ra.resource.try(:open_url?)
result[:task_id] = ra.deliver_task(values, @target, @requester)
else
ra.deliver_queue(values, @target, @requester)
end
end
result
end
def generate_request(state, values)
make_request(nil, values.merge(:cart_state => state))
end
def validate_dialog
@dialog.try(:validate_field_data) || []
end
def create_values
create_values_hash.tap do |value|
value[:src_id] = @target.id
value[:request_options] = request_options if request_options.present?
end
end
def request_class
@target.request_class
end
def has_request_class?
!request_class.nil? rescue false
end
def request_type
@target.request_type
end
def load_resource_action(values = nil)
id = values ? values.fetch_path(:workflow_settings, :resource_action_id) : @settings[:resource_action_id]
ResourceAction.find_by(:id => id)
end
def create_values_hash
{
:dialog => @dialog.try(:automate_values_hash),
:workflow_settings => @settings,
:initiator => @initiator
}
end
def init_field_hash
@dialog.dialog_fields.index_by { |df| df.name }
end
def set_value(name, value)
dlg_field = dialog_field(name)
if dlg_field.nil?
Rails.logger.warn("ResourceActionWorkflow.set_value dialog field with name <#{name.class.name}:#{name.inspect}> not found")
return nil
end
dlg_field.value = value
# TODO: Return list of changed field names
nil
end
def value(name)
@dialog.field(name)&.value
end
def dialog_field(name)
@dialog.field(name)
end
def validate(_values = nil)
validate_dialog.blank?
end
private
def load_dialog(resource_action, values, options)
if resource_action.nil?
resource_action = load_resource_action(values)
@settings[:resource_action_id] = resource_action.id if resource_action
end
dialog = resource_action.dialog if resource_action
load_proper_dialog_values(dialog, options, values) if dialog
dialog
end
def load_proper_dialog_values(dialog, options, values)
dialog.target_resource = @target
if options[:display_view_only]
dialog.init_fields_with_values_for_request(values)
elsif options[:provision_workflow] || options[:init_defaults]
dialog.initialize_value_context(values)
dialog.load_values_into_fields(values, false)
elsif options[:submit_workflow]
dialog.load_values_into_fields(values)
elsif options[:refresh]
dialog.initialize_static_values
dialog.load_values_into_fields(values, false)
elsif options[:reconfigure]
dialog.initialize_with_given_values(values)
else
dialog.initialize_value_context(values)
end
end
def create_request?(values)
ra = load_resource_action(values)
!ra.resource.kind_of?(CustomButton) && has_request_class?
end
end