Skip to content

Commit

Permalink
add validate method in vault service (#392)
Browse files Browse the repository at this point in the history
* add validate method in vault service

* rewrite vault Validate
  • Loading branch information
randmonkey authored Jan 3, 2024
1 parent abc671e commit 896a8a3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@
- [0.2.0](#020)
- [0.1.0](#010)

## Unreleased

- Added `Validate` method in vault service to validate vaults.
[#392](https://github.com/Kong/go-kong/pull/392)

## [v0.48.0]

> Release date: 2023/10/30
Expand Down
24 changes: 24 additions & 0 deletions kong/vault_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package kong
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
)

// AbstractVaultService handles Vaults in Kong.
Expand All @@ -20,6 +22,8 @@ type AbstractVaultService interface {
List(ctx context.Context, opt *ListOpt) ([]*Vault, *ListOpt, error)
// ListAll fetches all Vaults in Kong.
ListAll(ctx context.Context) ([]*Vault, error)
// Validate validates a Vault against its schema.
Validate(ctx context.Context, vault *Vault) (bool, string, error)
}

// VaultService handles Vaults in Kong.
Expand Down Expand Up @@ -152,3 +156,23 @@ func (s *VaultService) ListAll(ctx context.Context) ([]*Vault, error) {
}
return vaults, nil
}

// Validate validates a vault against its schema.
// returns validate result (passed/failed) and the message from the schema validation service if validation fails.
// returns a non-nil error if failed to call the schema validation service.
func (s *VaultService) Validate(ctx context.Context, vault *Vault) (bool, string, error) {
const endpoint = "/schemas/vaults/validate"
req, err := s.client.NewRequest(http.MethodPost, endpoint, nil, vault)
if err != nil {
return false, "", err
}
if _, err := s.client.Do(ctx, req, nil); err != nil {

var apiErr *APIError
if ok := errors.As(err, &apiErr); !ok || apiErr.Code() != http.StatusBadRequest {
return false, "", err
}
return false, apiErr.message, nil
}
return true, "", nil
}

0 comments on commit 896a8a3

Please sign in to comment.