Skip to content

Commit

Permalink
Implementing Galadriel Server Management Handlers API (#150)
Browse files Browse the repository at this point in the history
Implementing Admin Handlers

Signed-off-by: Victor Vieira Barros Leal da Silveira <[email protected]>
  • Loading branch information
Victorblsilveira authored May 16, 2023
1 parent 558818e commit 5ad8753
Show file tree
Hide file tree
Showing 10 changed files with 1,019 additions and 284 deletions.
11 changes: 5 additions & 6 deletions pkg/common/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,26 @@ package http
import (
"errors"
"fmt"
"net/http"

"github.com/labstack/echo/v4"
)

// WriteResponse parses a struct into a json and writes in the response
func WriteResponse(ctx echo.Context, body interface{}) error {
func WriteResponse(ctx echo.Context, code int, body interface{}) error {
if body == nil {
return errors.New("body is required")
}

if err := ctx.JSON(http.StatusOK, body); err != nil {
if err := ctx.JSON(code, body); err != nil {
return fmt.Errorf("failed to write response body: %v", err)
}

return nil
}

// BodylessResponse wraps error echo body-less responses.
func BodylessResponse(ctx echo.Context) error {
if err := ctx.NoContent(http.StatusOK); err != nil {
// BodilessResponse wraps error echo body-less responses.
func BodilessResponse(ctx echo.Context, code int) error {
if err := ctx.NoContent(code); err != nil {
return fmt.Errorf("failed to respond without body: %v", err)
}

Expand Down
13 changes: 7 additions & 6 deletions pkg/common/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,22 @@ func Setup() *HTTPSetup {
func TestWriteResponse(t *testing.T) {
t.Run("Error when nil body is passed", func(t *testing.T) {
setup := Setup()
err := WriteResponse(setup.EchoContext, nil)
err := WriteResponse(setup.EchoContext, http.StatusOK, nil)
assert.EqualError(t, err, "body is required")
assert.Empty(t, setup.Recorder.Body)
})

t.Run("No error when an empty body is passed", func(t *testing.T) {
setup := Setup()
err := WriteResponse(setup.EchoContext, TestBody{})
err := WriteResponse(setup.EchoContext, http.StatusOK, TestBody{})
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, setup.Recorder.Code)
})

t.Run("Ensuring that the body is being full filled with the entity", func(t *testing.T) {
expectedResponseBody := TestBody{Name: "teste"}
setup := Setup()
err := WriteResponse(setup.EchoContext, expectedResponseBody)
err := WriteResponse(setup.EchoContext, http.StatusOK, expectedResponseBody)
assert.NoError(t, err)

responseBody := TestBody{}
Expand All @@ -60,10 +61,10 @@ func TestWriteResponse(t *testing.T) {
})
}

func TestBodylessResponse(t *testing.T) {
func TestBodilessResponse(t *testing.T) {
t.Run("Ensuring that the body is empty", func(t *testing.T) {
setup := Setup()
err := BodylessResponse(setup.EchoContext)
err := BodilessResponse(setup.EchoContext, http.StatusOK)
assert.NoError(t, err)

assert.NoError(t, err)
Expand All @@ -75,7 +76,7 @@ func TestBodylessResponse(t *testing.T) {
func TestFromBody(t *testing.T) {
t.Run("Ensuring that the body is empty", func(t *testing.T) {
setup := Setup()
err := BodylessResponse(setup.EchoContext)
err := BodilessResponse(setup.EchoContext, http.StatusOK)
assert.NoError(t, err)

assert.NoError(t, err)
Expand Down
294 changes: 142 additions & 152 deletions pkg/server/api/admin/admin.gen.go

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions pkg/server/api/admin/admin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ tags:
paths:
/trust-domain/{trustDomainName}:
get:
operationId: GetTrustDomainByName
tags:
- Trust Domain
summary: Get a specific trust domain
Expand All @@ -43,6 +44,7 @@ paths:
default:
$ref: '#/components/responses/Default'
put:
operationId: PutTrustDomainByName
tags:
- Trust Domain
summary: Update a specific trust domain
Expand Down Expand Up @@ -70,6 +72,7 @@ paths:

/trust-domain:
put:
operationId: PutTrustDomain
tags:
- Trust Domain
summary: Add a specific trust domain
Expand All @@ -90,15 +93,15 @@ paths:

/relationships:
get:
operationId: GetRelationships
tags:
- Relationships
summary: Get relationships
parameters:
- name: status
in: query
schema:
type: string
enum: [approved, denied, pending]
$ref: '../../../common/api/schemas.yaml#/components/schemas/ConsentStatus'
description: relationship status from a Trust Domain perspective,
- name: trustDomainName
in: query
Expand All @@ -117,6 +120,7 @@ paths:
default:
$ref: '#/components/responses/Default'
put:
operationId: PutRelationship
tags:
- Relationships
summary: Create a relationship request between two Trust Domains
Expand All @@ -138,6 +142,7 @@ paths:

/relationships/{relationshipID}:
get:
operationId: GetRelationshipByID
tags:
- Relationships
summary: Get a specific relationship
Expand Down
26 changes: 18 additions & 8 deletions pkg/server/datastore/fakedatabase.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,15 @@ func (db *FakeDatabase) CreateOrUpdateTrustDomain(ctx context.Context, req *enti
return nil, err
}

req.ID = uuid.NullUUID{
UUID: uuid.New(),
Valid: true,
if !req.ID.Valid {
req.ID = uuid.NullUUID{
UUID: uuid.New(),
Valid: true,
}
req.CreatedAt = time.Now()
}

req.CreatedAt = time.Now()
req.UpdatedAt = time.Now()

db.trustDomains[req.ID.UUID] = req

return req, nil
Expand Down Expand Up @@ -174,9 +175,7 @@ func (db *FakeDatabase) CreateOrUpdateBundle(ctx context.Context, req *entity.Bu
}
}

req.CreatedAt = time.Now()
req.UpdatedAt = time.Now()

db.bundles[req.ID.UUID] = req

return req, nil
Expand Down Expand Up @@ -390,7 +389,18 @@ func (db *FakeDatabase) CreateOrUpdateRelationship(ctx context.Context, req *ent
return nil, err
}

return nil, nil
if !req.ID.Valid {
req.ID = uuid.NullUUID{
Valid: true,
UUID: uuid.New(),
}
req.CreatedAt = time.Now()
}

req.UpdatedAt = time.Now()
db.relationships[req.ID.UUID] = req

return req, nil
}

func (db *FakeDatabase) FindRelationshipByID(ctx context.Context, relationshipID uuid.UUID) (*entity.Relationship, error) {
Expand Down
2 changes: 0 additions & 2 deletions pkg/server/endpoints/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import (
"github.com/stretchr/testify/require"
)

const testTrustDomain = "test.com"

type AuthNTestSetup struct {
EchoCtx echo.Context
Middleware *AuthenticationMiddleware
Expand Down
12 changes: 6 additions & 6 deletions pkg/server/endpoints/harvester.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (h *HarvesterAPIHandlers) GetRelationships(echoCtx echo.Context, params har
apiRelationships = append(apiRelationships, api.RelationshipFromEntity(r))
}

return chttp.WriteResponse(echoCtx, apiRelationships)
return chttp.WriteResponse(echoCtx, http.StatusOK, apiRelationships)
}

// PatchRelationship accept/denies relationships requests - (PATCH /relationships/{relationshipID})
Expand Down Expand Up @@ -145,7 +145,7 @@ func (h *HarvesterAPIHandlers) PatchRelationship(echoCtx echo.Context, relations
return h.handleErrorAndLog(err, msg, http.StatusInternalServerError)
}

if err = chttp.BodylessResponse(echoCtx); err != nil {
if err = chttp.BodilessResponse(echoCtx, http.StatusOK); err != nil {
return h.handleErrorAndLog(err, err.Error(), http.StatusInternalServerError)
}

Expand Down Expand Up @@ -215,7 +215,7 @@ func (h *HarvesterAPIHandlers) Onboard(echoCtx echo.Context, params harvester.On
return h.handleErrorAndLog(err, msg, http.StatusInternalServerError)
}

return chttp.WriteResponse(echoCtx, jwtToken)
return chttp.WriteResponse(echoCtx, http.StatusOK, jwtToken)
}

// GetNewJWTToken renews a JWT access token - (GET /trust-domain/jwt)
Expand Down Expand Up @@ -255,7 +255,7 @@ func (h *HarvesterAPIHandlers) GetNewJWTToken(echoCtx echo.Context) error {
return h.handleErrorAndLog(err, msg, http.StatusInternalServerError)
}

return chttp.WriteResponse(echoCtx, newToken)
return chttp.WriteResponse(echoCtx, http.StatusOK, newToken)
}

// BundleSync synchronize the status of trust bundles between server and harvester - (POST /trust-domain/{trustDomainName}/bundles/sync)
Expand Down Expand Up @@ -294,7 +294,7 @@ func (h *HarvesterAPIHandlers) BundleSync(echoCtx echo.Context, trustDomainName
return h.handleErrorAndLog(err, msg, http.StatusInternalServerError)
}

return chttp.WriteResponse(echoCtx, resp)
return chttp.WriteResponse(echoCtx, http.StatusOK, resp)
}

// BundlePut uploads a new trust bundle to the server - (PUT /trust-domain/{trustDomainName}/bundles)
Expand Down Expand Up @@ -351,7 +351,7 @@ func (h *HarvesterAPIHandlers) BundlePut(echoCtx echo.Context, trustDomainName a
return h.handleErrorAndLog(err, msg, http.StatusInternalServerError)
}

if err = chttp.BodylessResponse(echoCtx); err != nil {
if err = chttp.BodilessResponse(echoCtx, http.StatusOK); err != nil {
return h.handleErrorAndLog(err, err.Error(), http.StatusInternalServerError)
}

Expand Down
Loading

0 comments on commit 5ad8753

Please sign in to comment.