Skip to content

Commit

Permalink
wip: creating system and associate operator
Browse files Browse the repository at this point in the history
  • Loading branch information
katallaxie authored Apr 29, 2024
1 parent bc720fb commit 9c77529
Show file tree
Hide file tree
Showing 13 changed files with 1,177 additions and 120 deletions.
107 changes: 78 additions & 29 deletions api/api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -809,10 +809,7 @@ paths:
produces:
- "application/json"
requestBody:
content:
application/json:
schema:
$ref: "#/components/schemas/System"
$ref: "#/components/requestBodies/CreateSystem"
responses:
"201":
description: Created
Expand All @@ -828,32 +825,22 @@ paths:

/systems/{systemId}:
get:
summary: List all managed systems.
summary: Gets a system by ID
operationId: getSystem
tags:
- Systems
description: |-
Returns a list of managed systems.
parameters:
- name: systemId
in: path
required: true
description: The id of the system to retrieve
schema:
type: string
- $ref: "#/components/parameters/systemId"
responses:
"200":
description: A paginated response of all systems
description: Successful
content:
application/json:
schema:
$ref: "#/components/schemas/System"
put:
summary: Updates a system by ID
operationId: updateSystem
parameters:
- name: systemId
in: path
required: true
description: The id of the system to update
schema:
type: string
- $ref: "#/components/parameters/systemId"
requestBody:
content:
application/json:
Expand All @@ -867,16 +854,47 @@ paths:
schema:
$ref: "#/components/schemas/System"

deleted:
delete:
summary: Deletes a system by ID
operationId: deleteSystem
parameters:
- name: systemId
in: path
required: true
description: The id of the system to delete
schema:
type: string
- $ref: "#/components/parameters/systemId"
responses:
"204":
description: Successful

/systems/{systemId}/operator:
get:
summary: Gets the operator for a system
operationId: getSystemOperator
parameters:
- $ref: "#/components/parameters/systemId"
responses:
"200":
description: Successful
content:
application/json:
schema:
$ref: "#/components/schemas/Operator"
put:
summary: Updates the operator for a system
operationId: updateSystemOperator
parameters:
- $ref: "#/components/parameters/systemId"
requestBody:
$ref: "#/components/requestBodies/UpdateSystemOperator"
responses:
"200":
description: Successful
content:
application/json:
schema:
$ref: "#/components/schemas/Operator"
delete:
summary: Deletes the operator for a system
operationId: deleteSystemOperator
parameters:
- $ref: "#/components/parameters/systemId"
responses:
"204":
description: Successful
Expand All @@ -893,6 +911,14 @@ definitions:

components:
parameters:
systemId:
in: path
name: systemId
required: true
schema:
type: string
format: uuid
description: The id of the system to retrieve.
teamId:
in: path
name: teamId
Expand Down Expand Up @@ -954,6 +980,29 @@ components:
description: The id of the operator to retrieve.

requestBodies:
CreateSystem:
content:
application/json:
schema:
type: object
required:
- name
properties:
name:
type: string
description:
type: string
UpdateSystemOperator:
content:
application/json:
schema:
type: object
required:
- operatorId
properties:
operatorId:
type: string
format: uuid
CreateTeam:
content:
application/json:
Expand Down
2 changes: 2 additions & 0 deletions internal/api/adapters/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var (
_ ports.Users = (*DB)(nil)
_ ports.Accounts = (*DB)(nil)
_ ports.Operators = (*DB)(nil)
_ ports.Systems = (*DB)(nil)
)

// DB ...
Expand All @@ -29,5 +30,6 @@ func (db *DB) RunMigrations() error {
&models.User{},
&models.Account{},
&models.Operator{},
&models.System{},
)
}
50 changes: 50 additions & 0 deletions internal/api/adapters/db/systems.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package db

import (
"context"

"github.com/google/uuid"
"github.com/zeiss/typhoon/internal/api/models"
)

// GetSystem ...
func (db *DB) GetSystem(ctx context.Context, id uuid.UUID) (*models.System, error) {
system := &models.System{}
if err := db.conn.Where("id = ?", id).Preload("Operator").First(system).Error; err != nil {
return nil, err
}

return system, nil
}

// CreateSystem ...
func (db *DB) CreateSystem(ctx context.Context, system *models.System) error {
return db.conn.WithContext(ctx).Create(system).Error
}

// ListSystems ...
func (db *DB) ListSystems(ctx context.Context, pagination models.Pagination[models.System]) (models.Pagination[models.System], error) {
systems := []models.System{}

err := db.conn.WithContext(ctx).Scopes(models.Paginate(&systems, &pagination, db.conn)).Limit(pagination.Limit).Offset(pagination.Offset).Find(&systems).Error
if err != nil {
return pagination, err
}
pagination.Rows = systems

return pagination, nil
}

