Skip to content

Commit 8764537

Browse files
committed
schema and model to support global data tags
1 parent bcc548e commit 8764537

File tree

5 files changed

+151
-75
lines changed

5 files changed

+151
-75
lines changed

internal/fleet/agent_policy/create.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func (r *agentPolicyResource) Create(ctx context.Context, req resource.CreateReq
2222
return
2323
}
2424

25-
body := planModel.toAPICreateModel()
25+
body := planModel.toAPICreateModel(ctx)
2626

2727
sysMonitoring := planModel.SysMonitoring.ValueBool()
2828
policy, diags := fleet.CreateAgentPolicy(ctx, client, body, sysMonitoring)
@@ -31,7 +31,7 @@ func (r *agentPolicyResource) Create(ctx context.Context, req resource.CreateReq
3131
return
3232
}
3333

34-
planModel.populateFromAPI(policy)
34+
planModel.populateFromAPI(ctx, policy)
3535

3636
resp.State.Set(ctx, planModel)
3737
resp.Diagnostics.Append(diags...)

internal/fleet/agent_policy/models.go

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,39 @@
11
package agent_policy
22

33
import (
4+
"context"
45
"slices"
56

67
"github.com/elastic/terraform-provider-elasticstack/generated/kbapi"
78
"github.com/elastic/terraform-provider-elasticstack/internal/utils"
9+
10+
"github.com/hashicorp/go-version"
11+
"github.com/hashicorp/terraform-plugin-framework/diag"
12+
"github.com/hashicorp/terraform-plugin-framework/path"
813
"github.com/hashicorp/terraform-plugin-framework/types"
914
)
1015

