Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Clivern committed May 3, 2020
1 parent c691bc3 commit 95e81df
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 12 deletions.
67 changes: 61 additions & 6 deletions internal/app/cmd/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
package cmd

import (
"context"
"fmt"
"net/http"
"os"

"github.com/clivern/beetle/internal/app/module"
Expand All @@ -23,17 +26,27 @@ var clusterCmd = &cobra.Command{
token := os.Getenv("REMOTE_BEETLE_TOKEN")

if url == "" {
panic("Error! beetle url is missing (eg. $ export REMOTE_BEETLE_URL=http://127.0.0.1")
module.DrawTable(
[]string{"Cluster", "Health"},
[][]string{{"Error! beetle url is missing ($ export REMOTE_BEETLE_URL=http[s]://remote_url) is required", ""}},
)
return
}

httpClient := module.NewHTTPClient()

if len(args) > 0 {
err, result = getCluster(args[0], url, token)
err, result = getCluster(httpClient, args[0], url, token)
} else {
err, result = getClusters(url, token)
err, result = getClusters(httpClient, url, token)
}

if err != nil {
panic(err.Error())
module.DrawTable(
[]string{"Cluster", "Health"},
[][]string{{fmt.Sprintf("Error! %s", err.Error()), ""}},
)
return
}

module.DrawTable(
Expand All @@ -48,15 +61,57 @@ func init() {
}

// getClusters Get Clusters List
func getClusters(beetleURL, token string) (error, [][]string) {
func getClusters(httpClient *module.HTTPClient, beetleURL, token string) (error, [][]string) {
response, err := httpClient.Get(
context.Background(),
fmt.Sprintf("%s/api/v1/cluster", beetleURL),
map[string]string{},
map[string]string{"X-AUTH-TOKEN": token},
)

if httpClient.GetStatusCode(response) != http.StatusOK || err != nil {
return fmt.Errorf("Unable to fetch remote data"), [][]string{}
}

_, err = httpClient.ToString(response)

if err != nil {
return fmt.Errorf("Invalid response"), [][]string{}
}

// @TODO
// convert json response to struct
// then into [][]string{}

return nil, [][]string{
{"staging", "down"},
{"production", "up"},
}
}

// getCluster Get Cluster
func getCluster(cluster string, beetleURL, token string) (error, [][]string) {
func getCluster(httpClient *module.HTTPClient, cluster, beetleURL, token string) (error, [][]string) {
response, err := httpClient.Get(
context.Background(),
fmt.Sprintf("%s/api/v1/cluster/%s", beetleURL, cluster),
map[string]string{},
map[string]string{"X-AUTH-TOKEN": token},
)

if httpClient.GetStatusCode(response) != http.StatusOK || err != nil {
return fmt.Errorf("Unable to fetch remote data"), [][]string{}
}

_, err = httpClient.ToString(response)

if err != nil {
return fmt.Errorf("Invalid response"), [][]string{}
}

// @TODO
// convert json response to struct
// then into [][]string{}

return nil, [][]string{
{"staging", "down"},
}
Expand Down
9 changes: 5 additions & 4 deletions internal/app/cmd/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
// TestClusterCMD test cases
func TestClusterCMD(t *testing.T) {
testingConfig := "config.testing.yml"
httpClient := module.NewHTTPClient()

// LoadConfigFile
t.Run("LoadConfigFile", func(t *testing.T) {
Expand All @@ -48,14 +49,14 @@ func TestClusterCMD(t *testing.T) {

// TestGetClusters
t.Run("TestGetClusters", func(t *testing.T) {
srv := pkg.MockServer(
srv := pkg.ServerMock(
"/api/v1/cluster",
`{"clusters": [{"name": "staging", "health": false},{"name": "production", "health": true}]}`,
)

defer srv.Close()

err, result := getClusters(srv.URL, "")
err, result := getClusters(httpClient, srv.URL, "")

pkg.Expect(t, nil, err)
pkg.Expect(t, result, [][]string{
Expand All @@ -66,14 +67,14 @@ func TestClusterCMD(t *testing.T) {

// TestGetCluster
t.Run("TestGetCluster", func(t *testing.T) {
srv := pkg.MockServer(
srv := pkg.ServerMock(
"/api/v1/cluster/staging",
`{"name": "staging", "health": false}`,
)

defer srv.Close()

err, result := getCluster("staging", srv.URL, "")
err, result := getCluster(httpClient, "staging", srv.URL, "")

pkg.Expect(t, nil, err)
pkg.Expect(t, result, [][]string{
Expand Down
4 changes: 2 additions & 2 deletions pkg/server_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"net/http/httptest"
)

// MockServer mocks http server
func MockServer(uri, response string) *httptest.Server {
// ServerMock mocks http server
func ServerMock(uri, response string) *httptest.Server {
handler := http.NewServeMux()
handler.HandleFunc(uri, func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(response))
Expand Down

0 comments on commit 95e81df

Please sign in to comment.