-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresponse.go
125 lines (107 loc) · 3.39 KB
/
response.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright (c) Autovia GmbH
// SPDX-License-Identifier: Apache-2.0
package structs
import (
"encoding/json"
"errors"
"net/http"
"strings"
"gopkg.in/yaml.v2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/types"
)
func RespondOK(w http.ResponseWriter, response string) error {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write([]byte(response))
return nil
}
func RespondError(args ...interface{}) StatusError {
code := 500
err := errors.New("Internal Server Error")
msg := ""
for _, arg := range args {
switch t := arg.(type) {
case string:
msg = t
case int:
code = t
case error:
err = t
}
}
return StatusError{Code: code, Err: err, Msg: msg}
}
func RespondText(w http.ResponseWriter, status int, payload string) error {
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(status)
w.Write([]byte(payload))
return nil
}
func RespondFormat[T any](r *http.Request, w http.ResponseWriter, status int, payload T) error {
if r.URL.Query().Get("format") == "json" {
return RespondJSON(w, http.StatusOK, payload)
}
return RespondYAML(w, http.StatusOK, payload)
}
func RespondYAML[T any](w http.ResponseWriter, status int, payload T) error {
yaml, err := yaml.Marshal(payload)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return nil
}
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(status)
w.Write(yaml)
return nil
}
type ObjectList struct {
UID types.UID `json:"uid"`
Name string `json:"name"`
Namespace string `json:"namespace"`
Group string `json:"group"`
Version string `json:"version"`
CreationTimestamp metav1.Time `json:"creationTimestamp"`
}
func RespondGraph(r *http.Request, w http.ResponseWriter, status int, list *unstructured.UnstructuredList, url *RequestUrl) error {
filter := r.URL.Query().Get("filter")
g := Graph{Nodes: []Node{}, Edges: []Edge{}}
node := g.AddNode("ns", url.Scope, url.Scope, NodeOptions{Type: "namespace", Group: true})
for _, item := range list.Items {
if strings.Contains(item.GetName(), filter) {
g.AddNode(url.Resource, string(item.GetUID()), item.GetName(), NodeOptions{Namespace: item.GetNamespace(), Type: url.Resource, ParentNode: node, Extent: "parent"})
}
}
return RespondJSON(w, http.StatusOK, g)
}
func RespondFilter(r *http.Request, w http.ResponseWriter, status int, list *unstructured.UnstructuredList) error {
filter := r.URL.Query().Get("filter")
var ret []ObjectList
for _, item := range list.Items {
if strings.Contains(item.GetName(), filter) {
ret = append(ret, ObjectList{
UID: item.GetUID(),
Name: item.GetName(),
Namespace: item.GetNamespace(),
Group: item.GroupVersionKind().Group,
Version: item.GroupVersionKind().Version,
CreationTimestamp: item.GetCreationTimestamp(),
})
}
}
return RespondJSON(w, http.StatusOK, ret)
}
func RespondJSON[T any](w http.ResponseWriter, status int, payload T) error {
response, err := json.Marshal(payload)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return nil
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
w.Write([]byte(response))
return nil
}