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-08-13 14:10:36 -04:00
|
|
|
include FileStoreMounter
|
2020-10-02 11:08:13 -04:00
|
|
|
include IgnorableColumns
|
|
|
|
# These columns are being removed since geo replication falls to the versioned state
|
|
|
|
# Tracking in https://gitlab.com/gitlab-org/gitlab/-/issues/258262
|
|
|
|
ignore_columns %i[verification_failure verification_retry_at verified_at verification_retry_count verification_checksum],
|
|
|
|
remove_with: '13.7',
|
|
|
|
remove_after: '2020-12-22'
|
2020-05-20 23:08:00 -04:00
|
|
|
|
2020-04-21 11:21:10 -04:00
|
|
|
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
|
|
|
|
2020-09-16 08:10:15 -04:00
|
|
|
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-09-30 08:09:53 -04:00
|
|
|
scope :ordered_by_name, -> { order(:name) }
|
2020-09-16 08:10:15 -04:00
|
|
|
|
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-09-16 08:10:15 -04:00
|
|
|
default_value_for(:versioning_enabled, true)
|
2020-03-27 08:07:43 -04:00
|
|
|
|
2020-08-13 14:10:36 -04:00
|
|
|
mount_file_store_uploader StateUploader
|
2020-03-27 08:07:43 -04:00
|
|
|
|
|
|
|
def file_store
|
|
|
|
super || StateUploader.default_store
|
|
|
|
end
|
2020-04-21 11:21:10 -04:00
|
|
|
|
2020-09-16 08:10:15 -04:00
|
|
|
def latest_file
|
|
|
|
versioning_enabled ? latest_version&.file : file
|
|
|
|
end
|
|
|
|
|
2020-04-21 11:21:10 -04:00
|
|
|
def locked?
|
|
|
|
self.lock_xid.present?
|
|
|
|
end
|
2020-09-16 08:10:15 -04:00
|
|
|
|
|
|
|
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-03-27 08:07:43 -04:00
|
|
|
end
|
|
|
|
end
|