Improve Argus metrics data retention policy (#401)
* update argus instance examples * Set update metrics to default when not provided * address PR comments
This commit is contained in:
parent
205763c8c2
commit
33e4417e61
3 changed files with 59 additions and 115 deletions
|
|
@ -3,7 +3,7 @@ resource "stackit_argus_instance" "example" {
|
|||
name = "example-instance"
|
||||
plan_name = "Monitoring-Medium-EU01"
|
||||
acl = ["1.1.1.1/32", "2.2.2.2/32"]
|
||||
metrics_retention_days = 7
|
||||
metrics_retention_days_5m_downsampling = 30
|
||||
metrics_retention_days_1h_downsampling = 365
|
||||
metrics_retention_days = 365
|
||||
metrics_retention_days_5m_downsampling = 60
|
||||
metrics_retention_days_1h_downsampling = 10
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,6 +36,13 @@ var (
|
|||
_ resource.ResourceWithImportState = &instanceResource{}
|
||||
)
|
||||
|
||||
const (
|
||||
// We need to set these defaults because we need to revert to them if the user stops setting them
|
||||
DefaultMetricsRetentionDays int64 = 90
|
||||
DefaultMetricsRetentionDays5mDownsampling int64 = 0
|
||||
DefaultMetricsRetentionDays1hDownsampling int64 = 0
|
||||
)
|
||||
|
||||
type Model struct {
|
||||
Id types.String `tfsdk:"id"` // needed by TF
|
||||
ProjectId types.String `tfsdk:"project_id"`
|
||||
|
|
@ -209,17 +216,17 @@ func (r *instanceResource) Schema(_ context.Context, _ resource.SchemaRequest, r
|
|||
Sensitive: true,
|
||||
},
|
||||
"metrics_retention_days": schema.Int64Attribute{
|
||||
Description: "Specifies for how many days the raw metrics are kept.",
|
||||
Description: "Specifies for how many days the raw metrics are kept. Default is set to `90`.",
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
},
|
||||
"metrics_retention_days_5m_downsampling": schema.Int64Attribute{
|
||||
Description: "Specifies for how many days the 5m downsampled metrics are kept. must be less than the value of the general retention. Default is set to `0` (disabled).",
|
||||
Description: "Specifies for how many days the 5m downsampled metrics are kept. It must be less than the value of the general retention. Default is set to `0` (disabled).",
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
},
|
||||
"metrics_retention_days_1h_downsampling": schema.Int64Attribute{
|
||||
Description: "Specifies for how many days the 1h downsampled metrics are kept. must be less than the value of the 5m downsampling retention. Default is set to `0` (disabled).",
|
||||
Description: "Specifies for how many days the 1h downsampled metrics are kept. It must be less than the value of the 5m downsampling retention. Default is set to `0` (disabled).",
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
},
|
||||
|
|
@ -363,26 +370,17 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
|
|||
return
|
||||
}
|
||||
|
||||
// If any of the metrics retention days are set, set the metrics retention policy
|
||||
if metricsRetentionDays != nil || metricsRetentionDays5mDownsampling != nil || metricsRetentionDays1hDownsampling != nil {
|
||||
// Need to get the metrics retention policy because update endpoint is a PUT and we need to send all fields
|
||||
metricsResp, err := r.client.GetMetricsStorageRetentionExecute(ctx, *instanceId, projectId)
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", fmt.Sprintf("Getting metrics retention policy: %v", err))
|
||||
return
|
||||
}
|
||||
// Update metrics retention policy
|
||||
metricsRetentionPayload, err := toUpdateMetricsStorageRetentionPayload(metricsRetentionDays, metricsRetentionDays5mDownsampling, metricsRetentionDays1hDownsampling)
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", fmt.Sprintf("Building metrics retention policy payload: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
metricsRetentionPayload, err := toUpdateMetricsStorageRetentionPayload(metricsRetentionDays, metricsRetentionDays5mDownsampling, metricsRetentionDays1hDownsampling, metricsResp)
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", fmt.Sprintf("Building metrics retention policy payload: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
_, err = r.client.UpdateMetricsStorageRetention(ctx, *instanceId, projectId).UpdateMetricsStorageRetentionPayload(*metricsRetentionPayload).Execute()
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", fmt.Sprintf("Setting metrics retention policy: %v", err))
|
||||
return
|
||||
}
|
||||
_, err = r.client.UpdateMetricsStorageRetention(ctx, *instanceId, projectId).UpdateMetricsStorageRetentionPayload(*metricsRetentionPayload).Execute()
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", fmt.Sprintf("Setting metrics retention policy: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
// Get metrics retention policy after update
|
||||
|
|
@ -565,25 +563,16 @@ func (r *instanceResource) Update(ctx context.Context, req resource.UpdateReques
|
|||
return
|
||||
}
|
||||
|
||||
// If any of the metrics retention days are set, set the metrics retention policy
|
||||
if metricsRetentionDays != nil || metricsRetentionDays5mDownsampling != nil || metricsRetentionDays1hDownsampling != nil {
|
||||
// Need to get the metrics retention policy because update endpoint is a PUT and we need to send all fields
|
||||
metricsResp, err := r.client.GetMetricsStorageRetentionExecute(ctx, instanceId, projectId)
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating instance", fmt.Sprintf("Getting metrics retention policy: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
metricsRetentionPayload, err := toUpdateMetricsStorageRetentionPayload(metricsRetentionDays, metricsRetentionDays5mDownsampling, metricsRetentionDays1hDownsampling, metricsResp)
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating instance", fmt.Sprintf("Building metrics retention policy payload: %v", err))
|
||||
return
|
||||
}
|
||||
_, err = r.client.UpdateMetricsStorageRetention(ctx, instanceId, projectId).UpdateMetricsStorageRetentionPayload(*metricsRetentionPayload).Execute()
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating instance", fmt.Sprintf("Setting metrics retention policy: %v", err))
|
||||
return
|
||||
}
|
||||
// Update metrics retention policy
|
||||
metricsRetentionPayload, err := toUpdateMetricsStorageRetentionPayload(metricsRetentionDays, metricsRetentionDays5mDownsampling, metricsRetentionDays1hDownsampling)
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating instance", fmt.Sprintf("Building metrics retention policy payload: %v", err))
|
||||
return
|
||||
}
|
||||
_, err = r.client.UpdateMetricsStorageRetention(ctx, instanceId, projectId).UpdateMetricsStorageRetentionPayload(*metricsRetentionPayload).Execute()
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating instance", fmt.Sprintf("Setting metrics retention policy: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
// Get metrics retention policy after update
|
||||
|
|
@ -800,37 +789,35 @@ func toCreatePayload(model *Model) (*argus.CreateInstancePayload, error) {
|
|||
}, nil
|
||||
}
|
||||
|
||||
func toUpdateMetricsStorageRetentionPayload(retentionDaysRaw, retentionDays5m, retentionDays1h *int64, resp *argus.GetMetricsStorageRetentionResponse) (*argus.UpdateMetricsStorageRetentionPayload, error) {
|
||||
var retentionTimeRaw string
|
||||
var retentionTime5m string
|
||||
var retentionTime1h string
|
||||
|
||||
if resp == nil || resp.MetricsRetentionTimeRaw == nil || resp.MetricsRetentionTime5m == nil || resp.MetricsRetentionTime1h == nil {
|
||||
return nil, fmt.Errorf("nil response")
|
||||
}
|
||||
// toUpdateMetricsStorageRetentionPayload creates a payload for updating the metrics storage retention policy.
|
||||
// If the retentionDaysRaw, retentionDays5m, or retentionDays1h are nil, the default values are used.
|
||||
func toUpdateMetricsStorageRetentionPayload(retentionDaysRaw, retentionDays5m, retentionDays1h *int64) (*argus.UpdateMetricsStorageRetentionPayload, error) {
|
||||
var retentionTimeRaw int64
|
||||
var retentionTime5m int64
|
||||
var retentionTime1h int64
|
||||
|
||||
if retentionDaysRaw == nil {
|
||||
retentionTimeRaw = *resp.MetricsRetentionTimeRaw
|
||||
retentionTimeRaw = DefaultMetricsRetentionDays
|
||||
} else {
|
||||
retentionTimeRaw = fmt.Sprintf("%dd", *retentionDaysRaw)
|
||||
retentionTimeRaw = *retentionDaysRaw
|
||||
}
|
||||
|
||||
if retentionDays5m == nil {
|
||||
retentionTime5m = *resp.MetricsRetentionTime5m
|
||||
retentionTime5m = DefaultMetricsRetentionDays5mDownsampling
|
||||
} else {
|
||||
retentionTime5m = fmt.Sprintf("%dd", *retentionDays5m)
|
||||
retentionTime5m = *retentionDays5m
|
||||
}
|
||||
|
||||
if retentionDays1h == nil {
|
||||
retentionTime1h = *resp.MetricsRetentionTime1h
|
||||
retentionTime1h = DefaultMetricsRetentionDays1hDownsampling
|
||||
} else {
|
||||
retentionTime1h = fmt.Sprintf("%dd", *retentionDays1h)
|
||||
retentionTime1h = *retentionDays1h
|
||||
}
|
||||
|
||||
return &argus.UpdateMetricsStorageRetentionPayload{
|
||||
MetricsRetentionTimeRaw: &retentionTimeRaw,
|
||||
MetricsRetentionTime5m: &retentionTime5m,
|
||||
MetricsRetentionTime1h: &retentionTime1h,
|
||||
MetricsRetentionTimeRaw: utils.Ptr(fmt.Sprintf("%dd", retentionTimeRaw)),
|
||||
MetricsRetentionTime5m: utils.Ptr(fmt.Sprintf("%dd", retentionTime5m)),
|
||||
MetricsRetentionTime1h: utils.Ptr(fmt.Sprintf("%dd", retentionTime1h)),
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package argus
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
|
|
@ -395,7 +396,6 @@ func TestToUpdateMetricsStorageRetentionPayload(t *testing.T) {
|
|||
retentionDaysRaw *int64
|
||||
retentionDays1h *int64
|
||||
retentionDays5m *int64
|
||||
getMetricsResp *argus.GetMetricsStorageRetentionResponse
|
||||
expected *argus.UpdateMetricsStorageRetentionPayload
|
||||
isValid bool
|
||||
}{
|
||||
|
|
@ -404,11 +404,6 @@ func TestToUpdateMetricsStorageRetentionPayload(t *testing.T) {
|
|||
utils.Ptr(int64(120)),
|
||||
utils.Ptr(int64(60)),
|
||||
utils.Ptr(int64(14)),
|
||||
&argus.GetMetricsStorageRetentionResponse{
|
||||
MetricsRetentionTimeRaw: utils.Ptr("60d"),
|
||||
MetricsRetentionTime1h: utils.Ptr("30d"),
|
||||
MetricsRetentionTime5m: utils.Ptr("7d"),
|
||||
},
|
||||
&argus.UpdateMetricsStorageRetentionPayload{
|
||||
MetricsRetentionTimeRaw: utils.Ptr("120d"),
|
||||
MetricsRetentionTime1h: utils.Ptr("60d"),
|
||||
|
|
@ -421,15 +416,10 @@ func TestToUpdateMetricsStorageRetentionPayload(t *testing.T) {
|
|||
utils.Ptr(int64(120)),
|
||||
nil,
|
||||
nil,
|
||||
&argus.GetMetricsStorageRetentionResponse{
|
||||
MetricsRetentionTimeRaw: utils.Ptr("60d"),
|
||||
MetricsRetentionTime1h: utils.Ptr("30d"),
|
||||
MetricsRetentionTime5m: utils.Ptr("7d"),
|
||||
},
|
||||
&argus.UpdateMetricsStorageRetentionPayload{
|
||||
MetricsRetentionTimeRaw: utils.Ptr("120d"),
|
||||
MetricsRetentionTime1h: utils.Ptr("30d"),
|
||||
MetricsRetentionTime5m: utils.Ptr("7d"),
|
||||
MetricsRetentionTime1h: utils.Ptr(fmt.Sprintf("%dd", DefaultMetricsRetentionDays1hDownsampling)),
|
||||
MetricsRetentionTime5m: utils.Ptr(fmt.Sprintf("%dd", DefaultMetricsRetentionDays5mDownsampling)),
|
||||
},
|
||||
true,
|
||||
},
|
||||
|
|
@ -438,15 +428,10 @@ func TestToUpdateMetricsStorageRetentionPayload(t *testing.T) {
|
|||
nil,
|
||||
utils.Ptr(int64(60)),
|
||||
nil,
|
||||
&argus.GetMetricsStorageRetentionResponse{
|
||||
MetricsRetentionTimeRaw: utils.Ptr("60d"),
|
||||
MetricsRetentionTime1h: utils.Ptr("30d"),
|
||||
MetricsRetentionTime5m: utils.Ptr("7d"),
|
||||
},
|
||||
&argus.UpdateMetricsStorageRetentionPayload{
|
||||
MetricsRetentionTimeRaw: utils.Ptr("60d"),
|
||||
MetricsRetentionTimeRaw: utils.Ptr(fmt.Sprintf("%dd", DefaultMetricsRetentionDays)),
|
||||
MetricsRetentionTime1h: utils.Ptr("60d"),
|
||||
MetricsRetentionTime5m: utils.Ptr("7d"),
|
||||
MetricsRetentionTime5m: utils.Ptr(fmt.Sprintf("%dd", DefaultMetricsRetentionDays5mDownsampling)),
|
||||
},
|
||||
true,
|
||||
},
|
||||
|
|
@ -455,14 +440,9 @@ func TestToUpdateMetricsStorageRetentionPayload(t *testing.T) {
|
|||
nil,
|
||||
nil,
|
||||
utils.Ptr(int64(14)),
|
||||
&argus.GetMetricsStorageRetentionResponse{
|
||||
MetricsRetentionTimeRaw: utils.Ptr("60d"),
|
||||
MetricsRetentionTime1h: utils.Ptr("30d"),
|
||||
MetricsRetentionTime5m: utils.Ptr("7d"),
|
||||
},
|
||||
&argus.UpdateMetricsStorageRetentionPayload{
|
||||
MetricsRetentionTimeRaw: utils.Ptr("60d"),
|
||||
MetricsRetentionTime1h: utils.Ptr("30d"),
|
||||
MetricsRetentionTimeRaw: utils.Ptr(fmt.Sprintf("%dd", DefaultMetricsRetentionDays)),
|
||||
MetricsRetentionTime1h: utils.Ptr(fmt.Sprintf("%dd", DefaultMetricsRetentionDays1hDownsampling)),
|
||||
MetricsRetentionTime5m: utils.Ptr("14d"),
|
||||
},
|
||||
true,
|
||||
|
|
@ -472,40 +452,17 @@ func TestToUpdateMetricsStorageRetentionPayload(t *testing.T) {
|
|||
nil,
|
||||
nil,
|
||||
nil,
|
||||
&argus.GetMetricsStorageRetentionResponse{
|
||||
MetricsRetentionTimeRaw: utils.Ptr("60d"),
|
||||
MetricsRetentionTime1h: utils.Ptr("30d"),
|
||||
MetricsRetentionTime5m: utils.Ptr("7d"),
|
||||
},
|
||||
&argus.UpdateMetricsStorageRetentionPayload{
|
||||
MetricsRetentionTimeRaw: utils.Ptr("60d"),
|
||||
MetricsRetentionTime1h: utils.Ptr("30d"),
|
||||
MetricsRetentionTime5m: utils.Ptr("7d"),
|
||||
MetricsRetentionTimeRaw: utils.Ptr(fmt.Sprintf("%dd", DefaultMetricsRetentionDays)),
|
||||
MetricsRetentionTime1h: utils.Ptr(fmt.Sprintf("%dd", DefaultMetricsRetentionDays1hDownsampling)),
|
||||
MetricsRetentionTime5m: utils.Ptr(fmt.Sprintf("%dd", DefaultMetricsRetentionDays5mDownsampling)),
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"nil_response",
|
||||
nil,
|
||||
nil,
|
||||
nil,
|
||||
nil,
|
||||
nil,
|
||||
false,
|
||||
},
|
||||
{
|
||||
"empty_response",
|
||||
nil,
|
||||
nil,
|
||||
nil,
|
||||
&argus.GetMetricsStorageRetentionResponse{},
|
||||
nil,
|
||||
false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.description, func(t *testing.T) {
|
||||
output, err := toUpdateMetricsStorageRetentionPayload(tt.retentionDaysRaw, tt.retentionDays5m, tt.retentionDays1h, tt.getMetricsResp)
|
||||
output, err := toUpdateMetricsStorageRetentionPayload(tt.retentionDaysRaw, tt.retentionDays5m, tt.retentionDays1h)
|
||||
if !tt.isValid && err == nil {
|
||||
t.Fatalf("Should have failed")
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue