This repository has been archived on 2023-08-14. You can view files and clone it, but cannot push or open issues or pull requests.
dex/storage/ent/client/connector.go
m.nabokikh 11859166d0 feat: Add ent-based sqlite3 storage
Signed-off-by: m.nabokikh <maksim.nabokikh@flant.com>
2021-04-30 17:47:54 +04:00

89 lines
2.6 KiB
Go

package client
import (
"context"
"github.com/dexidp/dex/storage"
)
// CreateConnector saves a connector into the database.
func (d *Database) CreateConnector(connector storage.Connector) error {
_, err := d.client.Connector.Create().
SetID(connector.ID).
SetName(connector.Name).
SetType(connector.Type).
SetResourceVersion(connector.ResourceVersion).
SetConfig(connector.Config).
Save(context.TODO())
if err != nil {
return convertDBError("create connector: %w", err)
}
return nil
}
// ListConnectors extracts an array of connectors from the database.
func (d *Database) ListConnectors() ([]storage.Connector, error) {
connectors, err := d.client.Connector.Query().All(context.TODO())
if err != nil {
return nil, convertDBError("list connectors: %w", err)
}
storageConnectors := make([]storage.Connector, 0, len(connectors))
for _, c := range connectors {
storageConnectors = append(storageConnectors, toStorageConnector(c))
}
return storageConnectors, nil
}
// GetConnector extracts a connector from the database by id.
func (d *Database) GetConnector(id string) (storage.Connector, error) {
connector, err := d.client.Connector.Get(context.TODO(), id)
if err != nil {
return storage.Connector{}, convertDBError("get connector: %w", err)
}
return toStorageConnector(connector), nil
}
// DeleteConnector deletes a connector from the database by id.
func (d *Database) DeleteConnector(id string) error {
err := d.client.Connector.DeleteOneID(id).Exec(context.TODO())
if err != nil {
return convertDBError("delete connector: %w", err)
}
return nil
}
// UpdateConnector changes a connector by id using an updater function and saves it to the database.
func (d *Database) UpdateConnector(id string, updater func(old storage.Connector) (storage.Connector, error)) error {
tx, err := d.client.Tx(context.TODO())
if err != nil {
return convertDBError("update connector tx: %w", err)
}
connector, err := tx.Connector.Get(context.TODO(), id)
if err != nil {
return rollback(tx, "update connector database: %w", err)
}
newConnector, err := updater(toStorageConnector(connector))
if err != nil {
return rollback(tx, "update connector updating: %w", err)
}
_, err = tx.Connector.UpdateOneID(newConnector.ID).
SetName(newConnector.Name).
SetType(newConnector.Type).
SetResourceVersion(newConnector.ResourceVersion).
SetConfig(newConnector.Config).
Save(context.TODO())
if err != nil {
return rollback(tx, "update connector uploading: %w", err)
}
if err = tx.Commit(); err != nil {
return rollback(tx, "update connector commit: %w", err)
}
return nil
}