From 86eebc61e8a230e0b72fe0f1af19cd38903ffa45 Mon Sep 17 00:00:00 2001 From: Nicholas Wiersma Date: Wed, 4 Dec 2024 14:10:23 +0200 Subject: [PATCH] feat: set resource revision for connectors --- server/api.go | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/server/api.go b/server/api.go index 4b210df3e4..4454c3ca49 100644 --- a/server/api.go +++ b/server/api.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "log/slog" + "strconv" "golang.org/x/crypto/bcrypt" @@ -430,10 +431,11 @@ func (d dexAPI) CreateConnector(ctx context.Context, req *api.CreateConnectorReq } c := storage.Connector{ - ID: req.Connector.Id, - Name: req.Connector.Name, - Type: req.Connector.Type, - Config: req.Connector.Config, + ID: req.Connector.Id, + Name: req.Connector.Name, + Type: req.Connector.Type, + ResourceVersion: "1", + Config: req.Connector.Config, } if err := d.s.CreateConnector(ctx, c); err != nil { if err == storage.ErrAlreadyExists { @@ -446,7 +448,7 @@ func (d dexAPI) CreateConnector(ctx context.Context, req *api.CreateConnectorReq return &api.CreateConnectorResp{}, nil } -func (d dexAPI) UpdateConnector(ctx context.Context, req *api.UpdateConnectorReq) (*api.UpdateConnectorResp, error) { +func (d dexAPI) UpdateConnector(_ context.Context, req *api.UpdateConnectorReq) (*api.UpdateConnectorResp, error) { if !featureflags.APIConnectorsCRUD.Enabled() { return nil, fmt.Errorf("%s feature flag is not enabled", featureflags.APIConnectorsCRUD.Name) } @@ -476,6 +478,10 @@ func (d dexAPI) UpdateConnector(ctx context.Context, req *api.UpdateConnectorReq old.Config = req.NewConfig } + if rev, err := strconv.Atoi(defaultTo(old.ResourceVersion, "0")); err == nil { + old.ResourceVersion = strconv.Itoa(rev + 1) + } + return old, nil } @@ -536,3 +542,11 @@ func (d dexAPI) ListConnectors(ctx context.Context, req *api.ListConnectorReq) ( Connectors: connectors, }, nil } + +func defaultTo[T comparable](v, def T) T { + var zeroT T + if v == zeroT { + return def + } + return v +}