Skip to content

Commit

Permalink
chore(backend): delete op to config (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
thevilledev committed Jul 6, 2023
1 parent eb3b67b commit e8ccc60
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 7 deletions.
28 changes: 25 additions & 3 deletions internal/plugin/path_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ const (
)

var (
errMissingAPIKey = errors.New("missing API key from configuration")
errTypeAssertionFailed = errors.New("type assertion failed")
errBackendNotConfigured = errors.New("backend not configured")
errMissingAPIKey = errors.New("missing API key from configuration")
errTypeAssertionFailed = errors.New("type assertion failed")
)

type backendConfig struct {
Expand Down Expand Up @@ -57,6 +58,9 @@ func (b *backend) pathConfig() []*framework.Path {
logical.CreateOperation: &framework.PathOperation{
Callback: b.pathConfigWrite,
},
logical.DeleteOperation: &framework.PathOperation{
Callback: b.pathConfigDelete,
},
},
},
}
Expand All @@ -71,7 +75,7 @@ func (b *backend) getConfig(ctx context.Context, storage logical.Storage) (*back
}

if e == nil || len(e.Value) == 0 {
return &backendConfig{}, nil
return nil, nil
}

if err = e.DecodeJSON(&config); err != nil {
Expand Down Expand Up @@ -141,3 +145,21 @@ func (b *backend) pathConfigWrite(ctx context.Context, req *logical.Request,

return &logical.Response{}, nil
}

func (b *backend) pathConfigDelete(ctx context.Context, req *logical.Request,
data *framework.FieldData) (*logical.Response, error) {
cfg, err := b.getConfig(ctx, req.Storage)
if err != nil {
return nil, err
}

if cfg == nil {
return nil, errBackendNotConfigured
}

if err = req.Storage.Delete(ctx, pathPatternConfig); err != nil {
return nil, err
}

return &logical.Response{}, nil
}
44 changes: 44 additions & 0 deletions internal/plugin/path_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,47 @@ func TestBackend_PathConfigWrite(t *testing.T) {
require.Equal(t, cfg.APIKey, "foo")
})
}

func TestBackend_PathConfigDelete(t *testing.T) {
t.Parallel()

t.Run("DeleteWithoutInit", func(t *testing.T) {
t.Parallel()

ctx := context.Background()
b, storage := newTestBackend(t)

res, err := b.HandleRequest(ctx, &logical.Request{
Storage: storage,
Operation: logical.DeleteOperation,
Path: pathPatternConfig,
})
require.Error(t, err)
require.Nil(t, res)
})

t.Run("DeleteSuccess", func(t *testing.T) {
t.Parallel()

ctx := context.Background()
b, storage := newTestBackend(t)

_, err := b.HandleRequest(ctx, &logical.Request{
Storage: storage,
Operation: logical.CreateOperation,
Path: pathPatternConfig,
Data: map[string]any{
"api_key": "foo",
},
})
require.NoError(t, err)

res, err := b.HandleRequest(ctx, &logical.Request{
Storage: storage,
Operation: logical.DeleteOperation,
Path: pathPatternConfig,
})
require.NoError(t, err)
require.NotNil(t, res)
})
}
4 changes: 2 additions & 2 deletions internal/plugin/path_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ func (b *backend) pathTokenWrite(ctx context.Context, req *logical.Request,
return nil, err
}

if cfg.APIKey == "" {
return nil, errMissingAPIKey
if cfg == nil {
return nil, errBackendNotConfigured
}

ttl := int64(0)
Expand Down
4 changes: 2 additions & 2 deletions internal/plugin/path_token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
func TestToken_Create(t *testing.T) {
t.Parallel()

t.Run("CreateTokenWithEmptyBackend", func(t *testing.T) {
t.Run("CreateTokenWithoutBackend", func(t *testing.T) {
t.Parallel()

b, storage := newTestBackend(t)
Expand All @@ -27,7 +27,7 @@ func TestToken_Create(t *testing.T) {
Path: pathPatternToken,
Data: map[string]any{},
})
require.Equal(t, err, errMissingAPIKey)
require.Equal(t, err, errBackendNotConfigured)
require.Nil(t, r)
})

Expand Down

0 comments on commit e8ccc60

Please sign in to comment.