|
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 | + |
39 | 46 | type Model struct { |
40 | 47 | Id types.String `tfsdk:"id"` // needed by TF |
41 | 48 | ProjectId types.String `tfsdk:"project_id"` |
@@ -209,17 +216,17 @@ func (r *instanceResource) Schema(_ context.Context, _ resource.SchemaRequest, r |
209 | 216 | Sensitive: true, |
210 | 217 | }, |
211 | 218 | "metrics_retention_days": schema.Int64Attribute{ |
212 | | - Description: "Specifies for how many days the raw metrics are kept.", |
| 219 | + Description: "Specifies for how many days the raw metrics are kept. Default is set to `90`.", |
213 | 220 | Optional: true, |
214 | 221 | Computed: true, |
215 | 222 | }, |
216 | 223 | "metrics_retention_days_5m_downsampling": schema.Int64Attribute{ |
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).", |
| 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).", |
218 | 225 | Optional: true, |
219 | 226 | Computed: true, |
220 | 227 | }, |
221 | 228 | "metrics_retention_days_1h_downsampling": schema.Int64Attribute{ |
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).", |
| 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).", |
223 | 230 | Optional: true, |
224 | 231 | Computed: true, |
225 | 232 | }, |
@@ -363,26 +370,17 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques |
363 | 370 | return |
364 | 371 | } |
365 | 372 |
|
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 | | - } |
374 | | - |
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 | | - } |
| 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 | + } |
380 | 379 |
|
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 | | - } |
| 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 |
386 | 384 | } |
387 | 385 |
|
388 | 386 | // Get metrics retention policy after update |
@@ -565,25 +563,16 @@ func (r *instanceResource) Update(ctx context.Context, req resource.UpdateReques |
565 | 563 | return |
566 | 564 | } |
567 | 565 |
|
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 | | - } |
| 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 |
587 | 576 | } |
588 | 577 |
|
589 | 578 | // Get metrics retention policy after update |
@@ -800,37 +789,35 @@ func toCreatePayload(model *Model) (*argus.CreateInstancePayload, error) { |
800 | 789 | }, nil |
801 | 790 | } |
802 | 791 |
|
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 | | - } |
| 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 |
811 | 798 |
|
812 | 799 | if retentionDaysRaw == nil { |
813 | | - retentionTimeRaw = *resp.MetricsRetentionTimeRaw |
| 800 | + retentionTimeRaw = DefaultMetricsRetentionDays |
814 | 801 | } else { |
815 | | - retentionTimeRaw = fmt.Sprintf("%dd", *retentionDaysRaw) |
| 802 | + retentionTimeRaw = *retentionDaysRaw |
816 | 803 | } |
817 | 804 |
|
818 | 805 | if retentionDays5m == nil { |
819 | | - retentionTime5m = *resp.MetricsRetentionTime5m |
| 806 | + retentionTime5m = DefaultMetricsRetentionDays5mDownsampling |
820 | 807 | } else { |
821 | | - retentionTime5m = fmt.Sprintf("%dd", *retentionDays5m) |
| 808 | + retentionTime5m = *retentionDays5m |
822 | 809 | } |
823 | 810 |
|
824 | 811 | if retentionDays1h == nil { |
825 | | - retentionTime1h = *resp.MetricsRetentionTime1h |
| 812 | + retentionTime1h = DefaultMetricsRetentionDays1hDownsampling |
826 | 813 | } else { |
827 | | - retentionTime1h = fmt.Sprintf("%dd", *retentionDays1h) |
| 814 | + retentionTime1h = *retentionDays1h |
828 | 815 | } |
829 | 816 |
|
830 | 817 | return &argus.UpdateMetricsStorageRetentionPayload{ |
831 | | - MetricsRetentionTimeRaw: &retentionTimeRaw, |
832 | | - MetricsRetentionTime5m: &retentionTime5m, |
833 | | - MetricsRetentionTime1h: &retentionTime1h, |
| 818 | + MetricsRetentionTimeRaw: utils.Ptr(fmt.Sprintf("%dd", retentionTimeRaw)), |
| 819 | + MetricsRetentionTime5m: utils.Ptr(fmt.Sprintf("%dd", retentionTime5m)), |
| 820 | + MetricsRetentionTime1h: utils.Ptr(fmt.Sprintf("%dd", retentionTime1h)), |
834 | 821 | }, nil |
835 | 822 | } |
836 | 823 |
|
|
0 commit comments