Skip to content

Commit

Permalink
feat: set resource revision for connectors (#3868)
Browse files Browse the repository at this point in the history
Signed-off-by: Nicholas Wiersma <nick@wiersma.co.za>
  • Loading branch information
nrwiersma authored Dec 17, 2024
1 parent 6110149 commit d521051
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions server/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"log/slog"
"strconv"

"golang.org/x/crypto/bcrypt"

Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

0 comments on commit d521051

Please sign in to comment.