16+
type globalDataTagModel struct {
17+
Name types.String `tfsdk:"name"`
18+
Value types.String `tfsdk:"value"`
19+
}
20+
21+
func newGlobalDataTagModel(data struct {
22+
Name string "json:\"name\""
23+
Value kbapi.AgentPolicy_GlobalDataTags_Value "json:\"value\""
24+
}, meta utils.ListMeta) globalDataTagModel {
25+
val, err := data.Value.AsAgentPolicyGlobalDataTagsValue0()
26+
if err != nil {
27+
panic(err)
28+
}
29+
return globalDataTagModel{
30+
Name: types.StringValue(data.Name),
31+
Value: types.StringValue(val),
32+
}
33+
}
34+
35+
var minVersionGlobalDataTags = version.Must(version.NewVersion("8.15.0"))
36+
1137
type agentPolicyModel struct {
1238
ID types.String `tfsdk:"id"`
1339
PolicyID types.String `tfsdk:"policy_id"`
@@ -22,9 +48,10 @@ type agentPolicyModel struct {
2248
MonitorMetrics types.Bool `tfsdk:"monitor_metrics"`
2349
SysMonitoring types.Bool `tfsdk:"sys_monitoring"`
2450
SkipDestroy types.Bool `tfsdk:"skip_destroy"`
51+
GlobalDataTags types.List `tfsdk:"global_data_tags"`
2552
}
2653

27-
func (model *agentPolicyModel) populateFromAPI(data *kbapi.AgentPolicy) {
54+
func (model *agentPolicyModel) populateFromAPI(ctx context.Context, data *kbapi.AgentPolicy) (diags diag.Diagnostics) {
2855
if data == nil {
2956
return
3057
}
@@ -54,10 +81,19 @@ func (model *agentPolicyModel) populateFromAPI(data *kbapi.AgentPolicy) {
5481
model.MonitoringOutputId = types.StringPointerValue(data.MonitoringOutputId)
5582
model.Name = types.StringValue(data.Name)
5683
model.Namespace = types.StringValue(data.Namespace)
84+
if *data.GlobalDataTags != nil {
85+
model.GlobalDataTags = utils.SliceToListType(ctx, *data.GlobalDataTags, getGlobalDataTagsType(), path.Root("global_data_tags"), &diags, newGlobalDataTagModel)
86+
}
87+
return
5788
}
5889

59-
func (model agentPolicyModel) toAPICreateModel() kbapi.PostFleetAgentPoliciesJSONRequestBody {
90+
func (model agentPolicyModel) toAPICreateModel(ctx context.Context) kbapi.PostFleetAgentPoliciesJSONRequestBody {
6091
monitoring := make([]kbapi.PostFleetAgentPoliciesJSONBodyMonitoringEnabled, 0, 2)
92+
tags := make([]struct {
93+
Name string `json:"name"`
94+
Value kbapi.PostFleetAgentPoliciesJSONBody_GlobalDataTags_Value `json:"value"`
95+
}, 0, len(model.GlobalDataTags.ElementsAs(ctx, getGlobalDataTagsType, false)))
96+
6197
if model.MonitorLogs.ValueBool() {
6298
monitoring = append(monitoring, kbapi.PostFleetAgentPoliciesJSONBodyMonitoringEnabledLogs)
6399
}
@@ -77,10 +113,14 @@ func (model agentPolicyModel) toAPICreateModel() kbapi.PostFleetAgentPoliciesJSO
77113
Namespace: model.Namespace.ValueString(),
78114
}
79115

116+
if len(tags) > 0 {
117+
body.GlobalDataTags = &tags
118+
}
119+
80120
return body
81121
}
82122

83-
func (model agentPolicyModel) toAPIUpdateModel() kbapi.PutFleetAgentPoliciesAgentpolicyidJSONRequestBody {
123+
func (model agentPolicyModel) toAPIUpdateModel(ctx context.Context) kbapi.PutFleetAgentPoliciesAgentpolicyidJSONRequestBody {
84124
monitoring := make([]kbapi.PutFleetAgentPoliciesAgentpolicyidJSONBodyMonitoringEnabled, 0, 2)
85125
if model.MonitorLogs.ValueBool() {
86126
monitoring = append(monitoring, kbapi.Logs)
@@ -89,6 +129,11 @@ func (model agentPolicyModel) toAPIUpdateModel() kbapi.PutFleetAgentPoliciesAgen
89129
monitoring = append(monitoring, kbapi.Metrics)
90130
}
91131

132+
tags := make([]struct {
133+
Name string `json:"name"`
134+
Value kbapi.PutFleetAgentPoliciesAgentpolicyidJSONBody_GlobalDataTags_Value `json:"value"`
135+
}, 0, len(model.GlobalDataTags.ElementsAs(ctx, getGlobalDataTagsType, false)))
136+
92137
body := kbapi.PutFleetAgentPoliciesAgentpolicyidJSONRequestBody{
93138
DataOutputId: model.DataOutputId.ValueStringPointer(),
94139
Description: model.Description.ValueStringPointer(),
@@ -100,5 +145,9 @@ func (model agentPolicyModel) toAPIUpdateModel() kbapi.PutFleetAgentPoliciesAgen
100145
Namespace: model.Namespace.ValueString(),
101146
}
102147

148+
if len(tags) > 0 {
149+
body.GlobalDataTags = &tags
150+
}
151+
103152
return body
104153
}

internal/fleet/agent_policy/read.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func (r *agentPolicyResource) Read(ctx context.Context, req resource.ReadRequest
3434
return
3535
}
3636

37-
stateModel.populateFromAPI(policy)
37+
stateModel.populateFromAPI(ctx, policy)
3838

3939
resp.State.Set(ctx, stateModel)
4040
resp.Diagnostics.Append(diags...)

internal/fleet/agent_policy/schema.go

Lines changed: 94 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package agent_policy
33
import (
44
"context"
55

6+
"github.com/hashicorp/terraform-plugin-framework/attr"
67
"github.com/hashicorp/terraform-plugin-framework/resource"
78
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
89
"github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"
@@ -12,74 +13,100 @@ import (
1213
)
1314

1415
func (r *agentPolicyResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
15-
resp.Schema.Description = "Creates a new Fleet Agent Policy. See https://www.elastic.co/guide/en/fleet/current/agent-policy.html"
16-
resp.Schema.Attributes = map[string]schema.Attribute{
17-
"id": schema.StringAttribute{
18-
Description: "The ID of this resource.",
19-
Computed: true,
20-
PlanModifiers: []planmodifier.String{
21-
stringplanmodifier.UseStateForUnknown(),
16+
resp.Schema = getSchema()
17+
}
18+
19+
func getSchema() schema.Schema {
20+
return schema.Schema{
21+
Description: "Creates a new Fleet Agent Policy. See https://www.elastic.co/guide/en/fleet/current/agent-policy.html",
22+
Attributes: map[string]schema.Attribute{
23+
"id": schema.StringAttribute{
24+
Description: "The ID of this resource.",
25+
Computed: true,
26+
PlanModifiers: []planmodifier.String{
27+
stringplanmodifier.UseStateForUnknown(),
28+
},
29+
},
30+
"policy_id": schema.StringAttribute{
31+
Description: "Unique identifier of the agent policy.",
32+
Computed: true,
33+
Optional: true,
34+
PlanModifiers: []planmodifier.String{
35+
stringplanmodifier.UseStateForUnknown(),
36+
stringplanmodifier.RequiresReplace(),
37+
},
38+
},
39+
"name": schema.StringAttribute{
40+
Description: "The name of the agent policy.",
41+
Required: true,
42+
},
43+
"namespace": schema.StringAttribute{
44+
Description: "The namespace of the agent policy.",
45+
Required: true,
46+
},
47+
"description": schema.StringAttribute{
48+
Description: "The description of the agent policy.",
49+
Optional: true,
50+
},
51+
"data_output_id": schema.StringAttribute{
52+
Description: "The identifier for the data output.",
53+
Optional: true,
54+
},
55+
"monitoring_output_id": schema.StringAttribute{
56+
Description: "The identifier for monitoring output.",
57+
Optional: true,
2258
},
23-
},
24-
"policy_id": schema.StringAttribute{
25-
Description: "Unique identifier of the agent policy.",
26-
Computed: true,
27-
Optional: true,
28-
PlanModifiers: []planmodifier.String{
29-
stringplanmodifier.UseStateForUnknown(),
30-
stringplanmodifier.RequiresReplace(),
59+
"fleet_server_host_id": schema.StringAttribute{
60+
Description: "The identifier for the Fleet server host.",
61+
Optional: true,
3162
},
32-
},
33-
"name": schema.StringAttribute{
34-
Description: "The name of the agent policy.",
35-
Required: true,
36-
},
37-
"namespace": schema.StringAttribute{
38-
Description: "The namespace of the agent policy.",
39-
Required: true,
40-
},
41-
"description": schema.StringAttribute{
42-
Description: "The description of the agent policy.",
43-
Optional: true,
44-
},
45-
"data_output_id": schema.StringAttribute{
46-
Description: "The identifier for the data output.",
47-
Optional: true,
48-
},
49-
"monitoring_output_id": schema.StringAttribute{
50-
Description: "The identifier for monitoring output.",
51-
Optional: true,
52-
},
53-
"fleet_server_host_id": schema.StringAttribute{
54-
Description: "The identifier for the Fleet server host.",
55-
Optional: true,
56-
},
57-
"download_source_id": schema.StringAttribute{
58-
Description: "The identifier for the Elastic Agent binary download server.",
59-
Optional: true,
60-
},
61-
"monitor_logs": schema.BoolAttribute{
62-
Description: "Enable collection of agent logs.",
63-
Computed: true,
64-
Optional: true,
65-
Default: booldefault.StaticBool(false),
66-
},
67-
"monitor_metrics": schema.BoolAttribute{
68-
Description: "Enable collection of agent metrics.",
69-
Computed: true,
70-
Optional: true,
71-
Default: booldefault.StaticBool(false),
72-
},
73-
"skip_destroy": schema.BoolAttribute{
74-
Description: "Set to true if you do not wish the agent policy to be deleted at destroy time, and instead just remove the agent policy from the Terraform state.",
75-
Optional: true,
76-
},
77-
"sys_monitoring": schema.BoolAttribute{
78-
Description: "Enable collection of system logs and metrics.",
79-
Optional: true,
80-
PlanModifiers: []planmodifier.Bool{
81-
boolplanmodifier.RequiresReplace(),
63+
"download_source_id": schema.StringAttribute{
64+
Description: "The identifier for the Elastic Agent binary download server.",
65+
Optional: true,
8266
},
83-
},
84-
}
67+
"monitor_logs": schema.BoolAttribute{
68+
Description: "Enable collection of agent logs.",
69+
Computed: true,
70+
Optional: true,
71+
Default: booldefault.StaticBool(false),
72+
},
73+
"monitor_metrics": schema.BoolAttribute{
74+
Description: "Enable collection of agent metrics.",
75+
Computed: true,
76+
Optional: true,
77+
Default: booldefault.StaticBool(false),
78+
},
79+
"skip_destroy": schema.BoolAttribute{
80+
Description: "Set to true if you do not wish the agent policy to be deleted at destroy time, and instead just remove the agent policy from the Terraform state.",
81+
Optional: true,
82+
},
83+
"sys_monitoring": schema.BoolAttribute{
84+
Description: "Enable collection of system logs and metrics.",
85+
Optional: true,
86+
PlanModifiers: []planmodifier.Bool{
87+
boolplanmodifier.RequiresReplace(),
88+
},
89+
},
90+
"global_data_tags": schema.ListNestedAttribute{
91+
Description: "User defined data tags to apply to all inputs. Values can be strings, or numbers",
92+
Optional: true,
93+
94+
NestedObject: schema.NestedAttributeObject{
95+
Attributes: map[string]schema.Attribute{
96+
"name": schema.StringAttribute{
97+
Description: "The name of the data tag.",
98+
Required: true,
99+
},
100+
"value": schema.StringAttribute{
101+
Description: "The value of the data tag.",
102+
Required: true,
103+
},
104+
},
105+
},
106+
},
107+
}}
108+
}
109+
110+
func getGlobalDataTagsType() attr.Type {
111+
return getSchema().Attributes["global_data_tags"].GetType().(attr.TypeWithElementType).ElementType()
85112
}

internal/fleet/agent_policy/update.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func (r *agentPolicyResource) Update(ctx context.Context, req resource.UpdateReq
2222
return
2323
}
2424

25-
body := planModel.toAPIUpdateModel()
25+
body := planModel.toAPIUpdateModel(ctx)
2626

2727
policyID := planModel.PolicyID.ValueString()
2828
policy, diags := fleet.UpdateAgentPolicy(ctx, client, policyID, body)
@@ -31,7 +31,7 @@ func (r *agentPolicyResource) Update(ctx context.Context, req resource.UpdateReq
3131
return
3232
}
3333

34-
planModel.populateFromAPI(policy)
34+
planModel.populateFromAPI(ctx, policy)
3535

3636
diags = resp.State.Set(ctx, planModel)
3737
resp.Diagnostics.Append(diags...)

0 commit comments

Comments
 (0)