make the responses of listing alerts apis consistent

Signed-off-by: junotx <junotx@126.com>
This commit is contained in:
junotx
2021-01-21 11:03:51 +08:00
parent 626c30b9ab
commit 5467b2a1a3
2 changed files with 15 additions and 9 deletions

View File

@@ -78,7 +78,7 @@ func AddToContainer(container *restful.Container, informers informers.InformerFa
ws.Route(ws.GET("/rules/{rule_name}/alerts"). ws.Route(ws.GET("/rules/{rule_name}/alerts").
To(handler.handleListCustomRuleAlerts). To(handler.handleListCustomRuleAlerts).
Doc("list the alerts of the cluster-level custom alerting rule with the specified name"). Doc("list the alerts of the cluster-level custom alerting rule with the specified name").
Returns(http.StatusOK, ksapi.StatusOK, []alertingv2alpha1.Alert{}). Returns(http.StatusOK, ksapi.StatusOK, alertingv2alpha1.AlertList{}).
Metadata(restfulspec.KeyOpenAPITags, []string{constants.AlertingTag})) Metadata(restfulspec.KeyOpenAPITags, []string{constants.AlertingTag}))
ws.Route(ws.POST("/rules"). ws.Route(ws.POST("/rules").
@@ -134,7 +134,7 @@ func AddToContainer(container *restful.Container, informers informers.InformerFa
ws.Route(ws.GET("/namespaces/{namespace}/rules/{rule_name}/alerts"). ws.Route(ws.GET("/namespaces/{namespace}/rules/{rule_name}/alerts").
To(handler.handleListCustomRuleAlerts). To(handler.handleListCustomRuleAlerts).
Doc("get the alerts of the custom alerting rule with the specified name in the specified namespace"). Doc("get the alerts of the custom alerting rule with the specified name in the specified namespace").
Returns(http.StatusOK, ksapi.StatusOK, []alertingv2alpha1.Alert{}). Returns(http.StatusOK, ksapi.StatusOK, alertingv2alpha1.AlertList{}).
Metadata(restfulspec.KeyOpenAPITags, []string{constants.AlertingTag})) Metadata(restfulspec.KeyOpenAPITags, []string{constants.AlertingTag}))
ws.Route(ws.POST("/namespaces/{namespace}/rules"). ws.Route(ws.POST("/namespaces/{namespace}/rules").
@@ -190,7 +190,7 @@ func AddToContainer(container *restful.Container, informers informers.InformerFa
ws.Route(ws.GET("/builtin/rules/{rule_id}/alerts"). ws.Route(ws.GET("/builtin/rules/{rule_id}/alerts").
To(handler.handleListBuiltinRuleAlerts). To(handler.handleListBuiltinRuleAlerts).
Doc("list the alerts of the builtin(non-custom) alerting rule with the specified id"). Doc("list the alerts of the builtin(non-custom) alerting rule with the specified id").
Returns(http.StatusOK, ksapi.StatusOK, []alertingv2alpha1.Alert{}). Returns(http.StatusOK, ksapi.StatusOK, alertingv2alpha1.AlertList{}).
Metadata(restfulspec.KeyOpenAPITags, []string{constants.AlertingTag})) Metadata(restfulspec.KeyOpenAPITags, []string{constants.AlertingTag}))
container.Add(ws) container.Add(ws)

View File

@@ -48,7 +48,7 @@ type Operator interface {
// GetCustomAlertingRule gets the custom alerting rule with the given name. // GetCustomAlertingRule gets the custom alerting rule with the given name.
GetCustomAlertingRule(ctx context.Context, namespace, ruleName string) (*v2alpha1.GettableAlertingRule, error) GetCustomAlertingRule(ctx context.Context, namespace, ruleName string) (*v2alpha1.GettableAlertingRule, error)
// ListCustomRuleAlerts lists the alerts of the custom alerting rule with the given name. // ListCustomRuleAlerts lists the alerts of the custom alerting rule with the given name.
ListCustomRuleAlerts(ctx context.Context, namespace, ruleName string) ([]*v2alpha1.Alert, error) ListCustomRuleAlerts(ctx context.Context, namespace, ruleName string) (*v2alpha1.AlertList, error)
// CreateCustomAlertingRule creates a custom alerting rule. // CreateCustomAlertingRule creates a custom alerting rule.
CreateCustomAlertingRule(ctx context.Context, namespace string, rule *v2alpha1.PostableAlertingRule) error CreateCustomAlertingRule(ctx context.Context, namespace string, rule *v2alpha1.PostableAlertingRule) error
// UpdateCustomAlertingRule updates the custom alerting rule with the given name. // UpdateCustomAlertingRule updates the custom alerting rule with the given name.
@@ -65,7 +65,7 @@ type Operator interface {
// GetBuiltinAlertingRule gets the builtin(non-custom) alerting rule with the given id // GetBuiltinAlertingRule gets the builtin(non-custom) alerting rule with the given id
GetBuiltinAlertingRule(ctx context.Context, ruleId string) (*v2alpha1.GettableAlertingRule, error) GetBuiltinAlertingRule(ctx context.Context, ruleId string) (*v2alpha1.GettableAlertingRule, error)
// ListBuiltinRuleAlerts lists the alerts of the builtin(non-custom) alerting rule with the given id // ListBuiltinRuleAlerts lists the alerts of the builtin(non-custom) alerting rule with the given id
ListBuiltinRuleAlerts(ctx context.Context, ruleId string) ([]*v2alpha1.Alert, error) ListBuiltinRuleAlerts(ctx context.Context, ruleId string) (*v2alpha1.AlertList, error)
} }
func NewOperator(informers informers.InformerFactory, func NewOperator(informers informers.InformerFactory,
@@ -184,7 +184,7 @@ func (o *operator) GetCustomAlertingRule(ctx context.Context, namespace, ruleNam
} }
func (o *operator) ListCustomRuleAlerts(ctx context.Context, namespace, ruleName string) ( func (o *operator) ListCustomRuleAlerts(ctx context.Context, namespace, ruleName string) (
[]*v2alpha1.Alert, error) { *v2alpha1.AlertList, error) {
rule, err := o.GetCustomAlertingRule(ctx, namespace, ruleName) rule, err := o.GetCustomAlertingRule(ctx, namespace, ruleName)
if err != nil { if err != nil {
@@ -193,7 +193,10 @@ func (o *operator) ListCustomRuleAlerts(ctx context.Context, namespace, ruleName
if rule == nil { if rule == nil {
return nil, v2alpha1.ErrAlertingRuleNotFound return nil, v2alpha1.ErrAlertingRuleNotFound
} }
return rule.Alerts, nil return &v2alpha1.AlertList{
Total: len(rule.Alerts),
Items: rule.Alerts,
}, nil
} }
func (o *operator) ListBuiltinAlertingRules(ctx context.Context, func (o *operator) ListBuiltinAlertingRules(ctx context.Context,
@@ -223,7 +226,7 @@ func (o *operator) GetBuiltinAlertingRule(ctx context.Context, ruleId string) (
return o.getBuiltinAlertingRule(ctx, ruleId) return o.getBuiltinAlertingRule(ctx, ruleId)
} }
func (o *operator) ListBuiltinRuleAlerts(ctx context.Context, ruleId string) ([]*v2alpha1.Alert, error) { func (o *operator) ListBuiltinRuleAlerts(ctx context.Context, ruleId string) (*v2alpha1.AlertList, error) {
rule, err := o.getBuiltinAlertingRule(ctx, ruleId) rule, err := o.getBuiltinAlertingRule(ctx, ruleId)
if err != nil { if err != nil {
return nil, err return nil, err
@@ -231,7 +234,10 @@ func (o *operator) ListBuiltinRuleAlerts(ctx context.Context, ruleId string) ([]
if rule == nil { if rule == nil {
return nil, v2alpha1.ErrAlertingRuleNotFound return nil, v2alpha1.ErrAlertingRuleNotFound
} }
return rule.Alerts, nil return &v2alpha1.AlertList{
Total: len(rule.Alerts),
Items: rule.Alerts,
}, nil
} }
func (o *operator) ListClusterAlertingRules(ctx context.Context, customFlag string, func (o *operator) ListClusterAlertingRules(ctx context.Context, customFlag string,