// DeleteSystem ...
func (db *DB) DeleteSystem(ctx context.Context, id uuid.UUID) error {
return db.conn.WithContext(ctx).Delete(&models.System{}, id).Error
}

// UpdateSystem ...
func (db *DB) UpdateSystem(ctx context.Context, system *models.System) (*models.System, error) {
if err := db.conn.WithContext(ctx).Save(system).Error; err != nil {
return nil, err
}

return system, nil
}
51 changes: 49 additions & 2 deletions internal/api/controllers/systems.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,60 @@
package controllers

import "github.com/zeiss/typhoon/internal/api/ports"
import (
"context"

"github.com/google/uuid"
"github.com/zeiss/typhoon/internal/api/models"
"github.com/zeiss/typhoon/internal/api/ports"
)

// SystemsController ...
type SystemsController struct {
db ports.Systems
}

// NewSystemsController ...
func NewSystemsController(db ports.Teams) *SystemsController {
func NewSystemsController(db ports.Systems) *SystemsController {
return &SystemsController{db}
}

// CreateSystem ...
func (c *SystemsController) CreateSystem(ctx context.Context, name, description string) (*models.System, error) {
system := &models.System{
Name: name,
Description: description,
}

if err := c.db.CreateSystem(ctx, system); err != nil {
return nil, err
}

return system, nil
}

// DeleteSystem ...
func (c *SystemsController) DeleteSystem(ctx context.Context, id uuid.UUID) error {
return c.db.DeleteSystem(ctx, id)
}

// GetSystem ...
func (c *SystemsController) GetSystem(ctx context.Context, id uuid.UUID) (*models.System, error) {
return c.db.GetSystem(ctx, id)
}

// ListSystems ...
func (c *SystemsController) ListSystems(ctx context.Context, pagination models.Pagination[models.System]) (models.Pagination[models.System], error) {
return c.db.ListSystems(ctx, pagination)
}

// UpdateSystemOperator ...
func (c *SystemsController) UpdateSystemOperator(ctx context.Context, systemId, operatorID uuid.UUID) (*models.System, error) {
system, err := c.db.GetSystem(ctx, systemId)
if err != nil {
return nil, err
}

system.OperatorID = &operatorID

return c.db.UpdateSystem(ctx, system)
}
3 changes: 3 additions & 0 deletions internal/api/models/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ type Operator struct {
Token Token `json:"token" gorm:"foreignKey:TokenID"`
TokenID string `json:"token_id"`

// Systems is the list of systems that the operator has.
Systems []System `json:"systems" gorm:"many2many:operator_systems;foreignKey:ID;joinForeignKey:OperatorID;joinReferences:SystemID"`

// Accounts is the list of accounts that the operator has.
SigningKeys []NKey `json:"signing_keys" gorm:"many2many:operator_signing_keys;foreignKey:ID;joinForeignKey:OperatorID;joinReferences:SigningKeyID"`

Expand Down
12 changes: 3 additions & 9 deletions internal/api/models/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,9 @@ type System struct {
Name string `json:"name" gorm:"unique"`
Description string `json:"description"`

CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `json:"deleted_at" gorm:"index"`
}

// Cluster ...
type Cluster struct {
ID uuid.UUID `json:"id" gorm:"type:uuid;default:gen_random_uuid()"`
URL string `json:"url"`
// Operator is the operator that the system belongs to.
Operator Operator `json:"operator" gorm:"foreignKey:OperatorID"`
OperatorID *uuid.UUID `json:"operator_id"`

CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Expand Down
20 changes: 19 additions & 1 deletion internal/api/ports/systems.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
package ports

import (
"context"

"github.com/google/uuid"
"github.com/zeiss/typhoon/internal/api/models"
)

// Systems is the interface that wraps the methods to access data.
type Systems interface{}
type Systems interface {
// CreateSystem creates a new system.
CreateSystem(ctx context.Context, system *models.System) error
// UpdateSystem updates a system.
UpdateSystem(ctx context.Context, system *models.System) (*models.System, error)
// GetSystem retrieves a system by its ID.
GetSystem(ctx context.Context, id uuid.UUID) (*models.System, error)
// ListSystems retrieves all systems.
ListSystems(ctx context.Context, pagination models.Pagination[models.System]) (models.Pagination[models.System], error)
// DeleteSystem deletes a system by its ID.
DeleteSystem(ctx context.Context, id uuid.UUID) error
}
Loading

0 comments on commit 9c77529

Please sign in to comment.