...

Source file src/github.com/prometheus/alertmanager/api/v2/restapi/operations/alertmanager_api.go

Documentation: github.com/prometheus/alertmanager/api/v2/restapi/operations

     1  // Code generated by go-swagger; DO NOT EDIT.
     2  
     3  // Copyright Prometheus Team
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //
     8  // http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  //
    16  
    17  package operations
    18  
    19  // This file was generated by the swagger tool.
    20  // Editing this file might prove futile when you re-run the swagger generate command
    21  
    22  import (
    23  	"fmt"
    24  	"net/http"
    25  	"strings"
    26  
    27  	"github.com/go-openapi/errors"
    28  	"github.com/go-openapi/loads"
    29  	"github.com/go-openapi/runtime"
    30  	"github.com/go-openapi/runtime/middleware"
    31  	"github.com/go-openapi/runtime/security"
    32  	"github.com/go-openapi/spec"
    33  	"github.com/go-openapi/strfmt"
    34  	"github.com/go-openapi/swag"
    35  
    36  	"github.com/prometheus/alertmanager/api/v2/restapi/operations/alert"
    37  	"github.com/prometheus/alertmanager/api/v2/restapi/operations/alertgroup"
    38  	"github.com/prometheus/alertmanager/api/v2/restapi/operations/general"
    39  	"github.com/prometheus/alertmanager/api/v2/restapi/operations/receiver"
    40  	"github.com/prometheus/alertmanager/api/v2/restapi/operations/silence"
    41  )
    42  
    43  // NewAlertmanagerAPI creates a new Alertmanager instance
    44  func NewAlertmanagerAPI(spec *loads.Document) *AlertmanagerAPI {
    45  	return &AlertmanagerAPI{
    46  		handlers:            make(map[string]map[string]http.Handler),
    47  		formats:             strfmt.Default,
    48  		defaultConsumes:     "application/json",
    49  		defaultProduces:     "application/json",
    50  		customConsumers:     make(map[string]runtime.Consumer),
    51  		customProducers:     make(map[string]runtime.Producer),
    52  		PreServerShutdown:   func() {},
    53  		ServerShutdown:      func() {},
    54  		spec:                spec,
    55  		useSwaggerUI:        false,
    56  		ServeError:          errors.ServeError,
    57  		BasicAuthenticator:  security.BasicAuth,
    58  		APIKeyAuthenticator: security.APIKeyAuth,
    59  		BearerAuthenticator: security.BearerAuth,
    60  
    61  		JSONConsumer: runtime.JSONConsumer(),
    62  
    63  		JSONProducer: runtime.JSONProducer(),
    64  
    65  		SilenceDeleteSilenceHandler: silence.DeleteSilenceHandlerFunc(func(params silence.DeleteSilenceParams) middleware.Responder {
    66  			return middleware.NotImplemented("operation silence.DeleteSilence has not yet been implemented")
    67  		}),
    68  		AlertgroupGetAlertGroupsHandler: alertgroup.GetAlertGroupsHandlerFunc(func(params alertgroup.GetAlertGroupsParams) middleware.Responder {
    69  			return middleware.NotImplemented("operation alertgroup.GetAlertGroups has not yet been implemented")
    70  		}),
    71  		AlertGetAlertsHandler: alert.GetAlertsHandlerFunc(func(params alert.GetAlertsParams) middleware.Responder {
    72  			return middleware.NotImplemented("operation alert.GetAlerts has not yet been implemented")
    73  		}),
    74  		ReceiverGetReceiversHandler: receiver.GetReceiversHandlerFunc(func(params receiver.GetReceiversParams) middleware.Responder {
    75  			return middleware.NotImplemented("operation receiver.GetReceivers has not yet been implemented")
    76  		}),
    77  		SilenceGetSilenceHandler: silence.GetSilenceHandlerFunc(func(params silence.GetSilenceParams) middleware.Responder {
    78  			return middleware.NotImplemented("operation silence.GetSilence has not yet been implemented")
    79  		}),
    80  		SilenceGetSilencesHandler: silence.GetSilencesHandlerFunc(func(params silence.GetSilencesParams) middleware.Responder {
    81  			return middleware.NotImplemented("operation silence.GetSilences has not yet been implemented")
    82  		}),
    83  		GeneralGetStatusHandler: general.GetStatusHandlerFunc(func(params general.GetStatusParams) middleware.Responder {
    84  			return middleware.NotImplemented("operation general.GetStatus has not yet been implemented")
    85  		}),
    86  		AlertPostAlertsHandler: alert.PostAlertsHandlerFunc(func(params alert.PostAlertsParams) middleware.Responder {
    87  			return middleware.NotImplemented("operation alert.PostAlerts has not yet been implemented")
    88  		}),
    89  		SilencePostSilencesHandler: silence.PostSilencesHandlerFunc(func(params silence.PostSilencesParams) middleware.Responder {
    90  			return middleware.NotImplemented("operation silence.PostSilences has not yet been implemented")
    91  		}),
    92  	}
    93  }
    94  
    95  /*AlertmanagerAPI API of the Prometheus Alertmanager (https://github.com/prometheus/alertmanager) */
    96  type AlertmanagerAPI struct {
    97  	spec            *loads.Document
    98  	context         *middleware.Context
    99  	handlers        map[string]map[string]http.Handler
   100  	formats         strfmt.Registry
   101  	customConsumers map[string]runtime.Consumer
   102  	customProducers map[string]runtime.Producer
   103  	defaultConsumes string
   104  	defaultProduces string
   105  	Middleware      func(middleware.Builder) http.Handler
   106  	useSwaggerUI    bool
   107  
   108  	// BasicAuthenticator generates a runtime.Authenticator from the supplied basic auth function.
   109  	// It has a default implementation in the security package, however you can replace it for your particular usage.
   110  	BasicAuthenticator func(security.UserPassAuthentication) runtime.Authenticator
   111  
   112  	// APIKeyAuthenticator generates a runtime.Authenticator from the supplied token auth function.
   113  	// It has a default implementation in the security package, however you can replace it for your particular usage.
   114  	APIKeyAuthenticator func(string, string, security.TokenAuthentication) runtime.Authenticator
   115  
   116  	// BearerAuthenticator generates a runtime.Authenticator from the supplied bearer token auth function.
   117  	// It has a default implementation in the security package, however you can replace it for your particular usage.
   118  	BearerAuthenticator func(string, security.ScopedTokenAuthentication) runtime.Authenticator
   119  
   120  	// JSONConsumer registers a consumer for the following mime types:
   121  	//   - application/json
   122  	JSONConsumer runtime.Consumer
   123  
   124  	// JSONProducer registers a producer for the following mime types:
   125  	//   - application/json
   126  	JSONProducer runtime.Producer
   127  
   128  	// SilenceDeleteSilenceHandler sets the operation handler for the delete silence operation
   129  	SilenceDeleteSilenceHandler silence.DeleteSilenceHandler
   130  	// AlertgroupGetAlertGroupsHandler sets the operation handler for the get alert groups operation
   131  	AlertgroupGetAlertGroupsHandler alertgroup.GetAlertGroupsHandler
   132  	// AlertGetAlertsHandler sets the operation handler for the get alerts operation
   133  	AlertGetAlertsHandler alert.GetAlertsHandler
   134  	// ReceiverGetReceiversHandler sets the operation handler for the get receivers operation
   135  	ReceiverGetReceiversHandler receiver.GetReceiversHandler
   136  	// SilenceGetSilenceHandler sets the operation handler for the get silence operation
   137  	SilenceGetSilenceHandler silence.GetSilenceHandler
   138  	// SilenceGetSilencesHandler sets the operation handler for the get silences operation
   139  	SilenceGetSilencesHandler silence.GetSilencesHandler
   140  	// GeneralGetStatusHandler sets the operation handler for the get status operation
   141  	GeneralGetStatusHandler general.GetStatusHandler
   142  	// AlertPostAlertsHandler sets the operation handler for the post alerts operation
   143  	AlertPostAlertsHandler alert.PostAlertsHandler
   144  	// SilencePostSilencesHandler sets the operation handler for the post silences operation
   145  	SilencePostSilencesHandler silence.PostSilencesHandler
   146  
   147  	// ServeError is called when an error is received, there is a default handler
   148  	// but you can set your own with this
   149  	ServeError func(http.ResponseWriter, *http.Request, error)
   150  
   151  	// PreServerShutdown is called before the HTTP(S) server is shutdown
   152  	// This allows for custom functions to get executed before the HTTP(S) server stops accepting traffic
   153  	PreServerShutdown func()
   154  
   155  	// ServerShutdown is called when the HTTP(S) server is shut down and done
   156  	// handling all active connections and does not accept connections any more
   157  	ServerShutdown func()
   158  
   159  	// Custom command line argument groups with their descriptions
   160  	CommandLineOptionsGroups []swag.CommandLineOptionsGroup
   161  
   162  	// User defined logger function.
   163  	Logger func(string, ...interface{})
   164  }
   165  
   166  // UseRedoc for documentation at /docs
   167  func (o *AlertmanagerAPI) UseRedoc() {
   168  	o.useSwaggerUI = false
   169  }
   170  
   171  // UseSwaggerUI for documentation at /docs
   172  func (o *AlertmanagerAPI) UseSwaggerUI() {
   173  	o.useSwaggerUI = true
   174  }
   175  
   176  // SetDefaultProduces sets the default produces media type
   177  func (o *AlertmanagerAPI) SetDefaultProduces(mediaType string) {
   178  	o.defaultProduces = mediaType
   179  }
   180  
   181  // SetDefaultConsumes returns the default consumes media type
   182  func (o *AlertmanagerAPI) SetDefaultConsumes(mediaType string) {
   183  	o.defaultConsumes = mediaType
   184  }
   185  
   186  // SetSpec sets a spec that will be served for the clients.
   187  func (o *AlertmanagerAPI) SetSpec(spec *loads.Document) {
   188  	o.spec = spec
   189  }
   190  
   191  // DefaultProduces returns the default produces media type
   192  func (o *AlertmanagerAPI) DefaultProduces() string {
   193  	return o.defaultProduces
   194  }
   195  
   196  // DefaultConsumes returns the default consumes media type
   197  func (o *AlertmanagerAPI) DefaultConsumes() string {
   198  	return o.defaultConsumes
   199  }
   200  
   201  // Formats returns the registered string formats
   202  func (o *AlertmanagerAPI) Formats() strfmt.Registry {
   203  	return o.formats
   204  }
   205  
   206  // RegisterFormat registers a custom format validator
   207  func (o *AlertmanagerAPI) RegisterFormat(name string, format strfmt.Format, validator strfmt.Validator) {
   208  	o.formats.Add(name, format, validator)
   209  }
   210  
   211  // Validate validates the registrations in the AlertmanagerAPI
   212  func (o *AlertmanagerAPI) Validate() error {
   213  	var unregistered []string
   214  
   215  	if o.JSONConsumer == nil {
   216  		unregistered = append(unregistered, "JSONConsumer")
   217  	}
   218  
   219  	if o.JSONProducer == nil {
   220  		unregistered = append(unregistered, "JSONProducer")
   221  	}
   222  
   223  	if o.SilenceDeleteSilenceHandler == nil {
   224  		unregistered = append(unregistered, "silence.DeleteSilenceHandler")
   225  	}
   226  	if o.AlertgroupGetAlertGroupsHandler == nil {
   227  		unregistered = append(unregistered, "alertgroup.GetAlertGroupsHandler")
   228  	}
   229  	if o.AlertGetAlertsHandler == nil {
   230  		unregistered = append(unregistered, "alert.GetAlertsHandler")
   231  	}
   232  	if o.ReceiverGetReceiversHandler == nil {
   233  		unregistered = append(unregistered, "receiver.GetReceiversHandler")
   234  	}
   235  	if o.SilenceGetSilenceHandler == nil {
   236  		unregistered = append(unregistered, "silence.GetSilenceHandler")
   237  	}
   238  	if o.SilenceGetSilencesHandler == nil {
   239  		unregistered = append(unregistered, "silence.GetSilencesHandler")
   240  	}
   241  	if o.GeneralGetStatusHandler == nil {
   242  		unregistered = append(unregistered, "general.GetStatusHandler")
   243  	}
   244  	if o.AlertPostAlertsHandler == nil {
   245  		unregistered = append(unregistered, "alert.PostAlertsHandler")
   246  	}
   247  	if o.SilencePostSilencesHandler == nil {
   248  		unregistered = append(unregistered, "silence.PostSilencesHandler")
   249  	}
   250  
   251  	if len(unregistered) > 0 {
   252  		return fmt.Errorf("missing registration: %s", strings.Join(unregistered, ", "))
   253  	}
   254  
   255  	return nil
   256  }
   257  
   258  // ServeErrorFor gets a error handler for a given operation id
   259  func (o *AlertmanagerAPI) ServeErrorFor(operationID string) func(http.ResponseWriter, *http.Request, error) {
   260  	return o.ServeError
   261  }
   262  
   263  // AuthenticatorsFor gets the authenticators for the specified security schemes
   264  func (o *AlertmanagerAPI) AuthenticatorsFor(schemes map[string]spec.SecurityScheme) map[string]runtime.Authenticator {
   265  	return nil
   266  }
   267  
   268  // Authorizer returns the registered authorizer
   269  func (o *AlertmanagerAPI) Authorizer() runtime.Authorizer {
   270  	return nil
   271  }
   272  
   273  // ConsumersFor gets the consumers for the specified media types.
   274  // MIME type parameters are ignored here.
   275  func (o *AlertmanagerAPI) ConsumersFor(mediaTypes []string) map[string]runtime.Consumer {
   276  	result := make(map[string]runtime.Consumer, len(mediaTypes))
   277  	for _, mt := range mediaTypes {
   278  		switch mt {
   279  		case "application/json":
   280  			result["application/json"] = o.JSONConsumer
   281  		}
   282  
   283  		if c, ok := o.customConsumers[mt]; ok {
   284  			result[mt] = c
   285  		}
   286  	}
   287  	return result
   288  }
   289  
   290  // ProducersFor gets the producers for the specified media types.
   291  // MIME type parameters are ignored here.
   292  func (o *AlertmanagerAPI) ProducersFor(mediaTypes []string) map[string]runtime.Producer {
   293  	result := make(map[string]runtime.Producer, len(mediaTypes))
   294  	for _, mt := range mediaTypes {
   295  		switch mt {
   296  		case "application/json":
   297  			result["application/json"] = o.JSONProducer
   298  		}
   299  
   300  		if p, ok := o.customProducers[mt]; ok {
   301  			result[mt] = p
   302  		}
   303  	}
   304  	return result
   305  }
   306  
   307  // HandlerFor gets a http.Handler for the provided operation method and path
   308  func (o *AlertmanagerAPI) HandlerFor(method, path string) (http.Handler, bool) {
   309  	if o.handlers == nil {
   310  		return nil, false
   311  	}
   312  	um := strings.ToUpper(method)
   313  	if _, ok := o.handlers[um]; !ok {
   314  		return nil, false
   315  	}
   316  	if path == "/" {
   317  		path = ""
   318  	}
   319  	h, ok := o.handlers[um][path]
   320  	return h, ok
   321  }
   322  
   323  // Context returns the middleware context for the alertmanager API
   324  func (o *AlertmanagerAPI) Context() *middleware.Context {
   325  	if o.context == nil {
   326  		o.context = middleware.NewRoutableContext(o.spec, o, nil)
   327  	}
   328  
   329  	return o.context
   330  }
   331  
   332  func (o *AlertmanagerAPI) initHandlerCache() {
   333  	o.Context() // don't care about the result, just that the initialization happened
   334  	if o.handlers == nil {
   335  		o.handlers = make(map[string]map[string]http.Handler)
   336  	}
   337  
   338  	if o.handlers["DELETE"] == nil {
   339  		o.handlers["DELETE"] = make(map[string]http.Handler)
   340  	}
   341  	o.handlers["DELETE"]["/silence/{silenceID}"] = silence.NewDeleteSilence(o.context, o.SilenceDeleteSilenceHandler)
   342  	if o.handlers["GET"] == nil {
   343  		o.handlers["GET"] = make(map[string]http.Handler)
   344  	}
   345  	o.handlers["GET"]["/alerts/groups"] = alertgroup.NewGetAlertGroups(o.context, o.AlertgroupGetAlertGroupsHandler)
   346  	if o.handlers["GET"] == nil {
   347  		o.handlers["GET"] = make(map[string]http.Handler)
   348  	}
   349  	o.handlers["GET"]["/alerts"] = alert.NewGetAlerts(o.context, o.AlertGetAlertsHandler)
   350  	if o.handlers["GET"] == nil {
   351  		o.handlers["GET"] = make(map[string]http.Handler)
   352  	}
   353  	o.handlers["GET"]["/receivers"] = receiver.NewGetReceivers(o.context, o.ReceiverGetReceiversHandler)
   354  	if o.handlers["GET"] == nil {
   355  		o.handlers["GET"] = make(map[string]http.Handler)
   356  	}
   357  	o.handlers["GET"]["/silence/{silenceID}"] = silence.NewGetSilence(o.context, o.SilenceGetSilenceHandler)
   358  	if o.handlers["GET"] == nil {
   359  		o.handlers["GET"] = make(map[string]http.Handler)
   360  	}
   361  	o.handlers["GET"]["/silences"] = silence.NewGetSilences(o.context, o.SilenceGetSilencesHandler)
   362  	if o.handlers["GET"] == nil {
   363  		o.handlers["GET"] = make(map[string]http.Handler)
   364  	}
   365  	o.handlers["GET"]["/status"] = general.NewGetStatus(o.context, o.GeneralGetStatusHandler)
   366  	if o.handlers["POST"] == nil {
   367  		o.handlers["POST"] = make(map[string]http.Handler)
   368  	}
   369  	o.handlers["POST"]["/alerts"] = alert.NewPostAlerts(o.context, o.AlertPostAlertsHandler)
   370  	if o.handlers["POST"] == nil {
   371  		o.handlers["POST"] = make(map[string]http.Handler)
   372  	}
   373  	o.handlers["POST"]["/silences"] = silence.NewPostSilences(o.context, o.SilencePostSilencesHandler)
   374  }
   375  
   376  // Serve creates a http handler to serve the API over HTTP
   377  // can be used directly in http.ListenAndServe(":8000", api.Serve(nil))
   378  func (o *AlertmanagerAPI) Serve(builder middleware.Builder) http.Handler {
   379  	o.Init()
   380  
   381  	if o.Middleware != nil {
   382  		return o.Middleware(builder)
   383  	}
   384  	if o.useSwaggerUI {
   385  		return o.context.APIHandlerSwaggerUI(builder)
   386  	}
   387  	return o.context.APIHandler(builder)
   388  }
   389  
   390  // Init allows you to just initialize the handler cache, you can then recompose the middleware as you see fit
   391  func (o *AlertmanagerAPI) Init() {
   392  	if len(o.handlers) == 0 {
   393  		o.initHandlerCache()
   394  	}
   395  }
   396  
   397  // RegisterConsumer allows you to add (or override) a consumer for a media type.
   398  func (o *AlertmanagerAPI) RegisterConsumer(mediaType string, consumer runtime.Consumer) {
   399  	o.customConsumers[mediaType] = consumer
   400  }
   401  
   402  // RegisterProducer allows you to add (or override) a producer for a media type.
   403  func (o *AlertmanagerAPI) RegisterProducer(mediaType string, producer runtime.Producer) {
   404  	o.customProducers[mediaType] = producer
   405  }
   406  
   407  // AddMiddlewareFor adds a http middleware to existing handler
   408  func (o *AlertmanagerAPI) AddMiddlewareFor(method, path string, builder middleware.Builder) {
   409  	um := strings.ToUpper(method)
   410  	if path == "/" {
   411  		path = ""
   412  	}
   413  	o.Init()
   414  	if h, ok := o.handlers[um][path]; ok {
   415  		o.handlers[method][path] = builder(h)
   416  	}
   417  }
   418  

View as plain text