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

add ability to set custom attributes on services via api #85

Merged
merged 5 commits into from
Oct 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
2 changes: 2 additions & 0 deletions app/controllers/api/services_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ class ServicesController < BaseController
include Subcollections::OrchestrationStacks
include Subcollections::MetricRollups
include Subcollections::GenericObjects
include Subcollections::CustomAttributes


def create_resource(_type, _id, data)
validate_service_data(data)
Expand Down
9 changes: 9 additions & 0 deletions config/api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2320,6 +2320,7 @@
- :orchestration_stacks
- :metric_rollups
- :generic_objects
- :custom_attributes
:collection_actions:
:get:
- :name: read
Expand Down Expand Up @@ -2395,6 +2396,14 @@
:get:
- :name: read
:identifier: generic_object_show_list
:custom_attributes_subcollection_actions:
:post:
- :name: add
:identifier: service_edit
- :name: edit
:identifier: service_edit
- :name: delete
:identifier: service_edit
:settings:
:description: Settings
:identifier: ops_settings
Expand Down
123 changes: 123 additions & 0 deletions spec/requests/services_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1096,4 +1096,127 @@ def expect_svc_with_vms
expect(response.parsed_body).to include(expected)
end
end

context "service custom_attributes" do
let(:service_url) { api_service_url(nil, svc) }
let(:ca1) { FactoryGirl.create(:custom_attribute, :name => "name1", :value => "value1") }
let(:ca2) { FactoryGirl.create(:custom_attribute, :name => "name2", :value => "value2") }
let(:ca1_url) { api_service_custom_attribute_url(nil, svc, ca1) }
let(:ca2_url) { api_service_custom_attribute_url(nil, svc, ca2) }

it "getting custom_attributes from a service with no custom_attributes" do
api_basic_authorize

get(api_service_custom_attributes_url(nil, svc))

expect_empty_query_result(:custom_attributes)
end

it "getting custom_attributes from a service" do
api_basic_authorize
svc.custom_attributes = [ca1, ca2]

get api_service_custom_attributes_url(nil, svc)

expect_query_result(:custom_attributes, 2)
expect_result_resources_to_include_hrefs("resources",
[api_service_custom_attribute_url(nil, svc, ca1),
api_service_custom_attribute_url(nil, svc, ca2)])
end

it "getting custom_attributes from a service in expanded form" do
api_basic_authorize
svc.custom_attributes = [ca1, ca2]

get api_service_custom_attributes_url(nil, svc), :params => { :expand => "resources" }

expect_query_result(:custom_attributes, 2)
expect_result_resources_to_include_data("resources", "name" => %w(name1 name2))
end

it "getting custom_attributes from a service using expand" do
api_basic_authorize action_identifier(:services, :read, :resource_actions, :get)
svc.custom_attributes = [ca1, ca2]

get service_url, :params => { :expand => "custom_attributes" }

expect_single_resource_query("id" => svc.id.to_s)
expect_result_resources_to_include_data("custom_attributes", "name" => %w(name1 name2))
end

it "delete a custom_attribute without appropriate role" do
api_basic_authorize
svc.custom_attributes = [ca1]

post(api_service_custom_attributes_url(nil, svc), :params => gen_request(:delete, nil, service_url))

expect(response).to have_http_status(:forbidden)
end

it "delete a custom_attribute from a service via the delete action" do
api_basic_authorize subcollection_action_identifier(:services, :custom_attributes, :delete)
svc.custom_attributes = [ca1]

post(api_service_custom_attributes_url(nil, svc), :params => gen_request(:delete, nil, ca1_url))

expect(response).to have_http_status(:ok)
expect(svc.reload.custom_attributes).to be_empty
end

it "add custom attribute to a service without a name" do
api_basic_authorize subcollection_action_identifier(:services, :custom_attributes, :edit)

post(api_service_custom_attributes_url(nil, svc), :params => gen_request(:add, "value" => "value1"))

expect_bad_request("Must specify a name")
end

it "add custom attributes to a service" do
api_basic_authorize subcollection_action_identifier(:services, :custom_attributes, :edit)

post(api_service_custom_attributes_url(nil, svc),
:params => gen_request(:add, [{"name" => "name1", "value" => "value1"},
{"name" => "name2", "value" => "value2"}]))

expect(response).to have_http_status(:ok)
expect_result_resources_to_include_data("results", "name" => %w(name1 name2))
expect(svc.custom_attributes.size).to eq(2)
expect(svc.custom_attributes.pluck(:value).sort).to eq(%w(value1 value2))
end

it "edit a custom attribute by name" do
api_basic_authorize subcollection_action_identifier(:services, :custom_attributes, :edit)
svc.custom_attributes = [ca1]

post(api_service_custom_attributes_url(nil, svc), :params => gen_request(:edit, "name" => "name1", "value" => "value one"))

expect(response).to have_http_status(:ok)
expect_result_resources_to_include_data("results", "value" => ["value one"])
expect(svc.reload.custom_attributes.first.value).to eq("value one")
end

it "edit a custom attribute by href" do
api_basic_authorize subcollection_action_identifier(:services, :custom_attributes, :edit)
svc.custom_attributes = [ca1]

post(api_service_custom_attributes_url(nil, svc), :params => gen_request(:edit, "href" => ca1_url, "value" => "new value1"))

expect(response).to have_http_status(:ok)
expect_result_resources_to_include_data("results", "value" => ["new value1"])
expect(svc.reload.custom_attributes.first.value).to eq("new value1")
end

it "edit multiple custom attributes" do
api_basic_authorize subcollection_action_identifier(:services, :custom_attributes, :edit)
svc.custom_attributes = [ca1, ca2]

post(api_service_custom_attributes_url(nil, svc),
:params => gen_request(:edit, [{"name" => "name1", "value" => "new value1"},
{"name" => "name2", "value" => "new value2"}]))

expect(response).to have_http_status(:ok)
expect_result_resources_to_include_data("results", "value" => ["new value1", "new value2"])
expect(svc.reload.custom_attributes.pluck(:value).sort).to eq(["new value1", "new value2"])
end
end
end