|
36 | 36 | _ resource.ResourceWithImportState = &instanceResource{} |
37 | 37 | ) |
38 | 38 |
|
39 | | -const ( |
40 | | - // We need to set these defaults because we need to revert to them if the user stops setting them |
41 | | - DefaultMetricsRetentionDays int64 = 90 |
42 | | - DefaultMetricsRetentionDays5mDownsampling int64 = 0 |
43 | | - DefaultMetricsRetentionDays1hDownsampling int64 = 0 |
44 | | -) |
45 | | - |
46 | 39 | type Model struct { |
47 | 40 | Id types.String `tfsdk:"id"` // needed by TF |
48 | 41 | ProjectId types.String `tfsdk:"project_id"` |
@@ -216,17 +209,17 @@ func (r *instanceResource) Schema(_ context.Context, _ resource.SchemaRequest, r |
216 | 209 | Sensitive: true, |
217 | 210 | }, |
218 | 211 | "metrics_retention_days": schema.Int64Attribute{ |
219 | | - Description: "Specifies for how many days the raw metrics are kept. Default is set to `90`.", |
| 212 | + Description: "Specifies for how many days the raw metrics are kept.", |
220 | 213 | Optional: true, |
221 | 214 | Computed: true, |
222 | 215 | }, |
223 | 216 | "metrics_retention_days_5m_downsampling": schema.Int64Attribute{ |
224 | | - 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).", |
| 217 | + 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).", |
225 | 218 | Optional: true, |
226 | 219 | Computed: true, |
227 | 220 | }, |
228 | 221 | "metrics_retention_days_1h_downsampling": schema.Int64Attribute{ |
229 | | - 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).", |
| 222 | + 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).", |
230 | 223 | Optional: true, |
231 | 224 | Computed: true, |
232 | 225 | }, |
@@ -370,17 +363,26 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques |
370 | 363 | return |
371 | 364 | } |
372 | 365 |
|
373 | | - // Update metrics retention policy |
374 | | - metricsRetentionPayload, err := toUpdateMetricsStorageRetentionPayload(metricsRetentionDays, metricsRetentionDays5mDownsampling, metricsRetentionDays1hDownsampling) |
375 | | - if err != nil { |
376 | | - core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", fmt.Sprintf("Building metrics retention policy payload: %v", err)) |
377 | | - return |
378 | | - } |
| 366 | + // If any of the metrics retention days are set, set the metrics retention policy |
| 367 | + if metricsRetentionDays != nil || metricsRetentionDays5mDownsampling != nil || metricsRetentionDays1hDownsampling != nil { |
| 368 | + // Need to get the metrics retention policy because update endpoint is a PUT and we need to send all fields |
| 369 | + metricsResp, err := r.client.GetMetricsStorageRetentionExecute(ctx, *instanceId, projectId) |
| 370 | + if err != nil { |
| 371 | + core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", fmt.Sprintf("Getting metrics retention policy: %v", err)) |
| 372 | + return |
| 373 | + } |
379 | 374 |
|
380 | | - _, err = r.client.UpdateMetricsStorageRetention(ctx, *instanceId, projectId).UpdateMetricsStorageRetentionPayload(*metricsRetentionPayload).Execute() |
381 | | - if err != nil { |
382 | | - core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", fmt.Sprintf("Setting metrics retention policy: %v", err)) |
383 | | - return |
| 375 | + metricsRetentionPayload, err := toUpdateMetricsStorageRetentionPayload(metricsRetentionDays, metricsRetentionDays5mDownsampling, metricsRetentionDays1hDownsampling, metricsResp) |
| 376 | + if err != nil { |
| 377 | + core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", fmt.Sprintf("Building metrics retention policy payload: %v", err)) |
| 378 | + return |
| 379 | + } |
| 380 | + |
| 381 | + _, err = r.client.UpdateMetricsStorageRetention(ctx, *instanceId, projectId).UpdateMetricsStorageRetentionPayload(*metricsRetentionPayload).Execute() |
| 382 | + if err != nil { |
| 383 | + core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", fmt.Sprintf("Setting metrics retention policy: %v", err)) |
| 384 | + return |
| 385 | + } |
384 | 386 | } |
385 | 387 |
|
386 | 388 | // Get metrics retention policy after update |
@@ -563,16 +565,25 @@ func (r *instanceResource) Update(ctx context.Context, req resource.UpdateReques |
563 | 565 | return |
564 | 566 | } |
565 | 567 |
|
566 | | - // Update metrics retention policy |
567 | | - metricsRetentionPayload, err := toUpdateMetricsStorageRetentionPayload(metricsRetentionDays, metricsRetentionDays5mDownsampling, metricsRetentionDays1hDownsampling) |
568 | | - if err != nil { |
569 | | - core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating instance", fmt.Sprintf("Building metrics retention policy payload: %v", err)) |
570 | | - return |
571 | | - } |
572 | | - _, err = r.client.UpdateMetricsStorageRetention(ctx, instanceId, projectId).UpdateMetricsStorageRetentionPayload(*metricsRetentionPayload).Execute() |
573 | | - if err != nil { |
574 | | - core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating instance", fmt.Sprintf("Setting metrics retention policy: %v", err)) |
575 | | - return |
| 568 | + // If any of the metrics retention days are set, set the metrics retention policy |
| 569 | + if metricsRetentionDays != nil || metricsRetentionDays5mDownsampling != nil || metricsRetentionDays1hDownsampling != nil { |
| 570 | + // Need to get the metrics retention policy because update endpoint is a PUT and we need to send all fields |
| 571 | + metricsResp, err := r.client.GetMetricsStorageRetentionExecute(ctx, instanceId, projectId) |
| 572 | + if err != nil { |
| 573 | + core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating instance", fmt.Sprintf("Getting metrics retention policy: %v", err)) |
| 574 | + return |
| 575 | + } |
| 576 | + |
| 577 | + metricsRetentionPayload, err := toUpdateMetricsStorageRetentionPayload(metricsRetentionDays, metricsRetentionDays5mDownsampling, metricsRetentionDays1hDownsampling, metricsResp) |
| 578 | + if err != nil { |
| 579 | + core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating instance", fmt.Sprintf("Building metrics retention policy payload: %v", err)) |
| 580 | + return |
| 581 | + } |
| 582 | + _, err = r.client.UpdateMetricsStorageRetention(ctx, instanceId, projectId).UpdateMetricsStorageRetentionPayload(*metricsRetentionPayload).Execute() |
| 583 | + if err != nil { |
| 584 | + core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating instance", fmt.Sprintf("Setting metrics retention policy: %v", err)) |
| 585 | + return |
| 586 | + } |
576 | 587 | } |
577 | 588 |
|
578 | 589 | // Get metrics retention policy after update |
@@ -789,35 +800,37 @@ func toCreatePayload(model *Model) (*argus.CreateInstancePayload, error) { |
789 | 800 | }, nil |
790 | 801 | } |
791 | 802 |
|
792 | | -// toUpdateMetricsStorageRetentionPayload creates a payload for updating the metrics storage retention policy. |
793 | | -// If the retentionDaysRaw, retentionDays5m, or retentionDays1h are nil, the default values are used. |
794 | | -func toUpdateMetricsStorageRetentionPayload(retentionDaysRaw, retentionDays5m, retentionDays1h *int64) (*argus.UpdateMetricsStorageRetentionPayload, error) { |
795 | | - var retentionTimeRaw int64 |
796 | | - var retentionTime5m int64 |
797 | | - var retentionTime1h int64 |
| 803 | +func toUpdateMetricsStorageRetentionPayload(retentionDaysRaw, retentionDays5m, retentionDays1h *int64, resp *argus.GetMetricsStorageRetentionResponse) (*argus.UpdateMetricsStorageRetentionPayload, error) { |
| 804 | + var retentionTimeRaw string |
| 805 | + var retentionTime5m string |
| 806 | + var retentionTime1h string |
| 807 | + |
| 808 | + if resp == nil || resp.MetricsRetentionTimeRaw == nil || resp.MetricsRetentionTime5m == nil || resp.MetricsRetentionTime1h == nil { |
| 809 | + return nil, fmt.Errorf("nil response") |
| 810 | + } |
798 | 811 |
|
799 | 812 | if retentionDaysRaw == nil { |
800 | | - retentionTimeRaw = DefaultMetricsRetentionDays |
| 813 | + retentionTimeRaw = *resp.MetricsRetentionTimeRaw |
801 | 814 | } else { |
802 | | - retentionTimeRaw = *retentionDaysRaw |
| 815 | + retentionTimeRaw = fmt.Sprintf("%dd", *retentionDaysRaw) |
803 | 816 | } |
804 | 817 |
|
805 | 818 | if retentionDays5m == nil { |
806 | | - retentionTime5m = DefaultMetricsRetentionDays5mDownsampling |
| 819 | + retentionTime5m = *resp.MetricsRetentionTime5m |
807 | 820 | } else { |
808 | | - retentionTime5m = *retentionDays5m |
| 821 | + retentionTime5m = fmt.Sprintf("%dd", *retentionDays5m) |
809 | 822 | } |
810 | 823 |
|
811 | 824 | if retentionDays1h == nil { |
812 | | - retentionTime1h = DefaultMetricsRetentionDays1hDownsampling |
| 825 | + retentionTime1h = *resp.MetricsRetentionTime1h |
813 | 826 | } else { |
814 | | - retentionTime1h = *retentionDays1h |
| 827 | + retentionTime1h = fmt.Sprintf("%dd", *retentionDays1h) |
815 | 828 | } |
816 | 829 |
|
817 | 830 | return &argus.UpdateMetricsStorageRetentionPayload{ |
818 | | - MetricsRetentionTimeRaw: utils.Ptr(fmt.Sprintf("%dd", retentionTimeRaw)), |
819 | | - MetricsRetentionTime5m: utils.Ptr(fmt.Sprintf("%dd", retentionTime5m)), |
820 | | - MetricsRetentionTime1h: utils.Ptr(fmt.Sprintf("%dd", retentionTime1h)), |
| 831 | + MetricsRetentionTimeRaw: &retentionTimeRaw, |
| 832 | + MetricsRetentionTime5m: &retentionTime5m, |
| 833 | + MetricsRetentionTime1h: &retentionTime1h, |
821 | 834 | }, nil |
822 | 835 | } |
823 | 836 |
|
|
0 commit comments