Class: Terraform::RemoteStateHandler

Inherits:
BaseService show all
Includes:
Gitlab::OptimisticLocking
Defined in:
app/services/terraform/remote_state_handler.rb

Constant Summary collapse

StateLockedError =
Class.new(StandardError)
StateDeletedError =
Class.new(StandardError)
UnauthorizedError =
Class.new(StandardError)

Constants included from Gitlab::OptimisticLocking

Gitlab::OptimisticLocking::MAX_RETRIES

Instance Attribute Summary

Attributes inherited from BaseService

#current_user, #params, #project

Instance Method Summary collapse

Methods included from Gitlab::OptimisticLocking

log_optimistic_lock_retries, retry_lock, retry_lock_histogram, retry_lock_logger

Methods inherited from BaseService

#initialize

Methods included from BaseServiceUtility

#deny_visibility_level, #event_service, #log_error, #log_info, #notification_service, #system_hook_service, #todo_service, #visibility_level

Methods included from Gitlab::Allowable

#can?

Constructor Details

This class inherits a constructor from BaseService

Instance Method Details

#find_with_lockObject



11
12
13
14
15
# File 'app/services/terraform/remote_state_handler.rb', line 11

def find_with_lock
  retrieve_with_lock(find_only: true) do |state|
    yield state if block_given?
  end
end

#handle_with_lockObject

Raises:



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/services/terraform/remote_state_handler.rb', line 17

def handle_with_lock
  raise UnauthorizedError unless can_modify_state?

  retrieve_with_lock do |state|
    raise StateLockedError unless lock_matches?(state)

    yield state if block_given?

    state.save! unless state.destroyed?
  end

  nil
end

#lock!Object

Raises:

  • (ArgumentError)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/services/terraform/remote_state_handler.rb', line 31

def lock!
  raise ArgumentError if params[:lock_id].blank?
  raise UnauthorizedError unless can_modify_state?

  retrieve_with_lock do |state|
    raise StateLockedError if state.locked?

    state.lock_xid = params[:lock_id]
    state.locked_by_user = current_user
    state.locked_at = Time.current

    state.save!
  end
end

#unlock!Object

Raises:



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/services/terraform/remote_state_handler.rb', line 46

def unlock!
  raise UnauthorizedError unless can_modify_state?

  retrieve_with_lock do |state|
    # force-unlock does not pass ID, so we ignore it if it is missing
    raise StateLockedError unless params[:lock_id].nil? || lock_matches?(state)

    state.lock_xid = nil
    state.locked_by_user = nil
    state.locked_at = nil

    state.save!
  end
end