| 
1 | 1 | package maintenance_window_test  | 
2 | 2 | 
 
  | 
3 | 3 | import (  | 
4 |  | -	"fmt"  | 
5 | 4 | 	"testing"  | 
6 | 5 | 
 
  | 
7 | 6 | 	"github.com/elastic/terraform-provider-elasticstack/internal/acctest"  | 
8 | 7 | 	"github.com/elastic/terraform-provider-elasticstack/internal/versionutils"  | 
9 | 8 | 	"github.com/hashicorp/go-version"  | 
10 |  | -	sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"  | 
11 | 9 | 	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"  | 
12 | 10 | )  | 
13 | 11 | 
 
  | 
14 |  | -var minDataViewAPISupport = version.Must(version.NewVersion("8.1.0"))  | 
15 |  | -var minFullDataviewSupport = version.Must(version.NewVersion("8.8.0"))  | 
 | 12 | +var minMaintenanceWindowAPISupport = version.Must(version.NewVersion("9.1.0"))  | 
16 | 13 | 
 
  | 
17 |  | -func TestAccResourceDataView(t *testing.T) {  | 
18 |  | -	indexName := "my-index-" + sdkacctest.RandStringFromCharSet(4, sdkacctest.CharSetAlphaNum)  | 
 | 14 | +func TestAccResourceMaintenanceWindow(t *testing.T) {  | 
19 | 15 | 
 
  | 
20 | 16 | 	resource.Test(t, resource.TestCase{  | 
21 | 17 | 		PreCheck:                 func() { acctest.PreCheck(t) },  | 
22 | 18 | 		ProtoV6ProviderFactories: acctest.Providers,  | 
23 | 19 | 		Steps: []resource.TestStep{  | 
24 | 20 | 			{  | 
25 |  | -				SkipFunc: versionutils.CheckIfVersionIsUnsupported(minDataViewAPISupport),  | 
26 |  | -				Config:   testAccResourceDataViewPre8_8DV(indexName),  | 
 | 21 | +				SkipFunc: versionutils.CheckIfVersionIsUnsupported(minMaintenanceWindowAPISupport),  | 
 | 22 | +				Config:   testAccResourceMaintenanceWindowCreate,  | 
27 | 23 | 				Check: resource.ComposeTestCheckFunc(  | 
28 |  | -					resource.TestCheckResourceAttrSet("elasticstack_kibana_data_view.dv", "id"),  | 
 | 24 | +					resource.TestCheckResourceAttr("elasticstack_kibana_maintenance_window.test_maintenance_window", "title", "Terraform Maintenance Window"),  | 
 | 25 | +					resource.TestCheckResourceAttr("elasticstack_kibana_maintenance_window.test_maintenance_window", "enabled", "true"),  | 
 | 26 | +					resource.TestCheckResourceAttr("elasticstack_kibana_maintenance_window.test_maintenance_window", "custom_schedule.0.start", "1992-01-01T05:00:00.200Z"),  | 
 | 27 | +					resource.TestCheckResourceAttr("elasticstack_kibana_maintenance_window.test_maintenance_window", "custom_schedule.0.duration", "10d"),  | 
 | 28 | +					resource.TestCheckResourceAttr("elasticstack_kibana_maintenance_window.test_maintenance_window", "custom_schedule.0.timezone", "UTC"),  | 
 | 29 | +					resource.TestCheckResourceAttr("elasticstack_kibana_maintenance_window.test_maintenance_window", "custom_schedule.0.recurring.0.every", "20d"),  | 
 | 30 | +					resource.TestCheckResourceAttr("elasticstack_kibana_maintenance_window.test_maintenance_window", "custom_schedule.0.recurring.0.end", "2029-05-17T05:05:00.000Z"),  | 
 | 31 | +					resource.TestCheckResourceAttr("elasticstack_kibana_maintenance_window.test_maintenance_window", "custom_schedule.0.recurring.0.on_week_day.0", "MO"),  | 
 | 32 | +					resource.TestCheckResourceAttr("elasticstack_kibana_maintenance_window.test_maintenance_window", "custom_schedule.0.recurring.0.on_week_day.1", "TU"),  | 
 | 33 | +					resource.TestCheckResourceAttr("elasticstack_kibana_maintenance_window.test_maintenance_window", "scope.0.alerting.0.kql", "_id: '1234'"),  | 
29 | 34 | 				),  | 
30 | 35 | 			},  | 
31 | 36 | 			{  | 
32 |  | -				SkipFunc: versionutils.CheckIfVersionIsUnsupported(minFullDataviewSupport),  | 
33 |  | -				Config:   testAccResourceDataViewBasicDV(indexName),  | 
 | 37 | +				SkipFunc: versionutils.CheckIfVersionIsUnsupported(minMaintenanceWindowAPISupport),  | 
 | 38 | +				Config:   testAccResourceMaintenanceWindowUpdate,  | 
34 | 39 | 				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"),  | 
 | 40 | +					resource.TestCheckResourceAttr("elasticstack_kibana_maintenance_window.test_maintenance_window", "title", "Terraform Maintenance Window UPDATED"),  | 
 | 41 | +					resource.TestCheckResourceAttr("elasticstack_kibana_maintenance_window.test_maintenance_window", "enabled", "false"),  | 
 | 42 | +					resource.TestCheckResourceAttr("elasticstack_kibana_maintenance_window.test_maintenance_window", "custom_schedule.0.start", "1999-02-02T05:00:00.200Z"),  | 
 | 43 | +					resource.TestCheckResourceAttr("elasticstack_kibana_maintenance_window.test_maintenance_window", "custom_schedule.0.duration", "12d"),  | 
 | 44 | +					resource.TestCheckResourceAttr("elasticstack_kibana_maintenance_window.test_maintenance_window", "custom_schedule.0.timezone", "Asia/Taipei"),  | 
 | 45 | +					resource.TestCheckResourceAttr("elasticstack_kibana_maintenance_window.test_maintenance_window", "custom_schedule.0.recurring.0.every", "21d"),  | 
 | 46 | +					resource.TestCheckResourceAttr("elasticstack_kibana_maintenance_window.test_maintenance_window", "custom_schedule.0.recurring.0.end", ""),  | 
 | 47 | +					resource.TestCheckResourceAttr("elasticstack_kibana_maintenance_window.test_maintenance_window", "custom_schedule.0.recurring.0.on_month_day.0", "1"),  | 
 | 48 | +					resource.TestCheckResourceAttr("elasticstack_kibana_maintenance_window.test_maintenance_window", "custom_schedule.0.recurring.0.on_month_day.1", "2"),  | 
 | 49 | +					resource.TestCheckResourceAttr("elasticstack_kibana_maintenance_window.test_maintenance_window", "custom_schedule.0.recurring.0.on_month_day.2", "3"),  | 
 | 50 | +					resource.TestCheckResourceAttr("elasticstack_kibana_maintenance_window.test_maintenance_window", "custom_schedule.0.recurring.0.on_month.0", "4"),  | 
 | 51 | +					resource.TestCheckResourceAttr("elasticstack_kibana_maintenance_window.test_maintenance_window", "custom_schedule.0.recurring.0.on_month.1", "5"),  | 
 | 52 | +					resource.TestCheckResourceAttr("elasticstack_kibana_maintenance_window.test_maintenance_window", "scope.0.alerting.0.kql", "_id: 'foobar'"),  | 
43 | 53 | 				),  | 
44 | 54 | 			},  | 
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 | 55 | 		},  | 
65 | 56 | 	})  | 
66 | 57 | }  | 
67 | 58 | 
 
  | 
