Commit 7cb54ab3 authored by allison.browne's avatar allison.browne

Store enabled flag on grafana integration and allow updates

Add `enabled` column to grafana_integration table. Allow update action
to update the enabled column to `true` or `false`
parent a5abb85a
......@@ -70,7 +70,7 @@ module Projects
project: [:slug, :name, :organization_slug, :organization_name]
],
grafana_integration_attributes: [:token, :grafana_url]
grafana_integration_attributes: [:token, :grafana_url, :enabled]
}
end
end
......
......@@ -362,6 +362,10 @@ module ProjectsHelper
@project.grafana_integration&.token
end
def grafana_integration_enabled
@project.grafana_integration&.enabled
end
private
def get_project_nav_tabs(project, current_user)
......
......@@ -14,6 +14,8 @@ class GrafanaIntegration < ApplicationRecord
validates :token, :project, presence: true
validates :enabled, inclusion: { in: [true, false] }
def client
@client ||= ::Grafana::Client.new(api_url: grafana_url.chomp('/'), token: token)
end
......
---
title: Add flag to enable and disable grafana integration
merge_request: 19234
author:
type: changed
......@@ -19,6 +19,7 @@ en:
project/grafana_integration:
token: "Grafana HTTP API Token"
grafana_url: "Grafana API URL"
grafana_enabled: "Grafana integration enabled"
views:
pagination:
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 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2019_10_26_041447) do
ActiveRecord::Schema.define(version: 2019_10_29_191901) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_trgm"
......@@ -1807,6 +1807,7 @@ ActiveRecord::Schema.define(version: 2019_10_26_041447) do
t.string "encrypted_token", limit: 255, null: false
t.string "encrypted_token_iv", limit: 255, 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"
end
......
......@@ -186,7 +186,8 @@ describe Projects::Settings::OperationsController do
{
grafana_integration_attributes: {
grafana_url: 'https://grafana.gitlab.com',
token: 'eyJrIjoicDRlRTREdjhhOEZ5WjZPWXUzazJOSW0zZHJUejVOd3IiLCJuIjoiVGVzdCBLZXkiLCJpZCI6MX0='
token: 'eyJrIjoicDRlRTREdjhhOEZ5WjZPWXUzazJOSW0zZHJUejVOd3IiLCJuIjoiVGVzdCBLZXkiLCJpZCI6MX0=',
enabled: 'true'
}
}
end
......
......@@ -5,5 +5,6 @@ FactoryBot.define do
project
grafana_url { 'https://grafana.example.com' }
token { SecureRandom.hex(10) }
enabled { 'true' }
end
end
......@@ -938,4 +938,22 @@ describe ProjectsHelper do
it { is_expected.to eq(grafana_integration.token) }
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
......@@ -34,5 +34,18 @@ describe GrafanaIntegration do
internal_url
).for(:grafana_url)
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
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