func getSloFromResourceData()

in internal/kibana/slo.go [511:688]


func getSloFromResourceData(d *schema.ResourceData) (models.Slo, diag.Diagnostics) {
	var diags diag.Diagnostics

	var indicator slo.SloResponseIndicator
	var indicatorType string
	for key := range indicatorAddressToType {
		_, exists := d.GetOk(key)
		if exists {
			indicatorType = key
		}
	}

	switch indicatorType {
	case "kql_custom_indicator":
		indicator = slo.SloResponseIndicator{
			IndicatorPropertiesCustomKql: &slo.IndicatorPropertiesCustomKql{
				Type: indicatorAddressToType[indicatorType],
				Params: slo.IndicatorPropertiesCustomKqlParams{
					Index:          d.Get(indicatorType + ".0.index").(string),
					Filter:         getOrNilString(indicatorType+".0.filter", d),
					Good:           d.Get(indicatorType + ".0.good").(string),
					Total:          d.Get(indicatorType + ".0.total").(string),
					TimestampField: d.Get(indicatorType + ".0.timestamp_field").(string),
				},
			},
		}

	case "apm_availability_indicator":
		indicator = slo.SloResponseIndicator{
			IndicatorPropertiesApmAvailability: &slo.IndicatorPropertiesApmAvailability{
				Type: indicatorAddressToType[indicatorType],
				Params: slo.IndicatorPropertiesApmAvailabilityParams{
					Service:         d.Get(indicatorType + ".0.service").(string),
					Environment:     d.Get(indicatorType + ".0.environment").(string),
					TransactionType: d.Get(indicatorType + ".0.transaction_type").(string),
					TransactionName: d.Get(indicatorType + ".0.transaction_name").(string),
					Filter:          getOrNilString(indicatorType+".0.filter", d),
					Index:           d.Get(indicatorType + ".0.index").(string),
				},
			},
		}

	case "apm_latency_indicator":
		indicator = slo.SloResponseIndicator{
			IndicatorPropertiesApmLatency: &slo.IndicatorPropertiesApmLatency{
				Type: indicatorAddressToType[indicatorType],
				Params: slo.IndicatorPropertiesApmLatencyParams{
					Service:         d.Get(indicatorType + ".0.service").(string),
					Environment:     d.Get(indicatorType + ".0.environment").(string),
					TransactionType: d.Get(indicatorType + ".0.transaction_type").(string),
					TransactionName: d.Get(indicatorType + ".0.transaction_name").(string),
					Filter:          getOrNilString(indicatorType+".0.filter", d),
					Index:           d.Get(indicatorType + ".0.index").(string),
					Threshold:       float64(d.Get(indicatorType + ".0.threshold").(int)),
				},
			},
		}

	case "histogram_custom_indicator":
		indicator = slo.SloResponseIndicator{
			IndicatorPropertiesHistogram: &slo.IndicatorPropertiesHistogram{
				Type: indicatorAddressToType[indicatorType],
				Params: slo.IndicatorPropertiesHistogramParams{
					Filter:         getOrNilString(indicatorType+".0.filter", d),
					Index:          d.Get(indicatorType + ".0.index").(string),
					TimestampField: d.Get(indicatorType + ".0.timestamp_field").(string),
					Good: slo.IndicatorPropertiesHistogramParamsGood{
						Field:       d.Get(indicatorType + ".0.good.0.field").(string),
						Aggregation: d.Get(indicatorType + ".0.good.0.aggregation").(string),
						Filter:      getOrNilString(indicatorType+".0.good.0.filter", d),
						From:        getOrNilFloat(indicatorType+".0.good.0.from", d),
						To:          getOrNilFloat(indicatorType+".0.good.0.to", d),
					},
					Total: slo.IndicatorPropertiesHistogramParamsTotal{
						Field:       d.Get(indicatorType + ".0.total.0.field").(string),
						Aggregation: d.Get(indicatorType + ".0.total.0.aggregation").(string),
						Filter:      getOrNilString(indicatorType+".0.total.0.filter", d),
						From:        getOrNilFloat(indicatorType+".0.total.0.from", d),
						To:          getOrNilFloat(indicatorType+".0.total.0.to", d),
					},
				},
			},
		}

	case "metric_custom_indicator":
		goodMetricsRaw := d.Get(indicatorType + ".0.good.0.metrics").([]interface{})
		var goodMetrics []slo.IndicatorPropertiesCustomMetricParamsGoodMetricsInner
		for n := range goodMetricsRaw {
			idx := fmt.Sprint(n)
			goodMetrics = append(goodMetrics, slo.IndicatorPropertiesCustomMetricParamsGoodMetricsInner{
				Name:        d.Get(indicatorType + ".0.good.0.metrics." + idx + ".name").(string),
				Field:       d.Get(indicatorType + ".0.good.0.metrics." + idx + ".field").(string),
				Aggregation: d.Get(indicatorType + ".0.good.0.metrics." + idx + ".aggregation").(string),
				Filter:      getOrNilString(indicatorType+".0.good.0.metrics."+idx+".filter", d),
			})
		}
		totalMetricsRaw := d.Get(indicatorType + ".0.total.0.metrics").([]interface{})
		var totalMetrics []slo.IndicatorPropertiesCustomMetricParamsTotalMetricsInner
		for n := range totalMetricsRaw {
			idx := fmt.Sprint(n)
			totalMetrics = append(totalMetrics, slo.IndicatorPropertiesCustomMetricParamsTotalMetricsInner{
				Name:        d.Get(indicatorType + ".0.total.0.metrics." + idx + ".name").(string),
				Field:       d.Get(indicatorType + ".0.total.0.metrics." + idx + ".field").(string),
				Aggregation: d.Get(indicatorType + ".0.total.0.metrics." + idx + ".aggregation").(string),
				Filter:      getOrNilString(indicatorType+".0.total.0.metrics."+idx+".filter", d),
			})
		}
		indicator = slo.SloResponseIndicator{
			IndicatorPropertiesCustomMetric: &slo.IndicatorPropertiesCustomMetric{
				Type: indicatorAddressToType[indicatorType],
				Params: slo.IndicatorPropertiesCustomMetricParams{
					Filter:         getOrNilString(indicatorType+".0.filter", d),
					Index:          d.Get(indicatorType + ".0.index").(string),
					TimestampField: d.Get(indicatorType + ".0.timestamp_field").(string),
					Good: slo.IndicatorPropertiesCustomMetricParamsGood{
						Equation: d.Get(indicatorType + ".0.good.0.equation").(string),
						Metrics:  goodMetrics,
					},
					Total: slo.IndicatorPropertiesCustomMetricParamsTotal{
						Equation: d.Get(indicatorType + ".0.total.0.equation").(string),
						Metrics:  totalMetrics,
					},
				},
			},
		}

	default:
		return models.Slo{}, diag.Errorf("unknown indicator type %s", indicatorType)
	}

	timeWindow := slo.TimeWindow{
		Type:     d.Get("time_window.0.type").(string),
		Duration: d.Get("time_window.0.duration").(string),
	}

	objective := slo.Objective{
		Target:          d.Get("objective.0.target").(float64),
		TimesliceTarget: getOrNilFloat("objective.0.timeslice_target", d),
		TimesliceWindow: getOrNilString("objective.0.timeslice_window", d),
	}

	settings := slo.Settings{
		SyncDelay: getOrNilString("settings.0.sync_delay", d),
		Frequency: getOrNilString("settings.0.frequency", d),
	}

	budgetingMethod := slo.BudgetingMethod(d.Get("budgeting_method").(string))

	slo := models.Slo{
		Name:            d.Get("name").(string),
		Description:     d.Get("description").(string),
		Indicator:       indicator,
		TimeWindow:      timeWindow,
		BudgetingMethod: budgetingMethod,
		Objective:       objective,
		Settings:        &settings,
		SpaceID:         d.Get("space_id").(string),
	}

	// Explicitly set SLO object id if provided, otherwise we'll use the autogenerated ID from the Kibana API response
	if sloID := getOrNilString("slo_id", d); sloID != nil && *sloID != "" {
		slo.SloID = *sloID
	}

	if groupBy, ok := d.GetOk("group_by"); ok {
		for _, g := range groupBy.([]interface{}) {
			slo.GroupBy = append(slo.GroupBy, g.(string))
		}
	}

	if tags, ok := d.GetOk("tags"); ok {
		for _, t := range tags.([]interface{}) {
			slo.Tags = append(slo.Tags, t.(string))
		}
	}

	return slo, diags
}