68 |  | -func testAccResourceDataViewPre8_8DV(indexName string) string {  | 
69 |  | -	return fmt.Sprintf(`  | 
 | 59 | +const testAccResourceMaintenanceWindowCreate = `  | 
70 | 60 | provider "elasticstack" {  | 
71 |  | -	elasticsearch {}  | 
72 |  | -	kibana {}  | 
 | 61 | +  elasticsearch {}  | 
 | 62 | +  kibana {}  | 
73 | 63 | }  | 
74 | 64 | 
  | 
75 |  | -resource "elasticstack_elasticsearch_index" "my_index" {  | 
76 |  | -	name                = "%s"  | 
77 |  | -	deletion_protection = false  | 
78 |  | -}  | 
 | 65 | +resource "elasticstack_kibana_maintenance_window" "test_maintenance_window" {  | 
 | 66 | +  title   	      = "Terraform Maintenance Window"  | 
 | 67 | +  enabled 	      = true  | 
79 | 68 | 
  | 
80 |  | -resource "elasticstack_kibana_data_view" "dv" {  | 
81 |  | -	data_view = {  | 
82 |  | -	  title = "%s*"  | 
83 |  | -	}  | 
84 |  | -}`, indexName, indexName)  | 
85 |  | -}  | 
 | 69 | +  custom_schedule = {  | 
 | 70 | +    start         = "1992-01-01T05:00:00.200Z"  | 
 | 71 | +    duration      = "10d"  | 
 | 72 | +	timezone      = "UTC"  | 
86 | 73 | 
  | 
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 |  | -}  | 
 | 74 | +    recurring = {  | 
 | 75 | +      every       = "20d"  | 
 | 76 | +      end         = "2029-05-17T05:05:00.000Z"  | 
 | 77 | +      on_week_day = ["MO", "TU"]  | 
 | 78 | +    }  | 
 | 79 | +  }  | 
98 | 80 | 
  | 
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)  | 
 | 81 | +  scope = {  | 
 | 82 | +    alerting = {  | 
 | 83 | +      kql         = "_id: '1234'"  | 
 | 84 | +    }  | 
 | 85 | +  }  | 
130 | 86 | }  | 
 | 87 | +`  | 
131 | 88 | 
 
  | 
132 |  | -func testAccResourceDataViewBasicDVUpdated(indexName string) string {  | 
133 |  | -	return fmt.Sprintf(`  | 
 | 89 | +const testAccResourceMaintenanceWindowUpdate = `  | 
134 | 90 | provider "elasticstack" {  | 
135 |  | -	elasticsearch {}  | 
136 |  | -	kibana {}  | 
 | 91 | +  elasticsearch {}  | 
 | 92 | +  kibana {}  | 
137 | 93 | }  | 
138 | 94 | 
  | 
139 |  | -resource "elasticstack_elasticsearch_index" "my_index" {  | 
140 |  | -	name                = "%s"  | 
141 |  | -	deletion_protection = false  | 
142 |  | -}  | 
 | 95 | +resource "elasticstack_kibana_maintenance_window" "test_maintenance_window" {  | 
 | 96 | +  title   		   = "Terraform Maintenance Window UPDATED"  | 
 | 97 | +  enabled 		   = false  | 
 | 98 | +
  | 
 | 99 | +  custom_schedule = {  | 
 | 100 | +    start          = "1999-02-02T05:00:00.200Z"  | 
 | 101 | +    duration       = "12d"  | 
 | 102 | +	timezone       = "Asia/Taipei"  | 
 | 103 | +
  | 
 | 104 | +    recurring = {  | 
 | 105 | +      every        = "21d"  | 
 | 106 | +	  on_month_day = [1, 2, 3]  | 
 | 107 | +	  on_month 	   = [4, 5]  | 
 | 108 | +    }  | 
 | 109 | +  }  | 
143 | 110 | 
  | 
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)  | 
 | 111 | +  scope = {  | 
 | 112 | +    alerting = {  | 
 | 113 | +      kql          = "_id: 'foobar'"  | 
 | 114 | +    }  | 
 | 115 | +  }  | 
153 | 116 | }  | 
 | 117 | +`  | 
0 commit comments