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)
UnauthorizedError =
Class.new(StandardError)

Instance Attribute Summary

Attributes inherited from BaseService

#current_user, #params, #project

Instance Method Summary collapse

Methods included from Gitlab::OptimisticLocking

retry_lock

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


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

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

#handle_with_lockObject

Raises:


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

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
end

#lock!Object

Raises:

  • (ArgumentError)

28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/services/terraform/remote_state_handler.rb', line 28

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:


43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/services/terraform/remote_state_handler.rb', line 43

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