From bfa16373ed1a7abbd7a4d884359d8f4ead07a970 Mon Sep 17 00:00:00 2001 From: Bherly Novrandy Date: Mon, 7 May 2018 14:30:00 +0700 Subject: [PATCH] [Bherly/Tara] Add setup & app status --- Gemfile | 1 + Gemfile.lock | 3 ++ app/models/app.rb | 38 ++++++++++++++++++- .../20180507062506_add_status_to_app.rb | 8 ++++ db/schema.rb | 11 ++++-- spec/factories/apps.rb | 4 +- spec/models/app_spec.rb | 8 ++++ 7 files changed, 67 insertions(+), 6 deletions(-) create mode 100644 db/migrate/20180507062506_add_status_to_app.rb diff --git a/Gemfile b/Gemfile index 1345c837..22ca8ba6 100644 --- a/Gemfile +++ b/Gemfile @@ -18,6 +18,7 @@ gem 'execjs' gem 'therubyracer' gem 'berkshelf' gem 'knife-solo' +gem 'enumerize' group :development, :test do gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] diff --git a/Gemfile.lock b/Gemfile.lock index 9c06bbf2..f5fdf436 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -135,6 +135,8 @@ GEM devise (>= 1.2.0) rubycas-client (>= 2.2.1) diff-lcs (1.3) + enumerize (2.2.2) + activesupport (>= 3.2) erubis (2.7.0) execjs (2.7.0) factory_bot (4.8.2) @@ -406,6 +408,7 @@ DEPENDENCIES byebug devise devise_cas_authenticatable + enumerize execjs factory_bot_rails figaro diff --git a/app/models/app.rb b/app/models/app.rb index 6841b6ec..b59eb18f 100644 --- a/app/models/app.rb +++ b/app/models/app.rb @@ -1,9 +1,25 @@ class App < ActiveRecord::Base + extend Enumerize validates_presence_of :name + + enumerize :app_status, in: %w(INACTIVE ACTIVE) + enumerize :setup_status, in: %w( + PENDING + BLUEPRINT_CREATION + BLUEPRINT_CREATION_ERROR + BLUEPRINT_EXECUTED + BLUEPRINT_EXECUTED_ERROR + PROVISIONING_STARTED + PROVISIONING_ERROR + CHEF_BOOTSTRAP_STARTED + CHEF_BOOTSTRAP_ERROR + FINISHED + ) + belongs_to :app_group, required: true belongs_to :log_template, required: true - after_create :generate_secret_key, :generate_receiver_end_point, :generate_kibana_address + after_create :generate_secret_key, :generate_receiver_end_point, :generate_kibana_address, :set_setup_status_pending, :set_app_status_inactive def generate_secret_key update_column(:secret_key, SecureRandom.base64) @@ -16,5 +32,23 @@ def generate_receiver_end_point def generate_kibana_address update_column(:kibana_address, 'http://dummy.kibana-address/') end - + + def set_setup_status_pending + set_setup_status('PENDING') + end + + def set_app_status_inactive + set_app_status('INACTIVE') + end + + private + + def set_setup_status(status) + self.setup_status=status + end + + def set_app_status(status) + self.app_status=status + end + end diff --git a/db/migrate/20180507062506_add_status_to_app.rb b/db/migrate/20180507062506_add_status_to_app.rb new file mode 100644 index 00000000..81f8f3c1 --- /dev/null +++ b/db/migrate/20180507062506_add_status_to_app.rb @@ -0,0 +1,8 @@ +class AddStatusToApp < ActiveRecord::Migration + def change + add_column :apps, :setup_status, :string, :default => "PENDING" + add_index :apps, :setup_status + add_column :apps, :app_status, :string, :default => "INACTIVE", :index => true + add_index :apps, :app_status + end +end diff --git a/db/schema.rb b/db/schema.rb index 9b3ee803..ed76da06 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20180507040330) do +ActiveRecord::Schema.define(version: 20180507062506) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -26,13 +26,18 @@ t.string "name" t.integer "log_template_id" t.integer "app_group_id" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false t.string "secret_key" t.string "kibana_address" t.string "receiver_end_point" + t.string "setup_status", default: "PENDING" + t.string "app_status", default: "INACTIVE" end + add_index "apps", ["app_status"], name: "index_apps_on_app_status", using: :btree + add_index "apps", ["setup_status"], name: "index_apps_on_setup_status", using: :btree + create_table "log_templates", force: :cascade do |t| t.string "name" t.integer "tps_limit" diff --git a/spec/factories/apps.rb b/spec/factories/apps.rb index 6469e7d5..d2033830 100644 --- a/spec/factories/apps.rb +++ b/spec/factories/apps.rb @@ -1,7 +1,9 @@ FactoryBot.define do factory :app do name "app1" - + setup_status "PENDING" + app_status "INACTIVE" + log_template app_group end diff --git a/spec/models/app_spec.rb b/spec/models/app_spec.rb index e268a457..19242610 100644 --- a/spec/models/app_spec.rb +++ b/spec/models/app_spec.rb @@ -33,5 +33,13 @@ app = FactoryBot.create(:app) expect(app.kibana_address).to eq('http://dummy.kibana-address/') end + it 'should has pending setup status' do + app = FactoryBot.create(:app) + expect(app.setup_status).to eq('PENDING') + end + it 'should has inactive app status' do + app = FactoryBot.create(:app) + expect(app.app_status).to eq('INACTIVE') + end end end