Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions app/controllers/api/configuration_script_payloads_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,29 @@ def api_resource_action_options
%w[include_encrypted_attributes]
end

def create_resource(type, _id, data)
# Get source_id and manager_id from configuration_script_source if provided
source_id = parse_id(data.delete('configuration_script_source'), :configuration_script_sources)
manager_id = parse_id(data.delete('manager_resource'), :providers) || data.delete('ems_id')

raise BadRequestError, _("Must specify name") unless data['name']
raise BadRequestError, _("Must specify payload") unless data['payload']
raise BadRequestError, _("Must specify manager_resource or configuration_script_source") unless manager_id || source_id

# Get manager_id from source if not already provided
if source_id && !manager_id
source = resource_search(source_id, :configuration_script_sources)
manager_id = source.manager_id
end

payload_attrs = data.slice('name', 'description', 'payload', 'payload_type').deep_symbolize_keys
payload_attrs[:manager_id] = manager_id
payload_attrs[:configuration_script_source_id] = source_id if source_id
payload_attrs[:payload_type] ||= 'json'

ConfigurationScriptPayload.create!(payload_attrs)
end

def edit_resource(type, id, data)
resource = resource_search(id, type)

Expand Down
21 changes: 19 additions & 2 deletions app/controllers/api/configuration_script_sources_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,25 @@ def create_resource(type, _id, data)
# Since we are passing a custom hash to create_ems_resource (instead of data variable)
# we need to manually remove it from data.
data.delete('id')
create_ems_resource(type, {'ems_id' => manager_id, 'name' => data['name']}, :supports => true) do |_manager, klass|
{:task_id => klass.create_in_provider_queue(manager_id, data.deep_symbolize_keys)}

# Handle manual scm_type - create directly without provider queue
if data['scm_type'] == 'manual'
raise BadRequestError, _("Must specify name") unless data['name']
raise BadRequestError, _("Must specify manager_resource or ems_id") unless manager_id

# Create the script source directly
script_source = ConfigurationScriptSource.create!(
:name => data['name'],
:description => data['description'],
:manager_id => manager_id,
:scm_type => 'manual'
)
script_source
else
# Use provider queue for git and other scm_types
create_ems_resource(type, {'ems_id' => manager_id, 'name' => data['name']}, :supports => true) do |_manager, klass|
{:task_id => klass.create_in_provider_queue(manager_id, data.deep_symbolize_keys)}
end
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ module ConfigurationScriptPayloads
def configuration_script_payloads_query_resource(object)
object.configuration_script_payloads
end

def create_resource_configuration_script_payloads(parent, _type, _id, data)
# When creating as a subcollection, automatically associate with parent script source
data['configuration_script_source'] = {"id" => parent.id.to_s}
data['manager_resource'] = {"id" => parent.manager_id.to_s} if parent.manager_id
create_resource(:configuration_script_payloads, nil, data)
end
end
end
end
5 changes: 5 additions & 0 deletions config/api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1000,6 +1000,8 @@
- :name: read
:identifier: embedded_configuration_script_payload_view
:post:
- :name: create
:identifier: embedded_configuration_script_payload_add
- :name: edit
:identifier: embedded_configuration_script_payload_edit
:resource_actions:
Expand All @@ -1013,6 +1015,9 @@
:get:
- :name: read
:identifier: embedded_configuration_script_payload_view
:post:
- :name: create
:identifier: embedded_configuration_script_payload_add
:authentications_subcollection_actions:
:get:
- :name: read
Expand Down