|
| 1 | +package v2 |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | +) |
| 9 | + |
| 10 | +const ( |
| 11 | + inhibitionRulesPath = "%s/monitor/alerts/v1/inhibition-rules" |
| 12 | + inhibitionRulePath = "%s/monitor/alerts/v1/inhibition-rules/%d" |
| 13 | +) |
| 14 | + |
| 15 | +var ErrInhibitionRuleNotFound = errors.New("inhibition rule not found") |
| 16 | + |
| 17 | +type InhibitionRuleInterface interface { |
| 18 | + Base |
| 19 | + GetInhibitionRule(ctx context.Context, id int) (InhibitionRule, error) |
| 20 | + CreateInhibitionRule(ctx context.Context, inhibitionRule InhibitionRule) (InhibitionRule, error) |
| 21 | + UpdateInhibitionRule(ctx context.Context, inhibitionRule InhibitionRule) (InhibitionRule, error) |
| 22 | + DeleteInhibitionRule(ctx context.Context, id int) error |
| 23 | +} |
| 24 | + |
| 25 | +func (client *Client) GetInhibitionRule(ctx context.Context, id int) (InhibitionRule, error) { |
| 26 | + response, err := client.requester.Request(ctx, http.MethodGet, client.getInhibitionRuleURL(id), nil) |
| 27 | + if err != nil { |
| 28 | + return InhibitionRule{}, err |
| 29 | + } |
| 30 | + defer response.Body.Close() |
| 31 | + |
| 32 | + if response.StatusCode == http.StatusNotFound { |
| 33 | + return InhibitionRule{}, ErrInhibitionRuleNotFound |
| 34 | + } |
| 35 | + if response.StatusCode != http.StatusOK { |
| 36 | + return InhibitionRule{}, client.ErrorFromResponse(response) |
| 37 | + } |
| 38 | + |
| 39 | + inhibitionRule, err := Unmarshal[InhibitionRule](response.Body) |
| 40 | + if err != nil { |
| 41 | + return InhibitionRule{}, err |
| 42 | + } |
| 43 | + |
| 44 | + return inhibitionRule, nil |
| 45 | +} |
| 46 | + |
| 47 | +func (client *Client) CreateInhibitionRule(ctx context.Context, inhibitionRule InhibitionRule) (InhibitionRule, error) { |
| 48 | + payload, err := Marshal(inhibitionRule) |
| 49 | + if err != nil { |
| 50 | + return InhibitionRule{}, err |
| 51 | + } |
| 52 | + |
| 53 | + response, err := client.requester.Request(ctx, http.MethodPost, client.getInhibitionRulesURL(), payload) |
| 54 | + if err != nil { |
| 55 | + return InhibitionRule{}, err |
| 56 | + } |
| 57 | + defer response.Body.Close() |
| 58 | + |
| 59 | + if response.StatusCode != http.StatusOK && response.StatusCode != http.StatusCreated { |
| 60 | + return InhibitionRule{}, client.ErrorFromResponse(response) |
| 61 | + } |
| 62 | + |
| 63 | + return Unmarshal[InhibitionRule](response.Body) |
| 64 | +} |
| 65 | + |
| 66 | +func (client *Client) UpdateInhibitionRule(ctx context.Context, inhibitionRule InhibitionRule) (InhibitionRule, error) { |
| 67 | + payload, err := Marshal(inhibitionRule) |
| 68 | + if err != nil { |
| 69 | + return InhibitionRule{}, err |
| 70 | + } |
| 71 | + |
| 72 | + response, err := client.requester.Request(ctx, http.MethodPut, client.getInhibitionRuleURL(inhibitionRule.ID), payload) |
| 73 | + if err != nil { |
| 74 | + return InhibitionRule{}, err |
| 75 | + } |
| 76 | + defer response.Body.Close() |
| 77 | + |
| 78 | + if response.StatusCode != http.StatusOK { |
| 79 | + return InhibitionRule{}, client.ErrorFromResponse(response) |
| 80 | + } |
| 81 | + |
| 82 | + return Unmarshal[InhibitionRule](response.Body) |
| 83 | +} |
| 84 | + |
| 85 | +func (client *Client) DeleteInhibitionRule(ctx context.Context, id int) error { |
| 86 | + response, err := client.requester.Request(ctx, http.MethodDelete, client.getInhibitionRuleURL(id), nil) |
| 87 | + if err != nil { |
| 88 | + return err |
| 89 | + } |
| 90 | + defer response.Body.Close() |
| 91 | + |
| 92 | + if response.StatusCode != http.StatusNoContent && response.StatusCode != http.StatusOK && response.StatusCode != http.StatusNotFound { |
| 93 | + return client.ErrorFromResponse(response) |
| 94 | + } |
| 95 | + |
| 96 | + return nil |
| 97 | +} |
| 98 | + |
| 99 | +func (client *Client) getInhibitionRulesURL() string { |
| 100 | + return fmt.Sprintf(inhibitionRulesPath, client.config.url) |
| 101 | +} |
| 102 | + |
| 103 | +func (client *Client) getInhibitionRuleURL(id int) string { |
| 104 | + return fmt.Sprintf(inhibitionRulePath, client.config.url, id) |
| 105 | +} |
0 commit comments