API token -> API key rename

This commit is contained in:
Torkel Ödegaard
2015-01-27 08:26:11 +01:00
parent db371d2a5d
commit 951ce0a102
10 changed files with 201 additions and 195 deletions
@@ -9,35 +9,35 @@ import (
)
func init() {
bus.AddHandler("sql", GetTokens)
bus.AddHandler("sql", GetTokenByToken)
bus.AddHandler("sql", UpdateToken)
bus.AddHandler("sql", DeleteToken)
bus.AddHandler("sql", AddToken)
bus.AddHandler("sql", GetApiKeys)
bus.AddHandler("sql", GetApiKeyByKey)
bus.AddHandler("sql", UpdateApiKey)
bus.AddHandler("sql", DeleteApiKey)
bus.AddHandler("sql", AddApiKey)
}
func GetTokens(query *m.GetTokensQuery) error {
func GetApiKeys(query *m.GetApiKeysQuery) error {
sess := x.Limit(100, 0).Where("account_id=?", query.AccountId).Asc("name")
query.Result = make([]*m.Token, 0)
query.Result = make([]*m.ApiKey, 0)
return sess.Find(&query.Result)
}
func DeleteToken(cmd *m.DeleteTokenCommand) error {
func DeleteApiKey(cmd *m.DeleteApiKeyCommand) error {
return inTransaction(func(sess *xorm.Session) error {
var rawSql = "DELETE FROM token WHERE id=? and account_id=?"
var rawSql = "DELETE FROM api_key WHERE id=? and account_id=?"
_, err := sess.Exec(rawSql, cmd.Id, cmd.AccountId)
return err
})
}
func AddToken(cmd *m.AddTokenCommand) error {
func AddApiKey(cmd *m.AddApiKeyCommand) error {
return inTransaction(func(sess *xorm.Session) error {
t := m.Token{
t := m.ApiKey{
AccountId: cmd.AccountId,
Name: cmd.Name,
Role: cmd.Role,
Token: cmd.Token,
Key: cmd.Key,
Created: time.Now(),
Updated: time.Now(),
}
@@ -50,32 +50,30 @@ func AddToken(cmd *m.AddTokenCommand) error {
})
}
func UpdateToken(cmd *m.UpdateTokenCommand) error {
func UpdateApiKey(cmd *m.UpdateApiKeyCommand) error {
return inTransaction(func(sess *xorm.Session) error {
t := m.Token{
t := m.ApiKey{
Id: cmd.Id,
AccountId: cmd.AccountId,
Name: cmd.Name,
Role: cmd.Role,
Updated: time.Now(),
}
_, err := sess.Where("id=? and account_id=?", t.Id, t.AccountId).Update(&t)
return err
})
}
func GetTokenByToken(query *m.GetTokenByTokenQuery) error {
var token m.Token
has, err := x.Where("token=?", query.Token).Get(&token)
func GetApiKeyByKey(query *m.GetApiKeyByKeyQuery) error {
var apikey m.ApiKey
has, err := x.Where("key=?", query.Key).Get(&apikey)
if err != nil {
return err
} else if has == false {
return m.ErrInvalidToken
return m.ErrInvalidApiKey
}
query.Result = &token
query.Result = &apikey
return nil
}
+16 -7
View File
@@ -2,13 +2,19 @@ package sqlstore
import . "github.com/torkelo/grafana-pro/pkg/services/sqlstore/migrator"
// --- Migration Guide line ---
// 1. Never change a migration that is committed and pushed to master
// 2. Always add new migrations (to change or undo previous migrations)
// 3. Some migraitons are not yet written (rename column, table, drop table, index etc)
// 4
func addMigrations(mg *Migrator) {
addMigrationLogMigrations(mg)
addUserMigrations(mg)
addAccountMigrations(mg)
addDashboardMigration(mg)
addDataSourceMigration(mg)
addTokenMigrations(mg)
addApiKeyMigrations(mg)
}
func addMigrationLogMigrations(mg *Migrator) {
@@ -131,19 +137,22 @@ func addDataSourceMigration(mg *Migrator) {
Table("data_source").Columns("account_id", "name").Unique())
}
func addTokenMigrations(mg *Migrator) {
mg.AddMigration("create token table", new(AddTableMigration).
Name("token").WithColumns(
func addApiKeyMigrations(mg *Migrator) {
mg.AddMigration("create api_key table", new(AddTableMigration).
Name("api_key").WithColumns(
&Column{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
&Column{Name: "account_id", Type: DB_BigInt, Nullable: false},
&Column{Name: "name", Type: DB_NVarchar, Length: 255, Nullable: false},
&Column{Name: "token", Type: DB_NVarchar, Length: 255, Nullable: false},
&Column{Name: "key", Type: DB_Varchar, Length: 64, Nullable: false},
&Column{Name: "role", Type: DB_NVarchar, Length: 255, Nullable: false},
&Column{Name: "created", Type: DB_DateTime, Nullable: false},
&Column{Name: "updated", Type: DB_DateTime, Nullable: false},
))
//------- indexes ------------------
mg.AddMigration("add index token.account_id", new(AddIndexMigration).
Table("token").Columns("account_id"))
mg.AddMigration("add index api_key.account_id", new(AddIndexMigration).
Table("api_key").Columns("account_id"))
mg.AddMigration("add index api_key.key", new(AddIndexMigration).
Table("api_key").Columns("key").Unique())
}