Class: OpenObserve::Templates

Inherits:
Resource
  • Object
show all
Includes:
DirectorySource
Defined in:
lib/openobserve/templates.rb

Overview

Alert templates — how a firing alert is formatted before it reaches a destination.

Served by the v1 alerting API, like Destinations.

Every request goes through Resource#raw. That also retires the generated ListTemplates200ResponseInner — a response schema the generator pressed into service as a write parameter, which accepted only the six attributes the checked-in spec declares and raised ArgumentError on any other.

Constant Summary collapse

DEFAULT_PATTERN =

Glob applied inside a #sync directory.

'*.json'
CAMEL_KEYS =

The model did one thing worth keeping: OpenObserve spells these two flags in camelCase while every other template field is snake_case. Callers keep writing is_default:, and the translation happens here rather than in a schema.

{ is_default: 'isDefault', is_prebuilt: 'isPrebuilt' }.freeze

Constants inherited from Resource

Resource::AUTH

Instance Method Summary collapse

Methods included from DirectorySource

#read_definitions

Methods inherited from Resource

#initialize

Constructor Details

This class inherits a constructor from OpenObserve::Resource

Instance Method Details

#create(**attributes) ⇒ OpenObserve::Template

Parameters:

  • attributes (Hash)

    template definition, as plain keys

Returns:



46
47
48
# File 'lib/openobserve/templates.rb', line 46

def create(**attributes)
  Template.from(call { raw(:POST, templates_path, body: payload(attributes)) })
end

#delete(name) ⇒ void

This method returns an undefined value.

Parameters:

  • name (String)

    template name



59
60
61
62
# File 'lib/openobserve/templates.rb', line 59

def delete(name)
  call { raw(:DELETE, "#{templates_path}/#{encode(name)}") }
  nil
end

#get(name) ⇒ OpenObserve::Template

Parameters:

  • name (String)

    template name

Returns:

Raises:



40
41
42
# File 'lib/openobserve/templates.rb', line 40

def get(name)
  Template.from(one('template') { raw(:GET, "#{templates_path}/#{encode(name)}") })
end

#listArray<OpenObserve::Template>

Returns:



23
24
25
26
27
# File 'lib/openobserve/templates.rb', line 23

def list
  payload = collection { raw(:GET, templates_path) }

  Array(payload).map { |template| Template.from(template) }
end

#prebuiltArray<OpenObserve::Template>

The templates OpenObserve ships with (Slack, PagerDuty, …), usable as a starting point.

Returns:



31
32
33
34
35
# File 'lib/openobserve/templates.rb', line 31

def prebuilt
  payload = collection { raw(:GET, "#{templates_path}/system/prebuilt") }

  Array(payload).map { |template| Template.from(template) }
end

#sync(directory, pattern: DEFAULT_PATTERN) ⇒ Hash{Symbol=>Array<String>}

Bring the templates of a directory into OpenObserve, matching on name.

Creates what is missing and overwrites what is there. Nothing is ever deleted — a template the directory no longer describes stays put, because another organisation's alerts may still point at it.

Safe to replay: a second run over an unchanged directory rewrites the same bodies.

Examples:

client.templates.sync("config/openobserve/templates")
# => { created: ["pagerduty"], updated: [] }

Parameters:

  • directory (String)

    directory holding the documents

  • pattern (String) (defaults to: DEFAULT_PATTERN)

    glob applied inside directory

Returns:

  • (Hash{Symbol=>Array<String>})

    names, under :created and :updated

Raises:



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/openobserve/templates.rb', line 80

def sync(directory, pattern: DEFAULT_PATTERN)
  known  = list.filter_map { |template| template['name'] }.to_set
  result = { created: [], updated: [] }

  read_definitions(directory, pattern, 'name').each do |name, definition|
    exists = known.include?(name)
    result[exists ? :updated : :created] << name
    exists ? update(name, **definition) : create(**definition)
  end

  result
end

#update(name, **attributes) ⇒ OpenObserve::Template

Parameters:

  • name (String)

    template name

  • attributes (Hash)

    fields to change, as plain keys

Returns:



53
54
55
# File 'lib/openobserve/templates.rb', line 53

def update(name, **attributes)
  Template.from(call { raw(:PUT, "#{templates_path}/#{encode(name)}", body: payload(attributes)) })
end