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