Skip to content

Commit 0f10127

Browse files
committed
reflect instead
1 parent 80e5831 commit 0f10127

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

internal/fleet/agent_policy/models.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package agent_policy
22

33
import (
44
"context"
5+
"reflect"
56
"slices"
67

78
"github.com/elastic/terraform-provider-elasticstack/generated/kbapi"
@@ -32,6 +33,18 @@ func newGlobalDataTagModel(data struct {
3233
}
3334
}
3435

36+
func hasKey(s interface{}, key string) bool {
37+
val := reflect.ValueOf(s)
38+
if val.Kind() == reflect.Ptr {
39+
val = val.Elem()
40+
}
41+
if val.Kind() != reflect.Struct {
42+
return false
43+
}
44+
field := val.FieldByName(key)
45+
return field.IsValid()
46+
}
47+
3548
type agentPolicyModel struct {
3649
ID types.String `tfsdk:"id"`
3750
PolicyID types.String `tfsdk:"policy_id"`
@@ -79,7 +92,7 @@ func (model *agentPolicyModel) populateFromAPI(ctx context.Context, data *kbapi.
7992
model.MonitoringOutputId = types.StringPointerValue(data.MonitoringOutputId)
8093
model.Name = types.StringValue(data.Name)
8194
model.Namespace = types.StringValue(data.Namespace)
82-
if serverVersion.GreaterThanOrEqual(MinVersionGlobalDataTags) && len(*data.GlobalDataTags) > 0 {
95+
if serverVersion.GreaterThanOrEqual(MinVersionGlobalDataTags) && hasKey(data, "GlobalDataTags") {
8396
var diag diag.Diagnostics
8497
gdt := utils.SliceToListType(ctx, *data.GlobalDataTags, getGlobalDataTagsType(), path.Root("global_data_tags"), &diag, newGlobalDataTagModel)
8598
if diag.HasError() {

0 commit comments

Comments
 (0)