Commit d7e79f4c authored by Andreas Brandl's avatar Andreas Brandl

Merge branch '33672-add-enable-checkbox-for-grafana-authentication-settings' into 'master'

Backend: Add checkbox to enable/disable grafana authentication

See merge request gitlab-org/gitlab!19234
parents aeda1813 9fbb5794
...@@ -70,7 +70,7 @@ module Projects ...@@ -70,7 +70,7 @@ module Projects
project: [:slug, :name, :organization_slug, :organization_name] project: [:slug, :name, :organization_slug, :organization_name]
], ],
grafana_integration_attributes: [:token, :grafana_url] grafana_integration_attributes: [:token, :grafana_url, :enabled]
} }
end end
end end
......
...@@ -362,6 +362,10 @@ module ProjectsHelper ...@@ -362,6 +362,10 @@ module ProjectsHelper
@project.grafana_integration&.token @project.grafana_integration&.token
end end
def grafana_integration_enabled?
@project.grafana_integration&.enabled?
end
private private
def get_project_nav_tabs(project, current_user) def get_project_nav_tabs(project, current_user)
......
...@@ -14,7 +14,11 @@ class GrafanaIntegration < ApplicationRecord ...@@ -14,7 +14,11 @@ class GrafanaIntegration < ApplicationRecord
validates :token, :project, presence: true validates :token, :project, presence: true
validates :enabled, inclusion: { in: [true, false] }
def client def client
return unless enabled?
@client ||= ::Grafana::Client.new(api_url: grafana_url.chomp('/'), token: token) @client ||= ::Grafana::Client.new(api_url: grafana_url.chomp('/'), token: token)
end end
end end
---
title: Migrate enabled flag on grafana_integrations table
merge_request: 19234
author:
type: changed
...@@ -19,6 +19,7 @@ en: ...@@ -19,6 +19,7 @@ en:
project/grafana_integration: project/grafana_integration:
token: "Grafana HTTP API Token" token: "Grafana HTTP API Token"
grafana_url: "Grafana API URL" grafana_url: "Grafana API URL"
grafana_enabled: "Grafana integration enabled"
views: views:
pagination: pagination:
previous: "Prev" previous: "Prev"
......
# frozen_string_literal: true
class AddEnabledToGrafanaIntegrations < ActiveRecord::Migration[5.2]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
disable_ddl_transaction!
def up
add_column_with_default(
:grafana_integrations,
:enabled,
:boolean,
allow_null: false,
default: false
)
end
def down
remove_column(:grafana_integrations, :enabled)
end
end
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2019_10_26_124116) do ActiveRecord::Schema.define(version: 2019_10_29_191901) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "pg_trgm" enable_extension "pg_trgm"
...@@ -1809,6 +1809,7 @@ ActiveRecord::Schema.define(version: 2019_10_26_124116) do ...@@ -1809,6 +1809,7 @@ ActiveRecord::Schema.define(version: 2019_10_26_124116) do
t.string "encrypted_token", limit: 255, null: false t.string "encrypted_token", limit: 255, null: false
t.string "encrypted_token_iv", limit: 255, null: false t.string "encrypted_token_iv", limit: 255, null: false
t.string "grafana_url", limit: 1024, null: false t.string "grafana_url", limit: 1024, null: false
t.boolean "enabled", default: false, null: false
t.index ["project_id"], name: "index_grafana_integrations_on_project_id" t.index ["project_id"], name: "index_grafana_integrations_on_project_id"
end end
......
...@@ -186,7 +186,8 @@ describe Projects::Settings::OperationsController do ...@@ -186,7 +186,8 @@ describe Projects::Settings::OperationsController do
{ {
grafana_integration_attributes: { grafana_integration_attributes: {
grafana_url: 'https://grafana.gitlab.com', grafana_url: 'https://grafana.gitlab.com',
token: 'eyJrIjoicDRlRTREdjhhOEZ5WjZPWXUzazJOSW0zZHJUejVOd3IiLCJuIjoiVGVzdCBLZXkiLCJpZCI6MX0=' token: 'eyJrIjoicDRlRTREdjhhOEZ5WjZPWXUzazJOSW0zZHJUejVOd3IiLCJuIjoiVGVzdCBLZXkiLCJpZCI6MX0=',
enabled: 'true'
} }
} }
end end
......
...@@ -5,5 +5,6 @@ FactoryBot.define do ...@@ -5,5 +5,6 @@ FactoryBot.define do
project project
grafana_url { 'https://grafana.example.com' } grafana_url { 'https://grafana.example.com' }
token { SecureRandom.hex(10) } token { SecureRandom.hex(10) }
enabled { true }
end end
end end
...@@ -938,4 +938,22 @@ describe ProjectsHelper do ...@@ -938,4 +938,22 @@ describe ProjectsHelper do
it { is_expected.to eq(grafana_integration.token) } it { is_expected.to eq(grafana_integration.token) }
end end
end end
describe '#grafana_integration_enabled?' do
let(:project) { create(:project) }
before do
helper.instance_variable_set(:@project, project)
end
subject { helper.grafana_integration_enabled? }
it { is_expected.to eq(nil) }
context 'grafana integration exists' do
let!(:grafana_integration) { create(:grafana_integration, project: project) }
it { is_expected.to eq(grafana_integration.enabled) }
end
end
end end
...@@ -34,5 +34,36 @@ describe GrafanaIntegration do ...@@ -34,5 +34,36 @@ describe GrafanaIntegration do
internal_url internal_url
).for(:grafana_url) ).for(:grafana_url)
end end
it 'disallows non-booleans in enabled column' do
is_expected.not_to allow_value(
nil
).for(:enabled)
end
it 'allows booleans in enabled column' do
is_expected.to allow_value(
true,
false
).for(:enabled)
end
end
describe '.client' do
subject(:grafana_integration) { create(:grafana_integration) }
context 'with grafana integration disabled' do
it 'returns a grafana client' do
expect(grafana_integration.client).to be_an_instance_of(::Grafana::Client)
end
end
context 'with grafana integration enabled' do
it 'returns nil' do
grafana_integration.update(enabled: false)
expect(grafana_integration.client).to be(nil)
end
end
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