Skip to content

Commit

Permalink
merge services
Browse files Browse the repository at this point in the history
  • Loading branch information
christeredvartsen committed Feb 9, 2024
1 parent 2b6cedf commit 7d71429
Show file tree
Hide file tree
Showing 13 changed files with 1,275 additions and 1,531 deletions.
1 change: 0 additions & 1 deletion internal/grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ func Run(ctx context.Context, listenAddress string, repo database.Database, audi
protoapi.RegisterTeamsServer(s, &TeamsServer{db: repo})
protoapi.RegisterUsersServer(s, &UsersServer{db: repo})
protoapi.RegisterReconcilersServer(s, &ReconcilersServer{db: repo})
protoapi.RegisterReconcilerResourcesServer(s, &ReconcilerResourcesServer{db: repo})
protoapi.RegisterAuditLogsServer(s, &AuditLogsServer{db: repo, auditlog: auditlog})

g, ctx := errgroup.WithContext(ctx)
Expand Down
103 changes: 0 additions & 103 deletions internal/grpc/reconciler_resources.go

This file was deleted.

86 changes: 86 additions & 0 deletions internal/grpc/reconcilers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ import (
"github.com/nais/api/pkg/protoapi"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/timestamppb"
"k8s.io/utils/ptr"
)

type ReconcilersServer struct {
db interface {
database.ReconcilerRepo
database.ReconcilerErrorRepo
database.ReconcilerResourceRepo
database.TeamRepo
}
protoapi.UnimplementedReconcilersServer
Expand Down Expand Up @@ -147,6 +149,90 @@ func (s *ReconcilersServer) Config(ctx context.Context, req *protoapi.ConfigReco
}, nil
}

func (s *ReconcilersServer) SaveResources(ctx context.Context, in *protoapi.SaveReconcilerResourceRequest) (*protoapi.SaveReconcilerResourceResponse, error) {
switch {
case in.ReconcilerName == "":
return nil, status.Error(400, "reconcilerName is required")
case in.TeamSlug == "":
return nil, status.Error(400, "teamSlug is required")
}

slg := slug.Slug(in.TeamSlug)
rn := in.ReconcilerName

for _, rr := range in.Resources {
switch {
case rr.Name == "":
return nil, status.Error(400, "name is required")
case len(rr.Value) == 0:
return nil, status.Error(400, "value is required")
}
_, err := s.db.UpsertReconcilerResource(ctx, rn, slg, rr.Name, rr.Value, rr.Metadata)
if err != nil {
return nil, err
}
}

return &protoapi.SaveReconcilerResourceResponse{}, nil
}

func (s *ReconcilersServer) Resources(ctx context.Context, req *protoapi.ListReconcilerResourcesRequest) (*protoapi.ListReconcilerResourcesResponse, error) {
var teamSlug *slug.Slug

if req.TeamSlug != "" {
slg := slug.Slug(req.TeamSlug)
teamSlug = &slg
}

limit, offset := pagination(req)
total := 0
res, err := s.db.GetReconcilerResources(ctx, req.ReconcilerName, teamSlug, database.Page{
Limit: limit,
Offset: offset,
})
if err != nil {
return nil, err
}

resp := &protoapi.ListReconcilerResourcesResponse{
PageInfo: pageInfo(req, total),
}
for _, rr := range res {
resp.Nodes = append(resp.Nodes, toProtoReconcilerResource(rr))
}
return resp, nil
}

func (s *ReconcilersServer) DeleteResources(ctx context.Context, req *protoapi.DeleteReconcilerResourcesRequest) (*protoapi.DeleteReconcilerResourcesResponse, error) {
if req.ReconcilerName == "" {
return nil, status.Error(400, "reconcilerName is required")
}

if req.TeamSlug == "" {
return nil, status.Error(400, "teamSlug is required")
}

teamSlug := slug.Slug(req.TeamSlug)
if err := s.db.DeleteAllReconcilerResources(ctx, req.ReconcilerName, teamSlug); err != nil {
return nil, err
}

return &protoapi.DeleteReconcilerResourcesResponse{}, nil
}

func toProtoReconcilerResource(res *database.ReconcilerResource) *protoapi.ReconcilerResource {
return &protoapi.ReconcilerResource{
Id: res.ID.String(),
ReconcilerName: res.ReconcilerName,
TeamSlug: string(res.TeamSlug),
Name: res.Name,
Value: res.Value,
Metadata: res.Metadata,
CreatedAt: timestamppb.New(res.CreatedAt.Time),
UpdatedAt: timestamppb.New(res.UpdatedAt.Time),
}
}

func toProtoReconciler(rec *database.Reconciler) *protoapi.Reconciler {
return &protoapi.Reconciler{
Name: rec.Name,
Expand Down
4 changes: 0 additions & 4 deletions pkg/apiclient/apiclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ func (a *APIClient) Teams() protoapi.TeamsClient {
return protoapi.NewTeamsClient(a.conn)
}

func (a *APIClient) ReconcilerResources() protoapi.ReconcilerResourcesClient {
return protoapi.NewReconcilerResourcesClient(a.conn)
}

func (a *APIClient) AuditLogs() protoapi.AuditLogsClient {
return protoapi.NewAuditLogsClient(a.conn)
}
Expand Down
19 changes: 8 additions & 11 deletions pkg/apiclient/mockclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,10 @@ func (t *TestingHelpers) FailNow() {
}

type MockServers struct {
AuditLogs *protoapi.MockAuditLogsServer
Reconcilers *protoapi.MockReconcilersServer
ReconcilerResources *protoapi.MockReconcilerResourcesServer
Teams *protoapi.MockTeamsServer
Users *protoapi.MockUsersServer
AuditLogs *protoapi.MockAuditLogsServer
Reconcilers *protoapi.MockReconcilersServer
Teams *protoapi.MockTeamsServer
Users *protoapi.MockUsersServer
}

func NewMockClient(t testing.TB) (*APIClient, *MockServers) {
Expand All @@ -80,16 +79,14 @@ func NewMockClient(t testing.TB) (*APIClient, *MockServers) {
}
th.Cleanup(th.printBuffer)
mockServers := &MockServers{
AuditLogs: protoapi.NewMockAuditLogsServer(th),
Reconcilers: protoapi.NewMockReconcilersServer(th),
ReconcilerResources: protoapi.NewMockReconcilerResourcesServer(th),
Teams: protoapi.NewMockTeamsServer(th),
Users: protoapi.NewMockUsersServer(th),
AuditLogs: protoapi.NewMockAuditLogsServer(th),
Reconcilers: protoapi.NewMockReconcilersServer(th),
Teams: protoapi.NewMockTeamsServer(th),
Users: protoapi.NewMockUsersServer(th),
}

protoapi.RegisterAuditLogsServer(s, mockServers.AuditLogs)
protoapi.RegisterReconcilersServer(s, mockServers.Reconcilers)
protoapi.RegisterReconcilerResourcesServer(s, mockServers.ReconcilerResources)
protoapi.RegisterTeamsServer(s, mockServers.Teams)
protoapi.RegisterUsersServer(s, mockServers.Users)

Expand Down
Loading

0 comments on commit 7d71429

Please sign in to comment.