Commit 90cd10e0 by Jason Wilder

CLI: Add account:delete command

parent c1d4acc0
......@@ -32,7 +32,7 @@ func main() {
app.Usage = "grafana web"
app.Version = version
app.Commands = []cli.Command{cmd.CmdWeb, cmd.CmdImportJson,
cmd.CmdListAccounts, cmd.CmdCreateAccount}
cmd.CmdListAccounts, cmd.CmdCreateAccount, cmd.CmdDeleteAccount}
app.Flags = append(app.Flags, []cli.Flag{}...)
app.Run(os.Args)
......
......@@ -40,6 +40,20 @@ var CmdCreateAccount = cli.Command{
},
}
var CmdDeleteAccount = cli.Command{
Name: "account:delete",
Usage: "delete an existing account",
Description: "Deletes an existing account",
Action: deleteAccount,
Flags: []cli.Flag{
cli.StringFlag{
Name: "config",
Value: "grafana.ini",
Usage: "path to config file",
},
},
}
func listAccounts(c *cli.Context) {
setting.NewConfigContext()
sqlstore.NewEngine()
......@@ -85,3 +99,27 @@ func createAccount(c *cli.Context) {
log.ConsoleInfof("Account %s created for admin user %s\n", name, adminUser.Email)
}
func deleteAccount(c *cli.Context) {
setting.NewConfigContext()
sqlstore.NewEngine()
sqlstore.EnsureAdminUser()
if !c.Args().Present() {
log.ConsoleFatal("Account name arg is required")
}
name := c.Args().First()
accountQuery := m.GetAccountByNameQuery{Name: name}
if err := bus.Dispatch(&accountQuery); err != nil {
log.ConsoleFatalf("Failed to find account: %s", err)
}
accountId := accountQuery.Result.Id
cmd := m.DeleteAccountCommand{Id: accountId}
if err := bus.Dispatch(&cmd); err != nil {
log.ConsoleFatalf("Failed to delete account: %s", err)
}
log.ConsoleInfof("Account %s deleted", name)
}
......@@ -29,6 +29,10 @@ type CreateAccountCommand struct {
Result Account `json:"-"`
}
type DeleteAccountCommand struct {
Id int64
}
type UpdateAccountCommand struct {
Name string `json:"name" binding:"Required"`
AccountId int64 `json:"-"`
......
......@@ -5,6 +5,7 @@ import (
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/events"
"github.com/grafana/grafana/pkg/log"
m "github.com/grafana/grafana/pkg/models"
)
......@@ -15,6 +16,7 @@ func init() {
bus.AddHandler("sql", UpdateAccount)
bus.AddHandler("sql", GetAccountByName)
bus.AddHandler("sql", GetAccountsQuery)
bus.AddHandler("sql", DeleteAccount)
}
func GetAccountsQuery(query *m.GetAccountsQuery) error {
......@@ -106,3 +108,29 @@ func UpdateAccount(cmd *m.UpdateAccountCommand) error {
return nil
})
}
func DeleteAccount(cmd *m.DeleteAccountCommand) error {
return inTransaction2(func(sess *session) error {
deletes := []string{
"DELETE FROM star WHERE EXISTS (SELECT 1 FROM dashboard WHERE account_id = ?)",
"DELETE FROM dashboard_tag WHERE EXISTS (SELECT 1 FROM dashboard WHERE account_id = ?)",
"DELETE FROM dashboard WHERE account_id = ?",
"DELETE FROM api_key WHERE account_id = ?",
"DELETE FROM data_source WHERE account_id = ?",
"DELETE FROM account_user WHERE account_id = ?",
"DELETE FROM user WHERE account_id = ?",
"DELETE FROM account WHERE id = ?",
}
for _, sql := range deletes {
log.Trace(sql)
_, err := sess.Exec(sql, cmd.Id)
if err != nil {
return err
}
}
return nil
})
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment