mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-13 03:42:35 -06:00
2d425494aa
These changes refactor the discovery implementation a bit to improve test coverage and remove duplication of common logic shared with the bundle plugin. Specifically, the downloading logic has been moved into a separate package that is shared by bundle and discovery. Second, test coverage in the discovery implementation is increased from ~15% to ~85%. These changes also include a few functional improvements: - The default decision paths can be updated dynamically - The decision logger can be enabled dynamically - Discovery downloading errors are reported in status updates - Discovery bundle is evaluated with all runtime params - Custom plugins can be created dynamically - Status updates include both discovery and bundle status Signed-off-by: Torin Sandall <torinsandall@gmail.com>
69 lines
1.8 KiB
Go
69 lines
1.8 KiB
Go
// Copyright 2018 The OPA Authors. All rights reserved.
|
|
// Use of this source code is governed by an Apache2
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package bundle
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/open-policy-agent/opa/ast"
|
|
"github.com/open-policy-agent/opa/server/types"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
const (
|
|
errCode = "bundle_error"
|
|
)
|
|
|
|
// Status represents the status of processing a bundle.
|
|
type Status struct {
|
|
Name string `json:"name"`
|
|
ActiveRevision string `json:"active_revision,omitempty"`
|
|
LastSuccessfulActivation time.Time `json:"last_successful_activation,omitempty"`
|
|
LastSuccessfulDownload time.Time `json:"last_successful_download,omitempty"`
|
|
Code string `json:"code,omitempty"`
|
|
Message string `json:"message,omitempty"`
|
|
Errors []error `json:"errors,omitempty"`
|
|
}
|
|
|
|
// SetActivateSuccess updates the status object to reflect a successful
|
|
// activation.
|
|
func (s *Status) SetActivateSuccess(revision string) {
|
|
s.LastSuccessfulActivation = time.Now().UTC()
|
|
s.ActiveRevision = revision
|
|
}
|
|
|
|
// SetDownloadSuccess updates the status object to reflect a successful
|
|
// download.
|
|
func (s *Status) SetDownloadSuccess() {
|
|
s.LastSuccessfulDownload = time.Now().UTC()
|
|
}
|
|
|
|
// SetError updates the status object to reflect a failure to download or
|
|
// activate. If err is nil, the error status is cleared.
|
|
func (s *Status) SetError(err error) {
|
|
|
|
if err == nil {
|
|
s.Code = ""
|
|
s.Message = ""
|
|
s.Errors = nil
|
|
return
|
|
}
|
|
|
|
cause := errors.Cause(err)
|
|
|
|
if astErr, ok := cause.(ast.Errors); ok {
|
|
s.Code = errCode
|
|
s.Message = types.MsgCompileModuleError
|
|
s.Errors = make([]error, len(astErr))
|
|
for i := range astErr {
|
|
s.Errors[i] = astErr[i]
|
|
}
|
|
} else {
|
|
s.Code = errCode
|
|
s.Message = err.Error()
|
|
s.Errors = nil
|
|
}
|
|
}
|