Skip to content

Commit

Permalink
fixes #37474 - feat: enable updating ansible override values via api
Browse files Browse the repository at this point in the history
Signed-off-by: gardar <[email protected]>
  • Loading branch information
gardar committed May 15, 2024
1 parent 8c6be4f commit e3f4c01
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
49 changes: 49 additions & 0 deletions app/controllers/api/v2/ansible_override_values_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,24 @@ def create
render 'api/v2/ansible_override_values/show'
end

api :PUT, "/ansible_override_values", N_("Update an override value")
param :ansible_variable_id, :identifier, :required => true
param_group :ansible_override_value, :as => :update

def update
@ansible_variable = find_ansible_variable
return unless validate_ansible_variable

@override_value = find_override_value
return unless validate_override_value

if @override_value.update(lookup_value_params['override_value'])
render 'api/v2/ansible_override_values/show'
else
render_error('custom_error', :status => :unprocessable_entity, :locals => { :message => @override_value.errors.full_messages.join(', ') })
end
end

api :DELETE, "/ansible_override_values/:id", N_("Destroy an override value")
param :id, :identifier, :required => true

Expand All @@ -45,9 +63,40 @@ def destroy
end
end

private

def find_ansible_variable
AnsibleVariable.authorized(:edit_ansible_variables).find_by(:id => params[:ansible_variable_id].to_i)
end

def find_override_value
@ansible_variable.lookup_values.find_by(:match => lookup_value_params['override_value']['match'])
end

def validate_ansible_variable
unless @ansible_variable
not_found("Ansible variable not found")
return false
end
true
end

def validate_override_value
unless @override_value
not_found("Override value not found")
return false
end
true
end

def lookup_value_params
params.permit(:ansible_variable_id, override_value: [:match, :value])
end

def resource_name
'ansible_variable'
end
end
end
end

6 changes: 5 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,11 @@
end
end

resources :ansible_override_values, :only => [:create, :destroy]
resources :ansible_override_values, :only => [:create, :destroy] do
collection do
put :update
end
end

resources :ansible_inventories, :only => [] do
collection do
Expand Down

0 comments on commit e3f4c01

Please sign in to comment.