@@ -2,8 +2,11 @@ package sumologic
22
33import (
44 "fmt"
5+ "strconv"
6+ "strings"
57 "testing"
68
9+ "github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
710 "github.com/hashicorp/terraform-plugin-sdk/helper/resource"
811 "github.com/hashicorp/terraform-plugin-sdk/terraform"
912)
@@ -31,7 +34,7 @@ func TestAccSumologicMetricsSearch_basic(t *testing.T) {
3134 Steps : []resource.TestStep {
3235 {
3336 Config : testAccSumologicMetricsSearch (tfResourceName , title , description , logQuery , desiredQuantizationInSecs ,
34- metricsQuery , literalRangeName ),
37+ metricsQuery [ 0 ] , literalRangeName ),
3538 },
3639 {
3740 ResourceName : fmt .Sprintf ("sumologic_metrics_search.%s" , tfResourceName ),
@@ -42,6 +45,138 @@ func TestAccSumologicMetricsSearch_basic(t *testing.T) {
4245 })
4346}
4447
48+ func TestAccSumologicMetricsSearch_create (t * testing.T ) {
49+ testNameSuffix := acctest .RandString (16 )
50+
51+ // create config
52+ var metricsSearch MetricsSearch
53+ title := "terraform_test_metrics_search_" + testNameSuffix
54+ description := "Test metrics search description"
55+ logQuery := ""
56+ literalRangeName := "today"
57+ desiredQuantizationInSecs := 0
58+
59+ metricsQuery := MetricsSearchQuery {
60+ RowId : "A" ,
61+ Query : "metric=cpu_idle | avg" ,
62+ }
63+
64+ tfResourceName := "tf_create_metrics_search_test"
65+ tfSearchResource := fmt .Sprintf ("sumologic_metrics_search.%s" , tfResourceName )
66+
67+ resource .Test (t , resource.TestCase {
68+ PreCheck : func () { testAccPreCheck (t ) },
69+ Providers : testAccProviders ,
70+ CheckDestroy : testAccCheckMetricsSearchDestroy (metricsSearch ),
71+ Steps : []resource.TestStep {
72+ {
73+ Config : testAccSumologicMetricsSearch (tfResourceName , title , description , logQuery , desiredQuantizationInSecs ,
74+ metricsQuery , literalRangeName ),
75+ Check : resource .ComposeTestCheckFunc (
76+ testAccCheckMetricsSearchExists (tfSearchResource , & metricsSearch , t ),
77+ resource .TestCheckResourceAttr (tfSearchResource ,
78+ "title" , title ),
79+ resource .TestCheckResourceAttr (tfSearchResource ,
80+ "description" , description ),
81+ // timerange
82+ resource .TestCheckResourceAttr (tfSearchResource , "time_range.#" , "1" ),
83+ resource .TestCheckResourceAttr (tfSearchResource ,
84+ "time_range.0.begin_bounded_time_range.0.from.0.literal_time_range.0.range_name" ,
85+ literalRangeName ),
86+
87+ resource .TestCheckResourceAttr (tfSearchResource , "desired_quantization_in_secs" , "0" ),
88+
89+ // metrics query
90+ resource .TestCheckResourceAttr (tfSearchResource , "metrics_queries.#" , "1" ),
91+ resource .TestCheckResourceAttr (tfSearchResource , "metrics_queries.0.row_id" , metricsQuery .RowId ),
92+ resource .TestCheckResourceAttr (tfSearchResource , "metrics_queries.0.query" , metricsQuery .Query ),
93+ ),
94+ },
95+ },
96+ })
97+ }
98+
99+ func TestAccSumologicMetricsSearch_update (t * testing.T ) {
100+ testNameSuffix := acctest .RandString (16 )
101+
102+ // create config
103+ var metricsSearch MetricsSearch
104+ title := "terraform_test_metrics_search_" + testNameSuffix
105+ description := "Test metrics search description"
106+ logQuery := ""
107+ literalRangeName := "today"
108+ desiredQuantizationInSecs := 0
109+
110+ metricsQuery := MetricsSearchQuery {
111+ RowId : "A" ,
112+ Query : "metric=cpu_idle | avg" ,
113+ }
114+
115+ newMetricsQuery := []MetricsSearchQuery {
116+ {
117+ RowId : "B" ,
118+ Query : "metric=cpu_total | avg" ,
119+ },
120+ }
121+
122+ tfResourceName := "tf_update_metrics_search_test"
123+ tfSearchResource := fmt .Sprintf ("sumologic_metrics_search.%s" , tfResourceName )
124+
125+ resource .Test (t , resource.TestCase {
126+ PreCheck : func () { testAccPreCheck (t ) },
127+ Providers : testAccProviders ,
128+ CheckDestroy : testAccCheckMetricsSearchDestroy (metricsSearch ),
129+ Steps : []resource.TestStep {
130+ {
131+ Config : testAccSumologicMetricsSearch (tfResourceName , title , description , logQuery , desiredQuantizationInSecs ,
132+ metricsQuery , literalRangeName ),
133+ Check : resource .ComposeTestCheckFunc (
134+ testAccCheckMetricsSearchExists (tfSearchResource , & metricsSearch , t ),
135+ resource .TestCheckResourceAttr (tfSearchResource ,
136+ "title" , title ),
137+ resource .TestCheckResourceAttr (tfSearchResource ,
138+ "description" , description ),
139+ // timerange
140+ resource .TestCheckResourceAttr (tfSearchResource , "time_range.#" , "1" ),
141+ resource .TestCheckResourceAttr (tfSearchResource ,
142+ "time_range.0.begin_bounded_time_range.0.from.0.literal_time_range.0.range_name" ,
143+ literalRangeName ),
144+
145+ resource .TestCheckResourceAttr (tfSearchResource , "desired_quantization_in_secs" , "0" ),
146+
147+ // metrics query
148+ resource .TestCheckResourceAttr (tfSearchResource , "metrics_queries.#" , "1" ),
149+ resource .TestCheckResourceAttr (tfSearchResource , "metrics_queries.0.row_id" , metricsQuery .RowId ),
150+ resource .TestCheckResourceAttr (tfSearchResource , "metrics_queries.0.query" , metricsQuery .Query ),
151+ ),
152+ },
153+ {
154+ Config : testAccSumologicUpdatedMetricsSearch (tfResourceName , title , description , logQuery , desiredQuantizationInSecs ,
155+ newMetricsQuery , literalRangeName ),
156+ Check : resource .ComposeTestCheckFunc (
157+ testAccCheckMetricsSearchExists (tfSearchResource , & metricsSearch , t ),
158+ resource .TestCheckResourceAttr (tfSearchResource ,
159+ "title" , title ),
160+ resource .TestCheckResourceAttr (tfSearchResource ,
161+ "description" , description ),
162+ // timerange
163+ resource .TestCheckResourceAttr (tfSearchResource , "time_range.#" , "1" ),
164+ resource .TestCheckResourceAttr (tfSearchResource ,
165+ "time_range.0.begin_bounded_time_range.0.from.0.literal_time_range.0.range_name" ,
166+ literalRangeName ),
167+
168+ resource .TestCheckResourceAttr (tfSearchResource , "desired_quantization_in_secs" , "0" ),
169+
170+ // metrics query
171+ resource .TestCheckResourceAttr (tfSearchResource , "metrics_queries.#" , "1" ),
172+ resource .TestCheckResourceAttr (tfSearchResource , "metrics_queries.0.row_id" , newMetricsQuery [0 ].RowId ),
173+ resource .TestCheckResourceAttr (tfSearchResource , "metrics_queries.0.query" , newMetricsQuery [0 ].Query ),
174+ ),
175+ },
176+ },
177+ })
178+ }
179+
45180func testAccCheckMetricsSearchDestroy (metricsSearch MetricsSearch ) resource.TestCheckFunc {
46181 return func (s * terraform.State ) error {
47182 client := testAccProvider .Meta ().(* Client )
@@ -59,7 +194,60 @@ func testAccCheckMetricsSearchDestroy(metricsSearch MetricsSearch) resource.Test
59194 }
60195}
61196
197+ func testAccCheckMetricsSearchExists (name string , metricsSearch * MetricsSearch , t * testing.T ) resource.TestCheckFunc {
198+ return func (s * terraform.State ) error {
199+ rs , ok := s .RootModule ().Resources [name ]
200+ if ! ok {
201+ return fmt .Errorf ("Error = %s. MetricsSearch not found: %s" , strconv .FormatBool (ok ), name )
202+ }
203+
204+ if strings .EqualFold (rs .Primary .ID , "" ) {
205+ return fmt .Errorf ("MetricsSearch ID is not set" )
206+ }
207+
208+ id := rs .Primary .ID
209+ client := testAccProvider .Meta ().(* Client )
210+ newMetricsSearch , err := client .GetMetricsSearch (id )
211+ if err != nil {
212+ return fmt .Errorf ("MetricsSearch (id=%s) not found" , id )
213+ }
214+ metricsSearch = newMetricsSearch
215+ return nil
216+ }
217+ }
218+
62219func testAccSumologicMetricsSearch (tfResourceName string , title string , description string , logQuery string ,
220+ desiredQuantizationInSecs int , metricsSearchQuery MetricsSearchQuery , literalRangeName string ) string {
221+
222+ return fmt .Sprintf (`
223+ data "sumologic_personal_folder" "personalFolder" {}
224+
225+ resource "sumologic_metrics_search" "%s" {
226+ title = "%s"
227+ description = "%s"
228+ log_query = "%s"
229+ parent_id = data.sumologic_personal_folder.personalFolder.id
230+ desired_quantization_in_secs = %d
231+ metrics_queries {
232+ row_id = "%s"
233+ query = "%s"
234+ }
235+ time_range {
236+ begin_bounded_time_range {
237+ from {
238+ literal_time_range {
239+ range_name = "%s"
240+ }
241+ }
242+ }
243+ }
244+ }
245+ ` , tfResourceName , title , description , logQuery , desiredQuantizationInSecs ,
246+ metricsSearchQuery .RowId , metricsSearchQuery .Query ,
247+ literalRangeName )
248+ }
249+
250+ func testAccSumologicUpdatedMetricsSearch (tfResourceName string , title string , description string , logQuery string ,
63251 desiredQuantizationInSecs int , metricsSearchQuery []MetricsSearchQuery , literalRangeName string ) string {
64252
65253 return fmt .Sprintf (`
0 commit comments