Skip to content

Commit 6e305df

Browse files
author
Sean Sain
committed
fix up acc tests and clean up the code
1 parent ff66a5d commit 6e305df

File tree

3 files changed

+80
-72
lines changed

3 files changed

+80
-72
lines changed

sumologic/resource_sumologic_lookup_table.go

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"log"
1717

1818
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
19+
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
1920
)
2021

2122
func resourceSumologicLookupTable() *schema.Resource {
@@ -37,25 +38,26 @@ func resourceSumologicLookupTable() *schema.Resource {
3738
Elem: &schema.Schema{
3839
Type: schema.TypeString,
3940
},
41+
ForceNew: true,
4042
},
4143

4244
"parent_folder_id": {
4345
Type: schema.TypeString,
4446
Optional: true,
45-
ForceNew: false,
47+
ForceNew: true,
4648
},
4749

4850
"description": {
49-
Type: schema.TypeString,
50-
Required: true,
51-
ForceNew: false,
51+
Type: schema.TypeString,
52+
ValidateFunc: validation.StringLenBetween(0, 1000),
53+
Required: true,
5254
},
5355

5456
"size_limit_action": {
55-
Type: schema.TypeString,
56-
Optional: true,
57-
ForceNew: false,
58-
Default: "StopIncomingMessages",
57+
Type: schema.TypeString,
58+
Optional: true,
59+
ValidateFunc: validation.StringInSlice([]string{"StopIncomingMessages", "DeleteOldData"}, false),
60+
Default: "StopIncomingMessages",
5961
},
6062

6163
"fields": {
@@ -72,25 +74,26 @@ func resourceSumologicLookupTable() *schema.Resource {
7274
},
7375

7476
"field_type": {
75-
Type: schema.TypeString,
76-
Required: true,
77-
ForceNew: false,
77+
Type: schema.TypeString,
78+
Required: true,
79+
ForceNew: false,
80+
ValidateFunc: validation.StringInSlice([]string{"boolean", "int", "long", "double", "string"}, false),
7881
},
7982
},
8083
},
84+
ForceNew: true,
8185
},
8286

8387
"ttl": {
8488
Type: schema.TypeInt,
8589
Optional: true,
86-
ForceNew: false,
8790
Default: 0,
8891
},
8992

9093
"name": {
9194
Type: schema.TypeString,
9295
Optional: true,
93-
ForceNew: false,
96+
ForceNew: true,
9497
},
9598
},
9699
}
@@ -109,8 +112,8 @@ func resourceSumologicLookupTableCreate(d *schema.ResourceData, meta interface{}
109112
d.SetId(id)
110113
}
111114

112-
fmt.Printf("##DEBUG## created lookup: %+v\n", d)
113-
fmt.Printf("##DEBUG## lookup id: %v\n", d.Id())
115+
log.Printf("created lookup: %+v\n", d)
116+
log.Printf("lookup id: %v\n", d.Id())
114117
return resourceSumologicLookupTableRead(d, meta)
115118
}
116119

@@ -119,7 +122,7 @@ func resourceSumologicLookupTableRead(d *schema.ResourceData, meta interface{})
119122

120123
id := d.Id()
121124
lookupTable, err := c.GetLookupTable(id)
122-
fmt.Printf("##DEBUG## read lookup: %+v\n", lookupTable)
125+
log.Printf("##DEBUG## read lookup: %+v\n", lookupTable)
123126
if err != nil {
124127
return err
125128
}
@@ -131,7 +134,9 @@ func resourceSumologicLookupTableRead(d *schema.ResourceData, meta interface{})
131134
}
132135

133136
d.Set("name", lookupTable.Name)
134-
d.Set("fields", lookupTable.Fields)
137+
if err := d.Set("fields", fieldsToList(lookupTable.Fields)); err != nil {
138+
return fmt.Errorf("error setting fields for resource %s: %s", d.Id(), err)
139+
}
135140
d.Set("ttl", lookupTable.Ttl)
136141
d.Set("primary_keys", lookupTable.PrimaryKeys)
137142
d.Set("parent_folder_id", lookupTable.ParentFolderId)
@@ -144,7 +149,7 @@ func resourceSumologicLookupTableRead(d *schema.ResourceData, meta interface{})
144149
func resourceSumologicLookupTableDelete(d *schema.ResourceData, meta interface{}) error {
145150
c := meta.(*Client)
146151

147-
fmt.Println("##DEBUG## resourceSumologicLookupTableDelete: ", d.Id())
152+
log.Printf("##DEBUG## resourceSumologicLookupTableDelete: %s", d.Id())
148153
return c.DeleteLookupTable(d.Id())
149154
}
150155

