-
Notifications
You must be signed in to change notification settings - Fork 65
/
specreconstructor.go
164 lines (137 loc) · 4.96 KB
/
specreconstructor.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright © 2022 Cisco Systems, Inc. and its affiliates.
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package specreconstructor
import (
"context"
"encoding/json"
"io/ioutil"
"net/http"
log "github.com/sirupsen/logrus"
oapicommon "github.com/openclarity/apiclarity/api3/common"
"github.com/openclarity/apiclarity/backend/pkg/modules/internal/core"
"github.com/openclarity/apiclarity/backend/pkg/modules/internal/specreconstructor/restapi"
)
//nolint:gochecknoinits
func init() {
core.RegisterModule(newModule)
}
const (
ModuleName = "specreconstructor"
ModuleDescription = "Reconstruct an openapi specification from traces"
ModuleVersion = "0.0.0"
EmptyJSON = "{}"
)
type specReconstructorPlugin struct {
httpHandler http.Handler
info *core.ModuleInfo
accessor core.BackendAccessor
}
func newModule(ctx context.Context, accessor core.BackendAccessor) (core.Module, error) {
plugin := specReconstructorPlugin{
httpHandler: nil,
accessor: accessor,
info: &core.ModuleInfo{
Name: ModuleName,
Description: ModuleDescription,
},
}
plugin.httpHandler = restapi.HandlerWithOptions(&specReconstructorPluginHTTPHandler{plugin: &plugin}, restapi.ChiServerOptions{BaseURL: core.BaseHTTPPath + "/" + ModuleName})
return &plugin, nil
}
func (d *specReconstructorPlugin) Info() core.ModuleInfo {
return *d.info
}
func (d *specReconstructorPlugin) EventNotify(ctx context.Context, event *core.Event) {
// This music doesn't use this
}
func (d *specReconstructorPlugin) HTTPHandler() http.Handler {
return d.httpHandler
}
type specReconstructorPluginHTTPHandler struct {
plugin *specReconstructorPlugin
}
func httpError(w http.ResponseWriter, statusCode int, err error) {
w.WriteHeader(statusCode)
_ = json.NewEncoder(w).Encode(map[string]interface{}{"message": err.Error()})
}
func httpResponse(writer http.ResponseWriter, statusCode int, data interface{}) {
writer.WriteHeader(statusCode)
if data != nil {
writer.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(writer).Encode(data); err != nil {
httpError(writer, http.StatusInternalServerError, err)
return
}
}
}
func (c *specReconstructorPluginHTTPHandler) PostAPIIDStart(w http.ResponseWriter, r *http.Request, apiID int64) {
log.Debugf("Called PostApiIdStart(%v)", apiID)
component := ModuleName
if err := c.plugin.accessor.GetTraceSamplingAccessor().AddHostToTrace(component, uint32(apiID)); err != nil {
log.Errorf("Failed to add API %v in APIs to trace: %v", apiID, err)
httpError(w, http.StatusInternalServerError, err)
return
}
log.Infof("Tracing successfully started for api=%d", apiID)
// Success...
httpResponse(w, http.StatusNoContent, nil)
}
func (c *specReconstructorPluginHTTPHandler) PostAPIIDStop(w http.ResponseWriter, r *http.Request, apiID int64) {
log.Debugf("Called PostApiIdStop(%v)", apiID)
component := ModuleName
if err := c.plugin.accessor.GetTraceSamplingAccessor().RemoveHostToTrace(component, uint32(apiID)); err != nil {
log.Errorf("Failed to remove API %v from APIs to trace: %v", apiID, err)
httpError(w, http.StatusInternalServerError, err)
return
}
log.Infof("Tracing successfully stoped for api=%d", apiID)
// Success...
httpResponse(w, http.StatusNoContent, nil)
}
func (c *specReconstructorPluginHTTPHandler) PostEnable(w http.ResponseWriter, r *http.Request) {
log.Debugf("Called PostEnable()")
// Decode the restapi.TestInput requesBody
body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Errorf("Can't read body content, error=(%v)", err)
httpError(w, http.StatusBadRequest, err)
return
}
log.Debugf(string(body))
var enable restapi.FeatureEnable
err = json.Unmarshal(body, &enable)
if err != nil {
log.Errorf("Failed to decode the request body, error=(%v)", err)
httpError(w, http.StatusInternalServerError, err)
return
}
component := ModuleName
flag := enable.Enable
if !*flag {
if err := c.plugin.accessor.GetTraceSamplingAccessor().ResetForComponent(component); err != nil {
log.Errorf("Failed to reset trace sampling for module %v: %v", component, err)
httpError(w, http.StatusInternalServerError, err)
return
}
}
// Success...
httpResponse(w, http.StatusNoContent, nil)
}
func (c *specReconstructorPluginHTTPHandler) GetVersion(w http.ResponseWriter, r *http.Request) {
if err := json.NewEncoder(w).Encode(oapicommon.ModuleVersion{Version: ModuleVersion}); err != nil {
httpError(w, http.StatusInternalServerError, err)
return
}
}