mirror of
https://github.com/grafana/grafana.git
synced 2025-08-02 04:31:36 +08:00

* API: Duplicate API Key Name Handle With Useful HTTP Code * 17447: make changes requested during review - use dialect.IsUniqueContraintViolation - change if statement to match others - return error properly * Revert "17447: make changes requested during review" This reverts commit a4a674ea83a9288701611f203f2a75531fb8a131. * API: useful http code on duplicate api key error w/ tests * API: API Key Duplicate Handling fixed small typo associated with error
77 lines
1.5 KiB
Go
77 lines
1.5 KiB
Go
package models
|
|
|
|
import (
|
|
"errors"
|
|
"time"
|
|
)
|
|
|
|
var ErrInvalidApiKey = errors.New("Invalid API Key")
|
|
var ErrInvalidApiKeyExpiration = errors.New("Negative value for SecondsToLive")
|
|
var ErrDuplicateApiKey = errors.New("API Key Organization ID And Name Must Be Unique")
|
|
|
|
type ApiKey struct {
|
|
Id int64
|
|
OrgId int64
|
|
Name string
|
|
Key string
|
|
Role RoleType
|
|
Created time.Time
|
|
Updated time.Time
|
|
Expires *int64
|
|
}
|
|
|
|
// ---------------------
|
|
// COMMANDS
|
|
type AddApiKeyCommand struct {
|
|
Name string `json:"name" binding:"Required"`
|
|
Role RoleType `json:"role" binding:"Required"`
|
|
OrgId int64 `json:"-"`
|
|
Key string `json:"-"`
|
|
SecondsToLive int64 `json:"secondsToLive"`
|
|
|
|
Result *ApiKey `json:"-"`
|
|
}
|
|
|
|
type UpdateApiKeyCommand struct {
|
|
Id int64 `json:"id"`
|
|
Name string `json:"name"`
|
|
Role RoleType `json:"role"`
|
|
|
|
OrgId int64 `json:"-"`
|
|
}
|
|
|
|
type DeleteApiKeyCommand struct {
|
|
Id int64 `json:"id"`
|
|
OrgId int64 `json:"-"`
|
|
}
|
|
|
|
// ----------------------
|
|
// QUERIES
|
|
|
|
type GetApiKeysQuery struct {
|
|
OrgId int64
|
|
IncludeInvalid bool
|
|
Result []*ApiKey
|
|
}
|
|
|
|
type GetApiKeyByNameQuery struct {
|
|
KeyName string
|
|
OrgId int64
|
|
Result *ApiKey
|
|
}
|
|
|
|
type GetApiKeyByIdQuery struct {
|
|
ApiKeyId int64
|
|
Result *ApiKey
|
|
}
|
|
|
|
// ------------------------
|
|
// DTO & Projections
|
|
|
|
type ApiKeyDTO struct {
|
|
Id int64 `json:"id"`
|
|
Name string `json:"name"`
|
|
Role RoleType `json:"role"`
|
|
Expiration *time.Time `json:"expiration,omitempty"`
|
|
}
|