Commit befda73e authored by Kirstie Cook's avatar Kirstie Cook Committed by Bob Van Landuyt

Add cluster annotations endpoint

Add cluster annotations endpoint specs
parent 9363545a
---
title: Create cluster annotations API endpoint
merge_request: 29502
author:
type: added
...@@ -18,15 +18,23 @@ module API ...@@ -18,15 +18,23 @@ module API
requires :description, type: String, desc: 'The description of the annotation' requires :description, type: String, desc: 'The description of the annotation'
end end
resource :environments do ANNOTATIONS_SOURCES = [
{ class: ::Environment, resource: :environments, create_service_param_key: :environment },
{ class: Clusters::Cluster, resource: :clusters, create_service_param_key: :cluster }
].freeze
ANNOTATIONS_SOURCES.each do |annotations_source|
resource annotations_source[:resource] do
post ':id/metrics_dashboard/annotations' do post ':id/metrics_dashboard/annotations' do
environment = ::Environment.find(params[:id]) annotations_source_object = annotations_source[:class].find(params[:id])
not_found! unless Feature.enabled?(:metrics_dashboard_annotations, annotations_source_object.project)
not_found! unless Feature.enabled?(:metrics_dashboard_annotations, environment.project) forbidden! unless can?(current_user, :create_metrics_dashboard_annotation, annotations_source_object)
forbidden! unless can?(current_user, :create_metrics_dashboard_annotation, environment) create_service_params = declared(params).merge(annotations_source[:create_service_param_key] => annotations_source_object)
result = ::Metrics::Dashboard::Annotations::CreateService.new(current_user, declared(params).merge(environment: environment)).execute result = ::Metrics::Dashboard::Annotations::CreateService.new(current_user, create_service_params).execute
if result[:status] == :success if result[:status] == :success
present result[:annotation], with: Entities::Metrics::Dashboard::Annotation present result[:annotation], with: Entities::Metrics::Dashboard::Annotation
...@@ -38,4 +46,5 @@ module API ...@@ -38,4 +46,5 @@ module API
end end
end end
end end
end
end end
...@@ -11,23 +11,27 @@ describe API::Metrics::Dashboard::Annotations do ...@@ -11,23 +11,27 @@ describe API::Metrics::Dashboard::Annotations do
let(:ending_at) { 1.hour.from_now.iso8601 } let(:ending_at) { 1.hour.from_now.iso8601 }
let(:params) { attributes_for(:metrics_dashboard_annotation, environment: environment, starting_at: starting_at, ending_at: ending_at, dashboard_path: dashboard)} let(:params) { attributes_for(:metrics_dashboard_annotation, environment: environment, starting_at: starting_at, ending_at: ending_at, dashboard_path: dashboard)}
describe 'POST /environments/:environment_id/metrics_dashboard/annotations' do shared_examples 'POST /:source_type/:id/metrics_dashboard/annotations' do |source_type|
before :all do let(:url) { "/#{source_type.pluralize}/#{source.id}/metrics_dashboard/annotations" }
before do
project.add_developer(user) project.add_developer(user)
end end
context "with :source_type == #{source_type.pluralize}" do
context 'feature flag metrics_dashboard_annotations' do context 'feature flag metrics_dashboard_annotations' do
context 'is on' do context 'is on' do
before do before do
stub_feature_flags(metrics_dashboard_annotations: { enabled: true, thing: project }) stub_feature_flags(metrics_dashboard_annotations: { enabled: true, thing: project })
end end
context 'with correct permissions' do context 'with correct permissions' do
context 'with valid parameters' do context 'with valid parameters' do
it 'creates a new annotation', :aggregate_failures do it 'creates a new annotation', :aggregate_failures do
post api("/environments/#{environment.id}/metrics_dashboard/annotations", user), params: params post api(url, user), params: params
expect(response).to have_gitlab_http_status(:created) expect(response).to have_gitlab_http_status(:created)
expect(json_response['environment_id']).to eq(environment.id) expect(json_response["#{source_type}_id"]).to eq(source.id)
expect(json_response['starting_at'].to_time).to eq(starting_at.to_time) expect(json_response['starting_at'].to_time).to eq(starting_at.to_time)
expect(json_response['ending_at'].to_time).to eq(ending_at.to_time) expect(json_response['ending_at'].to_time).to eq(ending_at.to_time)
expect(json_response['description']).to eq(params[:description]) expect(json_response['description']).to eq(params[:description])
...@@ -37,8 +41,7 @@ describe API::Metrics::Dashboard::Annotations do ...@@ -37,8 +41,7 @@ describe API::Metrics::Dashboard::Annotations do
context 'with invalid parameters' do context 'with invalid parameters' do
it 'returns error messsage' do it 'returns error messsage' do
post api("/environments/#{environment.id}/metrics_dashboard/annotations", user), post api(url, user), params: { dashboard_path: nil, starting_at: nil, description: nil }
params: { dashboard_path: nil, starting_at: nil, description: nil }
expect(response).to have_gitlab_http_status(:bad_request) expect(response).to have_gitlab_http_status(:bad_request)
expect(json_response['message']).to include({ "starting_at" => ["can't be blank"], "description" => ["can't be blank"], "dashboard_path" => ["can't be blank"] }) expect(json_response['message']).to include({ "starting_at" => ["can't be blank"], "description" => ["can't be blank"], "dashboard_path" => ["can't be blank"] })
...@@ -49,10 +52,11 @@ describe API::Metrics::Dashboard::Annotations do ...@@ -49,10 +52,11 @@ describe API::Metrics::Dashboard::Annotations do
before do before do
params[:undeclared_param] = 'xyz' params[:undeclared_param] = 'xyz'
end end
it 'filters out undeclared params' do it 'filters out undeclared params' do
expect(::Metrics::Dashboard::Annotations::CreateService).to receive(:new).with(user, hash_excluding(:undeclared_param)) expect(::Metrics::Dashboard::Annotations::CreateService).to receive(:new).with(user, hash_excluding(:undeclared_param))
post api("/environments/#{environment.id}/metrics_dashboard/annotations", user), params: params post api(url, user), params: params
end end
end end
end end
...@@ -65,23 +69,52 @@ describe API::Metrics::Dashboard::Annotations do ...@@ -65,23 +69,52 @@ describe API::Metrics::Dashboard::Annotations do
end end
it 'returns error messsage' do it 'returns error messsage' do
post api("/environments/#{environment.id}/metrics_dashboard/annotations", guest), params: params post api(url, guest), params: params
expect(response).to have_gitlab_http_status(:forbidden) expect(response).to have_gitlab_http_status(:forbidden)
end end
end end
end end
context 'is off' do context 'is off' do
before do before do
stub_feature_flags(metrics_dashboard_annotations: { enabled: false, thing: project }) stub_feature_flags(metrics_dashboard_annotations: { enabled: false })
end end
it 'returns error messsage' do it 'returns error messsage' do
post api("/environments/#{environment.id}/metrics_dashboard/annotations", user), params: params post api(url, user), params: params
expect(response).to have_gitlab_http_status(:not_found) expect(response).to have_gitlab_http_status(:not_found)
end end
end end
end end
end end
end
describe 'environment' do
it_behaves_like 'POST /:source_type/:id/metrics_dashboard/annotations', 'environment' do
let(:source) { environment }
end
end
describe 'group cluster' do
it_behaves_like 'POST /:source_type/:id/metrics_dashboard/annotations', 'cluster' do
let_it_be(:group) { create(:group) }
let_it_be(:cluster) { create(:cluster_for_group, groups: [group]) }
before do
group.add_developer(user)
end
let(:source) { cluster }
end
end
describe 'project cluster' do
it_behaves_like 'POST /:source_type/:id/metrics_dashboard/annotations', 'cluster' do
let_it_be(:cluster) { create(:cluster, projects: [project]) }
let(:source) { cluster }
end
end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment