grafana/pkg/services/rendering/capabilities.go
Artur Wierzbicki 254c59725e
Rendering: add capabilities check (#44470)
* #44449: add feature check to rendering service

* #44449: formatting

* #44449: rename feature -> capability (https://developer.mozilla.org/en-US/docs/Web/API/MediaStreamTrack/getCapabilities, https://developer.mozilla.org/en-US/docs/Web/API/InputDeviceInfo/getCapabilities)

* #44449: refactor

* #44449: remove commented code

* Update pkg/services/rendering/capabilities.go

Co-authored-by: Agnès Toulet <35176601+AgnesToulet@users.noreply.github.com>

* #44449: review fixes

Co-authored-by: Agnès Toulet <35176601+AgnesToulet@users.noreply.github.com>
2022-01-28 21:24:15 +04:00

56 lines
1.8 KiB
Go

package rendering
import (
"errors"
"github.com/Masterminds/semver"
)
type Capability struct {
name CapabilityName
semverConstraint string
}
type CapabilityName string
const (
ScalingDownImages CapabilityName = "ScalingDownImages"
FullHeightImages CapabilityName = "FullHeightImages"
)
var ErrUnknownCapability = errors.New("unknown capability")
var ErrInvalidPluginVersion = errors.New("invalid plugin version")
func (rs *RenderingService) HasCapability(capability CapabilityName) (CapabilitySupportRequestResult, error) {
if !rs.IsAvailable() {
return CapabilitySupportRequestResult{IsSupported: false, SemverConstraint: ""}, ErrRenderUnavailable
}
var semverConstraint string
for i := range rs.capabilities {
if rs.capabilities[i].name == capability {
semverConstraint = rs.capabilities[i].semverConstraint
break
}
}
if semverConstraint == "" {
return CapabilitySupportRequestResult{}, ErrUnknownCapability
}
compiledSemverConstraint, err := semver.NewConstraint(semverConstraint)
if err != nil {
rs.log.Error("Failed to parse semver constraint", "constraint", semverConstraint, "capability", capability, "error", err.Error())
return CapabilitySupportRequestResult{IsSupported: false, SemverConstraint: semverConstraint}, ErrUnknownCapability
}
imageRendererVersion := rs.Version()
compiledImageRendererVersion, err := semver.NewVersion(imageRendererVersion)
if err != nil {
rs.log.Error("Failed to parse plugin version", "version", imageRendererVersion, "error", err.Error())
return CapabilitySupportRequestResult{IsSupported: false, SemverConstraint: semverConstraint}, ErrInvalidPluginVersion
}
return CapabilitySupportRequestResult{IsSupported: compiledSemverConstraint.Check(compiledImageRendererVersion), SemverConstraint: semverConstraint}, nil
}