Skip to content

Commit 914c338

Browse files
committed
WIP
1 parent 5638a59 commit 914c338

File tree

5 files changed

+444
-0
lines changed

5 files changed

+444
-0
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
package maintenance_window_test
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/elastic/terraform-provider-elasticstack/internal/acctest"
8+
"github.com/elastic/terraform-provider-elasticstack/internal/versionutils"
9+
"github.com/hashicorp/go-version"
10+
sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
12+
)
13+
14+
var minDataViewAPISupport = version.Must(version.NewVersion("8.1.0"))
15+
var minFullDataviewSupport = version.Must(version.NewVersion("8.8.0"))
16+
17+
func TestAccResourceDataView(t *testing.T) {
18+
indexName := "my-index-" + sdkacctest.RandStringFromCharSet(4, sdkacctest.CharSetAlphaNum)
19+
20+
resource.Test(t, resource.TestCase{
21+
PreCheck: func() { acctest.PreCheck(t) },
22+
ProtoV6ProviderFactories: acctest.Providers,
23+
Steps: []resource.TestStep{
24+
{
25+
SkipFunc: versionutils.CheckIfVersionIsUnsupported(minDataViewAPISupport),
26+
Config: testAccResourceDataViewPre8_8DV(indexName),
27+
Check: resource.ComposeTestCheckFunc(
28+
resource.TestCheckResourceAttrSet("elasticstack_kibana_data_view.dv", "id"),
29+
),
30+
},
31+
{
32+
SkipFunc: versionutils.CheckIfVersionIsUnsupported(minFullDataviewSupport),
33+
Config: testAccResourceDataViewBasicDV(indexName),
34+
Check: resource.ComposeTestCheckFunc(
35+
resource.TestCheckResourceAttrSet("elasticstack_kibana_data_view.dv", "id"),
36+
resource.TestCheckResourceAttr("elasticstack_kibana_data_view.dv", "override", "true"),
37+
resource.TestCheckResourceAttr("elasticstack_kibana_data_view.dv", "data_view.name", indexName),
38+
resource.TestCheckResourceAttr("elasticstack_kibana_data_view.dv", "data_view.source_filters.#", "2"),
39+
resource.TestCheckResourceAttr("elasticstack_kibana_data_view.dv", "data_view.field_formats.event_time.id", "date_nanos"),
40+
resource.TestCheckResourceAttr("elasticstack_kibana_data_view.dv", "data_view.field_formats.machine.ram.params.pattern", "0,0.[000] b"),
41+
resource.TestCheckResourceAttr("elasticstack_kibana_data_view.dv", "data_view.runtime_field_map.runtime_shape_name.script_source", "emit(doc['shape_name'].value)"),
42+
resource.TestCheckResourceAttr("elasticstack_kibana_data_view.dv", "data_view.field_attrs.ingest_failure.custom_label", "error.ingest_failure"),
43+
),
44+
},
45+
{
46+
SkipFunc: versionutils.CheckIfVersionIsUnsupported(minFullDataviewSupport),
47+
Config: testAccResourceDataViewBasicDVUpdated(indexName),
48+
Check: resource.ComposeTestCheckFunc(
49+
resource.TestCheckResourceAttrSet("elasticstack_kibana_data_view.dv", "id"),
50+
resource.TestCheckResourceAttr("elasticstack_kibana_data_view.dv", "override", "false"),
51+
resource.TestCheckResourceAttr("elasticstack_kibana_data_view.dv", "data_view.name", indexName),
52+
resource.TestCheckNoResourceAttr("elasticstack_kibana_data_view.dv", "data_view.source_filters"),
53+
resource.TestCheckNoResourceAttr("elasticstack_kibana_data_view.dv", "data_view.field_formats"),
54+
resource.TestCheckNoResourceAttr("elasticstack_kibana_data_view.dv", "data_view.runtime_field_map"),
55+
),
56+
},
57+
{
58+
SkipFunc: versionutils.CheckIfVersionIsUnsupported(minFullDataviewSupport),
59+
Config: testAccResourceDataViewBasicDVUpdated(indexName),
60+
ImportState: true,
61+
ImportStateVerify: true,
62+
ResourceName: "elasticstack_kibana_data_view.dv",
63+
},
64+
},
65+
})
66+
}
67+
68+
func testAccResourceDataViewPre8_8DV(indexName string) string {
69+
return fmt.Sprintf(`
70+
provider "elasticstack" {
71+
elasticsearch {}
72+
kibana {}
73+
}
74+
75+
resource "elasticstack_elasticsearch_index" "my_index" {
76+
name = "%s"
77+
deletion_protection = false
78+
}
79+
80+
resource "elasticstack_kibana_data_view" "dv" {
81+
data_view = {
82+
title = "%s*"
83+
}
84+
}`, indexName, indexName)
85+
}
86+
87+
func testAccResourceDataViewBasicDV(indexName string) string {
88+
return fmt.Sprintf(`
89+
provider "elasticstack" {
90+
elasticsearch {}
91+
kibana {}
92+
}
93+
94+
resource "elasticstack_elasticsearch_index" "my_index" {
95+
name = "%s"
96+
deletion_protection = false
97+
}
98+
99+
resource "elasticstack_kibana_data_view" "dv" {
100+
override = true
101+
data_view = {
102+
title = "%s*"
103+
name = "%s"
104+
time_field_name = "@timestamp"
105+
source_filters = ["event_time", "machine.ram"]
106+
allow_no_index = true
107+
namespaces = ["default", "foo", "bar"]
108+
field_formats = {
109+
event_time = {
110+
id = "date_nanos"
111+
}
112+
"machine.ram" = {
113+
id = "number"
114+
params = {
115+
pattern = "0,0.[000] b"
116+
}
117+
}
118+
}
119+
runtime_field_map = {
120+
runtime_shape_name = {
121+
type = "keyword"
122+
script_source = "emit(doc['shape_name'].value)"
123+
}
124+
}
125+
field_attrs = {
126+
ingest_failure = { custom_label = "error.ingest_failure", count = 6 },
127+
}
128+
}
129+
}`, indexName, indexName, indexName)
130+
}
131+
132+
func testAccResourceDataViewBasicDVUpdated(indexName string) string {
133+
return fmt.Sprintf(`
134+
provider "elasticstack" {
135+
elasticsearch {}
136+
kibana {}
137+
}
138+
139+
resource "elasticstack_elasticsearch_index" "my_index" {
140+
name = "%s"
141+
deletion_protection = false
142+
}
143+
144+
resource "elasticstack_kibana_data_view" "dv" {
145+
override = false
146+
data_view = {
147+
title = "%s*"
148+
name = "%s"
149+
time_field_name = "@timestamp"
150+
allow_no_index = true
151+
}
152+
}`, indexName, indexName, indexName)
153+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package maintenance_window
2+
3+
import (
4+
"context"
5+
6+
"github.com/elastic/terraform-provider-elasticstack/internal/clients/kibana_oapi"
7+
"github.com/hashicorp/terraform-plugin-framework/resource"
8+
)
9+
10+
func (r *MaintenanceWindowResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
11+
var planModel MaintenanceWindowModel
12+
13+
diags := req.Plan.Get(ctx, &planModel)
14+
resp.Diagnostics.Append(diags...)
15+
if resp.Diagnostics.HasError() {
16+
return
17+
}
18+
19+
// body, diags := planModel.toAPICreateModel(ctx)
20+
// resp.Diagnostics.Append(diags...)
21+
// if resp.Diagnostics.HasError() {
22+
// return
23+
// }
24+
25+
// client, err := r.client.GetKibanaOapiClient()
26+
// if err != nil {
27+
// resp.Diagnostics.AddError(err.Error(), "")
28+
// return
29+
// }
30+
31+
// spaceID := planModel.SpaceID.ValueString()
32+
maintenance_window, diags := kibana_oapi.GetMaintenanceWindow()
33+
resp.Diagnostics.Append(diags...)
34+
if resp.Diagnostics.HasError() {
35+
return
36+
}
37+
38+
// diags = planModel.populateFromAPI(ctx, dataView, spaceID)
39+
// resp.Diagnostics.Append(diags...)
40+
// if resp.Diagnostics.HasError() {
41+
// return
42+
// }
43+
44+
// diags = resp.State.Set(ctx, planModel)
45+
// resp.Diagnostics.Append(diags...)
46+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package maintenance_window
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-framework/types"
5+
)
6+
7+
type MaintenanceWindowModel struct {
8+
ID types.String `tfsdk:"id"`
9+
SpaceID types.String `tfsdk:"space_id"`
10+
Title types.String `tfsdk:"title"`
11+
Enabled types.Bool `tfsdk:"enabled"`
12+
CustomSchedule MaintenanceWindowSchedule `tfsdk:"custom_schedule"`
13+
Scope MaintenanceWindowScope `tfsdk:"scope"`
14+
}
15+
16+
type MaintenanceWindowScope struct {
17+
Alerting MaintenanceWindowAlertingScope `tfsdk:"alerting"`
18+
}
19+
20+
type MaintenanceWindowAlertingScope struct {
21+
Kql types.String `tfsdk:"kql"`
22+
}
23+
24+
type MaintenanceWindowSchedule struct {
25+
Start types.String `tfsdk:"start"`
26+
Duration types.String `tfsdk:"duration"`
27+
Timezone types.String `tfsdk:"timezone"`
28+
// Recurring *MaintenanceWindowScheduleRecurring
29+
}
30+
31+
// type MaintenanceWindowScheduleRecurring struct {
32+
// End *string
33+
// Every *string
34+
// OnWeekDay *[]string
35+
// OnMonthDay *[]float32
36+
// OnMonth *[]float32
37+
// }
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package maintenance_window
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/elastic/terraform-provider-elasticstack/internal/clients"
8+
"github.com/hashicorp/terraform-plugin-framework/resource"
9+
)
10+
11+
var (
12+
_ resource.Resource = &MaintenanceWindowResource{}
13+
_ resource.ResourceWithConfigure = &MaintenanceWindowResource{}
14+
// _ resource.ResourceWithImportState = &MaintenanceWindowResource{}
15+
)
16+
17+
// NewResource is a helper function to simplify the provider implementation.
18+
func NewResource() resource.Resource {
19+
return &MaintenanceWindowResource{}
20+
}
21+
22+
type MaintenanceWindowResource struct {
23+
client *clients.ApiClient
24+
}
25+
26+
func (r *MaintenanceWindowResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
27+
client, diags := clients.ConvertProviderData(req.ProviderData)
28+
resp.Diagnostics.Append(diags...)
29+
r.client = client
30+
}
31+
32+
// Metadata returns the provider type name.
33+
func (r *MaintenanceWindowResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
34+
resp.TypeName = fmt.Sprintf("%s_%s", req.ProviderTypeName, "kibana_maintenance_window_2")
35+
}
36+
37+
// func (r *MaintenanceWindowResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
38+
// composite, diags := clients.CompositeIdFromStrFw(req.ID)
39+
// resp.Diagnostics.Append(diags...)
40+
// if diags.HasError() {
41+
// return
42+
// }
43+
44+
// stateModel := maintenanceWindowModel{
45+
// ID: types.StringValue(req.ID),
46+
// SpaceID: types.StringValue(composite.ClusterId),
47+
// Override: types.BoolValue(false),
48+
// MaintenanceWindow: types.ObjectUnknown(getMaintenanceWindowAttrTypes()),
49+
// }
50+
51+
// diags = resp.State.Set(ctx, stateModel)
52+
// resp.Diagnostics.Append(diags...)
53+
// }

0 commit comments

Comments
 (0)