Skip to content

Commit

Permalink
Merge pull request #440 from miha-plesko/expose-create-subnet
Browse files Browse the repository at this point in the history
Expose CloudSubnet creation
  • Loading branch information
abellotti authored Aug 6, 2018
2 parents 0c8ed89 + b0a5110 commit 1d56cf5
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
13 changes: 13 additions & 0 deletions app/controllers/api/subcollections/cloud_subnets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@ module CloudSubnets
def cloud_subnets_query_resource(object)
object.respond_to?(:cloud_subnets) ? Array(object.cloud_subnets) : []
end

def cloud_subnets_create_resource(parent, _type, _id, data = {})
data.deep_symbolize_keys!
raise 'Must specify a name for the subnet' unless data[:name]

begin
message = "Creating subnet #{data[:name]}"
task_id = queue_object_action(parent, message, :method_name => "create_cloud_subnet", :args => [data])
action_result(true, message, :task_id => task_id)
rescue StandardError => e
action_result(false, e.to_s)
end
end
end
end
end
3 changes: 3 additions & 0 deletions config/api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,9 @@
:get:
- :name: read
:identifier: cloud_subnet_show_list
:post:
- :name: create
:identifier: cloud_subnet_new
:subresource_actions:
:get:
- :name: read
Expand Down
31 changes: 31 additions & 0 deletions spec/requests/cloud_subnets_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
RSpec.describe 'CloudSubnets API' do
let(:ems) { FactoryGirl.create(:ems_network) }

describe 'GET /api/cloud_subnets' do
it 'lists all cloud subnets with an appropriate role' do
cloud_subnet = FactoryGirl.create(:cloud_subnet)
Expand Down Expand Up @@ -46,4 +48,33 @@
expect(response).to have_http_status(:forbidden)
end
end

describe "POST /api/providers/:c_id/cloud_subnets" do
it "can queue the creation of a subnet" do
api_basic_authorize(action_identifier(:cloud_subnets, :create, :subcollection_actions))

post(api_provider_cloud_subnets_url(nil, ems), :params => { :name => "test-subnet" })

expected = {
'results' => [
a_hash_including(
"success" => true,
"message" => "Creating subnet test-subnet",
"task_id" => anything,
"task_href" => a_string_matching(api_tasks_url)
)
]
}
expect(response.parsed_body).to include(expected)
expect(response).to have_http_status(:ok)
end

it "will not create a subnet unless authorized" do
api_basic_authorize

post(api_provider_cloud_subnets_url(nil, ems), :params => { :name => "test-flavor" })

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

0 comments on commit 1d56cf5

Please sign in to comment.