Class: Api::V2::TfStatesController

Inherits:
BaseController
  • Object
show all
Includes:
Api::Version2
Defined in:
app/controllers/api/v2/tf_states_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/controllers/api/v2/tf_states_controller.rb', line 32

def create
  raw_state = request.body.read
  if raw_state.blank?
    render plain: 'Missing state body', status: :unprocessable_entity
    return
  end
  begin
    JSON.parse(raw_state)

    state = ForemanOpentofu::TfState.find_or_initialize_by(name: params[:name])
    state.state = raw_state
    state.save!
    render plain: '', status: :ok
  rescue JSON::ParserError => e
    Rails.logger.error("Invalid state JSON: #{e.message}")
    render plain: 'Invalid state format', status: :unprocessable_entity
  end
end

#destroyObject



51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/controllers/api/v2/tf_states_controller.rb', line 51

def destroy
  # TODO: at the moment we want to get 200 OK, if the TfState does not exist.
  #       normally, this would fail with 401, because of no valid token.
  #       Needs re-evaluation, if this is a security risk.
  state = ForemanOpentofu::TfState.where(name: params[:name])
  if state.any?
    authorize
    state.first.destroy
  end

  render plain: '', status: :ok
end

#resource_classObject



64
65
66
# File 'app/controllers/api/v2/tf_states_controller.rb', line 64

def resource_class
  @resource_class ||= ForemanOpentofu::TfState
end

#showObject



22
23
24
25
26
27
28
29
30
# File 'app/controllers/api/v2/tf_states_controller.rb', line 22

def show
  state = ForemanOpentofu::TfState.find_by(name: params[:name])

  if state
    render plain: state.state, content_type: 'application/json'
  else
    render plain: '', status: :not_found
  end
end