2015-08-18 05:57:27 +05:30
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
2015-12-08 06:49:55 +05:30
|
|
|
"database/sql"
|
2015-08-18 05:57:27 +05:30
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2015-08-25 04:05:44 +05:30
|
|
|
"github.com/go-gorp/gorp"
|
2015-08-18 05:57:27 +05:30
|
|
|
|
|
|
|
"github.com/coreos/dex/connector"
|
2015-12-08 06:49:55 +05:30
|
|
|
"github.com/coreos/dex/repo"
|
2015-08-18 05:57:27 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
connectorConfigTableName = "connector_config"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
register(table{
|
|
|
|
name: connectorConfigTableName,
|
|
|
|
model: connectorConfigModel{},
|
|
|
|
autoinc: false,
|
|
|
|
pkey: []string{"id"},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func newConnectorConfigModel(cfg connector.ConnectorConfig) (*connectorConfigModel, error) {
|
|
|
|
b, err := json.Marshal(cfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
m := &connectorConfigModel{
|
|
|
|
ID: cfg.ConnectorID(),
|
|
|
|
Type: cfg.ConnectorType(),
|
|
|
|
Config: string(b),
|
|
|
|
}
|
|
|
|
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type connectorConfigModel struct {
|
|
|
|
ID string `db:"id"`
|
|
|
|
Type string `db:"type"`
|
|
|
|
Config string `db:"config"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *connectorConfigModel) ConnectorConfig() (connector.ConnectorConfig, error) {
|
|
|
|
cfg, err := connector.NewConnectorConfigFromType(m.Type)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = json.Unmarshal([]byte(m.Config), cfg); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return cfg, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewConnectorConfigRepo(dbm *gorp.DbMap) *ConnectorConfigRepo {
|
|
|
|
return &ConnectorConfigRepo{dbMap: dbm}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ConnectorConfigRepo struct {
|
|
|
|
dbMap *gorp.DbMap
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *ConnectorConfigRepo) All() ([]connector.ConnectorConfig, error) {
|
2016-02-09 05:31:16 +05:30
|
|
|
qt := r.dbMap.Dialect.QuotedTableForQuery("", connectorConfigTableName)
|
2015-08-18 05:57:27 +05:30
|
|
|
q := fmt.Sprintf("SELECT * FROM %s", qt)
|
|
|
|
objs, err := r.dbMap.Select(&connectorConfigModel{}, q)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cfgs := make([]connector.ConnectorConfig, len(objs))
|
|
|
|
for i, obj := range objs {
|
|
|
|
m, ok := obj.(*connectorConfigModel)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("unable to cast connector to connectorConfigModel")
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg, err := m.ConnectorConfig()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cfgs[i] = cfg
|
|
|
|
}
|
|
|
|
|
|
|
|
return cfgs, nil
|
|
|
|
}
|
|
|
|
|
2015-12-08 06:49:55 +05:30
|
|
|
func (r *ConnectorConfigRepo) GetConnectorByID(tx repo.Transaction, id string) (connector.ConnectorConfig, error) {
|
2016-02-09 05:31:16 +05:30
|
|
|
qt := r.dbMap.Dialect.QuotedTableForQuery("", connectorConfigTableName)
|
2015-12-08 06:49:55 +05:30
|
|
|
q := fmt.Sprintf("SELECT * FROM %s WHERE id = $1", qt)
|
|
|
|
var c connectorConfigModel
|
2016-02-09 05:31:16 +05:30
|
|
|
if err := executor(r.dbMap, tx).SelectOne(&c, q, id); err != nil {
|
2015-12-08 06:49:55 +05:30
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return nil, connector.ErrorNotFound
|
|
|
|
}
|
2015-12-08 10:28:36 +05:30
|
|
|
return nil, err
|
2015-12-08 06:49:55 +05:30
|
|
|
}
|
|
|
|
return c.ConnectorConfig()
|
|
|
|
}
|
|
|
|
|
2015-08-18 05:57:27 +05:30
|
|
|
func (r *ConnectorConfigRepo) Set(cfgs []connector.ConnectorConfig) error {
|
|
|
|
insert := make([]interface{}, len(cfgs))
|
|
|
|
for i, cfg := range cfgs {
|
|
|
|
m, err := newConnectorConfigModel(cfg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
insert[i] = m
|
|
|
|
}
|
|
|
|
|
|
|
|
tx, err := r.dbMap.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-09 05:31:16 +05:30
|
|
|
defer tx.Rollback()
|
2015-08-18 05:57:27 +05:30
|
|
|
|
2016-02-09 05:31:16 +05:30
|
|
|
qt := r.dbMap.Dialect.QuotedTableForQuery("", connectorConfigTableName)
|
2015-08-18 05:57:27 +05:30
|
|
|
q := fmt.Sprintf("DELETE FROM %s", qt)
|
2016-02-09 05:31:16 +05:30
|
|
|
if _, err = tx.Exec(q); err != nil {
|
2015-08-18 05:57:27 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-02-09 05:31:16 +05:30
|
|
|
if err = tx.Insert(insert...); err != nil {
|
2015-08-18 05:57:27 +05:30
|
|
|
return fmt.Errorf("DB insert failed %#v: %v", insert, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return tx.Commit()
|
|
|
|
}
|