Skip to content

Commit

Permalink
Added command to delete team
Browse files Browse the repository at this point in the history
  • Loading branch information
dimitrovvlado committed Sep 28, 2018
1 parent 522183a commit 335ee49
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 1 deletion.
File renamed without changes.
1 change: 1 addition & 0 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ func newDeletCmd(client *grafana.Client, out io.Writer) *cobra.Command {

deleteCmd.AddCommand(newDatasourceDeleteCommand(client, out))
deleteCmd.AddCommand(newDashboardDeleteCommand(client, out))
deleteCmd.AddCommand(newTeamDeleteCommand(client, out))
return deleteCmd
}
7 changes: 6 additions & 1 deletion cmd/grafctl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
)

type requestCase struct {
method string
requestURI string
handler func(w http.ResponseWriter)
}
Expand All @@ -19,7 +20,11 @@ func mockClient(cases []requestCase) *grafana.Client {
var apiStub = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
for _, req := range cases {
if req.requestURI == r.RequestURI {
req.handler(w)
if req.method != "" && req.method == r.Method {
req.handler(w)
} else {
req.handler(w)
}
}
}
}))
Expand Down
60 changes: 60 additions & 0 deletions cmd/team_delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package cmd

import (
"fmt"
"io"
"log"

"github.com/dimitrovvlado/grafctl/grafana"
"github.com/spf13/cobra"
)

type teamDeleteCmd struct {
client *grafana.Client
out io.Writer
teamID string
}

func newTeamDeleteCommand(client *grafana.Client, out io.Writer) *cobra.Command {
i := &teamDeleteCmd{
client: client,
out: out,
}
deleteTeamCmd := &cobra.Command{
Use: "team",
Aliases: []string{},
Short: "Delete team by ID",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
log.Println("Command 'delete' requires an ID")
cmd.Help()
return nil
}
ensureClient(i.client)
i.teamID = args[0]
return i.run()
},
}

return deleteTeamCmd
}

// run deletes a ds
func (i *teamDeleteCmd) run() error {
team, err := i.client.GetTeam(i.teamID)
if err != nil {
switch err {
case grafana.ErrNotFound:
log.Printf("Team with ID \"%s\" not found", i.teamID)
default:
log.Println(err)
}
} else {
err = i.client.DeleteTeam(team)
if err != nil {
return err
}
fmt.Fprintln(i.out, fmt.Sprintf("Team \"%s\" deleted", team.Name))
}
return nil
}
61 changes: 61 additions & 0 deletions cmd/team_delete_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package cmd

import (
"bytes"
"fmt"
"log"
"net/http"
"testing"

"github.com/dimitrovvlado/grafctl/grafana"
"github.com/stretchr/testify/require"
)

func TestDeleteTeam(t *testing.T) {
b := helperLoadBytes(t, "team.json")
client := mockClient([]requestCase{
{
requestURI: fmt.Sprintf("%s/3", grafana.TeamsEndpoint),
method: http.MethodGet,
handler: func(w http.ResponseWriter) {
w.Write(b)
},
},
{
requestURI: fmt.Sprintf("%s/3", grafana.DashboardsUIDEndpoint),
method: http.MethodDelete,
handler: func(w http.ResponseWriter) {
w.Write(b)
},
},
})

var buf bytes.Buffer
cmd := newTeamDeleteCommand(client, &buf)
cmd.RunE(cmd, []string{"3"})

require.Equal(t, "Team \"MyTestTeam\" deleted\n", buf.String())
}

func TestDeleteNotExistingTeam(t *testing.T) {
client := mockClient([]requestCase{
{
requestURI: fmt.Sprintf("%s/3", grafana.TeamsEndpoint),
method: http.MethodGet,
handler: func(w http.ResponseWriter) {
w.WriteHeader(404)
},
},
})

var buf bytes.Buffer
log.SetOutput(&buf)
cmd := newTeamDeleteCommand(client, &buf)
cmd.RunE(cmd, []string{"3"})

require.Equal(t, "Team with ID \"3\" not found\n", buf.String())
}

func init() {
log.SetFlags(log.Flags() &^ (log.Ldate | log.Ltime))
}
30 changes: 30 additions & 0 deletions grafana/teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,26 @@ import (
"net/http"
)

//GetTeam returns a team by ID
func (c *Client) GetTeam(teamID string) (Team, error) {
resp, err := c.doRequest(&request{
method: http.MethodGet,
endpoint: fmt.Sprintf("%s/%s", TeamsEndpoint, teamID),
})
if err != nil {
return Team{}, err
}

// Decode the response into a TeamPage response object.
var team Team
if err := decodeResponse(resp, &team); err != nil {
// Read the body of the request, ignore the error since we are already in the error state.
return Team{}, fmt.Errorf("decoding response from request to failed, err -> %v", err)
}

return team, nil
}

//SearchTeams returns a list of teams
func (c *Client) SearchTeams(opt *SearchTeamsOptions) (TeamPage, error) {
resp, err := c.doRequest(&request{
Expand Down Expand Up @@ -47,3 +67,13 @@ func (c *Client) CreateTeam(team Team) (int, error) {
}
return int(teamResponse["teamId"].(float64)), nil
}

// DeleteTeam deletes a team
func (c *Client) DeleteTeam(team Team) error {
resp, err := c.doRequest(&request{
method: http.MethodDelete,
endpoint: fmt.Sprintf("%s/%d", TeamsEndpoint, team.ID),
})
defer resp.Body.Close()
return err
}

0 comments on commit 335ee49

Please sign in to comment.