Skip to content

Commit

Permalink
clean up typos, remove legacy error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
fearful-symmetry committed Sep 28, 2023
1 parent 1003798 commit 6312c8f
Show file tree
Hide file tree
Showing 8 changed files with 10 additions and 64 deletions.
2 changes: 1 addition & 1 deletion libbeat/idxmgmt/idxmgmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func MakeDefaultSupport(ilmSupport lifecycle.SupportFactory) SupportFactory {
}

// consider lifecycles enabled if the user has explicitly enabled them,
// or if no `enabled`` setting has been set by the user, thus reverting to a default of enabled.
// or if no `enabled` setting has been set by the user, thus reverting to a default of enabled.
enabled := false
if cfg.Lifecycle.DSL.Enabled() || cfg.Lifecycle.ILM.Enabled() {
enabled = true
Expand Down
6 changes: 3 additions & 3 deletions libbeat/idxmgmt/lifecycle/client_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func checkILMEnabled(enabled bool, c VersionCheckerClient) (bool, error) {

ver := c.GetVersion()
if ver.LessThan(esMinDefaultILMVersion) {
return false, errf(ErrESVersionNotSupported, "Elasticsearch %v does not support ILM", ver.String())
return false, fmt.Errorf("%w: Elasticsearch %v does not support ILM", ErrESVersionNotSupported, ver.String())
}
return true, nil
}
Expand All @@ -100,12 +100,12 @@ func createPolicy(cfg Config, info beat.Info, defaultPolicy mapstr.M) (Policy, e
if path := cfg.PolicyFile; path != "" {
contents, err := os.ReadFile(path)
if err != nil {
return Policy{}, fmt.Errorf("failed to read policy file '%v': %w", path, err)
return Policy{}, fmt.Errorf("failed to read policy file '%s': %w", path, err)
}

var body map[string]interface{}
if err := json.Unmarshal(contents, &body); err != nil {
return Policy{}, fmt.Errorf("failed to decode policy file '%v': %w", path, err)
return Policy{}, fmt.Errorf("failed to decode policy file '%s': %w", path, err)
}

policy.Body = body
Expand Down
2 changes: 1 addition & 1 deletion libbeat/idxmgmt/lifecycle/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type LifecycleConfig struct {
}

// RawConfig half-unpacks the policy config, allowing us to tell if a user has explicitly
// enabled a given config value
// enabled a given config value
type RawConfig struct {
ILM *config.C `config:"setup.ilm"`
DSL *config.C `config:"setup.dsl"`
Expand Down
44 changes: 0 additions & 44 deletions libbeat/idxmgmt/lifecycle/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,8 @@ package lifecycle

import (
"errors"
"fmt"
)

// Error indicates an error + reason describing the last error.
// The Reason() method returns a sentinal error value for comparison.
type Error struct {
reason error
cause error
message string
}

var (
ErrESVersionNotSupported = errors.New("ILM is not supported by the Elasticsearch version in use")
ErrILMCheckRequestFailed = errors.New("request checking for ILM availability failed")
Expand All @@ -38,38 +29,3 @@ var (
ErrRequestFailed = errors.New("request failed")
ErrOpNotAvailable = errors.New("operation not available, no lifecycle manager is enabled")
)

func errOf(reason error) error {
return &Error{reason: reason}
}

func errf(reason error, msg string, vs ...interface{}) error {
return wrapErrf(nil, reason, msg, vs...)
}

func wrapErrf(cause, reason error, msg string, vs ...interface{}) error {
return &Error{
cause: cause,
reason: reason,
message: fmt.Sprintf(msg, vs...),
}
}

// Cause returns the errors cause, if present.
func (e *Error) Cause() error { return e.cause }

// Reason returns a sentinal error value define within the ilm package.
func (e *Error) Reason() error { return e.reason }

// Error returns the formatted error string.
func (e *Error) Error() string {
msg := e.message
if e.message == "" {
msg = e.reason.Error()
}

if e.cause != nil {
return fmt.Sprintf("%v: %+v", msg, e.cause)
}
return msg
}
6 changes: 3 additions & 3 deletions libbeat/idxmgmt/lifecycle/es_client_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func NewESClientHandler(c ESClient, info beat.Info, cfg RawConfig) (*ESClientHan
if lifecycleCfg.Enabled { // these are-enabled checks should happen elsewhere, but check again here just in case
policy, err = createPolicy(lifecycleCfg, info, defaultPolicy)
if err != nil {
return nil, fmt.Errorf("error creating DSL policy: %w", err)
return nil, fmt.Errorf("error creating a lifecycle policy: %w", err)
}
}

Expand Down Expand Up @@ -126,8 +126,8 @@ func (h *ESClientHandler) IsElasticsearch() bool {
func (h *ESClientHandler) HasPolicy() (bool, error) {
status, b, err := h.client.Request("GET", h.putPath, "", nil, nil)
if err != nil && status != 404 {
return false, wrapErrf(err, ErrRequestFailed,
"failed to check for policy name '%v': (status=%v) %s", h.name, status, b)
return false, fmt.Errorf("%w: failed to check for policy name '%v': (status=%v) (err=%w) %s",
ErrRequestFailed, h.name, status, err, b)
}
return status == 200, nil
}
Expand Down
11 changes: 1 addition & 10 deletions libbeat/idxmgmt/lifecycle/ilm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,7 @@ func TestDefaultSupport_Init(t *testing.T) {

s := tmp.(*stdSupport)
assert := assert.New(t)
assert.Equal(true, s.Enabled())
})

t.Run("with custom alias config with fieldref", func(t *testing.T) {
tmp, err := DefaultSupport(nil, info, true)
require.NoError(t, err)

s := tmp.(*stdSupport)
assert := assert.New(t)
assert.Equal(true, s.Enabled())
assert.True(s.Enabled())
})

}
Expand Down
2 changes: 1 addition & 1 deletion libbeat/idxmgmt/lifecycle/noop_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (*noopSupport) Manager(_ ClientHandler) Manager { return (*noopManager)(nil
func (*noopManager) CheckEnabled() (bool, error) { return false, nil }

// EnsurePolicy no-op
func (*noopManager) EnsurePolicy(_ bool) (bool, error) { return false, errOf(ErrOpNotAvailable) }
func (*noopManager) EnsurePolicy(_ bool) (bool, error) { return false, ErrOpNotAvailable }

// Policyname no-op
func (*noopManager) PolicyName() string { return "" }
1 change: 0 additions & 1 deletion libbeat/idxmgmt/std_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,6 @@ func TestIndexManager_VerifySetup(t *testing.T) {
require.NoError(t, err)
manager := support.Manager(clientHandler, nil)
ok, warn := manager.VerifySetup(setup.loadTmpl, setup.loadILM)
t.Logf("%s", warn)
assert.Equal(t, setup.ok, ok)
assert.Contains(t, warn, setup.warn)
clientHandler.assertInvariants(t)
Expand Down

0 comments on commit 6312c8f

Please sign in to comment.