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 all commits
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
7 changes: 7 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/api/apisix/types"
"github.com/api7/adc/pkg/common"
)
Expand Down Expand Up @@ -50,5 +51,11 @@ func newValidateCmd() *cobra.Command {

// validateContent validates the content of the configuration file
func validateContent(c *types.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
return err
}
v.Validate()
return nil
}
106 changes: 106 additions & 0 deletions internal/pkg/validator/validator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package validator

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

"github.com/api7/adc/pkg/api/apisix/types"
"github.com/api7/adc/pkg/data"
)

type Validator struct {
localConfig *types.Configuration

evenChan *data.Event
}

func NewValidator(local *types.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()
}

type RespData struct {
ErrMsg string `json: "error_msg"`
}

func (v *Validator) validateResource(resourceType string, resource interface{}) 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 fmt.Errorf(errWrap, resourceType, nameOrID, err)
}

req, err := http.NewRequest(http.MethodPost, endpoint, bytes.NewBuffer(jsonData))
if err != nil {
return fmt.Errorf(errWrap, resourceType, nameOrID, err)
}
resp, err := httpClient.Do(req)
if err != nil {
return fmt.Errorf(errWrap, resourceType, nameOrID, err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
respData := &RespData{}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf(errWrap, resourceType, nameOrID, err)
}
err = json.Unmarshal(body, respData)
if err != nil {
return fmt.Errorf(errWrap, resourceType, nameOrID, err)
}
if resp != nil {
return fmt.Errorf(errWrap, resourceType, nameOrID, respData.ErrMsg)
}
return fmt.Errorf(errWrap, resourceType, nameOrID, "invalid")
}
return nil
}

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

for _, service := range v.localConfig.Services {
service := service
err := v.validateResource("service", service)
if err != nil {
allErr = append(allErr, err)
}
}

return allErr
}