Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions internal/server/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package server

import (
"fmt"
"strings"

exposev1 "github.com/agynio/expose/.gen/go/agynio/api/expose/v1"
"github.com/agynio/expose/internal/store"
Expand All @@ -21,6 +22,9 @@ func parseUUID(value string, field string) (uuid.UUID, error) {
}

func toProtoExposure(exposure store.Exposure) *exposev1.Exposure {
if exposure.Status == store.ExposureStatusActive && !exposureResourcesComplete(exposure) {
panic(fmt.Sprintf("active exposure %s has incomplete OpenZiti resources", exposure.ID))
}
return &exposev1.Exposure{
Meta: &exposev1.EntityMeta{
Id: exposure.ID.String(),
Expand All @@ -38,6 +42,13 @@ func toProtoExposure(exposure store.Exposure) *exposev1.Exposure {
}
}

func exposureResourcesComplete(exposure store.Exposure) bool {
return strings.TrimSpace(exposure.OpenZitiServiceID) != "" &&
strings.TrimSpace(exposure.OpenZitiBindPolicyID) != "" &&
strings.TrimSpace(exposure.OpenZitiDialPolicyID) != "" &&
strings.TrimSpace(exposure.URL) != ""
}

func toProtoExposureStatus(status store.ExposureStatus) exposev1.ExposureStatus {
switch status {
case store.ExposureStatusProvisioning:
Expand Down
24 changes: 24 additions & 0 deletions internal/server/converter_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package server

import (
"fmt"
"strings"
"testing"

Expand Down Expand Up @@ -61,6 +62,29 @@ func TestParseUUID(t *testing.T) {
}
}

func TestToProtoExposurePanicsForActiveWithoutResources(t *testing.T) {
exposure := store.Exposure{
ID: uuid.New(),
WorkloadID: uuid.New(),
AgentID: uuid.New(),
Port: 8080,
Status: store.ExposureStatusActive,
}

defer func() {
recovered := recover()
if recovered == nil {
t.Fatal("expected panic for active exposure with incomplete resources")
}
message := fmt.Sprint(recovered)
if !strings.Contains(message, "incomplete OpenZiti resources") {
t.Fatalf("unexpected panic: %v", recovered)
}
}()

_ = toProtoExposure(exposure)
}

func TestToProtoExposureStatus(t *testing.T) {
tests := []struct {
name string
Expand Down
40 changes: 25 additions & 15 deletions internal/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -695,18 +695,24 @@ func TestAddExposureExplicitClusterAdmin(t *testing.T) {
created = exposure
return nil
}
storeMock.updateExposureProvisioned = func(context.Context, uuid.UUID, store.ExposureResourceIDs) error {
var provisioned store.ExposureResourceIDs
storeMock.updateExposureProvisioned = func(_ context.Context, _ uuid.UUID, resources store.ExposureResourceIDs) error {
provisioned = resources
return nil
}
storeMock.getExposure = func(_ context.Context, id uuid.UUID) (store.Exposure, error) {
return store.Exposure{
ID: id,
WorkloadID: workloadID,
AgentID: agentID,
Port: 8080,
Status: store.ExposureStatusActive,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
ID: id,
WorkloadID: workloadID,
AgentID: agentID,
Port: 8080,
OpenZitiServiceID: provisioned.OpenZitiServiceID,
OpenZitiBindPolicyID: provisioned.OpenZitiBindPolicyID,
OpenZitiDialPolicyID: provisioned.OpenZitiDialPolicyID,
URL: provisioned.URL,
Status: store.ExposureStatusActive,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}, nil
}

Expand Down Expand Up @@ -1229,13 +1235,17 @@ func TestListExposuresSuccess(t *testing.T) {
listExposuresByWorkload: func(_ context.Context, id uuid.UUID, size int32, cursor *store.PageCursor) (store.ListResult, error) {
return store.ListResult{
Exposures: []store.Exposure{{
ID: workloadID,
WorkloadID: workloadID,
AgentID: uuid.New(),
Port: 8080,
Status: store.ExposureStatusActive,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
ID: workloadID,
WorkloadID: workloadID,
AgentID: uuid.New(),
Port: 8080,
OpenZitiServiceID: "svc-id",
OpenZitiBindPolicyID: "bind-id",
OpenZitiDialPolicyID: "dial-id",
URL: "http://exposed.ziti:8080",
Status: store.ExposureStatusActive,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}},
NextCursor: &store.PageCursor{AfterID: nextID},
}, nil
Expand Down
Loading