-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
522183a
commit 335ee49
Showing
6 changed files
with
158 additions
and
1 deletion.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters