Création de checks par l'API
This commit is contained in:
parent
29d79b2175
commit
f348601aca
|
@ -0,0 +1,38 @@
|
||||||
|
class Api::V1::CheckReportsController < Api::V1::BaseController
|
||||||
|
|
||||||
|
# POST /check-reports or /check-reports.json
|
||||||
|
def create
|
||||||
|
@check_report = CheckReport.new(check_report_params)
|
||||||
|
|
||||||
|
if @check_report.checks.empty?
|
||||||
|
render json: { message: "At least 1 check must be present" }, status: :unprocessable_entity
|
||||||
|
else
|
||||||
|
checks_params = ActionController::Parameters.new(@check_report.checks).permit(:name, :description).map { |check|
|
||||||
|
{
|
||||||
|
hostname: @check_report.hostname,
|
||||||
|
date: @check_report.date,
|
||||||
|
name: check.fetch(:name, ""),
|
||||||
|
description: check.fetch(:description, "")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
checks = Check.create(checks_params)
|
||||||
|
|
||||||
|
persisted = checks.count(&:persisted?)
|
||||||
|
invalid = checks.count(&:invalid?)
|
||||||
|
|
||||||
|
if checks.all?(:persisted?)
|
||||||
|
render json: { message: "#{persisted} checks created" }, status: :created
|
||||||
|
else
|
||||||
|
render json: { message: "error while creating checks : #{persisted} persisted, #{invalid} invalid" }, status: :unprocessable_entity
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
# Only allow a list of trusted parameters through.
|
||||||
|
def check_report_params
|
||||||
|
params.require(:check_report).permit(:hostname, :date, {checks: [:name, :description]})
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
|
@ -1,26 +0,0 @@
|
||||||
class Api::V1::ChecksController < Api::V1::BaseController
|
|
||||||
|
|
||||||
# POST /checks or /checks.json
|
|
||||||
def create
|
|
||||||
@check = Check.new(check_params)
|
|
||||||
|
|
||||||
if @check.save
|
|
||||||
render json: { message: "created" }, status: :created
|
|
||||||
else
|
|
||||||
render json: @check.errors, status: :unprocessable_entity
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
# Use callbacks to share common setup or constraints between actions.
|
|
||||||
def set_check
|
|
||||||
@check = Check.find(params[:id])
|
|
||||||
end
|
|
||||||
|
|
||||||
# Only allow a list of trusted parameters through.
|
|
||||||
def check_params
|
|
||||||
params.require(:check).permit(:name, :description, :hostname)
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
class CheckReport
|
||||||
|
include ActiveModel::API
|
||||||
|
include ActiveModel::Attributes
|
||||||
|
|
||||||
|
attribute :hostname, :string
|
||||||
|
attribute :date, :datetime, default: -> { Time.now }
|
||||||
|
attribute :checks, array: true
|
||||||
|
end
|
|
@ -43,7 +43,7 @@
|
||||||
<%= check.description %>
|
<%= check.description %>
|
||||||
</td>
|
</td>
|
||||||
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
|
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
|
||||||
<%= check.created_at %>
|
<%= check.date %>
|
||||||
</td>
|
</td>
|
||||||
<td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
|
<td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
|
||||||
<%= link_to "Show", check, class: "text-indigo-600 hover:text-indigo-900" %>
|
<%= link_to "Show", check, class: "text-indigo-600 hover:text-indigo-900" %>
|
||||||
|
@ -55,7 +55,7 @@
|
||||||
</div>
|
</div>
|
||||||
<!-- END Table -->
|
<!-- END Table -->
|
||||||
|
|
||||||
<div class="bg-white px-4 py-3 flex items-center justify-between border-t border-gray-200 sm:px-6">
|
<div class="px-4 py-3 flex items-center justify-between border-t border-gray-200 sm:px-6">
|
||||||
<div class="flex-1 flex justify-between sm:hidden">
|
<div class="flex-1 flex justify-between sm:hidden">
|
||||||
<%= link_to_previous_page @checks, 'Previous page', class: "ml-3 paginate-page paginate-page-single" %>
|
<%= link_to_previous_page @checks, 'Previous page', class: "ml-3 paginate-page paginate-page-single" %>
|
||||||
<%= link_to_next_page @checks, 'Next Page', class: "paginate-page paginate-page-single" %>
|
<%= link_to_next_page @checks, 'Next Page', class: "paginate-page paginate-page-single" %>
|
||||||
|
|
|
@ -4,7 +4,7 @@ Rails.application.routes.draw do
|
||||||
namespace :v1 do
|
namespace :v1 do
|
||||||
defaults format: :json do
|
defaults format: :json do
|
||||||
get '/ping', to: 'base#ping'
|
get '/ping', to: 'base#ping'
|
||||||
resources :checks, only: [:create]
|
resources :check_reports, path: "check-reports", only: [:create]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
class AddDateToChecks < ActiveRecord::Migration[7.0]
|
||||||
|
def change
|
||||||
|
add_column :checks, :date, :datetime, null: false
|
||||||
|
end
|
||||||
|
end
|
|
@ -10,7 +10,7 @@
|
||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# It's strongly recommended that you check this file into your version control system.
|
||||||
|
|
||||||
ActiveRecord::Schema.define(version: 2022_01_24_181635) do
|
ActiveRecord::Schema.define(version: 2022_01_27_153040) do
|
||||||
|
|
||||||
create_table "api_keys", force: :cascade do |t|
|
create_table "api_keys", force: :cascade do |t|
|
||||||
t.string "token"
|
t.string "token"
|
||||||
|
@ -27,6 +27,7 @@ ActiveRecord::Schema.define(version: 2022_01_24_181635) do
|
||||||
t.string "hostname"
|
t.string "hostname"
|
||||||
t.datetime "created_at", precision: 6, null: false
|
t.datetime "created_at", precision: 6, null: false
|
||||||
t.datetime "updated_at", precision: 6, null: false
|
t.datetime "updated_at", precision: 6, null: false
|
||||||
|
t.datetime "date", precision: 6, null: false
|
||||||
t.index ["hostname"], name: "index_checks_on_hostname"
|
t.index ["hostname"], name: "index_checks_on_hostname"
|
||||||
t.index ["name"], name: "index_checks_on_name"
|
t.index ["name"], name: "index_checks_on_name"
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue