Skip to content

Commit

Permalink
wip: list accounts transformers
Browse files Browse the repository at this point in the history
  • Loading branch information
katallaxie authored May 8, 2024
1 parent a4405d4 commit 7f20fd6
Show file tree
Hide file tree
Showing 16 changed files with 2,458 additions and 7,187 deletions.
452 changes: 46 additions & 406 deletions api/api.yml

Large diffs are not rendered by default.

444 changes: 45 additions & 399 deletions api/out.yml

Large diffs are not rendered by default.

81 changes: 21 additions & 60 deletions internal/api/controllers/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,38 @@ import (
"github.com/google/uuid"
"github.com/zeiss/typhoon/internal/api/models"
"github.com/zeiss/typhoon/internal/api/ports"

openapi "github.com/zeiss/typhoon/pkg/apis"
)

// AccountsController is the interface that wraps the methods to access accounts.
type AccountsController interface {
// CreateAccount creates a new account.
CreateAccount(ctx context.Context, name string, operatorID uuid.UUID) (*models.Account, error)
// UpdateAccount updates an account.
UpdateAccount(ctx context.Context, req UpdateOperatorAccountRequestObject) (*models.Account, error)
// DeleteToken deletes a token.
DeleteToken(ctx context.Context, accountID uuid.UUID) error
// CreateSigningKeyGroup creates a new signing key group.
CreateSigningKeyGroup(ctx context.Context) (*models.Account, error)
// ListSigningKeys of an account.
ListSigningKeys(ctx context.Context, accountID uuid.UUID, pagination models.Pagination[models.NKey]) (models.Pagination[models.NKey], error)
// ListAccounts ...
ListAccounts(ctx context.Context, input ListAccountsInput) (ListAccountsOutput, error)
}

// ListAccountsInput ...
type ListAccountsInput struct {
SystemID uuid.UUID
Limit int
Offset int
}

// ListAccountsOutput ...
type ListAccountsOutput struct {
Accounts []models.Account
Total int
Offset int
Limit int
}

// CreateOperatorAccountRequestObject ...
type UpdateOperatorAccountRequestObject = openapi.UpdateOperatorAccountRequestObject
var _ AccountsController = (*accountsController)(nil)

