Skip to content

Commit

Permalink
wip: create operator
Browse files Browse the repository at this point in the history
  • Loading branch information
katallaxie authored Apr 23, 2024
1 parent 070ea0f commit c6ee763
Show file tree
Hide file tree
Showing 12 changed files with 185 additions and 533 deletions.
74 changes: 22 additions & 52 deletions api/api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,53 +48,6 @@ paths:
schema:
$ref: "#/components/schemas/Version"

/keys:
get:
summary: List all NKeys
operationId: listKeys
tags:
- NKey
produces:
- "application/json"
parameters:
- $ref: "#/components/parameters/offsetParam"
- $ref: "#/components/parameters/limitParam"
security:
- bearerAuth: ["read:keys"]
responses:
"200":
description: Successfull response
content:
application/json:
schema:
allOf:
- $ref: "#/components/schemas/PaginatedResult"
- type: object
properties:
results:
type: array
items:
$ref: "#/components/schemas/KeyPair"
post:
summary: Creates a new NKey
operationId: createKey
tags:
- NKey
produces:
- "application/json"
responses:
"201":
description: Created
content:
application/json:
schema:
$ref: "#/components/schemas/KeyPair"
links:
GetKeyById:
operationId: getKey
parameters:
keyId: "$response.body#/id"

/operators:
get:
summary: List all operators
Expand Down Expand Up @@ -130,10 +83,7 @@ paths:
produces:
- "application/json"
requestBody:
content:
application/json:
schema:
$ref: "#/components/schemas/Operator"
$ref: "#/components/requestBodies/CreateOperator"
responses:
"201":
description: Created
Expand Down Expand Up @@ -652,6 +602,20 @@ components:
application/json:
schema:
$ref: "#/components/schemas/Team"
CreateOperator:
content:
application/json:
schema:
type: object
required:
- name
properties:
name:
type: string
description:
type: string
contactEmail:
type: string

securitySchemes:
cookieAuth:
Expand Down Expand Up @@ -745,10 +709,16 @@ components:
properties:
id:
type: string
format: uuid
format: base32
readOnly: true
name:
type: string
key:
$ref: "#/components/schemas/KeyPair"
signingKeys:
type: array
items:
$ref: "#/components/schemas/KeyPair"
description:
type: string
contactEmail:
Expand Down
3 changes: 2 additions & 1 deletion cmd/api/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,9 @@ func (s *WebSrv) Start(ctx context.Context, ready server.ReadyFunc, run server.R
tc := controllers.NewTeamsController(db)
sc := controllers.NewSystemsController(db)
vc := controllers.NewVersionController(build)
oc := controllers.NewOperatorsController(db)

handlers := services.NewApiHandlers(sc, tc, vc)
handlers := services.NewApiHandlers(sc, tc, vc, oc)

handler := openapi.NewStrictHandler(handlers, nil)
openapi.RegisterHandlers(app, handler)
Expand Down
20 changes: 18 additions & 2 deletions internal/api/adapters/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (
)

var (
_ ports.Teams = (*DB)(nil)
_ ports.Systems = (*DB)(nil)
_ ports.Teams = (*DB)(nil)
_ ports.Systems = (*DB)(nil)
_ ports.Operators = (*DB)(nil)
)

// DB ...
Expand All @@ -30,3 +31,18 @@ func (db *DB) RunMigrations() error {
&models.System{},
)
}

// GetOperator ...
func (db *DB) GetOperator(id string) (*models.Operator, error) {
operator := &models.Operator{}
if err := db.conn.Where("id = ?", id).First(operator).Error; err != nil {
return nil, err
}

return operator, nil
}

// CreateOperator ...
func (db *DB) CreateOperator(operator *models.Operator) error {
return db.conn.Create(operator).Error
}
50 changes: 50 additions & 0 deletions internal/api/controllers/operators.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package controllers

import (
"github.com/nats-io/nkeys"
"github.com/zeiss/typhoon/internal/api/models"
"github.com/zeiss/typhoon/internal/api/ports"
)

// OperatorsController ...
type OperatorsController struct {
db ports.Operators
}

