Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add validator #8

Merged
merged 7 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cmd/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/fatih/color"
"github.com/spf13/cobra"

"github.com/api7/adc/internal/pkg/validator"
"github.com/api7/adc/pkg/common"
"github.com/api7/adc/pkg/data"
)
Expand Down Expand Up @@ -50,5 +51,10 @@ func newValidateCmd() *cobra.Command {

// validateContent validates the content of the configuration file
func validateContent(c *data.Configuration) error {
v, err := validator.NewValidator(c)
if err != nil {
color.Red("Failed to create validator: %v", err)
tao12345666333 marked this conversation as resolved.
Show resolved Hide resolved
}
v.Validate()
return nil
}
89 changes: 89 additions & 0 deletions internal/pkg/validator/validator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package validator

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"reflect"

"github.com/api7/adc/pkg/data"
)

type Validator struct {
localConfig *data.Configuration

evenChan *data.Event
}

func NewValidator(local *data.Configuration) (*Validator, error) {
return &Validator{
localConfig: local,
}, nil
}

type ErrorsWrapper struct {
Errors []error
}

func (v ErrorsWrapper) Error() string {
var errStr string
for _, e := range v.Errors {
errStr += e.Error()
if !errors.Is(e, v.Errors[len(v.Errors)-1]) {
errStr += "\n"
}
}
return errStr
}

func getResourceNameOrID(resource interface{}) string {
value := reflect.ValueOf(resource)
value = reflect.Indirect(value)
nameOrID := value.FieldByName("Name")
if !nameOrID.IsValid() {
nameOrID = value.FieldByName("ID")
}
return nameOrID.String()
}

func (v *Validator) validateResource(resourceType string, resource interface{}) (bool, error) {
nameOrID := getResourceNameOrID(resource)
errWrap := "validate resource '%s (%s)': %s"
endpoint := fmt.Sprintf("/apisix/admin/schema/validate/%s", resourceType)
httpClient := &http.Client{}
jsonData, err := json.Marshal(resource)
if err != nil {
return false, fmt.Errorf(errWrap, resourceType, nameOrID, err)
}

req, err := http.NewRequest(http.MethodPost, endpoint, bytes.NewBuffer(jsonData))
if err != nil {
return false, fmt.Errorf(errWrap, resourceType, nameOrID, err)
}
resp, err := httpClient.Do(req)
if err != nil {
return false, fmt.Errorf(errWrap, resourceType, nameOrID, err)
}
defer resp.Body.Close()
return resp.StatusCode == http.StatusOK, nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lingsamuel I think sdk can support this API.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should parse the invalid info from the response body and return as an error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, let me check the response content of the APISIX API

}

func (v *Validator) Validate() []error {
allErr := []error{}

for _, service := range v.localConfig.Services {
service := service
valid, err := v.validateResource("service", service)
tao12345666333 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
allErr = append(allErr, err)
}

if !valid {
// we can use event to record it or just log it
}
}

return allErr
}