Skip to content

Commit c94d7f9

Browse files
committed
requested changes
1 parent e607598 commit c94d7f9

File tree

9 files changed

+146
-43
lines changed

9 files changed

+146
-43
lines changed

internal/fleet/agent_policy/models.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"slices"
55

66
fleetapi "github.com/elastic/terraform-provider-elasticstack/generated/fleet"
7+
"github.com/elastic/terraform-provider-elasticstack/internal/utils"
78
"github.com/hashicorp/terraform-plugin-framework/types"
89
)
910

@@ -36,12 +37,20 @@ func (model *agentPolicyModel) populateFromAPI(data *fleetapi.AgentPolicy) {
3637
model.FleetServerHostId = types.StringPointerValue(data.FleetServerHostId)
3738

3839
if data.MonitoringEnabled != nil {
39-
model.MonitorLogs = types.BoolValue(slices.Contains(data.MonitoringEnabled, fleetapi.AgentPolicyMonitoringEnabledLogs))
40-
model.MonitorMetrics = types.BoolValue(slices.Contains(data.MonitoringEnabled, fleetapi.AgentPolicyMonitoringEnabledMetrics))
41-
} else {
42-
model.MonitorLogs = types.BoolNull()
43-
model.MonitorMetrics = types.BoolNull()
40+
if slices.Contains(data.MonitoringEnabled, fleetapi.AgentPolicyMonitoringEnabledLogs) {
41+
model.MonitorLogs = types.BoolValue(true)
42+
}
43+
if slices.Contains(data.MonitoringEnabled, fleetapi.AgentPolicyMonitoringEnabledMetrics) {
44+
model.MonitorMetrics = types.BoolValue(true)
45+
}
4446
}
47+
if !utils.IsKnown(model.MonitorLogs) {
48+
model.MonitorLogs = types.BoolValue(false)
49+
}
50+
if !utils.IsKnown(model.MonitorLogs) {
51+
model.MonitorLogs = types.BoolValue(false)
52+
}
53+
4554
model.MonitoringOutputId = types.StringPointerValue(data.MonitoringOutputId)
4655
model.Name = types.StringValue(data.Name)
4756
model.Namespace = types.StringValue(data.Namespace)

internal/fleet/agent_policy/resource_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,48 @@ import (
1919

2020
var minVersionAgentPolicy = version.Must(version.NewVersion("8.6.0"))
2121

22+
func TestAccResourceAgentPolicyFromSDK(t *testing.T) {
23+
policyName := sdkacctest.RandStringFromCharSet(22, sdkacctest.CharSetAlphaNum)
24+
25+
resource.Test(t, resource.TestCase{
26+
PreCheck: func() { acctest.PreCheck(t) },
27+
CheckDestroy: checkResourceAgentPolicyDestroy,
28+
Steps: []resource.TestStep{
29+
{
30+
ExternalProviders: map[string]resource.ExternalProvider{
31+
"elasticstack": {
32+
Source: "elastic/elasticstack",
33+
VersionConstraint: "0.11.7",
34+
},
35+
},
36+
SkipFunc: versionutils.CheckIfVersionIsUnsupported(minVersionAgentPolicy),
37+
Config: testAccResourceAgentPolicyCreate(policyName, false),
38+
Check: resource.ComposeTestCheckFunc(
39+
resource.TestCheckResourceAttr("elasticstack_fleet_agent_policy.test_policy", "name", fmt.Sprintf("Policy %s", policyName)),
40+
resource.TestCheckResourceAttr("elasticstack_fleet_agent_policy.test_policy", "namespace", "default"),
41+
resource.TestCheckResourceAttr("elasticstack_fleet_agent_policy.test_policy", "description", "Test Agent Policy"),
42+
resource.TestCheckResourceAttr("elasticstack_fleet_agent_policy.test_policy", "monitor_logs", "true"),
43+
resource.TestCheckResourceAttr("elasticstack_fleet_agent_policy.test_policy", "monitor_metrics", "false"),
44+
resource.TestCheckResourceAttr("elasticstack_fleet_agent_policy.test_policy", "skip_destroy", "false"),
45+
),
46+
},
47+
{
48+
ProtoV6ProviderFactories: acctest.Providers,
49+
SkipFunc: versionutils.CheckIfVersionIsUnsupported(minVersionAgentPolicy),
50+
Config: testAccResourceAgentPolicyCreate(policyName, false),
51+
Check: resource.ComposeTestCheckFunc(
52+
resource.TestCheckResourceAttr("elasticstack_fleet_agent_policy.test_policy", "name", fmt.Sprintf("Policy %s", policyName)),
53+
resource.TestCheckResourceAttr("elasticstack_fleet_agent_policy.test_policy", "namespace", "default"),
54+
resource.TestCheckResourceAttr("elasticstack_fleet_agent_policy.test_policy", "description", "Test Agent Policy"),
55+
resource.TestCheckResourceAttr("elasticstack_fleet_agent_policy.test_policy", "monitor_logs", "true"),
56+
resource.TestCheckResourceAttr("elasticstack_fleet_agent_policy.test_policy", "monitor_metrics", "false"),
57+
resource.TestCheckResourceAttr("elasticstack_fleet_agent_policy.test_policy", "skip_destroy", "false"),
58+
),
59+
},
60+
},
61+
})
62+
}
63+
2264
func TestAccResourceAgentPolicy(t *testing.T) {
2365
policyName := sdkacctest.RandStringFromCharSet(22, sdkacctest.CharSetAlphaNum)
2466

internal/fleet/agent_policy/schema.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/hashicorp/terraform-plugin-framework/resource"
77
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
8+
"github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"
89
"github.com/hashicorp/terraform-plugin-framework/resource/schema/boolplanmodifier"
910
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
1011
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
@@ -57,11 +58,13 @@ func (r *agentPolicyResource) Schema(ctx context.Context, req resource.SchemaReq
5758
Description: "Enable collection of agent logs.",
5859
Computed: true,
5960
Optional: true,
61+
Default: booldefault.StaticBool(false),
6062
},
6163
"monitor_metrics": schema.BoolAttribute{
6264
Description: "Enable collection of agent metrics.",
6365
Computed: true,
6466
Optional: true,
67+
Default: booldefault.StaticBool(false),
6568
},
6669
"skip_destroy": schema.BoolAttribute{
6770
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.",

internal/fleet/agent_policy/update.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ func (r *agentPolicyResource) Update(ctx context.Context, req resource.UpdateReq
2323
}
2424

2525
body := planModel.toAPIUpdateModel()
26+
2627
policyID := planModel.PolicyID.ValueString()
2728
policy, diags := fleet.UpdateAgentPolicy(ctx, client, policyID, body)
2829
resp.Diagnostics.Append(diags...)

internal/fleet/integration/create.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,42 @@ import (
44
"context"
55

66
"github.com/elastic/terraform-provider-elasticstack/internal/clients/fleet"
7+
"github.com/hashicorp/terraform-plugin-framework/diag"
78
"github.com/hashicorp/terraform-plugin-framework/resource"
9+
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
810
"github.com/hashicorp/terraform-plugin-framework/types"
911
)
1012

1113
func (r *integrationResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
14+
r.create(ctx, req.Plan, &resp.State, resp.Diagnostics)
15+
}
16+
17+
func (r integrationResource) create(ctx context.Context, plan tfsdk.Plan, state *tfsdk.State, respDiags diag.Diagnostics) {
1218
var planModel integrationModel
1319

14-
diags := req.Plan.Get(ctx, &planModel)
15-
resp.Diagnostics.Append(diags...)
16-
if resp.Diagnostics.HasError() {
20+
diags := plan.Get(ctx, &planModel)
21+
respDiags.Append(diags...)
22+
if respDiags.HasError() {
1723
return
1824
}
1925

2026
client, err := r.client.GetFleetClient()
2127
if err != nil {
22-
resp.Diagnostics.AddError(err.Error(), "")
28+
respDiags.AddError(err.Error(), "")
2329
return
2430
}
2531

2632
name := planModel.Name.ValueString()
2733
version := planModel.Version.ValueString()
2834
force := planModel.Force.ValueBool()
2935
diags = fleet.InstallPackage(ctx, client, name, version, force)
30-
resp.Diagnostics.Append(diags...)
31-
if resp.Diagnostics.HasError() {
36+
respDiags.Append(diags...)
37+
if respDiags.HasError() {
3238
return
3339
}
3440

3541
planModel.ID = types.StringValue(getPackageID(name, version))
3642

37-
diags = resp.State.Set(ctx, planModel)
38-
resp.Diagnostics.Append(diags...)
43+
diags = state.Set(ctx, planModel)
44+
respDiags.Append(diags...)
3945
}

internal/fleet/integration/resource_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,37 @@ import (
1212

1313
var minVersionIntegration = version.Must(version.NewVersion("8.6.0"))
1414

15+
func TestAccResourceIntegrationFromSDK(t *testing.T) {
16+
resource.Test(t, resource.TestCase{
17+
PreCheck: func() { acctest.PreCheck(t) },
18+
Steps: []resource.TestStep{
19+
{
20+
ExternalProviders: map[string]resource.ExternalProvider{
21+
"elasticstack": {
22+
Source: "elastic/elasticstack",
23+
VersionConstraint: "0.11.7",
24+
},
25+
},
26+
SkipFunc: versionutils.CheckIfVersionIsUnsupported(minVersionIntegration),
27+
Config: testAccResourceIntegration,
28+
Check: resource.ComposeTestCheckFunc(
29+
resource.TestCheckResourceAttr("elasticstack_fleet_integration.test_integration", "name", "tcp"),
30+
resource.TestCheckResourceAttr("elasticstack_fleet_integration.test_integration", "version", "1.16.0"),
31+
),
32+
},
33+
{
34+
ProtoV6ProviderFactories: acctest.Providers,
35+
SkipFunc: versionutils.CheckIfVersionIsUnsupported(minVersionIntegration),
36+
Config: testAccResourceIntegration,
37+
Check: resource.ComposeTestCheckFunc(
38+
resource.TestCheckResourceAttr("elasticstack_fleet_integration.test_integration", "name", "tcp"),
39+
resource.TestCheckResourceAttr("elasticstack_fleet_integration.test_integration", "version", "1.16.0"),
40+
),
41+
},
42+
},
43+
})
44+
}
45+
1546
func TestAccResourceIntegration(t *testing.T) {
1647
resource.Test(t, resource.TestCase{
1748
PreCheck: func() { acctest.PreCheck(t) },

internal/fleet/integration/update.go

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,9 @@ package integration
33
import (
44
"context"
55

6-
"github.com/elastic/terraform-provider-elasticstack/internal/clients/fleet"
76
"github.com/hashicorp/terraform-plugin-framework/resource"
8-
"github.com/hashicorp/terraform-plugin-framework/types"
97
)
108

119
func (r *integrationResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
12-
var planModel integrationModel
13-
14-
diags := req.Plan.Get(ctx, &planModel)
15-
resp.Diagnostics.Append(diags...)
16-
if resp.Diagnostics.HasError() {
17-
return
18-
}
19-
20-
client, err := r.client.GetFleetClient()
21-
if err != nil {
22-
resp.Diagnostics.AddError(err.Error(), "")
23-
return
24-
}
25-
26-
name := planModel.Name.ValueString()
27-
version := planModel.Version.ValueString()
28-
force := planModel.Force.ValueBool()
29-
diags = fleet.InstallPackage(ctx, client, name, version, force)
30-
resp.Diagnostics.Append(diags...)
31-
if resp.Diagnostics.HasError() {
32-
return
33-
}
34-
35-
planModel.ID = types.StringValue(getPackageID(name, version))
36-
37-
diags = resp.State.Set(ctx, planModel)
38-
resp.Diagnostics.Append(diags...)
10+
r.create(ctx, req.Plan, &resp.State, resp.Diagnostics)
3911
}

internal/fleet/server_host/models.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (model serverHostModel) toAPICreateModel(ctx context.Context) (body fleetap
4444

4545
func (model serverHostModel) toAPIUpdateModel(ctx context.Context) (body fleetapi.UpdateFleetServerHostsJSONRequestBody, diags diag.Diagnostics) {
4646
body = fleetapi.UpdateFleetServerHostsJSONRequestBody{
47-
HostUrls: utils.ListTypeToSlice_String(ctx, model.Hosts, path.Root("hosts"), diags),
47+
HostUrls: utils.Pointer(utils.ListTypeToSlice_String(ctx, model.Hosts, path.Root("hosts"), diags)),
4848
IsDefault: model.Default.ValueBoolPointer(),
4949
Name: model.Name.ValueStringPointer(),
5050
}

internal/fleet/server_host/resource_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,45 @@ import (
1818

1919
var minVersionFleetServerHost = version.Must(version.NewVersion("8.6.0"))
2020

21+
func TestAccResourceFleetServerHostFromSDK(t *testing.T) {
22+
policyName := sdkacctest.RandString(22)
23+
24+
resource.Test(t, resource.TestCase{
25+
PreCheck: func() { acctest.PreCheck(t) },
26+
CheckDestroy: checkResourceFleetServerHostDestroy,
27+
Steps: []resource.TestStep{
28+
{
29+
ExternalProviders: map[string]resource.ExternalProvider{
30+
"elasticstack": {
31+
Source: "elastic/elasticstack",
32+
VersionConstraint: "0.11.7",
33+
},
34+
},
35+
SkipFunc: versionutils.CheckIfVersionIsUnsupported(minVersionFleetServerHost),
36+
Config: testAccResourceFleetServerHostCreate(policyName),
37+
38+
Check: resource.ComposeTestCheckFunc(
39+
resource.TestCheckResourceAttr("elasticstack_fleet_server_host.test_host", "name", fmt.Sprintf("FleetServerHost %s", policyName)),
40+
resource.TestCheckResourceAttr("elasticstack_fleet_server_host.test_host", "id", "fleet-server-host-id"),
41+
resource.TestCheckResourceAttr("elasticstack_fleet_server_host.test_host", "default", "false"),
42+
resource.TestCheckResourceAttr("elasticstack_fleet_server_host.test_host", "hosts.0", "https://fleet-server:8220"),
43+
),
44+
},
45+
{
46+
ProtoV6ProviderFactories: acctest.Providers,
47+
SkipFunc: versionutils.CheckIfVersionIsUnsupported(minVersionFleetServerHost),
48+
Config: testAccResourceFleetServerHostCreate(policyName),
49+
Check: resource.ComposeTestCheckFunc(
50+
resource.TestCheckResourceAttr("elasticstack_fleet_server_host.test_host", "name", fmt.Sprintf("FleetServerHost %s", policyName)),
51+
resource.TestCheckResourceAttr("elasticstack_fleet_server_host.test_host", "id", "fleet-server-host-id"),
52+
resource.TestCheckResourceAttr("elasticstack_fleet_server_host.test_host", "default", "false"),
53+
resource.TestCheckResourceAttr("elasticstack_fleet_server_host.test_host", "hosts.0", "https://fleet-server:8220"),
54+
),
55+
},
56+
},
57+
})
58+
}
59+
2160
func TestAccResourceFleetServerHost(t *testing.T) {
2261
policyName := sdkacctest.RandString(22)
2362

0 commit comments

Comments
 (0)