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

fix: add placeholder for validate #16

Merged
merged 5 commits into from
Aug 24, 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
6 changes: 3 additions & 3 deletions adc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ services:
hosts:
- foo.com
- bar.com
upstreams:
- name: upstream1
targets:
upstream:
name: upstream1
nodes:
- host: 10.10.16.12
port: 8080
weight: 50
Expand Down
9 changes: 9 additions & 0 deletions internal/pkg/validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ func (v *Validator) Validate() []error {

for _, service := range v.localConfig.Services {
service := service
if service.ID == "" {
service.ID = service.Name
}
if service.Upstream.ID == "" {
service.Upstream.ID = service.Upstream.Name
}
err := v.cluster.Service().Validate(context.Background(), service)
if err != nil {
allErr = append(allErr, err)
Expand All @@ -51,6 +57,9 @@ func (v *Validator) Validate() []error {

for _, route := range v.localConfig.Routes {
route := route
if route.ID == "" {
route.ID = route.Name
}
err := v.cluster.Route().Validate(context.Background(), route)
if err != nil {
allErr = append(allErr, err)
Expand Down
22 changes: 11 additions & 11 deletions pkg/api/apisix/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,31 +50,31 @@ type Route struct {

// Service is the abstraction of a backend service on API gateway.
type Service struct {
ID string `json:"id"`
ID string `json:"id" yaml:"id"`

Name string `json:"name"`
Description string `json:"desc,omitempty"`
Name string `json:"name" yaml:"name"`
Description string `json:"desc,omitempty" yaml:"desc,omitempty"`
// Labels are used for resource classification and indexing
Labels StringArray `json:"labels,omitempty"`
Labels StringArray `json:"labels,omitempty" yaml:"labels,omitempty"`
// HTTP hosts for this service.
Hosts []string `json:"hosts,omitempty"`
Hosts []string `json:"hosts,omitempty" yaml:"hosts,omitempty"`
// Plugin settings on Service level
Plugins Plugins `json:"plugins,omitempty"`
Plugins Plugins `json:"plugins,omitempty" yaml:"plugins,omitempty"`
// Upstream settings for the Service.
Upstream Upstream `json:"upstream,omitempty"`
Upstream Upstream `json:"upstream,omitempty" yaml:"upstream,omitempty"`
// UpstreamId settings for the Service.
UpstreamId string `json:"upstream_id,omitempty"`
UpstreamId string `json:"upstream_id,omitempty" yaml:"upstream_id,omitempty"`
// Enables a websocket. Set to false by default.
EnableWebsocket bool `json:"enable_websocket,omitempty"`
EnableWebsocket bool `json:"enable_websocket,omitempty" yaml:"enable_websocket,omitempty"`
}

// Upstream is the definition of the upstream on Service.
type Upstream struct {
// ID is the upstream name. It should be unique among all upstreams
// in the same service.
ID string `json:"id"`
ID string `json:"id" yaml:"id"`

Name string `json:"name"`
Name string `json:"name" yaml:"name"`
Type string `json:"type,omitempty" yaml:"type,omitempty"`
HashOn string `json:"hash_on,omitempty" yaml:"hash_on,omitempty"`
Key string `json:"key,omitempty" yaml:"key,omitempty"`
Expand Down
24 changes: 24 additions & 0 deletions test/cli/suites/validate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package suites

import (
"bytes"
"os/exec"

"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
)

var _ = ginkgo.Describe("`adc validate` tests", func() {
ginkgo.Context("Basic functions", func() {
_ = NewScaffold()
ginkgo.It("should validate schema", func() {
var pingOutput bytes.Buffer
cmd := exec.Command("adc", "validate", "-f", "testdata/test.yaml")
cmd.Stdout = &pingOutput
err := cmd.Run()
gomega.Expect(err).To(gomega.BeNil())

gomega.Expect(pingOutput.String()).To(gomega.Equal("Get file content success: config name: test, version: test, routes: 1, services: 1.\nValidate file content success\n"))
})
})
})