// NewOperatorsController ...
func NewOperatorsController(db ports.Operators) *OperatorsController {
return &OperatorsController{db}
}

// CreateOperator ...
func (c *OperatorsController) CreateOperator(name string) (*models.Operator, error) {
key, err := nkeys.CreateOperator()
if err != nil {
return nil, err
}

id, err := key.PublicKey()
if err != nil {
return nil, err
}

seed, err := key.Seed()
if err != nil {
return nil, err
}

op := &models.Operator{
ID: id,
Key: models.NKey{
ID: id,
Seed: seed,
},
}

err = c.db.CreateOperator(op)
if err != nil {
return nil, err
}

return op, nil
}
5 changes: 2 additions & 3 deletions internal/api/models/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ package models
import (
"time"

"github.com/google/uuid"
"gorm.io/gorm"
)

// Operator ...
type Operator struct {
ID uuid.UUID `json:"id" gorm:"type:uuid;default:gen_random_uuid()"`
Name string `json:"name"`
ID string `json:"id" gorm:"primaryKey"`
Name string `json:"name"`

// Key is the issuer key identifier.
Key NKey `json:"key"`
Expand Down
11 changes: 11 additions & 0 deletions internal/api/ports/operators.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ports

import "github.com/zeiss/typhoon/internal/api/models"

// Operators is the interface that wraps the methods to access data.
type Operators interface {
// GetOperator returns the operator with the given ID.
GetOperator(id string) (*models.Operator, error)
// CreateOperator creates a new operator.
CreateOperator(operator *models.Operator) error
}
4 changes: 2 additions & 2 deletions internal/api/ports/repos.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package ports

// Repositories is the interface that wraps the methods to access data.
type Repositories interface {
Teams
Systems
Build
Systems
Teams
}
21 changes: 16 additions & 5 deletions internal/api/services/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,26 @@ var _ openapi.StrictServerInterface = (*ApiHandlers)(nil)

// ApiHandlers ...
type ApiHandlers struct {
systems *controllers.SystemsController
teams *controllers.TeamsController
version *controllers.VersionController
systems *controllers.SystemsController
teams *controllers.TeamsController
version *controllers.VersionController
operators *controllers.OperatorsController
openapi.Unimplemented
}

// NewApiHandlers ...
func NewApiHandlers(systems *controllers.SystemsController, teams *controllers.TeamsController, version *controllers.VersionController) *ApiHandlers {
return &ApiHandlers{systems: systems, teams: teams, version: version}
func NewApiHandlers(systems *controllers.SystemsController, teams *controllers.TeamsController, version *controllers.VersionController, operators *controllers.OperatorsController) *ApiHandlers {
return &ApiHandlers{systems: systems, teams: teams, version: version, operators: operators}
}

// CreateOperator ...
func (a *ApiHandlers) CreateOperator(ctx context.Context, req openapi.CreateOperatorRequestObject) (openapi.CreateOperatorResponseObject, error) {
operator, err := a.operators.CreateOperator(req.Body.Name)
if err != nil {
return nil, err
}

return openapi.CreateOperator201JSONResponse(openapi.Operator{Id: &operator.ID, Name: operator.Name}), nil
}

// Version ...
Expand Down
10 changes: 0 additions & 10 deletions pkg/apis/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,6 @@ type Unimplemented struct{}

var _ StrictServerInterface = (*Unimplemented)(nil)

// ListKeys ...
func (u *Unimplemented) ListKeys(ctx context.Context, request ListKeysRequestObject) (ListKeysResponseObject, error) {
return nil, errors.New("not implemented")
}

// CreateKey ...
func (u *Unimplemented) CreateKey(ctx context.Context, request CreateKeyRequestObject) (CreateKeyResponseObject, error) {
return nil, errors.New("not implemented")
}

// CreateOperator ...
func (u *Unimplemented) CreateOperator(ctx context.Context, request CreateOperatorRequestObject) (CreateOperatorResponseObject, error) {
return nil, errors.New("not implemented")
Expand Down
Loading

0 comments on commit c6ee763

Please sign in to comment.