Skip to content
This repository has been archived by the owner on Jul 6, 2023. It is now read-only.

Commit

Permalink
fixed merge conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
Zandrr committed Aug 6, 2015
2 parents 0527512 + 25d18bf commit 2610c69
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 44 deletions.
28 changes: 18 additions & 10 deletions client/go/commands/cluster_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,25 @@ func (a *CreateNewClusterCommand) Exec(args []string) error {
return errors.New(s)
}

//check json response
var body glusterfs.ClusterInfoResponse
err = utils.GetJsonFromResponse(r, &body)
if err != nil {
fmt.Println("Error: Bad json response from server")
return err
}

//if all is well, print stuff
fmt.Fprintf(stdout, "Cluster id: %v", body.Id)
if a.options.Json {
// Print JSON body
s, err := utils.GetStringFromResponse(r)
if err != nil {
return err
}
fmt.Fprint(stdout, s)
} else {

//check json response
var body glusterfs.ClusterInfoResponse
err = utils.GetJsonFromResponse(r, &body)
if err != nil {
fmt.Println("Error: Bad json response from server")
return err
}
//if all is well, print stuff
fmt.Fprintf(stdout, "Cluster id: %v", body.Id)
}
return nil

}
7 changes: 5 additions & 2 deletions client/go/commands/cluster_destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,11 @@ func (a *DestroyClusterCommand) Exec(args []string) error {
}

//if all is well, print stuff
fmt.Fprintf(stdout, "Successfully destroyed cluster with id: %v ", clusterId)

if !a.options.Json {
fmt.Fprintf(stdout, "Successfully destroyed cluster with id: %v ", clusterId)
} else {
return errors.New("Cannot return json for cluster destroy")
}
return nil

}
43 changes: 25 additions & 18 deletions client/go/commands/cluster_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ func (a *GetClusterInfoCommand) Name() string {
}

func (a *GetClusterInfoCommand) Exec(args []string) error {

//parse flags and set id
a.flags.Parse(args)

Expand Down Expand Up @@ -97,27 +96,35 @@ func (a *GetClusterInfoCommand) Exec(args []string) error {
}
return errors.New(s)
}
if a.options.Json {
// Print JSON body
s, err := utils.GetStringFromResponse(r)
if err != nil {
return err
}
fmt.Fprint(stdout, s)
} else {

//check json response
var body glusterfs.ClusterInfoResponse
err = utils.GetJsonFromResponse(r, &body)
if err != nil {
fmt.Println("Error: Bad json response from server")
return err
}
//check json response
var body glusterfs.ClusterInfoResponse
err = utils.GetJsonFromResponse(r, &body)
if err != nil {
fmt.Println("Error: Bad json response from server")
return err
}

//print revelent results
str := "Cluster: " + clusterId + " \n" + "Nodes: \n"
for _, node := range body.Nodes {
str += node + "\n"
}
//print revelent results
str := "Cluster: " + clusterId + " \n" + "Nodes: \n"
for _, node := range body.Nodes {
str += node + "\n"
}

str += "Volumes: \n"
for _, volume := range body.Volumes {
str += volume + "\n"
str += "Volumes: \n"
for _, volume := range body.Volumes {
str += volume + "\n"
}
fmt.Fprintf(stdout, str)
}

fmt.Fprintf(stdout, str)
return nil

}
34 changes: 22 additions & 12 deletions client/go/commands/cluster_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,20 +94,30 @@ func (a *GetClusterListCommand) Exec(args []string) error {
return errors.New(s)
}

//check json response
var body glusterfs.ClusterListResponse
err = utils.GetJsonFromResponse(r, &body)
if err != nil {
fmt.Println("Error: Bad json response from server")
return err
}
if a.options.Json {
// Print JSON body
s, err := utils.GetStringFromResponse(r)
if err != nil {
return err
}
fmt.Fprint(stdout, s)
} else {

//check json response
var body glusterfs.ClusterListResponse
err = utils.GetJsonFromResponse(r, &body)
if err != nil {
fmt.Println("Error: Bad json response from server")
return err
}

//if all is well, print stuff
str := "Clusters: \n"
for _, cluster := range body.Clusters {
str += cluster + "\n"
// Print to user cluster lists
str := "Clusters: \n"
for _, cluster := range body.Clusters {
str += cluster + "\n"
}
fmt.Fprintf(stdout, str)
}
fmt.Fprintf(stdout, str)
return nil

}
89 changes: 89 additions & 0 deletions client/go/commands/flags_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
//
// Copyright (c) 2015 The heketi Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package commands

import (
"bytes"
"encoding/json"
"github.com/gorilla/mux"
"github.com/heketi/heketi/apps/glusterfs"
"github.com/heketi/heketi/tests"
"net/http/httptest"
"os"
"strings"
"testing"
)

func TestJsonFlagsCreate(t *testing.T) {
defer os.Remove("heketi.db")

// Create the app
app := glusterfs.NewApp()
defer app.Close()
router := mux.NewRouter()
app.SetRoutes(router)

// Setup the server
ts := httptest.NewServer(router)
defer ts.Close()

//set options
options := &Options{
Url: ts.URL,
Json: true,
}

//create b to get values of stdout
var b bytes.Buffer
defer tests.Patch(&stdout, &b).Restore()

//create mock cluster and mock destroy
mockCluster := NewCreateNewClusterCommand(options)

//create new cluster and assert json
err := mockCluster.Exec([]string{})
tests.Assert(t, err == nil)
var clusterInfoResCreate glusterfs.ClusterInfoResponse
err = json.Unmarshal(b.Bytes(), &clusterInfoResCreate)
tests.Assert(t, err == nil, err)
tests.Assert(t, strings.Contains(b.String(), clusterInfoResCreate.Id), clusterInfoResCreate)
b.Reset()

//get cluster info and assert json
clusterInfo := NewGetClusterInfoCommand(options)
args := []string{clusterInfoResCreate.Id}
err = clusterInfo.Exec(args)
tests.Assert(t, err == nil)
tests.Assert(t, strings.Contains(b.String(), clusterInfoResCreate.Id))
b.Reset()

//get cluster list and assert json
clusterList := NewGetClusterListCommand(options)
err = clusterList.Exec([]string{})
tests.Assert(t, err == nil)
var clusterListResList glusterfs.ClusterInfoResponse
err = json.Unmarshal(b.Bytes(), &clusterListResList)
tests.Assert(t, strings.Contains(b.String(), clusterListResList.Id))
b.Reset()

//destroy cluster and assert proper json response
clusterDestroy := NewDestroyClusterCommand(options)
err = clusterDestroy.Exec(args)
tests.Assert(t, err != nil)
tests.Assert(t, strings.Contains(b.String(), ""))

}
3 changes: 2 additions & 1 deletion client/go/commands/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
package commands

type Options struct {
Url string
Url string
Json bool
}
2 changes: 1 addition & 1 deletion client/go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Use "heketi [command] -help" for more information about a command
func init() {

flag.StringVar(&options.Url, "server", "", "server url goes here.")
flag.BoolVar(&options.Json, "json", false, "get response as json")

flag.Usage = func() {
fmt.Println(usageTemplateMain)
Expand All @@ -70,7 +71,6 @@ func main() {
for _, cmd := range cmds {
if flag.Arg(0) == cmd.Name() {

//check for err
err := cmd.Exec(flag.Args()[1:])
if err != nil {
fmt.Fprintf(stdout, "Error: %v\n", err)
Expand Down

0 comments on commit 2610c69

Please sign in to comment.