-
Notifications
You must be signed in to change notification settings - Fork 898
/
service_templates.rb
126 lines (106 loc) · 4.67 KB
/
service_templates.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
module Api
module Subcollections
module ServiceTemplates
def service_templates_query_resource(object)
klass = collection_class(:service_templates)
object ? klass.where(:service_template_catalog_id => object.id) : {}
end
def service_templates_assign_resource(object, type, id = nil, _data = nil)
raise BadRequestError, "Must specify an id for Assigning a #{type} resource" unless id
service_template_subcollection_action(type, id) do |st|
api_log_info("Assigning #{service_template_ident(st)}")
assign_service_template(object, st)
end
end
def service_templates_unassign_resource(object, type, id = nil, _data = nil)
raise BadRequestError, "Must specify an id for Unassigning a #{type} resource" unless id
service_template_subcollection_action(type, id) do |st|
api_log_info("Unassigning #{service_template_ident(st)}")
unassign_service_template(object, st)
end
end
def service_templates_order_resource(_object, type, id = nil, data = nil)
klass = collection_class(:service_templates)
service_template = resource_search(id, type, klass)
workflow = ServiceTemplateWorkflow.create(service_template, data || {})
request_result = workflow.submit_request
errors = request_result[:errors]
if errors.present?
raise BadRequestError, "Failed to order #{service_template_ident(service_template)} - #{errors.join(", ")}"
end
request_result[:request]
end
def service_templates_refresh_dialog_fields_resource(object, type, id = nil, data = nil)
raise BadRequestError, "Must specify an id for Refreshing dialog fields of a #{type} resource" unless id
service_template_subcollection_action(type, id) do |st|
api_log_info("Refreshing dialog fields for #{service_template_ident(st)}")
refresh_dialog_fields_service_template(object, st, data)
end
end
def delete_resource_service_templates(_parent, type, id, data)
delete_resource(type, id, data)
end
private
def service_template_ident(st)
"Service Template id:#{st.id} name:'#{st.name}'"
end
def service_template_subcollection_action(type, id)
klass = collection_class(:service_templates)
result =
begin
st = resource_search(id, type, klass)
yield(st) if block_given?
rescue => err
action_result(false, err.to_s)
end
add_subcollection_resource_to_result(result, type, st) if st
add_parent_href_to_result(result)
log_result(result)
result
end
def assign_service_template(object, st)
stcid = st.service_template_catalog_id
if stcid
action_result(stcid == object.id, "Service Template #{st.id} is currently assigned to Service Catalog #{stcid}")
else
object.service_templates << st
action_result(true, "Assigning #{service_template_ident(st)}")
end
rescue => err
action_result(false, err.to_s)
end
def unassign_service_template(object, st)
stcid = st.service_template_catalog_id
if stcid
if stcid != object.id
action_result(false, "Service Template #{st.id} is not currently assigned to Service Catalog #{stcid}")
else
object.service_templates -= Array.wrap(st)
action_result(true, "Unassigning #{service_template_ident(st)}")
end
else
action_result(true, "Service Template #{st.id} is not currently assigned to a Service Catalog")
end
rescue => err
action_result(false, err.to_s)
end
def refresh_dialog_fields_service_template(_object, st, data)
data ||= {}
dialog_fields = Hash(data["dialog_fields"])
refresh_fields = data["fields"]
return action_result(false, "Must specify fields to refresh") if refresh_fields.blank?
dialog = define_service_template_dialog(st, dialog_fields)
return action_result(false, "Service Template has no provision dialog defined") unless dialog
refresh_dialog_fields_action(dialog, refresh_fields, service_template_ident(st))
rescue => err
action_result(false, err.to_s)
end
def define_service_template_dialog(st, dialog_fields)
resource_action = st.resource_actions.find_by(:action => "Provision")
workflow = ResourceActionWorkflow.new({}, User.current_user, resource_action, :target => st)
dialog_fields.each { |key, value| workflow.set_value(key, value) }
workflow.dialog
end
end
end
end