diff --git a/lib/kennel/importer.rb b/lib/kennel/importer.rb index ff7d63a..8cf55aa 100644 --- a/lib/kennel/importer.rb +++ b/lib/kennel/importer.rb @@ -3,7 +3,7 @@ module Kennel class Importer TITLES = [:name, :title].freeze - SORT_ORDER = [*TITLES, :id, :kennel_id, :type, :tags, :query, *Models::Record.subclasses.map { |k| k::TRACKING_FIELDS }, :template_variables].freeze + SORT_ORDER = [*TITLES, :id, :kennel_id, :type, :tags, :query, :sli_specification, *Models::Record.subclasses.map { |k| k::TRACKING_FIELDS }, :template_variables].freeze def initialize(api) @api = api @@ -19,10 +19,15 @@ def import(resource, id) raise(ArgumentError, "#{resource} is not supported") data = @api.show(model.api_resource, id) + # get sli specification before normalization. + sli_specification = data[:sli_specification] || data.dig(:sli_specification, :time_slice) + id = data.fetch(:id) # keep native value model.normalize({}, data) # removes id data[:id] = id + data[:sli_specification] = sli_specification if sli_specification + title_field = TITLES.detect { |f| data[f] } title = data.fetch(title_field) title.tr!(Kennel::Models::Record::LOCK, "") # avoid double lock icon diff --git a/lib/kennel/models/slo.rb b/lib/kennel/models/slo.rb index 9f64598..cf27aee 100644 --- a/lib/kennel/models/slo.rb +++ b/lib/kennel/models/slo.rb @@ -14,7 +14,7 @@ class Slo < Record thresholds: [] }.freeze - settings :type, :description, :thresholds, :query, :tags, :monitor_ids, :monitor_tags, :name, :groups + settings :type, :description, :thresholds, :query, :tags, :monitor_ids, :monitor_tags, :name, :groups, :sli_specification defaults( tags: -> { @project.tags }, @@ -35,7 +35,9 @@ def build_json type: type ) - if v = query + if type == 'time_slice' + data[:sliSpecification] = :sli_specification + elsif v = query data[:query] = v end diff --git a/test/kennel/models/slo_test.rb b/test/kennel/models/slo_test.rb index f28f958..1292da4 100644 --- a/test/kennel/models/slo_test.rb +++ b/test/kennel/models/slo_test.rb @@ -11,12 +11,15 @@ class TestSlo < Kennel::Models::Slo def slo(options = {}) Kennel::Models::Slo.new( + type = options[:type] || "metric" options.delete(:project) || project, - { - type: -> { "metric" }, + slo_options ={ + type: -> { type }, name: -> { "Foo" }, kennel_id: -> { "m1" } - }.merge(options) + } + slo_options[:sliSpecification] = -> { options[:sliSpecification] } if type == "time_slice" + Kennel::Models::Slo.new(project, slo_options.merge(options)) ) end @@ -59,6 +62,32 @@ def slo(options = {}) ) end + it "includes sliSpecification for time slices" do + sli_spec = { + timeSlice: { + query: { + formula: { + formulaExpression: "query1" + }, + queries: [{ + metricQuery: { + name: "query1", + query: "ewma_7(avg:system_cpu{} by {env})" + } + }] + }, + comparator: "<=", + threshold: 40 + } + } + + expected_basic_json[:sliSpecification] = sli_spec + assert_json_equal( + slo(type: "time_slice", sli_specification: sli_spec).build_json, + expected_basic_json + ) + end + it "sets id when updating by id" do expected_basic_json[:id] = 123 assert_json_equal(