|
| 1 | +require 'grape' |
| 2 | + |
| 3 | +class OverseerStepsApi < Grape::API |
| 4 | + helpers AuthenticationHelpers |
| 5 | + helpers AuthorisationHelpers |
| 6 | + helpers SidekiqHelper |
| 7 | + |
| 8 | + before do |
| 9 | + authenticated? |
| 10 | + end |
| 11 | + |
| 12 | + desc 'Add an overseer step' |
| 13 | + params do |
| 14 | + requires :overseer_step, type: Hash do |
| 15 | + requires :name, type: String |
| 16 | + optional :description, type: String |
| 17 | + optional :display_name, type: String |
| 18 | + optional :display_description, type: String |
| 19 | + optional :run_command, type: String |
| 20 | + optional :timeout_ms, type: Integer |
| 21 | + # TODO: rename to execution_order || exec_order? |
| 22 | + optional :sort_order, type: Integer |
| 23 | + requires :step_type, type: String |
| 24 | + optional :stdin_input_file, type: String |
| 25 | + optional :expected_output_file, type: String |
| 26 | + optional :feedback_message, type: String |
| 27 | + optional :status_on_success, type: String |
| 28 | + optional :status_on_failure, type: String |
| 29 | + optional :halt_on_success, type: Boolean |
| 30 | + optional :halt_on_failure, type: Boolean |
| 31 | + optional :show_expected_output, type: Boolean |
| 32 | + optional :show_stdin, type: Boolean |
| 33 | + optional :show_stdout, type: Boolean |
| 34 | + optional :enabled, type: Boolean |
| 35 | + end |
| 36 | + requires :task_def_id, type: Integer |
| 37 | + end |
| 38 | + post '/units/:unit_id/task_definitions/:task_def_id/overseer_steps' do |
| 39 | + # unless authorise? current_user, User, :admin_overseer |
| 40 | + # error!({ error: 'Not authorised to create overseer images' }, 403) |
| 41 | + # end |
| 42 | + # TODO: ensure correct permissions |
| 43 | + |
| 44 | + task_definition = TaskDefinition.find(params[:task_def_id]) |
| 45 | + |
| 46 | + unless Doubtfire::Application.config.overseer_enabled |
| 47 | + error!({ error: 'Overseer is not enabled. Enable Overseer before updating settings.' }, 403) |
| 48 | + end |
| 49 | + |
| 50 | + # status_on_success = TaskStatus.status_for_name(params[:status_on_success]) |
| 51 | + # status_on_failure = TaskStatus.status_for_name(params[:status_on_failure]) |
| 52 | + |
| 53 | + status_on_success_id = params[:status_on_success].present? && params[:status_on_success] != 'no_change' ? TaskStatus.status_for_name(params[:status_on_success])&.id : nil |
| 54 | + status_on_failure_id = params[:status_on_failure].present? && params[:status_on_failure] != 'no_change' ? TaskStatus.status_for_name(params[:status_on_failure])&.id : nil |
| 55 | + |
| 56 | + # status_on_success_id = TaskStatus.status_for_name(params[:status_on_success])&.id |
| 57 | + # status_on_failure_id = TaskStatus.status_for_name(params[:status_on_failure])&.id |
| 58 | + |
| 59 | + overseer_step_params = ActionController::Parameters.new(params) |
| 60 | + .require(:overseer_step) |
| 61 | + .permit( |
| 62 | + :name, |
| 63 | + :description, |
| 64 | + :display_name, |
| 65 | + :display_description, |
| 66 | + :run_command, |
| 67 | + :timeout_ms, |
| 68 | + :sort_order, |
| 69 | + :step_type, |
| 70 | + :stdin_input_file, |
| 71 | + :expected_output_file, |
| 72 | + :feedback_message, |
| 73 | + :status_on_success_id, |
| 74 | + :status_on_failure_id, |
| 75 | + :halt_on_success, |
| 76 | + :halt_on_failure, |
| 77 | + :show_expected_output, |
| 78 | + :show_stdin, |
| 79 | + :show_stdout, |
| 80 | + :enabled |
| 81 | + ) |
| 82 | + .merge(task_definition_id: task_definition.id, |
| 83 | + status_on_success_id: status_on_success_id, |
| 84 | + status_on_failure_id: status_on_failure_id) |
| 85 | + |
| 86 | + result = OverseerStep.create!(overseer_step_params) |
| 87 | + |
| 88 | + if result.nil? |
| 89 | + error!({ error: 'No overseer step added' }, 403) |
| 90 | + else |
| 91 | + present result, with: Entities::OverseerStepEntity |
| 92 | + end |
| 93 | + end |
| 94 | + |
| 95 | + desc 'Update an overseer step' |
| 96 | + params do |
| 97 | + requires :overseer_step, type: Hash do |
| 98 | + optional :name, type: String |
| 99 | + optional :description, type: String |
| 100 | + optional :display_name, type: String |
| 101 | + optional :display_description, type: String |
| 102 | + optional :run_command, type: String |
| 103 | + optional :timeout_ms, type: Integer |
| 104 | + optional :sort_order, type: Integer |
| 105 | + optional :step_type, type: String |
| 106 | + optional :stdin_input_file, type: String |
| 107 | + optional :expected_output_file, type: String |
| 108 | + optional :feedback_message, type: String |
| 109 | + optional :status_on_success, type: String |
| 110 | + optional :status_on_failure, type: String |
| 111 | + optional :halt_on_success, type: Boolean |
| 112 | + optional :halt_on_failure, type: Boolean |
| 113 | + optional :show_expected_output, type: Boolean |
| 114 | + optional :show_stdin, type: Boolean |
| 115 | + optional :show_stdout, type: Boolean |
| 116 | + optional :enabled, type: Boolean |
| 117 | + end |
| 118 | + requires :task_def_id, type: Integer |
| 119 | + end |
| 120 | + put '/units/:unit_id/task_definitions/:task_def_id/overseer_steps/:id' do |
| 121 | + # unless authorise? current_user, User, :admin_overseer |
| 122 | + # error!({ error: 'Not authorised to create overseer images' }, 403) |
| 123 | + # end |
| 124 | + # TODO: ensure correct permissions |
| 125 | + |
| 126 | + unless Doubtfire::Application.config.overseer_enabled |
| 127 | + error!({ error: 'Overseer is not enabled. Enable Overseer before updating settings.' }, 403) |
| 128 | + end |
| 129 | + |
| 130 | + overseer_step = OverseerStep.find(params[:id]) |
| 131 | + |
| 132 | + puts params.inspect |
| 133 | + |
| 134 | + status_on_success_id = params[:status_on_success].present? ? TaskStatus.status_for_name(params[:status_on_success])&.id : nil |
| 135 | + status_on_failure_id = params[:status_on_failure].present? ? TaskStatus.status_for_name(params[:status_on_failure])&.id : nil |
| 136 | + |
| 137 | + overseer_step_params = ActionController::Parameters.new(params) |
| 138 | + .require(:overseer_step) |
| 139 | + .permit( |
| 140 | + :name, |
| 141 | + :description, |
| 142 | + :display_name, |
| 143 | + :display_description, |
| 144 | + :run_command, |
| 145 | + :timeout_ms, |
| 146 | + :sort_order, |
| 147 | + :step_type, |
| 148 | + :stdin_input_file, |
| 149 | + :expected_output_file, |
| 150 | + :feedback_message, |
| 151 | + :status_on_success_id, |
| 152 | + :status_on_failure_id, |
| 153 | + :halt_on_success, |
| 154 | + :halt_on_failure, |
| 155 | + :show_expected_output, |
| 156 | + :show_stdin, |
| 157 | + :show_stdout, |
| 158 | + :enabled |
| 159 | + ) |
| 160 | + .merge( |
| 161 | + status_on_success_id: status_on_success_id, |
| 162 | + status_on_failure_id: status_on_failure_id |
| 163 | + ) |
| 164 | + # .merge(task_definition_id: task_definition.id) |
| 165 | + |
| 166 | + overseer_step.update!(overseer_step_params) |
| 167 | + |
| 168 | + present overseer_step, with: Entities::OverseerStepEntity |
| 169 | + end |
| 170 | + |
| 171 | + # desc 'Update an overseer image' |
| 172 | + # params do |
| 173 | + # requires :overseer_image, type: Hash do |
| 174 | + # optional :name, type: String, desc: 'The name of the overseer image' |
| 175 | + # optional :tag, type: String, desc: 'The tag used to receive from container repo' |
| 176 | + # end |
| 177 | + # end |
| 178 | + # put '/admin/overseer_images/:id' do |
| 179 | + # unless authorise? current_user, User, :admin_overseer |
| 180 | + # error!({ error: 'Not authorised to update an overseer image' }, 403) |
| 181 | + # end |
| 182 | + # unless Doubtfire::Application.config.overseer_enabled |
| 183 | + # error!({ error: 'Overseer is not enabled. Enable Overseer before updating settings.' }, 403) |
| 184 | + # end |
| 185 | + |
| 186 | + # overseer_image = OverseerImage.find(params[:id]) |
| 187 | + |
| 188 | + # overseer_image_params = ActionController::Parameters.new(params) |
| 189 | + # .require(:overseer_image) |
| 190 | + # .permit(:name, |
| 191 | + # :tag) |
| 192 | + |
| 193 | + # # Clear image status and text when updating |
| 194 | + # overseer_image_params[:pulled_image_status] = nil |
| 195 | + # overseer_image_params[:pulled_image_text] = nil |
| 196 | + # overseer_image_params[:last_pulled_date] = nil |
| 197 | + |
| 198 | + # overseer_image.update!(overseer_image_params) |
| 199 | + # present overseer_image, with: Entities::OverseerImageEntity |
| 200 | + # end |
| 201 | + |
| 202 | + # desc 'Delete an overseer image' |
| 203 | + # delete '/admin/overseer_images/:id' do |
| 204 | + # unless authorise? current_user, User, :admin_overseer |
| 205 | + # error!({ error: 'Not authorised to delete an overseer image' }, 403) |
| 206 | + # end |
| 207 | + |
| 208 | + # overseer_image = OverseerImage.find(params[:id]) |
| 209 | + # overseer_image.destroy |
| 210 | + # error!({ error: overseer_image.errors.full_messages.last }, 403) unless overseer_image.destroyed? |
| 211 | + |
| 212 | + # present overseer_image.destroyed?, with: Grape::Presenters::Presenter |
| 213 | + # end |
| 214 | + |
| 215 | + # desc 'Get all overseer images' |
| 216 | + # get '/admin/overseer_images' do |
| 217 | + # unless authorise? current_user, User, :use_overseer |
| 218 | + # error!({ error: 'Not authorised to get overseer images' }, 403) |
| 219 | + # end |
| 220 | + |
| 221 | + # if Doubtfire::Application.config.overseer_enabled |
| 222 | + # present OverseerImage.all, with: Entities::OverseerImageEntity |
| 223 | + # else |
| 224 | + # present [], with: Grape::Presenters::Presenter |
| 225 | + # end |
| 226 | + # end |
| 227 | + |
| 228 | + # desc 'Get all overseer images' |
| 229 | + # get '/admin/overseer_images/:id' do |
| 230 | + # unless authorise? current_user, User, :use_overseer |
| 231 | + # error!({ error: 'Not authorised to get overseer images' }, 403) |
| 232 | + # end |
| 233 | + |
| 234 | + # if Doubtfire::Application.config.overseer_enabled |
| 235 | + # present OverseerImage.find(params[:id]), with: Entities::OverseerImageEntity |
| 236 | + # else |
| 237 | + # present [], with: Grape::Presenters::Presenter |
| 238 | + # end |
| 239 | + # end |
| 240 | + |
| 241 | + # desc 'Get overseer image by id and pull image' |
| 242 | + # put '/admin/overseer_images/:id/pull_image' do |
| 243 | + # unless authorise? current_user, User, :admin_overseer |
| 244 | + # error!({ error: 'Not authorised to pull an overseer image' }, 403) |
| 245 | + # end |
| 246 | + # unless Doubtfire::Application.config.overseer_enabled |
| 247 | + # error!({ error: 'Overseer is not enabled. Enable Overseer before updating settings.' }, 403) |
| 248 | + # end |
| 249 | + |
| 250 | + # overseer_image = OverseerImage.find(params[:id]) |
| 251 | + |
| 252 | + # job_id = PullDockerImageJob.perform_async(overseer_image.id) |
| 253 | + # job = setup_job(job_id) |
| 254 | + |
| 255 | + # present job, with: Entities::SidekiqJobEntity |
| 256 | + # end |
| 257 | +end |
0 commit comments