forked from theforeman/foreman_webhooks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhook_templates_controller.rb
More file actions
109 lines (95 loc) · 4.02 KB
/
Copy pathwebhook_templates_controller.rb
File metadata and controls
109 lines (95 loc) · 4.02 KB
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
# frozen_string_literal: true
module Api
module V2
class WebhookTemplatesController < V2::BaseController
include Api::Version2
include ForemanWebhooks::Controller::Parameters::WebhookTemplate
include Foreman::Controller::TemplateImport
before_action :find_resource, only: %i[show update destroy clone export]
api :GET, '/webhook_templates/', N_('List webhook templates')
param_group :search_and_pagination, ::Api::V2::BaseController
add_scoped_search_description_for(WebhookTemplate)
def index
@webhook_templates = resource_scope_for_index
end
api :GET, '/webhook_templates/:id', N_('Show webhook template details')
param :id, :identifier, required: true
def show; end
def_param_group :webhook_template do
param :webhook_template, Hash, action_aware: true, required: true do
param :name, String, required: true
param :description, String
param :template, String, required: true
param :snippet, :bool, allow_nil: true
param :audit_comment, String, allow_nil: true
param :locked, :bool, desc: N_('Whether or not the template is locked for editing')
param :default, :bool,
desc: N_('Whether or not the template is added automatically to new organizations and locations')
param_group :taxonomies, ::Api::V2::BaseController
end
end
def_param_group :webhook_template_clone do
param :webhook_template, Hash, required: true, action_aware: true do
param :name, String, required: true, desc: N_('Template name')
end
end
api :POST, '/webhook_templates/', N_('Create a webhook template')
param_group :webhook_template, as: :create
def create
@webhook_template = WebhookTemplate.new(webhook_template_params)
process_response @webhook_template.save
end
api :POST, '/webhook_templates/import', N_('Import a webhook template')
param :webhook_template, Hash, required: true, action_aware: true do
param :name, String, required: true, desc: N_('Template name')
param :template, String, required: true, desc: N_('Template contents including metadata')
param_group :taxonomies, ::Api::V2::BaseController
end
param_group :template_import_options, ::Api::V2::BaseController
def import
@webhook_template = WebhookTemplate.import!(*import_attrs_for(:webhook_template))
process_response @webhook_template
end
api :PUT, '/webhook_templates/:id', N_('Update a webhook template')
param :id, :identifier, required: true
param_group :webhook_template, as: :update
def update
process_response @webhook_template.update(webhook_template_params)
end
api :DELETE, '/webhook_templates/:id', N_('Delete a webhook template')
param :id, :identifier, required: true
def destroy
process_response @webhook_template.destroy
end
api :POST, '/webhook_templates/:id/clone', N_('Clone a template')
param :id, :identifier, required: true
param_group :webhook_template_clone, as: :create
def clone
original = @webhook_template
@webhook_template = original.dup
@webhook_template.cloned_from = original
@webhook_template.name = params[:webhook_template][:name]
@webhook_template.locked = false
process_response @webhook_template.save
end
api :GET, '/webhook_templates/:id/export', N_('Export a webhook template to ERB')
param :id, :identifier, required: true
def export
send_data @webhook_template.to_erb, type: 'text/plain',
disposition: 'attachment',
filename: @webhook_template.filename
end
private
def action_permission
case params[:action]
when 'clone', 'import'
'create'
when 'export'
'view'
else
super
end
end
end
end
end