Class: Api::V2::TfStatesController

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

Instance Method Summary collapse

Instance Method Details

#createObject



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 33

def create
  raw_state = request.raw_post
  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!
    head :ok
  rescue JSON::ParserError
    render plain: 'Invalid state format', status: :bad_request
  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



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

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

  return head :not_found unless state

  render plain: state.state, content_type: 'application/json'
end