@@ -198,3 +203,17 @@ func resourceToLookupTableField(data interface{}) LookupTableField {
198203

199204
return lookupTableField
200205
}
206+
207+
func fieldsToList(lookupTableField []LookupTableField) []map[string]interface{} {
208+
var s []map[string]interface{}
209+
210+
for _, t := range lookupTableField {
211+
mapping := map[string]interface{}{
212+
"field_name": t.FieldName,
213+
"field_type": t.FieldType,
214+
}
215+
s = append(s, mapping)
216+
}
217+
218+
return s
219+
}

sumologic/resource_sumologic_lookup_table_test.go

Lines changed: 42 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ import (
2424
func TestAccSumologicLookupTable_basic(t *testing.T) {
2525
var lookupTable LookupTable
2626
testName := "SampleLookupTable"
27-
testFields := []LookupTableField{{"\"FieldName1\"", "\"boolean\""}}
27+
testFieldName := "FieldName1"
28+
testFieldType := "boolean"
2829
testTtl := 100
29-
testPrimaryKeys := []string{"\"FieldName1\""}
30+
testPrimaryKeys := "FieldName1"
3031
testSizeLimitAction := "StopIncomingMessages"
3132
testDescription := "This is a sample lookup table description."
3233

@@ -36,7 +37,7 @@ func TestAccSumologicLookupTable_basic(t *testing.T) {
3637
CheckDestroy: testAccCheckLookupTableDestroy(lookupTable),
3738
Steps: []resource.TestStep{
3839
{
39-
Config: testAccCheckSumologicLookupTableConfigImported(testName, testFields, testTtl, testPrimaryKeys, testSizeLimitAction, testDescription),
40+
Config: testAccCheckSumologicLookupTableConfigImported(testName, testFieldName, testFieldType, testTtl, testPrimaryKeys, testSizeLimitAction, testDescription),
4041
},
4142
{
4243
ResourceName: "sumologic_lookup_table.foo",
@@ -50,9 +51,10 @@ func TestAccSumologicLookupTable_basic(t *testing.T) {
5051
func TestAccLookupTable_create(t *testing.T) {
5152
var lookupTable LookupTable
5253
testName := "SampleLookupTable"
53-
testFields := []LookupTableField{{"\"FieldName1\"", "\"boolean\""}}
54+
testFieldName := "FieldName1"
55+
testFieldType := "boolean"
5456
testTtl := 100
55-
testPrimaryKeys := []string{"\"FieldName1\""}
57+
testPrimaryKeys := "FieldName1"
5658
testSizeLimitAction := "StopIncomingMessages"
5759
testDescription := "This is a sample lookup table description."
5860
resource.Test(t, resource.TestCase{
@@ -61,16 +63,16 @@ func TestAccLookupTable_create(t *testing.T) {
6163
CheckDestroy: testAccCheckLookupTableDestroy(lookupTable),
6264
Steps: []resource.TestStep{
6365
{
64-
Config: testAccSumologicLookupTable(testName, testFields, testTtl, testPrimaryKeys, testSizeLimitAction, testDescription),
66+
Config: testAccSumologicLookupTable(testName, testFieldName, testFieldType, testTtl, testPrimaryKeys, testSizeLimitAction, testDescription),
6567
Check: resource.ComposeTestCheckFunc(
6668
testAccCheckLookupTableExists("sumologic_lookup_table.test", &lookupTable, t),
6769
testAccCheckLookupTableAttributes("sumologic_lookup_table.test"),
6870
resource.TestCheckResourceAttr("sumologic_lookup_table.test", "name", testName),
6971
resource.TestCheckResourceAttr("sumologic_lookup_table.test", "fields.#", "1"),
70-
resource.TestCheckResourceAttr("sumologic_lookup_table.test", "fields.0.field_name", testFields[0].FieldName),
71-
resource.TestCheckResourceAttr("sumologic_lookup_table.test", "fields.0.field_name", testFields[0].FieldType),
72+
resource.TestCheckResourceAttr("sumologic_lookup_table.test", "fields.0.field_name", testFieldName),
73+
resource.TestCheckResourceAttr("sumologic_lookup_table.test", "fields.0.field_type", testFieldType),
7274
resource.TestCheckResourceAttr("sumologic_lookup_table.test", "ttl", strconv.Itoa(testTtl)),
73-
resource.TestCheckResourceAttr("sumologic_lookup_table.test", "primary_keys.0", strings.Replace(testPrimaryKeys[0], "\"", "", 2)),
75+
resource.TestCheckResourceAttr("sumologic_lookup_table.test", "primary_keys.0", testPrimaryKeys),
7476
resource.TestCheckResourceAttr("sumologic_lookup_table.test", "size_limit_action", testSizeLimitAction),
7577
resource.TestCheckResourceAttr("sumologic_lookup_table.test", "description", testDescription),
7678
),
@@ -82,48 +84,46 @@ func TestAccLookupTable_create(t *testing.T) {
8284
func TestAccLookupTable_update(t *testing.T) {
8385
var lookupTable LookupTable
8486
testName := "SampleLookupTable"
85-
testFields := []LookupTableField{{"\"FieldName1\"", "\"boolean\""}}
87+
testFieldName := "FieldName1"
88+
testFieldType := "boolean"
8689
testTtl := 100
87-
testPrimaryKeys := []string{"\"FieldName1\""}
90+
testPrimaryKeys := "FieldName1"
8891
testSizeLimitAction := "StopIncomingMessages"
8992
testDescription := "This is a sample lookup table description."
9093

91-
testUpdatedName := "SampleLookupTableUpdate"
92-
testUpdatedFields := []LookupTableField{{"\"FieldName1\"", "\"boolean\""}}
9394
testUpdatedTtl := 101
94-
testUpdatedPrimaryKeys := []string{"\"FieldName1\""}
9595
testUpdatedSizeLimitAction := "DeleteOldData"
96-
testUpdatedDescription := "This is a sample lookup table description.Update"
96+
testUpdatedDescription := "This is a sample lookup table description Updated"
9797

9898
resource.Test(t, resource.TestCase{
9999
PreCheck: func() { testAccPreCheck(t) },
100100
Providers: testAccProviders,
101101
CheckDestroy: testAccCheckLookupTableDestroy(lookupTable),
102102
Steps: []resource.TestStep{
103103
{
104-
Config: testAccSumologicLookupTable(testName, testFields, testTtl, testPrimaryKeys, testSizeLimitAction, testDescription),
104+
Config: testAccSumologicLookupTable(testName, testFieldName, testFieldType, testTtl, testPrimaryKeys, testSizeLimitAction, testDescription),
105105
Check: resource.ComposeTestCheckFunc(
106106
testAccCheckLookupTableExists("sumologic_lookup_table.test", &lookupTable, t),
107107
testAccCheckLookupTableAttributes("sumologic_lookup_table.test"),
108108
resource.TestCheckResourceAttr("sumologic_lookup_table.test", "name", testName),
109109
resource.TestCheckResourceAttr("sumologic_lookup_table.test", "fields.#", "1"),
110-
resource.TestCheckResourceAttr("sumologic_lookup_table.test", "fields.0.field_name", testFields[0].FieldName),
111-
resource.TestCheckResourceAttr("sumologic_lookup_table.test", "fields.0.field_name", testFields[0].FieldType),
110+
resource.TestCheckResourceAttr("sumologic_lookup_table.test", "fields.0.field_name", testFieldName),
111+
resource.TestCheckResourceAttr("sumologic_lookup_table.test", "fields.0.field_type", testFieldType),
112112
resource.TestCheckResourceAttr("sumologic_lookup_table.test", "ttl", strconv.Itoa(testTtl)),
113-
resource.TestCheckResourceAttr("sumologic_lookup_table.test", "primary_keys.0", strings.Replace(testPrimaryKeys[0], "\"", "", 2)),
113+
resource.TestCheckResourceAttr("sumologic_lookup_table.test", "primary_keys.0", testPrimaryKeys),
114114
resource.TestCheckResourceAttr("sumologic_lookup_table.test", "size_limit_action", testSizeLimitAction),
115115
resource.TestCheckResourceAttr("sumologic_lookup_table.test", "description", testDescription),
116116
),
117117
},
118118
{
119-
Config: testAccSumologicLookupTableUpdate(testUpdatedName, testUpdatedFields, testUpdatedTtl, testUpdatedPrimaryKeys, testUpdatedSizeLimitAction, testUpdatedDescription),
119+
Config: testAccSumologicLookupTableUpdate(testName, testFieldName, testFieldType, testUpdatedTtl, testPrimaryKeys, testUpdatedSizeLimitAction, testUpdatedDescription),
120120
Check: resource.ComposeTestCheckFunc(
121-
resource.TestCheckResourceAttr("sumologic_lookup_table.test", "name", testUpdatedName),
121+
resource.TestCheckResourceAttr("sumologic_lookup_table.test", "name", testName),
122122
resource.TestCheckResourceAttr("sumologic_lookup_table.test", "fields.#", "1"),
123-
resource.TestCheckResourceAttr("sumologic_lookup_table.test", "fields.0.field_name", testUpdatedFields[0].FieldName),
124-
resource.TestCheckResourceAttr("sumologic_lookup_table.test", "fields.0.field_name", testUpdatedFields[0].FieldType),
123+
resource.TestCheckResourceAttr("sumologic_lookup_table.test", "fields.0.field_name", testFieldName),
124+
resource.TestCheckResourceAttr("sumologic_lookup_table.test", "fields.0.field_type", testFieldType),
125125
resource.TestCheckResourceAttr("sumologic_lookup_table.test", "ttl", strconv.Itoa(testUpdatedTtl)),
126-
resource.TestCheckResourceAttr("sumologic_lookup_table.test", "primary_keys.0", strings.Replace(testUpdatedPrimaryKeys[0], "\"", "", 2)),
126+
resource.TestCheckResourceAttr("sumologic_lookup_table.test", "primary_keys.0", testPrimaryKeys),
127127
resource.TestCheckResourceAttr("sumologic_lookup_table.test", "size_limit_action", testUpdatedSizeLimitAction),
128128
resource.TestCheckResourceAttr("sumologic_lookup_table.test", "description", testUpdatedDescription),
129129
),
@@ -135,18 +135,9 @@ func TestAccLookupTable_update(t *testing.T) {
135135
func testAccCheckLookupTableDestroy(lookupTable LookupTable) resource.TestCheckFunc {
136136
return func(s *terraform.State) error {
137137
client := testAccProvider.Meta().(*Client)
138-
for _, r := range s.RootModule().Resources {
139-
fmt.Printf("##DEBUG## r=%+v\n", r)
140-
fmt.Printf("##DEBUG## r.Primary=%+v\n", r.Primary)
141-
fmt.Printf("##DEBUG## r.id=%+v\n", r.Primary.ID)
142-
id := r.Primary.ID
143-
u, err := client.GetLookupTable(id)
144-
if err != nil {
145-
return fmt.Errorf("Encountered an error: " + err.Error())
146-
}
147-
if u != nil {
148-
return fmt.Errorf("LookupTable %s still exists", id)
149-
}
138+
_, err := client.GetLookupTable(lookupTable.ID)
139+
if err == nil {
140+
return fmt.Errorf("Lookup Table still exists")
150141
}
151142
return nil
152143
}
@@ -176,67 +167,65 @@ func testAccCheckLookupTableExists(name string, lookupTable *LookupTable, t *tes
176167
}
177168
}
178169

179-
func testAccCheckSumologicLookupTableConfigImported(name string, fields []LookupTableField, ttl int, primaryKeys []string, sizeLimitAction string, description string) string {
170+
func testAccCheckSumologicLookupTableConfigImported(name string, testFieldName string, testFieldType string, ttl int, primaryKeys string, sizeLimitAction string, description string) string {
180171
return fmt.Sprintf(`
181172
data "sumologic_personal_folder" "personalFolder" {}
182173
resource "sumologic_lookup_table" "foo" {
183174
name = "%s"
184175
fields {
185-
field_name = %s
186-
field_type = %s
176+
field_name = "%s"
177+
field_type = "%s"
187178
}
188179
ttl = %d
189-
primary_keys = %v
180+
primary_keys = ["%s"]
190181
parent_folder_id = "${data.sumologic_personal_folder.personalFolder.id}"
191182
size_limit_action = "%s"
192183
description = "%s"
193184
}
194-
`, name, fields[0].FieldName, fields[0].FieldType, ttl, primaryKeys, sizeLimitAction, description)
185+
`, name, testFieldName, testFieldType, ttl, primaryKeys, sizeLimitAction, description)
195186
}
196187

197-
func testAccSumologicLookupTable(name string, fields []LookupTableField, ttl int, primaryKeys []string, sizeLimitAction string, description string) string {
188+
func testAccSumologicLookupTable(name string, testFieldName string, testFieldType string, ttl int, primaryKeys string, sizeLimitAction string, description string) string {
198189
return fmt.Sprintf(`
199190
data "sumologic_personal_folder" "personalFolder" {}
200191
resource "sumologic_lookup_table" "test" {
201192
name = "%s"
202193
fields {
203-
field_name = %s
204-
field_type = %s
194+
field_name = "%s"
195+
field_type = "%s"
205196
}
206197
ttl = %d
207-
primary_keys = %v
198+
primary_keys = ["%s"]
208199
parent_folder_id = "${data.sumologic_personal_folder.personalFolder.id}"
209200
size_limit_action = "%s"
210201
description = "%s"
211202
}
212-
`, name, fields[0].FieldName, fields[0].FieldType, ttl, primaryKeys, sizeLimitAction, description)
203+
`, name, testFieldName, testFieldType, ttl, primaryKeys, sizeLimitAction, description)
213204
}
214205

215-
func testAccSumologicLookupTableUpdate(name string, fields []LookupTableField, ttl int, primaryKeys []string, sizeLimitAction string, description string) string {
206+
func testAccSumologicLookupTableUpdate(name string, testFieldName string, testFieldType string, ttl int, primaryKeys string, sizeLimitAction string, description string) string {
216207
return fmt.Sprintf(`
217208
data "sumologic_personal_folder" "personalFolder" {}
218209
resource "sumologic_lookup_table" "test" {
219210
name = "%s"
220211
fields {
221-
field_name = %s
222-
field_type = %s
212+
field_name = "%s"
213+
field_type = "%s"
223214
}
224215
ttl = %d
225-
primary_keys = %v
216+
primary_keys = ["%s"]
226217
parent_folder_id = "${data.sumologic_personal_folder.personalFolder.id}"
227218
size_limit_action = "%s"
228219
description = "%s"
229220
}
230-
`, name, fields[0].FieldName, fields[0].FieldType, ttl, primaryKeys, sizeLimitAction, description)
221+
`, name, testFieldName, testFieldType, ttl, primaryKeys, sizeLimitAction, description)
231222
}
232223

233224
func testAccCheckLookupTableAttributes(name string) resource.TestCheckFunc {
234225
return func(s *terraform.State) error {
235226
f := resource.ComposeTestCheckFunc(
236227
resource.TestCheckResourceAttrSet(name, "name"),
237-
resource.TestCheckResourceAttrSet(name, "fields"),
238228
resource.TestCheckResourceAttrSet(name, "ttl"),
239-
resource.TestCheckResourceAttrSet(name, "primary_keys"),
240229
resource.TestCheckResourceAttrSet(name, "parent_folder_id"),
241230
resource.TestCheckResourceAttrSet(name, "size_limit_action"),
242231
resource.TestCheckResourceAttrSet(name, "description"),

sumologic/sumologic_lookup_table.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func (s *Client) DeleteLookupTable(id string) error {
7272

7373
urlWithParams := fmt.Sprintf(urlWithoutParams+paramString, sprintfArgs...)
7474

75-
fmt.Println("##DEBUG## deleting lookuptable: ", id)
75+
log.Printf("deleting lookuptable: %s", id)
7676
_, err := s.Delete(urlWithParams)
7777

7878
return err

0 commit comments

Comments
 (0)