func resourceSloRead()

in internal/kibana/slo.go [754:938]


func resourceSloRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
	client, diags := clients.NewApiClientFromSDKResource(d, meta)
	if diags.HasError() {
		return diags
	}
	compId, diags := clients.CompositeIdFromStr(d.Id())
	if diags.HasError() {
		return diags
	}
	id := compId.ResourceId
	spaceId := compId.ClusterId

	s, diags := kibana.GetSlo(ctx, client, id, spaceId)
	if s == nil && diags == nil {
		d.SetId("")
		return nil
	}
	if diags.HasError() {
		return diags
	}

	indicator := []interface{}{}
	var indicatorAddress string
	switch {
	case s.Indicator.IndicatorPropertiesApmAvailability != nil:
		indicatorAddress = indicatorTypeToAddress[s.Indicator.IndicatorPropertiesApmAvailability.Type]
		params := s.Indicator.IndicatorPropertiesApmAvailability.Params
		indicator = append(indicator, map[string]interface{}{
			"environment":      params.Environment,
			"service":          params.Service,
			"transaction_type": params.TransactionType,
			"transaction_name": params.TransactionName,
			"index":            params.Index,
			"filter":           params.Filter,
		})

	case s.Indicator.IndicatorPropertiesApmLatency != nil:
		indicatorAddress = indicatorTypeToAddress[s.Indicator.IndicatorPropertiesApmLatency.Type]
		params := s.Indicator.IndicatorPropertiesApmLatency.Params
		indicator = append(indicator, map[string]interface{}{
			"environment":      params.Environment,
			"service":          params.Service,
			"transaction_type": params.TransactionType,
			"transaction_name": params.TransactionName,
			"index":            params.Index,
			"filter":           params.Filter,
			"threshold":        params.Threshold,
		})

	case s.Indicator.IndicatorPropertiesCustomKql != nil:
		indicatorAddress = indicatorTypeToAddress[s.Indicator.IndicatorPropertiesCustomKql.Type]
		params := s.Indicator.IndicatorPropertiesCustomKql.Params
		indicator = append(indicator, map[string]interface{}{
			"index":           params.Index,
			"filter":          params.Filter,
			"good":            params.Good,
			"total":           params.Total,
			"timestamp_field": params.TimestampField,
		})

	case s.Indicator.IndicatorPropertiesHistogram != nil:
		indicatorAddress = indicatorTypeToAddress[s.Indicator.IndicatorPropertiesHistogram.Type]
		params := s.Indicator.IndicatorPropertiesHistogram.Params
		good := []map[string]interface{}{{
			"field":       params.Good.Field,
			"aggregation": params.Good.Aggregation,
			"filter":      params.Good.Filter,
			"from":        params.Good.From,
			"to":          params.Good.To,
		}}
		total := []map[string]interface{}{{
			"field":       params.Total.Field,
			"aggregation": params.Total.Aggregation,
			"filter":      params.Total.Filter,
			"from":        params.Total.From,
			"to":          params.Total.To,
		}}
		indicator = append(indicator, map[string]interface{}{
			"index":           params.Index,
			"filter":          params.Filter,
			"timestamp_field": params.TimestampField,
			"good":            good,
			"total":           total,
		})

	case s.Indicator.IndicatorPropertiesCustomMetric != nil:
		indicatorAddress = indicatorTypeToAddress[s.Indicator.IndicatorPropertiesCustomMetric.Type]
		params := s.Indicator.IndicatorPropertiesCustomMetric.Params
		goodMetrics := []map[string]interface{}{}
		for _, m := range params.Good.Metrics {
			goodMetrics = append(goodMetrics, map[string]interface{}{
				"name":        m.Name,
				"aggregation": m.Aggregation,
				"field":       m.Field,
				"filter":      m.Filter,
			})
		}
		good := []map[string]interface{}{{
			"equation": params.Good.Equation,
			"metrics":  goodMetrics,
		}}
		totalMetrics := []map[string]interface{}{}
		for _, m := range params.Total.Metrics {
			totalMetrics = append(totalMetrics, map[string]interface{}{
				"name":        m.Name,
				"aggregation": m.Aggregation,
				"field":       m.Field,
				"filter":      m.Filter,
			})
		}
		total := []map[string]interface{}{{
			"equation": params.Total.Equation,
			"metrics":  totalMetrics,
		}}
		indicator = append(indicator, map[string]interface{}{
			"index":           params.Index,
			"filter":          params.Filter,
			"timestamp_field": params.TimestampField,
			"good":            good,
			"total":           total,
		})

	default:
		return diag.Errorf("indicator not set")
	}

	if err := d.Set(indicatorAddress, indicator); err != nil {
		return diag.FromErr(err)
	}

	time_window := []interface{}{
		map[string]interface{}{
			"duration": s.TimeWindow.Duration,
			"type":     s.TimeWindow.Type,
		},
	}
	if err := d.Set("time_window", time_window); err != nil {
		return diag.FromErr(err)
	}

	objective := []interface{}{
		map[string]interface{}{
			"target":           s.Objective.Target,
			"timeslice_target": s.Objective.TimesliceTarget,
			"timeslice_window": s.Objective.TimesliceWindow,
		},
	}
	if err := d.Set("objective", objective); err != nil {
		return diag.FromErr(err)
	}

	if err := d.Set("settings", []interface{}{
		map[string]interface{}{
			"sync_delay": s.Settings.SyncDelay,
			"frequency":  s.Settings.Frequency,
		},
	}); err != nil {
		return diag.FromErr(err)
	}

	if err := d.Set("group_by", s.GroupBy); err != nil {
		return diag.FromErr(err)
	}

	if err := d.Set("slo_id", s.SloID); err != nil {
		return diag.FromErr(err)
	}
	if err := d.Set("space_id", s.SpaceID); err != nil {
		return diag.FromErr(err)
	}
	if err := d.Set("name", s.Name); err != nil {
		return diag.FromErr(err)
	}
	if err := d.Set("description", s.Description); err != nil {
		return diag.FromErr(err)
	}
	if err := d.Set("budgeting_method", s.BudgetingMethod); err != nil {
		return diag.FromErr(err)
	}
	if err := d.Set("tags", s.Tags); err != nil {
		return diag.FromErr(err)
	}

	return diags
}