Skip to content

Commit

Permalink
Merge pull request #14449 from abellotti/api_orchestration_stack_stdout
Browse files Browse the repository at this point in the history
Adding support for a format_attributes parameter
  • Loading branch information
gtanzillo authored Mar 27, 2017
2 parents 168690d + d0701b9 commit e60fd0f
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 1 deletion.
8 changes: 8 additions & 0 deletions app/controllers/api/base_controller/parameters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ def search_option?(what)
search_options.map(&:downcase).include?(what.to_s)
end

def format_attributes
params['format_attributes'].to_s.split(",").map { |af| af.split("=").map(&:strip) }
end

def attribute_format(attr)
format_attributes.detect { |af| af.first == attr }.try(:second)
end

def attribute_selection
if !@req.attributes.empty? || @additional_attributes
@req.attributes | Array(@additional_attributes) | ID_ATTRS
Expand Down
7 changes: 7 additions & 0 deletions app/controllers/api/subcollections/orchestration_stacks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ module OrchestrationStacks
def orchestration_stacks_query_resource(object)
object.orchestration_stacks
end

#
# Virtual attribute accessors
#
def fetch_orchestration_stacks_stdout(resource)
resource.stdout(attribute_format("stdout"))
end
end
end
end
7 changes: 7 additions & 0 deletions app/models/orchestration_stack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class OrchestrationStack < ApplicationRecord
virtual_total :total_security_groups, :security_groups
virtual_total :total_cloud_networks, :cloud_networks

virtual_column :stdout, :type => :string

alias_method :orchestration_stack_parameters, :parameters
alias_method :orchestration_stack_outputs, :outputs
alias_method :orchestration_stack_resources, :resources
Expand Down Expand Up @@ -76,6 +78,11 @@ def cloud_networks
def directs_and_indirects(direct_attrs)
MiqPreloader.preload_and_map(subtree, direct_attrs)
end

def stdout(format = nil)
format.nil? ? try(:raw_stdout) : try(:raw_stdout, format)
end

private :directs_and_indirects

def self.create_stack(orchestration_manager, stack_name, template, options = {})
Expand Down
28 changes: 28 additions & 0 deletions spec/requests/api/services_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,34 @@ def expect_svc_with_vms
expect(response.parsed_body).to include(expected)
end

it 'can query a specific orchestration stack asking for stdout' do
api_basic_authorize subcollection_action_identifier(:services, :orchestration_stacks, :read, :get)

allow_any_instance_of(OrchestrationStack).to receive(:stdout).with(nil).and_return("default text stdout")
run_get("#{services_url(svc.id)}/orchestration_stacks/#{os.id}", :attributes => "stdout")

expected = {
'id' => os.id,
'stdout' => "default text stdout"
}
expect(response).to have_http_status(:ok)
expect(response.parsed_body).to include(expected)
end

it 'can query a specific orchestration stack asking for stdout in alternate format' do
api_basic_authorize subcollection_action_identifier(:services, :orchestration_stacks, :read, :get)

allow_any_instance_of(OrchestrationStack).to receive(:stdout).with("json").and_return("json stdout")
run_get("#{services_url(svc.id)}/orchestration_stacks/#{os.id}", :attributes => "stdout", :format_attributes => "stdout=json")

expected = {
'id' => os.id,
'stdout' => "json stdout"
}
expect(response).to have_http_status(:ok)
expect(response.parsed_body).to include(expected)
end

it 'will not return orchestration stacks without an appropriate role' do
api_basic_authorize

Expand Down
2 changes: 1 addition & 1 deletion tools/rest_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Cli
SCRIPTDIR_ACTIONS = %w(ls run).freeze
SUB_COMMANDS = ACTIONS + %w(edit vi) + SCRIPTDIR_ACTIONS
API_PARAMETERS = %w(expand hide attributes decorators limit offset
depth search_options
depth search_options format_attributes
sort_by sort_order sort_options
filter by_tag provider_class collection_class requester_type).freeze

Expand Down

0 comments on commit e60fd0f

Please sign in to comment.