Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce code duplication in provider handlers #3349

Merged
merged 1 commit into from
May 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 38 additions & 36 deletions internal/controlplane/handlers_providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"database/sql"
"errors"
"fmt"

"github.com/google/uuid"
"github.com/rs/zerolog"
Expand All @@ -26,6 +27,8 @@ import (
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/types/known/structpb"

config "github.com/stacklok/minder/internal/config/server"
"github.com/stacklok/minder/internal/crypto"
"github.com/stacklok/minder/internal/db"
"github.com/stacklok/minder/internal/engine"
"github.com/stacklok/minder/internal/providers"
Expand All @@ -39,34 +42,21 @@ func (s *Server) GetProvider(ctx context.Context, req *minderv1.GetProviderReque
entityCtx := engine.EntityFromContext(ctx)
projectID := entityCtx.Project.ID

prov, err := s.providerStore.GetByNameInSpecificProject(ctx, projectID, req.GetName())
dbProv, err := s.providerStore.GetByNameInSpecificProject(ctx, projectID, req.GetName())
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, util.UserVisibleError(codes.NotFound, "provider not found")
}
return nil, status.Errorf(codes.Internal, "error getting provider: %v", err)
}

var cfg *structpb.Struct

if len(prov.Definition) > 0 {
cfg = &structpb.Struct{}
if err := protojson.Unmarshal(prov.Definition, cfg); err != nil {
return nil, status.Errorf(codes.Internal, "error unmarshalling provider definition: %v", err)
}
prov, err := protobufProviderFromDB(ctx, s.store, s.cryptoEngine, &s.cfg.Provider, dbProv)
if err != nil {
return nil, status.Errorf(codes.Internal, "error creating provider: %v", err)
}

return &minderv1.GetProviderResponse{
Provider: &minderv1.Provider{
Name: prov.Name,
Project: projectID.String(),
Version: prov.Version,
Implements: protobufProviderImplementsFromDB(ctx, *prov),
AuthFlows: protobufProviderAuthFlowFromDB(ctx, *prov),
Config: cfg,
CredentialsState: providers.GetCredentialStateForProvider(ctx, *prov, s.store, s.cryptoEngine, &s.cfg.Provider),
Class: string(prov.Class),
},
Provider: prov,
}, nil
}

Expand Down Expand Up @@ -110,26 +100,13 @@ func (s *Server) ListProviders(ctx context.Context, req *minderv1.ListProvidersR
zerolog.Ctx(ctx).Debug().Int("count", len(list)).Msg("providers")

provs := make([]*minderv1.Provider, 0, len(list))
for _, p := range list {
var cfg *structpb.Struct

if len(p.Definition) > 0 {
cfg = &structpb.Struct{}
if err := protojson.Unmarshal(p.Definition, cfg); err != nil {
return nil, status.Errorf(codes.Internal, "error unmarshalling provider definition: %v", err)
}
for _, dbProv := range list {
prov, err := protobufProviderFromDB(ctx, s.store, s.cryptoEngine, &s.cfg.Provider, &dbProv)
if err != nil {
return nil, status.Errorf(codes.Internal, "error creating provider: %v", err)
}

provs = append(provs, &minderv1.Provider{
Name: p.Name,
Project: projectID.String(),
Version: p.Version,
Implements: protobufProviderImplementsFromDB(ctx, p),
AuthFlows: protobufProviderAuthFlowFromDB(ctx, p),
CredentialsState: providers.GetCredentialStateForProvider(ctx, p, s.store, s.cryptoEngine, &s.cfg.Provider),
Config: cfg,
Class: string(p.Class),
})
provs = append(provs, prov)
}

cursor := ""
Expand Down Expand Up @@ -209,6 +186,31 @@ func (s *Server) DeleteProviderByID(
}, nil
}

func protobufProviderFromDB(
ctx context.Context, store db.Store,
cryptoEngine crypto.Engine, pc *config.ProviderConfig, p *db.Provider,
) (*minderv1.Provider, error) {
var cfg *structpb.Struct

if len(p.Definition) > 0 {
cfg = &structpb.Struct{}
if err := protojson.Unmarshal(p.Definition, cfg); err != nil {
return nil, fmt.Errorf("error unmarshalling provider definition: %w", err)
}
}

return &minderv1.Provider{
Name: p.Name,
Project: p.ProjectID.String(),
Version: p.Version,
Implements: protobufProviderImplementsFromDB(ctx, *p),
AuthFlows: protobufProviderAuthFlowFromDB(ctx, *p),
Config: cfg,
CredentialsState: providers.GetCredentialStateForProvider(ctx, *p, store, cryptoEngine, pc),
Class: string(p.Class),
}, nil
}

func protobufProviderImplementsFromDB(ctx context.Context, p db.Provider) []minderv1.ProviderType {
impls := make([]minderv1.ProviderType, 0, len(p.Implements))
for _, i := range p.Implements {
Expand Down