Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove remaining decorators from the API #235

Merged
merged 1 commit into from
Dec 5, 2017
Merged
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
36 changes: 2 additions & 34 deletions app/controllers/api/subcollections/vms.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,21 @@ def vms_query_resource(object)
vms = object.try(:vms) || []

vm_attrs = attribute_selection_for("vms")
vm_decorators = decorator_selection_for("vms")

return vms if vm_attrs.blank? && vm_decorators.blank?
return vms if vm_attrs.blank?

vms.collect do |vm|
attributes_hash = create_resource_attributes_hash(vm_attrs, vm)
decorators_hash = create_vm_decorators_hash(vm_decorators, vm)

conflictless_hash = attributes_hash.merge(decorators_hash || {}) do |key, _, _|
raise BadRequestError, "Requested both an attribute and a decorator of the same name: #{key}"
end

vm.as_json.merge(conflictless_hash)
vm.as_json.merge(attributes_hash)
end
end

private

def decorator_selection
params['decorators'].to_s.split(",")
end

def decorator_selection_for(collection)
decorator_selection.collect do |attr|
/\A#{collection}\.(?<name>.*)\z/.match(attr) { |m| m[:name] }
end.compact
end

def create_vm_attributes_hash(vm_attrs, vm)
vm_attrs.each_with_object({}) do |attr, hash|
hash[attr] = vm.public_send(attr.to_sym) if vm.respond_to?(attr.to_sym)
end.compact
end

def create_vm_decorators_hash(vm_decorators, vm)
hash = {}
if vm_decorators.include? 'supports_console?'
hash['supports_console?'] = vm.supports_console?
end
if vm_decorators.include? 'supports_cockpit?'
hash['supports_cockpit?'] = vm.supports_launch_cockpit?
end
if vm_decorators.include?('supports_vnc_console?')
hash['supports_vnc_console?'] = vm.console_supported?('VNC')
end
hash
end
end
end
end
8 changes: 0 additions & 8 deletions spec/requests/services_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -487,14 +487,6 @@ def expect_svc_with_vms
{"id" => vm2.id.to_s, "cpu_total_cores" => 4}])
end

it "can query vms as subcollection via decorators with additional decorators" do
get api_service_url(nil, svc1), :params => { :expand => "vms", :attributes => "", :decorators => "vms.supports_console?,vms.supports_cockpit?,vms.supports_vnc_console?" }

expect_svc_with_vms
expect_results_to_match_hash("vms", [{"id" => vm1.id.to_s, "supports_console?" => true, "supports_cockpit?" => anything, "supports_vnc_console?" => anything},
{"id" => vm2.id.to_s, "supports_console?" => true, "supports_cockpit?" => anything, "supports_vnc_console?" => anything}])
end

it "cannot query vms via both virtual attribute and subcollection" do
get api_service_url(nil, svc1), :params => { :expand => "vms", :attributes => "vms" }

Expand Down