2020-03-27 08:07:43 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Terraform
|
|
|
|
class State < ApplicationRecord
|
2020-05-20 23:08:00 -04:00
|
|
|
include UsageStatistics
|
|
|
|
|
2020-04-21 11:21:10 -04:00
|
|
|
DEFAULT = '{"version":1}'.freeze
|
|
|
|
HEX_REGEXP = %r{\A\h+\z}.freeze
|
|
|
|
UUID_LENGTH = 32
|
|
|
|
|
2020-03-27 08:07:43 -04:00
|
|
|
belongs_to :project
|
2020-04-21 11:21:10 -04:00
|
|
|
belongs_to :locked_by_user, class_name: 'User'
|
2020-03-27 08:07:43 -04:00
|
|
|
|
|
|
|
validates :project_id, presence: true
|
2020-04-21 11:21:10 -04:00
|
|
|
validates :uuid, presence: true, uniqueness: true, length: { is: UUID_LENGTH },
|
|
|
|
format: { with: HEX_REGEXP, message: 'only allows hex characters' }
|
|
|
|
|
|
|
|
default_value_for(:uuid, allows_nil: false) { SecureRandom.hex(UUID_LENGTH / 2) }
|
2020-03-27 08:07:43 -04:00
|
|
|
|
|
|
|
after_save :update_file_store, if: :saved_change_to_file?
|
|
|
|
|
|
|
|
mount_uploader :file, StateUploader
|
|
|
|
|
2020-04-21 11:21:10 -04:00
|
|
|
default_value_for(:file) { CarrierWaveStringFile.new(DEFAULT) }
|
|
|
|
|
2020-03-27 08:07:43 -04:00
|
|
|
def update_file_store
|
|
|
|
# The file.object_store is set during `uploader.store!`
|
|
|
|
# which happens after object is inserted/updated
|
|
|
|
self.update_column(:file_store, file.object_store)
|
|
|
|
end
|
|
|
|
|
|
|
|
def file_store
|
|
|
|
super || StateUploader.default_store
|
|
|
|
end
|
2020-04-21 11:21:10 -04:00
|
|
|
|
|
|
|
def locked?
|
|
|
|
self.lock_xid.present?
|
|
|
|
end
|
2020-03-27 08:07:43 -04:00
|
|
|
end
|
|
|
|
end
|