github.com/cilium/cilium@v1.16.2/api/v1/operator/server/restapi/cilium_operator_api.go (about) 1 // Code generated by go-swagger; DO NOT EDIT. 2 3 // Copyright Authors of Cilium 4 // SPDX-License-Identifier: Apache-2.0 5 6 package restapi 7 8 // This file was generated by the swagger tool. 9 // Editing this file might prove futile when you re-run the swagger generate command 10 11 import ( 12 "fmt" 13 "net/http" 14 "strings" 15 16 "github.com/go-openapi/errors" 17 "github.com/go-openapi/loads" 18 "github.com/go-openapi/runtime" 19 "github.com/go-openapi/runtime/middleware" 20 "github.com/go-openapi/runtime/security" 21 "github.com/go-openapi/spec" 22 "github.com/go-openapi/strfmt" 23 "github.com/go-openapi/swag" 24 25 "github.com/cilium/cilium/api/v1/operator/server/restapi/cluster" 26 "github.com/cilium/cilium/api/v1/operator/server/restapi/metrics" 27 "github.com/cilium/cilium/api/v1/operator/server/restapi/operator" 28 ) 29 30 // NewCiliumOperatorAPI creates a new CiliumOperator instance 31 func NewCiliumOperatorAPI(spec *loads.Document) *CiliumOperatorAPI { 32 return &CiliumOperatorAPI{ 33 handlers: make(map[string]map[string]http.Handler), 34 formats: strfmt.Default, 35 defaultConsumes: "application/json", 36 defaultProduces: "application/json", 37 customConsumers: make(map[string]runtime.Consumer), 38 customProducers: make(map[string]runtime.Producer), 39 PreServerShutdown: func() {}, 40 ServerShutdown: func() {}, 41 spec: spec, 42 useSwaggerUI: false, 43 ServeError: errors.ServeError, 44 BasicAuthenticator: security.BasicAuth, 45 APIKeyAuthenticator: security.APIKeyAuth, 46 BearerAuthenticator: security.BearerAuth, 47 48 JSONConsumer: runtime.JSONConsumer(), 49 50 JSONProducer: runtime.JSONProducer(), 51 TxtProducer: runtime.TextProducer(), 52 53 ClusterGetClusterHandler: cluster.GetClusterHandlerFunc(func(params cluster.GetClusterParams) middleware.Responder { 54 return middleware.NotImplemented("operation cluster.GetCluster has not yet been implemented") 55 }), 56 OperatorGetHealthzHandler: operator.GetHealthzHandlerFunc(func(params operator.GetHealthzParams) middleware.Responder { 57 return middleware.NotImplemented("operation operator.GetHealthz has not yet been implemented") 58 }), 59 MetricsGetMetricsHandler: metrics.GetMetricsHandlerFunc(func(params metrics.GetMetricsParams) middleware.Responder { 60 return middleware.NotImplemented("operation metrics.GetMetrics has not yet been implemented") 61 }), 62 } 63 } 64 65 /*CiliumOperatorAPI Cilium */ 66 type CiliumOperatorAPI struct { 67 spec *loads.Document 68 context *middleware.Context 69 handlers map[string]map[string]http.Handler 70 formats strfmt.Registry 71 customConsumers map[string]runtime.Consumer 72 customProducers map[string]runtime.Producer 73 defaultConsumes string 74 defaultProduces string 75 Middleware func(middleware.Builder) http.Handler 76 useSwaggerUI bool 77 78 // BasicAuthenticator generates a runtime.Authenticator from the supplied basic auth function. 79 // It has a default implementation in the security package, however you can replace it for your particular usage. 80 BasicAuthenticator func(security.UserPassAuthentication) runtime.Authenticator 81 82 // APIKeyAuthenticator generates a runtime.Authenticator from the supplied token auth function. 83 // It has a default implementation in the security package, however you can replace it for your particular usage. 84 APIKeyAuthenticator func(string, string, security.TokenAuthentication) runtime.Authenticator 85 86 // BearerAuthenticator generates a runtime.Authenticator from the supplied bearer token auth function. 87 // It has a default implementation in the security package, however you can replace it for your particular usage. 88 BearerAuthenticator func(string, security.ScopedTokenAuthentication) runtime.Authenticator 89 90 // JSONConsumer registers a consumer for the following mime types: 91 // - application/json 92 JSONConsumer runtime.Consumer 93 94 // JSONProducer registers a producer for the following mime types: 95 // - application/json 96 JSONProducer runtime.Producer 97 // TxtProducer registers a producer for the following mime types: 98 // - text/plain 99 TxtProducer runtime.Producer 100 101 // ClusterGetClusterHandler sets the operation handler for the get cluster operation 102 ClusterGetClusterHandler cluster.GetClusterHandler 103 // OperatorGetHealthzHandler sets the operation handler for the get healthz operation 104 OperatorGetHealthzHandler operator.GetHealthzHandler 105 // MetricsGetMetricsHandler sets the operation handler for the get metrics operation 106 MetricsGetMetricsHandler metrics.GetMetricsHandler 107 108 // ServeError is called when an error is received, there is a default handler 109 // but you can set your own with this 110 ServeError func(http.ResponseWriter, *http.Request, error) 111 112 // PreServerShutdown is called before the HTTP(S) server is shutdown 113 // This allows for custom functions to get executed before the HTTP(S) server stops accepting traffic 114 PreServerShutdown func() 115 116 // ServerShutdown is called when the HTTP(S) server is shut down and done 117 // handling all active connections and does not accept connections any more 118 ServerShutdown func() 119 120 // Custom command line argument groups with their descriptions 121 CommandLineOptionsGroups []swag.CommandLineOptionsGroup 122 123 // User defined logger function. 124 Logger func(string, ...interface{}) 125 } 126 127 // UseRedoc for documentation at /docs 128 func (o *CiliumOperatorAPI) UseRedoc() { 129 o.useSwaggerUI = false 130 } 131 132 // UseSwaggerUI for documentation at /docs 133 func (o *CiliumOperatorAPI) UseSwaggerUI() { 134 o.useSwaggerUI = true 135 } 136 137 // SetDefaultProduces sets the default produces media type 138 func (o *CiliumOperatorAPI) SetDefaultProduces(mediaType string) { 139 o.defaultProduces = mediaType 140 } 141 142 // SetDefaultConsumes returns the default consumes media type 143 func (o *CiliumOperatorAPI) SetDefaultConsumes(mediaType string) { 144 o.defaultConsumes = mediaType 145 } 146 147 // SetSpec sets a spec that will be served for the clients. 148 func (o *CiliumOperatorAPI) SetSpec(spec *loads.Document) { 149 o.spec = spec 150 } 151 152 // DefaultProduces returns the default produces media type 153 func (o *CiliumOperatorAPI) DefaultProduces() string { 154 return o.defaultProduces 155 } 156 157 // DefaultConsumes returns the default consumes media type 158 func (o *CiliumOperatorAPI) DefaultConsumes() string { 159 return o.defaultConsumes 160 } 161 162 // Formats returns the registered string formats 163 func (o *CiliumOperatorAPI) Formats() strfmt.Registry { 164 return o.formats 165 } 166 167 // RegisterFormat registers a custom format validator 168 func (o *CiliumOperatorAPI) RegisterFormat(name string, format strfmt.Format, validator strfmt.Validator) { 169 o.formats.Add(name, format, validator) 170 } 171 172 // Validate validates the registrations in the CiliumOperatorAPI 173 func (o *CiliumOperatorAPI) Validate() error { 174 var unregistered []string 175 176 if o.JSONConsumer == nil { 177 unregistered = append(unregistered, "JSONConsumer") 178 } 179 180 if o.JSONProducer == nil { 181 unregistered = append(unregistered, "JSONProducer") 182 } 183 if o.TxtProducer == nil { 184 unregistered = append(unregistered, "TxtProducer") 185 } 186 187 if o.ClusterGetClusterHandler == nil { 188 unregistered = append(unregistered, "cluster.GetClusterHandler") 189 } 190 if o.OperatorGetHealthzHandler == nil { 191 unregistered = append(unregistered, "operator.GetHealthzHandler") 192 } 193 if o.MetricsGetMetricsHandler == nil { 194 unregistered = append(unregistered, "metrics.GetMetricsHandler") 195 } 196 197 if len(unregistered) > 0 { 198 return fmt.Errorf("missing registration: %s", strings.Join(unregistered, ", ")) 199 } 200 201 return nil 202 } 203 204 // ServeErrorFor gets a error handler for a given operation id 205 func (o *CiliumOperatorAPI) ServeErrorFor(operationID string) func(http.ResponseWriter, *http.Request, error) { 206 return o.ServeError 207 } 208 209 // AuthenticatorsFor gets the authenticators for the specified security schemes 210 func (o *CiliumOperatorAPI) AuthenticatorsFor(schemes map[string]spec.SecurityScheme) map[string]runtime.Authenticator { 211 return nil 212 } 213 214 // Authorizer returns the registered authorizer 215 func (o *CiliumOperatorAPI) Authorizer() runtime.Authorizer { 216 return nil 217 } 218 219 // ConsumersFor gets the consumers for the specified media types. 220 // MIME type parameters are ignored here. 221 func (o *CiliumOperatorAPI) ConsumersFor(mediaTypes []string) map[string]runtime.Consumer { 222 result := make(map[string]runtime.Consumer, len(mediaTypes)) 223 for _, mt := range mediaTypes { 224 switch mt { 225 case "application/json": 226 result["application/json"] = o.JSONConsumer 227 } 228 229 if c, ok := o.customConsumers[mt]; ok { 230 result[mt] = c 231 } 232 } 233 return result 234 } 235 236 // ProducersFor gets the producers for the specified media types. 237 // MIME type parameters are ignored here. 238 func (o *CiliumOperatorAPI) ProducersFor(mediaTypes []string) map[string]runtime.Producer { 239 result := make(map[string]runtime.Producer, len(mediaTypes)) 240 for _, mt := range mediaTypes { 241 switch mt { 242 case "application/json": 243 result["application/json"] = o.JSONProducer 244 case "text/plain": 245 result["text/plain"] = o.TxtProducer 246 } 247 248 if p, ok := o.customProducers[mt]; ok { 249 result[mt] = p 250 } 251 } 252 return result 253 } 254 255 // HandlerFor gets a http.Handler for the provided operation method and path 256 func (o *CiliumOperatorAPI) HandlerFor(method, path string) (http.Handler, bool) { 257 if o.handlers == nil { 258 return nil, false 259 } 260 um := strings.ToUpper(method) 261 if _, ok := o.handlers[um]; !ok { 262 return nil, false 263 } 264 if path == "/" { 265 path = "" 266 } 267 h, ok := o.handlers[um][path] 268 return h, ok 269 } 270 271 // Context returns the middleware context for the cilium operator API 272 func (o *CiliumOperatorAPI) Context() *middleware.Context { 273 if o.context == nil { 274 o.context = middleware.NewRoutableContext(o.spec, o, nil) 275 } 276 277 return o.context 278 } 279 280 func (o *CiliumOperatorAPI) initHandlerCache() { 281 o.Context() // don't care about the result, just that the initialization happened 282 if o.handlers == nil { 283 o.handlers = make(map[string]map[string]http.Handler) 284 } 285 286 if o.handlers["GET"] == nil { 287 o.handlers["GET"] = make(map[string]http.Handler) 288 } 289 o.handlers["GET"]["/cluster"] = cluster.NewGetCluster(o.context, o.ClusterGetClusterHandler) 290 if o.handlers["GET"] == nil { 291 o.handlers["GET"] = make(map[string]http.Handler) 292 } 293 o.handlers["GET"]["/healthz"] = operator.NewGetHealthz(o.context, o.OperatorGetHealthzHandler) 294 if o.handlers["GET"] == nil { 295 o.handlers["GET"] = make(map[string]http.Handler) 296 } 297 o.handlers["GET"]["/metrics"] = metrics.NewGetMetrics(o.context, o.MetricsGetMetricsHandler) 298 } 299 300 // Serve creates a http handler to serve the API over HTTP 301 // can be used directly in http.ListenAndServe(":8000", api.Serve(nil)) 302 func (o *CiliumOperatorAPI) Serve(builder middleware.Builder) http.Handler { 303 o.Init() 304 305 if o.Middleware != nil { 306 return o.Middleware(builder) 307 } 308 if o.useSwaggerUI { 309 return o.context.APIHandlerSwaggerUI(builder) 310 } 311 return o.context.APIHandler(builder) 312 } 313 314 // Init allows you to just initialize the handler cache, you can then recompose the middleware as you see fit 315 func (o *CiliumOperatorAPI) Init() { 316 if len(o.handlers) == 0 { 317 o.initHandlerCache() 318 } 319 } 320 321 // RegisterConsumer allows you to add (or override) a consumer for a media type. 322 func (o *CiliumOperatorAPI) RegisterConsumer(mediaType string, consumer runtime.Consumer) { 323 o.customConsumers[mediaType] = consumer 324 } 325 326 // RegisterProducer allows you to add (or override) a producer for a media type. 327 func (o *CiliumOperatorAPI) RegisterProducer(mediaType string, producer runtime.Producer) { 328 o.customProducers[mediaType] = producer 329 } 330 331 // AddMiddlewareFor adds a http middleware to existing handler 332 func (o *CiliumOperatorAPI) AddMiddlewareFor(method, path string, builder middleware.Builder) { 333 um := strings.ToUpper(method) 334 if path == "/" { 335 path = "" 336 } 337 o.Init() 338 if h, ok := o.handlers[um][path]; ok { 339 o.handlers[um][path] = builder(h) 340 } 341 }