-
Notifications
You must be signed in to change notification settings - Fork 4
/
elementalregistration_controller.go
84 lines (69 loc) · 3.11 KB
/
elementalregistration_controller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package api
import (
"encoding/json"
"fmt"
"net/http"
"github.com/go-logr/logr"
"github.com/gorilla/mux"
infrastructurev1beta1 "github.com/rancher-sandbox/cluster-api-provider-elemental/api/v1beta1"
"github.com/rancher-sandbox/cluster-api-provider-elemental/internal/log"
"github.com/swaggest/openapi-go"
k8sapierrors "k8s.io/apimachinery/pkg/api/errors"
"sigs.k8s.io/controller-runtime/pkg/client"
k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
)
var _ OpenAPIDecoratedHandler = (*GetElementalRegistrationHandler)(nil)
var _ http.Handler = (*GetElementalRegistrationHandler)(nil)
type GetElementalRegistrationHandler struct {
logger logr.Logger
k8sClient client.Client
}
func NewGetElementalRegistrationHandler(logger logr.Logger, k8sClient client.Client) *GetElementalRegistrationHandler {
return &GetElementalRegistrationHandler{
logger: logger,
k8sClient: k8sClient,
}
}
func (h *GetElementalRegistrationHandler) SetupOpenAPIOperation(oc openapi.OperationContext) error {
oc.SetSummary("Get ElementalRegistration")
oc.SetDescription("This endpoint returns an ElementalRegistration.")
oc.AddReqStructure(RegistrationGetRequest{})
oc.AddRespStructure(RegistrationResponse{}, WithDecoration("Returns the ElementalRegistration", "application/json", http.StatusOK))
oc.AddRespStructure(nil, WithDecoration("If the ElementalRegistration is not found", "text/html", http.StatusNotFound))
oc.AddRespStructure(nil, WithDecoration("", "text/html", http.StatusInternalServerError))
return nil
}
func (h *GetElementalRegistrationHandler) ServeHTTP(response http.ResponseWriter, request *http.Request) {
pathVars := mux.Vars(request)
namespace := pathVars["namespace"]
registrationName := pathVars["registrationName"]
logger := h.logger.WithValues(log.KeyNamespace, namespace).
WithValues(log.KeyElementalRegistration, registrationName)
logger.Info("Getting ElementalRegistration")
// Fetch registration
registration := &infrastructurev1beta1.ElementalRegistration{}
if err := h.k8sClient.Get(request.Context(), k8sclient.ObjectKey{Namespace: namespace, Name: registrationName}, registration); err != nil {
if k8sapierrors.IsNotFound(err) {
response.WriteHeader(http.StatusNotFound)
WriteResponse(logger, response, fmt.Sprintf("ElementalRegistration '%s' not found", registrationName))
} else {
logger.Error(err, "Could not fetch ElementalRegistration")
response.WriteHeader(http.StatusInternalServerError)
WriteResponse(logger, response, fmt.Sprintf("Could not fetch ElementalRegistration '%s'", registrationName))
}
return
}
registrationResponse := RegistrationResponse{}
registrationResponse.fromElementalRegistration(*registration)
// Serialize to JSON
responseBytes, err := json.Marshal(registrationResponse)
if err != nil {
logger.Error(err, "Could not encode response body")
response.WriteHeader(http.StatusInternalServerError)
WriteResponse(logger, response, fmt.Errorf("Could not encode response body: %w", err).Error())
return
}
response.Header().Set("Content-Type", "application/json")
response.WriteHeader(http.StatusOK)
WriteResponseBytes(logger, response, responseBytes)
}