mirror of
https://github.com/shankar0123/certctl.git
synced 2026-06-07 20:31:30 +00:00
M-2 PR-B: Collapse IssuerService + TargetService to ctx-first signatures
- Delete bare TestConnection wrapper in IssuerService; rename
TestConnectionWithContext → TestConnection
- Delete TestTargetConnection delegate shim in TargetService (canonical
TestConnection already ctx-first)
- Add ctx first param to 10 handler-interface methods
(ListIssuers/GetIssuer/CreateIssuer/UpdateIssuer/DeleteIssuer and
ListTargets/GetTarget/CreateTarget/UpdateTarget/DeleteTarget)
- Replace 16 context.Background() call sites with received ctx
- Thread r.Context() through 12 HTTP handler sites in issuers.go and
targets.go (outer TargetHandler.TestTargetConnection HTTP method name
preserved for router compatibility)
- Update MockIssuerService, MockTargetService, and mockTargetService
(integration) for ctx-first forwarding; update test callsite literals
Audit complete. Commit: 1f6cf0eafa. Sections: 12. Findings: 2/7/10/4/6.
This commit is contained in:
@@ -2,6 +2,7 @@ package handler
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
@@ -15,52 +16,52 @@ import (
|
||||
|
||||
// MockIssuerService is a mock implementation of IssuerService interface.
|
||||
type MockIssuerService struct {
|
||||
ListIssuersFn func(page, perPage int) ([]domain.Issuer, int64, error)
|
||||
GetIssuerFn func(id string) (*domain.Issuer, error)
|
||||
CreateIssuerFn func(issuer domain.Issuer) (*domain.Issuer, error)
|
||||
UpdateIssuerFn func(id string, issuer domain.Issuer) (*domain.Issuer, error)
|
||||
DeleteIssuerFn func(id string) error
|
||||
TestConnectionFn func(id string) error
|
||||
ListIssuersFn func(ctx context.Context, page, perPage int) ([]domain.Issuer, int64, error)
|
||||
GetIssuerFn func(ctx context.Context, id string) (*domain.Issuer, error)
|
||||
CreateIssuerFn func(ctx context.Context, issuer domain.Issuer) (*domain.Issuer, error)
|
||||
UpdateIssuerFn func(ctx context.Context, id string, issuer domain.Issuer) (*domain.Issuer, error)
|
||||
DeleteIssuerFn func(ctx context.Context, id string) error
|
||||
TestConnectionFn func(ctx context.Context, id string) error
|
||||
}
|
||||
|
||||
func (m *MockIssuerService) ListIssuers(page, perPage int) ([]domain.Issuer, int64, error) {
|
||||
func (m *MockIssuerService) ListIssuers(ctx context.Context, page, perPage int) ([]domain.Issuer, int64, error) {
|
||||
if m.ListIssuersFn != nil {
|
||||
return m.ListIssuersFn(page, perPage)
|
||||
return m.ListIssuersFn(ctx, page, perPage)
|
||||
}
|
||||
return nil, 0, nil
|
||||
}
|
||||
|
||||
func (m *MockIssuerService) GetIssuer(id string) (*domain.Issuer, error) {
|
||||
func (m *MockIssuerService) GetIssuer(ctx context.Context, id string) (*domain.Issuer, error) {
|
||||
if m.GetIssuerFn != nil {
|
||||
return m.GetIssuerFn(id)
|
||||
return m.GetIssuerFn(ctx, id)
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (m *MockIssuerService) CreateIssuer(issuer domain.Issuer) (*domain.Issuer, error) {
|
||||
func (m *MockIssuerService) CreateIssuer(ctx context.Context, issuer domain.Issuer) (*domain.Issuer, error) {
|
||||
if m.CreateIssuerFn != nil {
|
||||
return m.CreateIssuerFn(issuer)
|
||||
return m.CreateIssuerFn(ctx, issuer)
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (m *MockIssuerService) UpdateIssuer(id string, issuer domain.Issuer) (*domain.Issuer, error) {
|
||||
func (m *MockIssuerService) UpdateIssuer(ctx context.Context, id string, issuer domain.Issuer) (*domain.Issuer, error) {
|
||||
if m.UpdateIssuerFn != nil {
|
||||
return m.UpdateIssuerFn(id, issuer)
|
||||
return m.UpdateIssuerFn(ctx, id, issuer)
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (m *MockIssuerService) DeleteIssuer(id string) error {
|
||||
func (m *MockIssuerService) DeleteIssuer(ctx context.Context, id string) error {
|
||||
if m.DeleteIssuerFn != nil {
|
||||
return m.DeleteIssuerFn(id)
|
||||
return m.DeleteIssuerFn(ctx, id)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MockIssuerService) TestConnection(id string) error {
|
||||
func (m *MockIssuerService) TestConnection(ctx context.Context, id string) error {
|
||||
if m.TestConnectionFn != nil {
|
||||
return m.TestConnectionFn(id)
|
||||
return m.TestConnectionFn(ctx, id)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -85,7 +86,7 @@ func TestListIssuers_Success(t *testing.T) {
|
||||
}
|
||||
|
||||
mock := &MockIssuerService{
|
||||
ListIssuersFn: func(page, perPage int) ([]domain.Issuer, int64, error) {
|
||||
ListIssuersFn: func(_ context.Context, page, perPage int) ([]domain.Issuer, int64, error) {
|
||||
return []domain.Issuer{iss1, iss2}, 2, nil
|
||||
},
|
||||
}
|
||||
@@ -113,7 +114,7 @@ func TestListIssuers_Success(t *testing.T) {
|
||||
func TestListIssuers_Pagination(t *testing.T) {
|
||||
var capturedPage, capturedPerPage int
|
||||
mock := &MockIssuerService{
|
||||
ListIssuersFn: func(page, perPage int) ([]domain.Issuer, int64, error) {
|
||||
ListIssuersFn: func(_ context.Context, page, perPage int) ([]domain.Issuer, int64, error) {
|
||||
capturedPage = page
|
||||
capturedPerPage = perPage
|
||||
return []domain.Issuer{}, 0, nil
|
||||
@@ -137,7 +138,7 @@ func TestListIssuers_Pagination(t *testing.T) {
|
||||
|
||||
func TestListIssuers_ServiceError(t *testing.T) {
|
||||
mock := &MockIssuerService{
|
||||
ListIssuersFn: func(page, perPage int) ([]domain.Issuer, int64, error) {
|
||||
ListIssuersFn: func(_ context.Context, page, perPage int) ([]domain.Issuer, int64, error) {
|
||||
return nil, 0, ErrMockServiceFailed
|
||||
},
|
||||
}
|
||||
@@ -169,7 +170,7 @@ func TestListIssuers_MethodNotAllowed(t *testing.T) {
|
||||
func TestGetIssuer_Success(t *testing.T) {
|
||||
now := time.Now()
|
||||
mock := &MockIssuerService{
|
||||
GetIssuerFn: func(id string) (*domain.Issuer, error) {
|
||||
GetIssuerFn: func(_ context.Context, id string) (*domain.Issuer, error) {
|
||||
return &domain.Issuer{
|
||||
ID: id,
|
||||
Name: "Local CA",
|
||||
@@ -195,7 +196,7 @@ func TestGetIssuer_Success(t *testing.T) {
|
||||
|
||||
func TestGetIssuer_NotFound(t *testing.T) {
|
||||
mock := &MockIssuerService{
|
||||
GetIssuerFn: func(id string) (*domain.Issuer, error) {
|
||||
GetIssuerFn: func(_ context.Context, id string) (*domain.Issuer, error) {
|
||||
return nil, ErrMockNotFound
|
||||
},
|
||||
}
|
||||
@@ -228,7 +229,7 @@ func TestGetIssuer_EmptyID(t *testing.T) {
|
||||
func TestCreateIssuer_Success(t *testing.T) {
|
||||
now := time.Now()
|
||||
mock := &MockIssuerService{
|
||||
CreateIssuerFn: func(issuer domain.Issuer) (*domain.Issuer, error) {
|
||||
CreateIssuerFn: func(_ context.Context, issuer domain.Issuer) (*domain.Issuer, error) {
|
||||
issuer.ID = "iss-new"
|
||||
issuer.CreatedAt = now
|
||||
issuer.UpdatedAt = now
|
||||
@@ -328,7 +329,7 @@ func TestCreateIssuer_NameTooLong(t *testing.T) {
|
||||
|
||||
func TestCreateIssuer_DuplicateName(t *testing.T) {
|
||||
mock := &MockIssuerService{
|
||||
CreateIssuerFn: func(issuer domain.Issuer) (*domain.Issuer, error) {
|
||||
CreateIssuerFn: func(_ context.Context, issuer domain.Issuer) (*domain.Issuer, error) {
|
||||
return nil, fmt.Errorf("failed to create issuer: duplicate key value violates unique constraint \"issuers_name_key\"")
|
||||
},
|
||||
}
|
||||
@@ -361,7 +362,7 @@ func TestCreateIssuer_DuplicateName(t *testing.T) {
|
||||
|
||||
func TestCreateIssuer_UnsupportedType(t *testing.T) {
|
||||
mock := &MockIssuerService{
|
||||
CreateIssuerFn: func(issuer domain.Issuer) (*domain.Issuer, error) {
|
||||
CreateIssuerFn: func(_ context.Context, issuer domain.Issuer) (*domain.Issuer, error) {
|
||||
return nil, fmt.Errorf("unsupported issuer type: FakeCA")
|
||||
},
|
||||
}
|
||||
@@ -394,7 +395,7 @@ func TestCreateIssuer_UnsupportedType(t *testing.T) {
|
||||
|
||||
func TestCreateIssuer_GenericServiceError(t *testing.T) {
|
||||
mock := &MockIssuerService{
|
||||
CreateIssuerFn: func(issuer domain.Issuer) (*domain.Issuer, error) {
|
||||
CreateIssuerFn: func(_ context.Context, issuer domain.Issuer) (*domain.Issuer, error) {
|
||||
return nil, fmt.Errorf("failed to encrypt config: cipher error")
|
||||
},
|
||||
}
|
||||
@@ -419,7 +420,7 @@ func TestCreateIssuer_GenericServiceError(t *testing.T) {
|
||||
|
||||
func TestUpdateIssuer_DuplicateName(t *testing.T) {
|
||||
mock := &MockIssuerService{
|
||||
UpdateIssuerFn: func(id string, issuer domain.Issuer) (*domain.Issuer, error) {
|
||||
UpdateIssuerFn: func(_ context.Context, id string, issuer domain.Issuer) (*domain.Issuer, error) {
|
||||
return nil, fmt.Errorf("failed to update issuer: duplicate key value violates unique constraint")
|
||||
},
|
||||
}
|
||||
@@ -445,7 +446,7 @@ func TestUpdateIssuer_DuplicateName(t *testing.T) {
|
||||
func TestDeleteIssuer_Success(t *testing.T) {
|
||||
var deletedID string
|
||||
mock := &MockIssuerService{
|
||||
DeleteIssuerFn: func(id string) error {
|
||||
DeleteIssuerFn: func(_ context.Context, id string) error {
|
||||
deletedID = id
|
||||
return nil
|
||||
},
|
||||
@@ -468,7 +469,7 @@ func TestDeleteIssuer_Success(t *testing.T) {
|
||||
|
||||
func TestDeleteIssuer_ServiceError(t *testing.T) {
|
||||
mock := &MockIssuerService{
|
||||
DeleteIssuerFn: func(id string) error {
|
||||
DeleteIssuerFn: func(_ context.Context, id string) error {
|
||||
return ErrMockServiceFailed
|
||||
},
|
||||
}
|
||||
@@ -487,7 +488,7 @@ func TestDeleteIssuer_ServiceError(t *testing.T) {
|
||||
|
||||
func TestTestConnection_Success(t *testing.T) {
|
||||
mock := &MockIssuerService{
|
||||
TestConnectionFn: func(id string) error {
|
||||
TestConnectionFn: func(_ context.Context, id string) error {
|
||||
return nil
|
||||
},
|
||||
}
|
||||
@@ -514,7 +515,7 @@ func TestTestConnection_Success(t *testing.T) {
|
||||
|
||||
func TestTestConnection_Failure(t *testing.T) {
|
||||
mock := &MockIssuerService{
|
||||
TestConnectionFn: func(id string) error {
|
||||
TestConnectionFn: func(_ context.Context, id string) error {
|
||||
return ErrMockServiceFailed
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
@@ -13,12 +14,12 @@ import (
|
||||
|
||||
// IssuerService defines the service interface for issuer operations.
|
||||
type IssuerService interface {
|
||||
ListIssuers(page, perPage int) ([]domain.Issuer, int64, error)
|
||||
GetIssuer(id string) (*domain.Issuer, error)
|
||||
CreateIssuer(issuer domain.Issuer) (*domain.Issuer, error)
|
||||
UpdateIssuer(id string, issuer domain.Issuer) (*domain.Issuer, error)
|
||||
DeleteIssuer(id string) error
|
||||
TestConnection(id string) error
|
||||
ListIssuers(ctx context.Context, page, perPage int) ([]domain.Issuer, int64, error)
|
||||
GetIssuer(ctx context.Context, id string) (*domain.Issuer, error)
|
||||
CreateIssuer(ctx context.Context, issuer domain.Issuer) (*domain.Issuer, error)
|
||||
UpdateIssuer(ctx context.Context, id string, issuer domain.Issuer) (*domain.Issuer, error)
|
||||
DeleteIssuer(ctx context.Context, id string) error
|
||||
TestConnection(ctx context.Context, id string) error
|
||||
}
|
||||
|
||||
// IssuerHandler handles HTTP requests for issuer operations.
|
||||
@@ -61,7 +62,7 @@ func (h IssuerHandler) ListIssuers(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
issuers, total, err := h.svc.ListIssuers(page, perPage)
|
||||
issuers, total, err := h.svc.ListIssuers(r.Context(), page, perPage)
|
||||
if err != nil {
|
||||
ErrorWithRequestID(w, http.StatusInternalServerError, "Failed to list issuers", requestID)
|
||||
return
|
||||
@@ -93,7 +94,7 @@ func (h IssuerHandler) GetIssuer(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
issuer, err := h.svc.GetIssuer(id)
|
||||
issuer, err := h.svc.GetIssuer(r.Context(), id)
|
||||
if err != nil {
|
||||
ErrorWithRequestID(w, http.StatusNotFound, "Issuer not found", requestID)
|
||||
return
|
||||
@@ -132,7 +133,7 @@ func (h IssuerHandler) CreateIssuer(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
created, err := h.svc.CreateIssuer(issuer)
|
||||
created, err := h.svc.CreateIssuer(r.Context(), issuer)
|
||||
if err != nil {
|
||||
h.logger.Error("failed to create issuer", "error", err, "name", issuer.Name, "type", issuer.Type)
|
||||
errMsg := err.Error()
|
||||
@@ -174,7 +175,7 @@ func (h IssuerHandler) UpdateIssuer(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
updated, err := h.svc.UpdateIssuer(id, issuer)
|
||||
updated, err := h.svc.UpdateIssuer(r.Context(), id, issuer)
|
||||
if err != nil {
|
||||
h.logger.Error("failed to update issuer", "error", err, "id", id)
|
||||
errMsg := err.Error()
|
||||
@@ -208,7 +209,7 @@ func (h IssuerHandler) DeleteIssuer(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.svc.DeleteIssuer(id); err != nil {
|
||||
if err := h.svc.DeleteIssuer(r.Context(), id); err != nil {
|
||||
if strings.Contains(err.Error(), "violates foreign key") || strings.Contains(err.Error(), "RESTRICT") {
|
||||
ErrorWithRequestID(w, http.StatusConflict, "Cannot delete issuer: certificates are still using this issuer", requestID)
|
||||
} else if strings.Contains(err.Error(), "not found") {
|
||||
@@ -241,7 +242,7 @@ func (h IssuerHandler) TestConnection(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
issuerID := parts[0]
|
||||
|
||||
if err := h.svc.TestConnection(issuerID); err != nil {
|
||||
if err := h.svc.TestConnection(r.Context(), issuerID); err != nil {
|
||||
ErrorWithRequestID(w, http.StatusInternalServerError, "Connection test failed", requestID)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package handler
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
@@ -13,52 +14,52 @@ import (
|
||||
|
||||
// MockTargetService is a mock implementation of TargetService interface.
|
||||
type MockTargetService struct {
|
||||
ListTargetsFn func(page, perPage int) ([]domain.DeploymentTarget, int64, error)
|
||||
GetTargetFn func(id string) (*domain.DeploymentTarget, error)
|
||||
CreateTargetFn func(target domain.DeploymentTarget) (*domain.DeploymentTarget, error)
|
||||
UpdateTargetFn func(id string, target domain.DeploymentTarget) (*domain.DeploymentTarget, error)
|
||||
DeleteTargetFn func(id string) error
|
||||
TestTargetConnectionFn func(id string) error
|
||||
ListTargetsFn func(ctx context.Context, page, perPage int) ([]domain.DeploymentTarget, int64, error)
|
||||
GetTargetFn func(ctx context.Context, id string) (*domain.DeploymentTarget, error)
|
||||
CreateTargetFn func(ctx context.Context, target domain.DeploymentTarget) (*domain.DeploymentTarget, error)
|
||||
UpdateTargetFn func(ctx context.Context, id string, target domain.DeploymentTarget) (*domain.DeploymentTarget, error)
|
||||
DeleteTargetFn func(ctx context.Context, id string) error
|
||||
TestConnectionFn func(ctx context.Context, id string) error
|
||||
}
|
||||
|
||||
func (m *MockTargetService) ListTargets(page, perPage int) ([]domain.DeploymentTarget, int64, error) {
|
||||
func (m *MockTargetService) ListTargets(ctx context.Context, page, perPage int) ([]domain.DeploymentTarget, int64, error) {
|
||||
if m.ListTargetsFn != nil {
|
||||
return m.ListTargetsFn(page, perPage)
|
||||
return m.ListTargetsFn(ctx, page, perPage)
|
||||
}
|
||||
return nil, 0, nil
|
||||
}
|
||||
|
||||
func (m *MockTargetService) GetTarget(id string) (*domain.DeploymentTarget, error) {
|
||||
func (m *MockTargetService) GetTarget(ctx context.Context, id string) (*domain.DeploymentTarget, error) {
|
||||
if m.GetTargetFn != nil {
|
||||
return m.GetTargetFn(id)
|
||||
return m.GetTargetFn(ctx, id)
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (m *MockTargetService) CreateTarget(target domain.DeploymentTarget) (*domain.DeploymentTarget, error) {
|
||||
func (m *MockTargetService) CreateTarget(ctx context.Context, target domain.DeploymentTarget) (*domain.DeploymentTarget, error) {
|
||||
if m.CreateTargetFn != nil {
|
||||
return m.CreateTargetFn(target)
|
||||
return m.CreateTargetFn(ctx, target)
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (m *MockTargetService) UpdateTarget(id string, target domain.DeploymentTarget) (*domain.DeploymentTarget, error) {
|
||||
func (m *MockTargetService) UpdateTarget(ctx context.Context, id string, target domain.DeploymentTarget) (*domain.DeploymentTarget, error) {
|
||||
if m.UpdateTargetFn != nil {
|
||||
return m.UpdateTargetFn(id, target)
|
||||
return m.UpdateTargetFn(ctx, id, target)
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (m *MockTargetService) DeleteTarget(id string) error {
|
||||
func (m *MockTargetService) DeleteTarget(ctx context.Context, id string) error {
|
||||
if m.DeleteTargetFn != nil {
|
||||
return m.DeleteTargetFn(id)
|
||||
return m.DeleteTargetFn(ctx, id)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MockTargetService) TestTargetConnection(id string) error {
|
||||
if m.TestTargetConnectionFn != nil {
|
||||
return m.TestTargetConnectionFn(id)
|
||||
func (m *MockTargetService) TestConnection(ctx context.Context, id string) error {
|
||||
if m.TestConnectionFn != nil {
|
||||
return m.TestConnectionFn(ctx, id)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -85,7 +86,7 @@ func TestListTargets_Success(t *testing.T) {
|
||||
}
|
||||
|
||||
mock := &MockTargetService{
|
||||
ListTargetsFn: func(page, perPage int) ([]domain.DeploymentTarget, int64, error) {
|
||||
ListTargetsFn: func(_ context.Context, page, perPage int) ([]domain.DeploymentTarget, int64, error) {
|
||||
return []domain.DeploymentTarget{t1, t2}, 2, nil
|
||||
},
|
||||
}
|
||||
@@ -113,7 +114,7 @@ func TestListTargets_Success(t *testing.T) {
|
||||
func TestListTargets_Pagination(t *testing.T) {
|
||||
var capturedPage, capturedPerPage int
|
||||
mock := &MockTargetService{
|
||||
ListTargetsFn: func(page, perPage int) ([]domain.DeploymentTarget, int64, error) {
|
||||
ListTargetsFn: func(_ context.Context, page, perPage int) ([]domain.DeploymentTarget, int64, error) {
|
||||
capturedPage = page
|
||||
capturedPerPage = perPage
|
||||
return []domain.DeploymentTarget{}, 0, nil
|
||||
@@ -137,7 +138,7 @@ func TestListTargets_Pagination(t *testing.T) {
|
||||
|
||||
func TestListTargets_ServiceError(t *testing.T) {
|
||||
mock := &MockTargetService{
|
||||
ListTargetsFn: func(page, perPage int) ([]domain.DeploymentTarget, int64, error) {
|
||||
ListTargetsFn: func(_ context.Context, page, perPage int) ([]domain.DeploymentTarget, int64, error) {
|
||||
return nil, 0, ErrMockServiceFailed
|
||||
},
|
||||
}
|
||||
@@ -169,7 +170,7 @@ func TestListTargets_MethodNotAllowed(t *testing.T) {
|
||||
func TestGetTarget_Success(t *testing.T) {
|
||||
now := time.Now()
|
||||
mock := &MockTargetService{
|
||||
GetTargetFn: func(id string) (*domain.DeploymentTarget, error) {
|
||||
GetTargetFn: func(_ context.Context, id string) (*domain.DeploymentTarget, error) {
|
||||
return &domain.DeploymentTarget{
|
||||
ID: id,
|
||||
Name: "NGINX Proxy",
|
||||
@@ -196,7 +197,7 @@ func TestGetTarget_Success(t *testing.T) {
|
||||
|
||||
func TestGetTarget_NotFound(t *testing.T) {
|
||||
mock := &MockTargetService{
|
||||
GetTargetFn: func(id string) (*domain.DeploymentTarget, error) {
|
||||
GetTargetFn: func(_ context.Context, id string) (*domain.DeploymentTarget, error) {
|
||||
return nil, ErrMockNotFound
|
||||
},
|
||||
}
|
||||
@@ -229,7 +230,7 @@ func TestGetTarget_EmptyID(t *testing.T) {
|
||||
func TestCreateTarget_Success(t *testing.T) {
|
||||
now := time.Now()
|
||||
mock := &MockTargetService{
|
||||
CreateTargetFn: func(target domain.DeploymentTarget) (*domain.DeploymentTarget, error) {
|
||||
CreateTargetFn: func(_ context.Context, target domain.DeploymentTarget) (*domain.DeploymentTarget, error) {
|
||||
target.ID = "t-new"
|
||||
target.CreatedAt = now
|
||||
target.UpdatedAt = now
|
||||
@@ -342,7 +343,7 @@ func TestCreateTarget_MethodNotAllowed(t *testing.T) {
|
||||
func TestUpdateTarget_Success(t *testing.T) {
|
||||
now := time.Now()
|
||||
mock := &MockTargetService{
|
||||
UpdateTargetFn: func(id string, target domain.DeploymentTarget) (*domain.DeploymentTarget, error) {
|
||||
UpdateTargetFn: func(_ context.Context, id string, target domain.DeploymentTarget) (*domain.DeploymentTarget, error) {
|
||||
return &domain.DeploymentTarget{
|
||||
ID: id,
|
||||
Name: target.Name,
|
||||
@@ -375,7 +376,7 @@ func TestUpdateTarget_Success(t *testing.T) {
|
||||
func TestDeleteTarget_Success(t *testing.T) {
|
||||
var deletedID string
|
||||
mock := &MockTargetService{
|
||||
DeleteTargetFn: func(id string) error {
|
||||
DeleteTargetFn: func(_ context.Context, id string) error {
|
||||
deletedID = id
|
||||
return nil
|
||||
},
|
||||
@@ -398,7 +399,7 @@ func TestDeleteTarget_Success(t *testing.T) {
|
||||
|
||||
func TestDeleteTarget_ServiceError(t *testing.T) {
|
||||
mock := &MockTargetService{
|
||||
DeleteTargetFn: func(id string) error {
|
||||
DeleteTargetFn: func(_ context.Context, id string) error {
|
||||
return ErrMockServiceFailed
|
||||
},
|
||||
}
|
||||
@@ -430,7 +431,7 @@ func TestDeleteTarget_EmptyID(t *testing.T) {
|
||||
|
||||
func TestTestTargetConnection_Success(t *testing.T) {
|
||||
mock := &MockTargetService{
|
||||
TestTargetConnectionFn: func(id string) error {
|
||||
TestConnectionFn: func(_ context.Context, id string) error {
|
||||
return nil
|
||||
},
|
||||
}
|
||||
@@ -457,7 +458,7 @@ func TestTestTargetConnection_Success(t *testing.T) {
|
||||
|
||||
func TestTestTargetConnection_Failed(t *testing.T) {
|
||||
mock := &MockTargetService{
|
||||
TestTargetConnectionFn: func(id string) error {
|
||||
TestConnectionFn: func(_ context.Context, id string) error {
|
||||
return ErrMockServiceFailed
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
@@ -12,12 +13,12 @@ import (
|
||||
|
||||
// TargetService defines the service interface for deployment target operations.
|
||||
type TargetService interface {
|
||||
ListTargets(page, perPage int) ([]domain.DeploymentTarget, int64, error)
|
||||
GetTarget(id string) (*domain.DeploymentTarget, error)
|
||||
CreateTarget(target domain.DeploymentTarget) (*domain.DeploymentTarget, error)
|
||||
UpdateTarget(id string, target domain.DeploymentTarget) (*domain.DeploymentTarget, error)
|
||||
DeleteTarget(id string) error
|
||||
TestTargetConnection(id string) error
|
||||
ListTargets(ctx context.Context, page, perPage int) ([]domain.DeploymentTarget, int64, error)
|
||||
GetTarget(ctx context.Context, id string) (*domain.DeploymentTarget, error)
|
||||
CreateTarget(ctx context.Context, target domain.DeploymentTarget) (*domain.DeploymentTarget, error)
|
||||
UpdateTarget(ctx context.Context, id string, target domain.DeploymentTarget) (*domain.DeploymentTarget, error)
|
||||
DeleteTarget(ctx context.Context, id string) error
|
||||
TestConnection(ctx context.Context, id string) error
|
||||
}
|
||||
|
||||
// TargetHandler handles HTTP requests for deployment target operations.
|
||||
@@ -54,7 +55,7 @@ func (h TargetHandler) ListTargets(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
targets, total, err := h.svc.ListTargets(page, perPage)
|
||||
targets, total, err := h.svc.ListTargets(r.Context(), page, perPage)
|
||||
if err != nil {
|
||||
ErrorWithRequestID(w, http.StatusInternalServerError, "Failed to list targets", requestID)
|
||||
return
|
||||
@@ -86,7 +87,7 @@ func (h TargetHandler) GetTarget(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
target, err := h.svc.GetTarget(id)
|
||||
target, err := h.svc.GetTarget(r.Context(), id)
|
||||
if err != nil {
|
||||
ErrorWithRequestID(w, http.StatusNotFound, "Target not found", requestID)
|
||||
return
|
||||
@@ -125,7 +126,7 @@ func (h TargetHandler) CreateTarget(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
created, err := h.svc.CreateTarget(target)
|
||||
created, err := h.svc.CreateTarget(r.Context(), target)
|
||||
if err != nil {
|
||||
ErrorWithRequestID(w, http.StatusInternalServerError, "Failed to create target", requestID)
|
||||
return
|
||||
@@ -158,7 +159,7 @@ func (h TargetHandler) UpdateTarget(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
updated, err := h.svc.UpdateTarget(id, target)
|
||||
updated, err := h.svc.UpdateTarget(r.Context(), id, target)
|
||||
if err != nil {
|
||||
ErrorWithRequestID(w, http.StatusInternalServerError, "Failed to update target", requestID)
|
||||
return
|
||||
@@ -183,7 +184,7 @@ func (h TargetHandler) DeleteTarget(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.svc.DeleteTarget(id); err != nil {
|
||||
if err := h.svc.DeleteTarget(r.Context(), id); err != nil {
|
||||
ErrorWithRequestID(w, http.StatusInternalServerError, "Failed to delete target", requestID)
|
||||
return
|
||||
}
|
||||
@@ -210,7 +211,7 @@ func (h TargetHandler) TestTargetConnection(w http.ResponseWriter, r *http.Reque
|
||||
}
|
||||
id := parts[0]
|
||||
|
||||
if err := h.svc.TestTargetConnection(id); err != nil {
|
||||
if err := h.svc.TestConnection(r.Context(), id); err != nil {
|
||||
JSON(w, http.StatusOK, map[string]interface{}{
|
||||
"status": "failed",
|
||||
"message": err.Error(),
|
||||
|
||||
Reference in New Issue
Block a user