type accountsController struct {
db ports.Repositories
Expand Down Expand Up @@ -122,60 +134,9 @@ func (c *accountsController) CreateAccount(ctx context.Context, name string, ope
// return account, nil
}

// UpdateAccount ...
func (c *accountsController) UpdateAccount(ctx context.Context, req UpdateOperatorAccountRequestObject) (*models.Account, error) {
return nil, nil
// account, err := c.db.GetAccount(ctx, req.AccountId)
// if err != nil {
// return nil, err
// }

// // TODO: support multiple signing keys
// if len(account.Operator.SigningKeys) < 1 {
// return nil, fmt.Errorf("operator %s has no signing keys", account.Operator.KeyID)
// }

// osk, err := nkeys.FromSeed(account.Operator.SigningKeys[0].Seed)
// if err != nil {
// return nil, err
// }

// ac, err := jwt.DecodeAccountClaims(account.Token.Token)
// if err != nil {
// return nil, err
// }
// ac.Exports = make([]*jwt.Export, 0)

// if len(*req.Body.Claims.Exports) > 0 {
// for _, e := range *req.Body.Claims.Exports {
// export := &jwt.Export{
// Name: utils.PtrStr(e.Name),
// Subject: jwt.Subject(utils.PtrStr(e.Subject)),
// Type: jwt.ExportType(*e.Type),
// ResponseType: jwt.ResponseType(*e.ResponseType),
// AccountTokenPosition: *e.AccountTokenPosition,
// Info: jwt.Info{
// Description: utils.PtrStr(e.Info.Description),
// InfoURL: utils.PtrStr(e.Info.InfoUrl),
// },
// }

// ac.Exports.Add(export)
// }
// }

// token, err := ac.Encode(osk)
// if err != nil {
// return nil, err
// }
// account.Token.Token = token

// err = c.db.UpdateAccount(ctx, account)
// if err != nil {
// return nil, err
// }

// return account, nil
// ListAccounts ...
func (c *accountsController) ListAccounts(ctx context.Context, input ListAccountsInput) (ListAccountsOutput, error) {
return ListAccountsOutput{}, nil
}

// DeleteToken ...
Expand Down
3 changes: 3 additions & 0 deletions internal/api/models/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (
"gorm.io/gorm"
)

// AccountPagination is the pagination for operators.
type AccountPagination Pagination[Operator]

// Account ...
type Account struct {
// ID is the unique identifier for the account.
Expand Down
12 changes: 0 additions & 12 deletions internal/api/models/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ import (
"time"

"github.com/google/uuid"
openapi "github.com/zeiss/typhoon/pkg/apis"
"gorm.io/gorm"
)

var _ ToAPI[openapi.Cluster] = (*Cluster)(nil)

// Cluster ...
type Cluster struct {
// ID is the unique identifier for the cluster.
Expand All @@ -29,12 +26,3 @@ type Cluster struct {
// DeletedAt is the time the cluster was deleted.
DeletedAt gorm.DeletedAt `json:"deleted_at" gorm:"index"`
}

// ToAPI converts the database model to the API model.
func (c *Cluster) ToAPI() *openapi.Cluster {
return &openapi.Cluster{
Name: c.Name,
Description: &c.Description,
ServerURL: c.ServerURL,
}
}
17 changes: 0 additions & 17 deletions internal/api/models/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"time"

"github.com/google/uuid"
openapi "github.com/zeiss/typhoon/pkg/apis"
"gorm.io/gorm"
)

Expand Down Expand Up @@ -55,19 +54,3 @@ type System struct {
// DeletedAt is the time the system was deleted.
DeletedAt gorm.DeletedAt `json:"deleted_at" gorm:"index"`
}

// FromAPI converts the API model to the database model.
func (s *System) FromAPI(api *openapi.System) {
s.Name = api.Name
s.Description = *api.Description

for _, cluster := range api.Clusters {
c := Cluster{
Name: cluster.Name,
Description: *cluster.Description,
ServerURL: cluster.ServerURL,
}

s.Clusters = append(s.Clusters, c)
}
}
20 changes: 20 additions & 0 deletions internal/api/services/accounts.go
Original file line number Diff line number Diff line change
@@ -1 +1,21 @@
package services

import (
"context"

"github.com/gofiber/fiber/v2"
openapi "github.com/zeiss/typhoon/pkg/apis"
"github.com/zeiss/typhoon/pkg/apis/transformers"
)

// ListAccounts ...
func (a *ApiHandlers) ListAccounts(ctx context.Context, request openapi.ListAccountsRequestObject) (openapi.ListAccountsResponseObject, error) {
input := transformers.FromListAccountRequest(request)

output, err := a.accounts.ListAccounts(ctx, input)
if err != nil {
return nil, fiber.NewError(fiber.StatusInternalServerError, err.Error())
}

return transformers.ToListAccountResponse(output), nil
}
63 changes: 0 additions & 63 deletions internal/api/services/api.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package services

import (
"bytes"
"context"

"github.com/zeiss/typhoon/internal/api/controllers"
Expand Down Expand Up @@ -48,16 +47,6 @@ func (a *ApiHandlers) DeleteSystem(ctx context.Context, req openapi.DeleteSystem
return openapi.DeleteSystem204Response(openapi.DeleteSystem204Response{}), nil
}

// DeleteOperatorAccountToken ...
func (a *ApiHandlers) DeleteOperatorAccountToken(ctx context.Context, req openapi.DeleteOperatorAccountTokenRequestObject) (openapi.DeleteOperatorAccountTokenResponseObject, error) {
err := a.accounts.DeleteToken(ctx, req.AccountId)
if err != nil {
return nil, err
}

return openapi.DeleteOperatorAccountToken204Response(openapi.DeleteOperatorAccountToken204Response{}), nil
}

// CreateOperatorSigningKeyGroup ...
func (a *ApiHandlers) CreateOperatorSigningKeyGroup(ctx context.Context, req openapi.CreateOperatorSigningKeyGroupRequestObject) (openapi.CreateOperatorSigningKeyGroupResponseObject, error) {
key, err := a.operators.CreateOperatorSigningKeyGroup(ctx, req.OperatorId, req.Body.Name, utils.PtrStr(req.Body.Description))
Expand Down Expand Up @@ -96,58 +85,6 @@ func (a *ApiHandlers) CreateOperator(ctx context.Context, req openapi.CreateOper
return openapi.CreateOperator201JSONResponse(openapi.Operator{Id: &operator.ID, Name: operator.Name}), nil
}

// CreateOperatorAccount ...
func (a *ApiHandlers) CreateOperatorAccount(ctx context.Context, req openapi.CreateOperatorAccountRequestObject) (openapi.CreateOperatorAccountResponseObject, error) {
account, err := a.accounts.CreateAccount(ctx, req.Body.Name, req.OperatorId)
if err != nil {
return nil, err
}

resp := openapi.CreateOperatorAccount201JSONResponse(
openapi.Account{
Id: &account.ID,
Name: account.Name,
CreatedAt: &account.CreatedAt,
UpdatedAt: &account.UpdatedAt,
DeletedAt: &account.DeletedAt.Time,
},
)

return openapi.CreateOperatorAccount201JSONResponse(resp), nil
}

// CreateOperatorAccountUser ...
func (a *ApiHandlers) CreateOperatorAccountUser(ctx context.Context, req openapi.CreateOperatorAccountUserRequestObject) (openapi.CreateOperatorAccountUserResponseObject, error) {
user, err := a.users.CreateUser(ctx, req.Body.Name, req.AccountId)
if err != nil {
return nil, err
}

return openapi.CreateOperatorAccountUser201JSONResponse(openapi.User{Id: &user.ID, Name: user.Name}), nil
}

// GetOperatorAccountUserCredentials ...
func (a *ApiHandlers) GetOperatorAccountUserCredentials(ctx context.Context, req openapi.GetOperatorAccountUserCredentialsRequestObject) (openapi.GetOperatorAccountUserCredentialsResponseObject, error) {
credentials, err := a.users.GetCredentials(ctx, req.UserId)
if err != nil {
return nil, err
}

body := bytes.NewReader(credentials)

return openapi.GetOperatorAccountUserCredentials200ApplicationoctetStreamResponse(openapi.GetOperatorAccountUserCredentials200ApplicationoctetStreamResponse{Body: body, ContentLength: int64(body.Len())}), nil
}

// UpdateOperatorAccount ...
func (a *ApiHandlers) UpdateOperatorAccount(ctx context.Context, req openapi.UpdateOperatorAccountRequestObject) (openapi.UpdateOperatorAccountResponseObject, error) {
account, err := a.accounts.UpdateAccount(ctx, controllers.UpdateOperatorAccountRequestObject(req))
if err != nil {
return nil, err
}

return openapi.UpdateOperatorAccount200JSONResponse(openapi.Account{Name: account.Name}), nil
}

// // GetOperator ...
// func (a *ApiHandlers) GetOperator(ctx context.Context, req openapi.GetOperatorRequestObject) (openapi.GetOperatorResponseObject, error) {
// operator, err := a.operators.GetOperator(ctx, req.OperatorId)
Expand Down
15 changes: 0 additions & 15 deletions internal/api/services/systems.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package services

import (
"context"
"net/http"

"github.com/zeiss/typhoon/internal/api/models"
"github.com/zeiss/typhoon/internal/utils"
Expand Down Expand Up @@ -81,17 +80,3 @@ func (a *ApiHandlers) CreateSystem(ctx context.Context, req openapi.CreateSystem

return openapi.CreateSystem201JSONResponse(res), nil
}

// GetSystemOperator ...
func (a *ApiHandlers) GetSystemOperator(ctx context.Context, req openapi.GetSystemOperatorRequestObject) (openapi.GetSystemOperatorResponseObject, error) {
system, err := a.systems.GetSystem(ctx, req.SystemId)
if err != nil {
return nil, err
}

if system.OperatorID == nil {
return openapi.GetSystemOperatordefaultJSONResponse(openapi.GetGroupdefaultJSONResponse{StatusCode: http.StatusNotFound, Body: openapi.ErrorNotFound("could not find an operator")}), nil
}

return openapi.GetSystemOperator200JSONResponse(openapi.Operator{Id: utils.PtrUUID(system.Operator.ID), Name: system.Operator.Name}), nil
}
2 changes: 1 addition & 1 deletion internal/api/services/teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ func (a *ApiHandlers) GetTeam(ctx context.Context, req openapi.GetTeamRequestObj
return openapi.GetTeamdefaultJSONResponse{}, nil
}

return openapi.GetTeam200JSONResponse(openapi.Team{Id: utils.UUIDPtr(team.ID), Name: team.Name, Description: team.Description, CreatedAt: utils.PtrTime(team.CreatedAt), UpdatedAt: utils.PtrTime(team.UpdatedAt), DeletedAt: utils.PtrTime(team.DeletedAt.Time)}), nil
return openapi.GetTeam200JSONResponse(openapi.Team{Id: utils.PtrUUID(team.ID), Name: team.Name, Description: team.Description, CreatedAt: utils.PtrTime(team.CreatedAt), UpdatedAt: utils.PtrTime(team.UpdatedAt), DeletedAt: utils.PtrTime(team.DeletedAt.Time)}), nil
}
15 changes: 12 additions & 3 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ func PtrInt(i int) *int {
return &i
}

// IntPtr returns a pointer to an int.
func IntPtr(i *int) int {
if i == nil {
return 0
}

return *i
}

// PtrUUID returns a pointer to a UUID.
func PtrUUID(u uuid.UUID) *uuid.UUID {
return &u
Expand Down Expand Up @@ -44,7 +53,7 @@ func TimePtr(t *time.Time) time.Time {
return *t
}

// PtrUUID returns a pointer to a UUID.
func UUIDPtr(u uuid.UUID) *uuid.UUID {
return &u
// UUIDPtr returns a pointer to a UUID.
func UUIDPtr(u *uuid.UUID) uuid.UUID {
return *u
}
Loading

0 comments on commit 7f20fd6

Please sign in to comment.