Skip to content

Commit

Permalink
feat: use admin-api to ping (#78)
Browse files Browse the repository at this point in the history
* feat: use admin-api to ping

Signed-off-by: Ling Samuel (WSL) <lingsamuelgrace@gmail.com>

* fix

Signed-off-by: Ling Samuel (WSL) <lingsamuelgrace@gmail.com>

* fix

Signed-off-by: Ling Samuel (WSL) <lingsamuelgrace@gmail.com>

* fix

Signed-off-by: Ling Samuel (WSL) <lingsamuelgrace@gmail.com>

---------

Signed-off-by: Ling Samuel (WSL) <lingsamuelgrace@gmail.com>
  • Loading branch information
lingsamuel authored Oct 19, 2023
1 parent 1b3e0c3 commit 7a41848
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 18 deletions.
20 changes: 16 additions & 4 deletions cmd/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,20 +147,32 @@ func saveConfiguration(cmd *cobra.Command) error {
reader := bufio.NewReader(os.Stdin)
if rootConfig.Server == "" {
fmt.Println("Please enter the APISIX server address: ")
server, _ := reader.ReadString('\n')
server, err := reader.ReadString('\n')
if err != nil {
return err
}
rootConfig.Server = strings.TrimSpace(server)
}

if !strings.HasPrefix(rootConfig.Server, "http://") && !strings.HasPrefix(rootConfig.Server, "https://") {
color.Yellow("APISIX address " + rootConfig.Server + " is configured without protocol, using HTTP")
rootConfig.Server = "http://" + rootConfig.Server
}
rootConfig.Server = strings.TrimSuffix(rootConfig.Server, "/")

_, err = url.Parse(rootConfig.Server)
if err != nil {
color.Red("Parse APISIX server address failed: %v", err)
return err
}

if rootConfig.Token == "" {
if rootConfig.Token == "" || overwrite {
fmt.Println("Please enter the APISIX token: ")
token, _ := reader.ReadString('\n')
rootConfig.Token = strings.TrimSpace(token)
token, err := reader.ReadString('\n')
if err != nil {
return err
}
rootConfig.Token = strings.TrimSpace(string(token))
}

// use viper to save the configuration
Expand Down
12 changes: 3 additions & 9 deletions cmd/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/spf13/cobra"

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

// newPingCmd represents the ping command
Expand All @@ -36,16 +35,11 @@ func pingAPISIX() error {
return err
}

err = cluster.Route().Validate(context.Background(), &types.Route{
ID: "test",
Name: "test",
Uri: "*",
UpstreamID: "abcd",
})
err = cluster.Ping()
if err != nil {
color.Red("Failed to ping APISIX: %v", err.Error())
color.Red("Failed to ping backend, response: %s", err.Error())
} else {
color.Green("Connected to APISIX successfully!")
color.Green("Connected to backend successfully!")
}
return nil
}
6 changes: 3 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func initConfig() {
if cfgFile == "" {
home, err := homedir.Dir()
if err != nil {
color.Red("Failed to get home dir: %s", err)
color.Red("Failed to get home dir: %s", err.Error())
os.Exit(1)
}
viper.AddConfigPath(home)
Expand All @@ -79,7 +79,7 @@ func initConfig() {
color.Yellow("Config file not found at %s. Creating...", cfgFile)
_, err := os.Create(os.ExpandEnv(cfgFile))
if err != nil {
color.Red("Failed to initialize configuration file: %s", err)
color.Red("Failed to initialize configuration file: %s", err.Error())
}
}

Expand All @@ -97,7 +97,7 @@ func initConfig() {
rootConfig.Insecure = viper.GetBool("insecure")
cluster, err := apisix.NewCluster(context.Background(), rootConfig.ClientConfig)
if err != nil {
color.RedString("Failed to create a new cluster: %v", err)
color.RedString("Failed to create a new cluster: %v", err.Error())
return
}
rootConfig.APISIXCluster = cluster
Expand Down
1 change: 1 addition & 0 deletions pkg/api/apisix/apisix.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Cluster interface {
PluginMetadata() PluginMetadata
StreamRoute() StreamRoute
Upstream() Upstream
Ping() error
}

type ResourceClient[T any] interface {
Expand Down
5 changes: 5 additions & 0 deletions pkg/api/apisix/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,8 @@ func (c *cluster) StreamRoute() StreamRoute {
func (c *cluster) Upstream() Upstream {
return c.upstream
}

func (c *cluster) Ping() error {
_, err := c.Route().List(context.Background())
return err
}
2 changes: 1 addition & 1 deletion test/cli/suites-basic/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var _ = ginkgo.Describe("`adc ping` tests", func() {
ginkgo.It("should connect to APISIX", func() {
output, err := s.Ping()
gomega.Expect(err).To(gomega.BeNil())
gomega.Expect(output).To(gomega.Equal("Connected to APISIX successfully!\n"))
gomega.Expect(output).To(gomega.Equal("Connected to backend successfully!\n"))
})
})
})
2 changes: 1 addition & 1 deletion test/mtls/suites-basic/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var _ = ginkgo.Describe("`adc ping` tests", func() {
ginkgo.It("should connect to APISIX", func() {
output, err := s.Ping()
gomega.Expect(err).To(gomega.BeNil())
gomega.Expect(output).To(gomega.Equal("Connected to APISIX successfully!\n"))
gomega.Expect(output).To(gomega.Equal("Connected to backend successfully!\n"))
})
})
})

0 comments on commit 7a41848

Please sign in to comment.