Skip to content

Commit

Permalink
wip: adapter state
Browse files Browse the repository at this point in the history
  • Loading branch information
katallaxie authored Jun 18, 2024
1 parent adaa915 commit 0760eb6
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 59 deletions.
8 changes: 8 additions & 0 deletions api/api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ paths:
- $ref: "#/components/parameters/teamId"
- $ref: "#/components/parameters/projectId"
- $ref: "#/components/parameters/environmentId"
- $ref: "#/components/parameters/lockId"
security:
- basic_auth: [] # Use basic auth to update the environment state
requestBody:
Expand Down Expand Up @@ -1110,6 +1111,13 @@ components:
required: true
schema:
type: string
lockId:
name: ID
in: query
required: false
schema:
type: string
format: uuid

schemas:
LockInfo:
Expand Down
5 changes: 5 additions & 0 deletions internal/adapters/database/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ type datastoreTx struct {
tx *gorm.DB
}

// GetLock ...
func (tx *datastoreTx) GetLock(ctx context.Context, lock *models.Lock) error {
return tx.tx.Where(lock).First(lock).Error
}

// CreateLock creates a new lock.
func (tx *datastoreTx) CreateLock(ctx context.Context, lock *models.Lock) error {
return tx.tx.Create(lock).Error
Expand Down
2 changes: 1 addition & 1 deletion internal/adapters/handlers/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ func (a *apiHandlers) UpdateEnvironmentState(ctx context.Context, request openap
return nil, fiber.NewError(fiber.StatusInternalServerError, err.Error())
}

return nil, nil
return dto.ToUpdateEnvironmentStateResponseObject(), nil
}

// Unlock the state of Terraform environment
Expand Down
32 changes: 27 additions & 5 deletions internal/controllers/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import (
"context"
"encoding/json"

"github.com/google/uuid"
"github.com/zeiss/knox/internal/models"
"github.com/zeiss/knox/internal/ports"
openapi "github.com/zeiss/knox/pkg/apis"

"gorm.io/datatypes"
)

Expand All @@ -29,14 +32,16 @@ type UpdateStateControllerCommand struct {
Project string `json:"project" form:"project"`
// Environment is the environment of the lock.
Environment string `json:"environment" form:"environment"`
// LockID is the ID of the lock.
LockID uuid.UUID `json:"lock_id" form:"lock_id"`
// State is the state of the lock.
State *map[string]interface{} `json:"state" form:"state"`
}

// StateController ...
type StateController interface {
// GetState ...
GetState(ctx context.Context, query GetStateControllerQuery) ([]byte, error)
GetState(ctx context.Context, query GetStateControllerQuery) (map[string]interface{}, error)
// UpdateState ...
UpdateState(ctx context.Context, cmd UpdateStateControllerCommand) error
}
Expand All @@ -52,8 +57,8 @@ func NewStateController(store ports.Datastore) *StateControllerImpl {
}

// GetState ...
func (c *StateControllerImpl) GetState(ctx context.Context, query GetStateControllerQuery) ([]byte, error) {
var data []byte
func (c *StateControllerImpl) GetState(ctx context.Context, query GetStateControllerQuery) (map[string]interface{}, error) {
var data map[string]interface{}

team := models.Team{
Slug: query.Team,
Expand Down Expand Up @@ -102,16 +107,33 @@ func (c *StateControllerImpl) GetState(ctx context.Context, query GetStateContro
return data, err
}

return state.Data, nil
var payload openapi.Payload
err = json.Unmarshal(state.Data, &payload)
if err != nil {
return data, err
}

return payload, nil
}

// UpdateState ...
func (c *StateControllerImpl) UpdateState(ctx context.Context, cmd UpdateStateControllerCommand) error {
lock := models.Lock{
ID: cmd.LockID,
}

err := c.store.ReadTx(ctx, func(ctx context.Context, tx ports.ReadTx) error {
return tx.GetLock(ctx, &lock)
})
if err != nil {
return err
}

team := models.Team{
Slug: cmd.Team,
}

err := c.store.ReadTx(ctx, func(ctx context.Context, tx ports.ReadTx) error {
err = c.store.ReadTx(ctx, func(ctx context.Context, tx ports.ReadTx) error {
return tx.GetTeam(ctx, &team)
})
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions internal/ports/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ type ReadTx interface {
GetTeam(context.Context, *models.Team) error
// GetState ...
GetState(context.Context, *models.State) error
// GetLock ...
GetLock(context.Context, *models.Lock) error
}

// ReadWriteTx provides methods for transactional read and write operations.
Expand Down
52 changes: 37 additions & 15 deletions pkg/apis/client.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

74 changes: 41 additions & 33 deletions pkg/apis/models.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 0760eb6

Please # to comment.