debian-mirror-gitlab/app/models/terraform/state.rb

58 lines
1.6 KiB
Ruby
Raw Normal View History

2020-04-22 19:07:51 +05:30
# frozen_string_literal: true
module Terraform
class State < ApplicationRecord
2020-06-23 00:09:42 +05:30
include UsageStatistics
2020-10-24 23:57:45 +05:30
include FileStoreMounter
2020-06-23 00:09:42 +05:30
2020-04-22 19:07:51 +05:30
HEX_REGEXP = %r{\A\h+\z}.freeze
UUID_LENGTH = 32
belongs_to :project
belongs_to :locked_by_user, class_name: 'User'
2020-11-24 15:15:51 +05:30
has_many :versions, class_name: 'Terraform::StateVersion', foreign_key: :terraform_state_id
has_one :latest_version, -> { ordered_by_version_desc }, class_name: 'Terraform::StateVersion', foreign_key: :terraform_state_id
scope :versioning_not_enabled, -> { where(versioning_enabled: false) }
2020-04-22 19:07:51 +05:30
validates :project_id, presence: true
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-11-24 15:15:51 +05:30
default_value_for(:versioning_enabled, true)
2020-04-22 19:07:51 +05:30
2020-10-24 23:57:45 +05:30
mount_file_store_uploader StateUploader
2020-04-22 19:07:51 +05:30
def file_store
super || StateUploader.default_store
end
2020-11-24 15:15:51 +05:30
def latest_file
versioning_enabled ? latest_version&.file : file
end
2020-10-24 23:57:45 +05:30
def local?
file_store == ObjectStorage::Store::LOCAL
end
2020-04-22 19:07:51 +05:30
def locked?
self.lock_xid.present?
end
2020-11-24 15:15:51 +05:30
def update_file!(data, version:)
if versioning_enabled?
new_version = versions.build(version: version)
new_version.assign_attributes(created_by_user: locked_by_user, file: data)
new_version.save!
else
self.file = data
save!
end
end
2020-04-22 19:07:51 +05:30
end
end
2020-10-24 23:57:45 +05:30
Terraform::State.prepend_if_ee('EE::Terraform::State')