Skip to content

Commit 0faece7

Browse files
author
Sean Sain
committed
deprecate lookup_by_name, destroy; clean up collector tests
1 parent 9af6b72 commit 0faece7

File tree

2 files changed

+22
-227
lines changed

2 files changed

+22
-227
lines changed

sumologic/resource_sumologic_collector.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,18 @@ func resourceSumologicCollector() *schema.Resource {
5151
Default: "",
5252
},
5353
"lookup_by_name": {
54-
Type: schema.TypeBool,
55-
Optional: true,
56-
ForceNew: false,
57-
Default: false,
54+
Deprecated: "We are deprecating the lookup_by_name attribute as collectors now have data sources.",
55+
Type: schema.TypeBool,
56+
Optional: true,
57+
ForceNew: false,
58+
Default: false,
5859
},
5960
"destroy": {
60-
Type: schema.TypeBool,
61-
Optional: true,
62-
ForceNew: false,
63-
Default: true,
61+
Deprecated: "We are deprecating the destroy attribute as all resources support lifecycle attribute prevent_destroy",
62+
Type: schema.TypeBool,
63+
Optional: true,
64+
ForceNew: false,
65+
Default: true,
6466
},
6567
},
6668
}

sumologic/resource_sumologic_collector_test.go

Lines changed: 12 additions & 219 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ import (
99
"github.com/hashicorp/terraform-plugin-sdk/terraform"
1010
)
1111

12-
func TestAccSumologicCollector_minimal(t *testing.T) {
13-
var collector *Collector
12+
func TestAccSumologicCollector_basic(t *testing.T) {
1413
resourceName := "sumologic_collector.test"
1514
resource.Test(t, resource.TestCase{
1615
Providers: testAccProviders,
@@ -19,8 +18,6 @@ func TestAccSumologicCollector_minimal(t *testing.T) {
1918
{
2019
Config: testAccSumologicCollectorConfigMinimal,
2120
Check: resource.ComposeTestCheckFunc(
22-
testAccCheckCollectorExists(resourceName, &collector, t),
23-
testAccCheckCollectorAttributes(resourceName, &collector),
2421
resource.TestCheckResourceAttrSet(resourceName, "id"),
2522
resource.TestCheckResourceAttr(resourceName, "name", "MyTerraformCollector1"),
2623
resource.TestCheckResourceAttr(resourceName, "description", ""),
@@ -38,8 +35,7 @@ func TestAccSumologicCollector_minimal(t *testing.T) {
3835
})
3936
}
4037

41-
func TestAccSumologicCollector_simple(t *testing.T) {
42-
var collector *Collector
38+
func TestAccSumologicCollector_create(t *testing.T) {
4339
resourceName := "sumologic_collector.test"
4440
resource.Test(t, resource.TestCase{
4541
Providers: testAccProviders,
@@ -48,84 +44,18 @@ func TestAccSumologicCollector_simple(t *testing.T) {
4844
{
4945
Config: testAccSumologicCollectorConfig,
5046
Check: resource.ComposeTestCheckFunc(
51-
testAccCheckCollectorExists(resourceName, &collector, t),
52-
testAccCheckCollectorAttributes(resourceName, &collector),
5347
resource.TestCheckResourceAttrSet(resourceName, "id"),
5448
resource.TestCheckResourceAttr(resourceName, "name", "MyTerraformCollector2"),
5549
resource.TestCheckResourceAttr(resourceName, "description", "MyCollectorDesc"),
5650
resource.TestCheckResourceAttr(resourceName, "category", "Cat"),
5751
resource.TestCheckResourceAttr(resourceName, "timezone", "Etc/UTC"),
5852
),
5953
},
60-
{
61-
ResourceName: resourceName,
62-
ImportState: true,
63-
ImportStateVerify: true,
64-
ImportStateVerifyIgnore: []string{"lookup_by_name", "destroy"},
65-
},
6654
},
6755
})
6856
}
6957

70-
// func TestAccSumologicCollectorLookupByName(t *testing.T) {
71-
// var collector *Collector
72-
// resourceName := "sumologic_collector.test"
73-
// resource.Test(t, resource.TestCase{
74-
// Providers: testAccProviders,
75-
// CheckDestroy: testAccCheckCollectorDestroy,
76-
// // TODO: if we keep lookup_by_name, we need to beef up the tests and have 2 steps
77-
// // TODO: first step creates the resource
78-
// // TODO: second step looks it up by name
79-
// Steps: []resource.TestStep{
80-
// {
81-
// Config: testAccSumologicCollectorConfigLookupByName,
82-
// Check: resource.ComposeTestCheckFunc(
83-
// testAccCheckCollectorExists(resourceName, &collector, t),
84-
// testAccCheckCollectorAttributes(resourceName, &collector),
85-
// resource.TestCheckResourceAttrSet(resourceName, "id"),
86-
// ),
87-
// },
88-
// {
89-
// ResourceName: resourceName,
90-
// ImportState: true,
91-
// ImportStateVerify: true,
92-
// ImportStateVerifyIgnore: []string{"lookup_by_name", "destroy"},
93-
// },
94-
// },
95-
// })
96-
// }
97-
98-
// func TestAccSumologicCollectorAllConfig(t *testing.T) {
99-
// var collector *Collector
100-
// resourceName := "sumologic_collector.test"
101-
// resource.Test(t, resource.TestCase{
102-
// Providers: testAccProviders,
103-
// CheckDestroy: testAccCheckCollectorDestroy,
104-
// Steps: []resource.TestStep{
105-
// {
106-
// Config: testAccSumologicCollectorConfigAll,
107-
// Check: resource.ComposeTestCheckFunc(
108-
// testAccCheckCollectorExists(resourceName, &collector, t),
109-
// testAccCheckCollectorAttributes(resourceName, &collector),
110-
// resource.TestCheckResourceAttrSet(resourceName, "id"),
111-
// resource.TestCheckResourceAttr(resourceName, "name", "CollectorName"),
112-
// resource.TestCheckResourceAttr(resourceName, "description", "CollectorDesc"),
113-
// resource.TestCheckResourceAttr(resourceName, "category", "Category"),
114-
// resource.TestCheckResourceAttr(resourceName, "timezone", "Europe/Berlin"),
115-
// ),
116-
// },
117-
// {
118-
// ResourceName: resourceName,
119-
// ImportState: true,
120-
// ImportStateVerify: true,
121-
// ImportStateVerifyIgnore: []string{"lookup_by_name", "destroy"},
122-
// },
123-
// },
124-
// })
125-
// }
126-
127-
func TestAccSumologicCollector_changeConfig(t *testing.T) {
128-
var collector *Collector
58+
func TestAccSumologicCollector_update(t *testing.T) {
12959
resourceName := "sumologic_collector.test"
13060
resource.Test(t, resource.TestCase{
13161
Providers: testAccProviders,
@@ -134,147 +64,25 @@ func TestAccSumologicCollector_changeConfig(t *testing.T) {
13464
{
13565
Config: testAccSumologicCollectorConfig,
13666
Check: resource.ComposeTestCheckFunc(
137-
testAccCheckCollectorExists(resourceName, &collector, t),
138-
testAccCheckCollectorAttributes(resourceName, &collector),
13967
resource.TestCheckResourceAttr(resourceName, "name", "MyTerraformCollector2"),
14068
resource.TestCheckResourceAttr(resourceName, "description", "MyCollectorDesc"),
14169
resource.TestCheckResourceAttr(resourceName, "category", "Cat"),
14270
resource.TestCheckResourceAttr(resourceName, "timezone", "Etc/UTC"),
14371
),
14472
},
14573
{
146-
ResourceName: resourceName,
147-
ImportState: true,
148-
ImportStateVerify: true,
149-
ImportStateVerifyIgnore: []string{"lookup_by_name", "destroy"},
150-
},
151-
{
152-
Config: testAccSumologicCollectorConfigAll,
74+
Config: testAccSumologicCollectorConfigUpdate,
15375
Check: resource.ComposeTestCheckFunc(
154-
// check the id of this resource is the same as the one in the previous step
155-
testAccCheckCollectorId(resourceName, &collector),
156-
testAccCheckCollectorExists(resourceName, &collector, t),
157-
testAccCheckCollectorAttributes(resourceName, &collector),
158-
resource.TestCheckResourceAttr(resourceName, "name", "CollectorName"),
159-
resource.TestCheckResourceAttr(resourceName, "description", "CollectorDesc"),
160-
resource.TestCheckResourceAttr(resourceName, "category", "Category"),
161-
resource.TestCheckResourceAttr(resourceName, "timezone", "Europe/Berlin"),
162-
),
163-
},
164-
},
165-
})
166-
}
167-
168-
func TestAccSumologicCollector_manualDeletion(t *testing.T) {
169-
var collector *Collector
170-
171-
deleteCollector := func() {
172-
c := testAccProvider.Meta().(*Client)
173-
_, err := c.GetCollector(collector.ID)
174-
if err != nil {
175-
t.Fatal(fmt.Sprintf("attempted to delete collector %d but it does not exist (%s)", collector.ID, err))
176-
}
177-
err = c.DeleteCollector(collector.ID)
178-
if err != nil {
179-
t.Fatal(fmt.Sprintf("failed to delete collector %d (%s)", collector.ID, err))
180-
}
181-
}
182-
183-
resource.Test(t, resource.TestCase{
184-
Providers: testAccProviders,
185-
Steps: []resource.TestStep{
186-
{
187-
Config: testAccSumologicCollectorConfig,
188-
Check: resource.ComposeTestCheckFunc(
189-
testAccCheckCollectorExists("sumologic_collector.test", &collector, t),
190-
resource.TestCheckResourceAttr("sumologic_collector.test", "name", "MyTerraformCollector2"),
191-
resource.TestCheckResourceAttr("sumologic_collector.test", "description", "MyCollectorDesc"),
192-
resource.TestCheckResourceAttr("sumologic_collector.test", "category", "Cat"),
193-
resource.TestCheckResourceAttr("sumologic_collector.test", "timezone", "Etc/UTC"),
194-
),
195-
},
196-
{
197-
PreConfig: deleteCollector, // simulate a manual deletion by deleting the collector between the 2 applies
198-
Config: testAccSumologicCollectorConfig,
199-
Check: resource.ComposeTestCheckFunc(
200-
testAccCheckCollectorExists("sumologic_collector.test", &collector, t),
201-
resource.TestCheckResourceAttr("sumologic_collector.test", "name", "MyTerraformCollector2"),
202-
resource.TestCheckResourceAttr("sumologic_collector.test", "description", "MyCollectorDesc"),
203-
resource.TestCheckResourceAttr("sumologic_collector.test", "category", "Cat"),
204-
resource.TestCheckResourceAttr("sumologic_collector.test", "timezone", "Etc/UTC"),
76+
resource.TestCheckResourceAttr(resourceName, "name", "MyTerraformCollector2Updated"),
77+
resource.TestCheckResourceAttr(resourceName, "description", "MyCollectorDescUpdated"),
78+
resource.TestCheckResourceAttr(resourceName, "category", "Cat"),
79+
resource.TestCheckResourceAttr(resourceName, "timezone", "Etc/UTC"),
20580
),
20681
},
20782
},
20883
})
20984
}
21085

211-
// TODO: if we keep the collector's destroy attribute we need to include a test checking if destroy=false works as expected
212-
213-
// Returns a function checking that the collector with the id from the state file has an expected id.
214-
// The expected id is specified in the collector passed as parameter
215-
func testAccCheckCollectorId(name string, collector **Collector) resource.TestCheckFunc {
216-
return func(s *terraform.State) error {
217-
rs, ok := s.RootModule().Resources[name]
218-
if !ok {
219-
return fmt.Errorf("not found: %s", name)
220-
}
221-
222-
if rs.Primary.ID == "" {
223-
return fmt.Errorf("collector ID is not set")
224-
}
225-
226-
id, err := strconv.Atoi(rs.Primary.ID)
227-
if err != nil {
228-
return fmt.Errorf("collector id should be int; got %s", rs.Primary.ID)
229-
}
230-
231-
expectedId := (**collector).ID
232-
if id != expectedId {
233-
return fmt.Errorf("incorrect collector id: got %d; expected %d", id, expectedId)
234-
}
235-
return nil
236-
}
237-
}
238-
239-
// Returns a function checking that the collector with the id from the state exists.
240-
// If the collecor exists, its attributes are updated in *collector
241-
func testAccCheckCollectorExists(name string, collector **Collector, t *testing.T) resource.TestCheckFunc {
242-
return func(s *terraform.State) error {
243-
rs, ok := s.RootModule().Resources[name]
244-
if !ok {
245-
return fmt.Errorf("not found: %s", name)
246-
}
247-
248-
if rs.Primary.ID == "" {
249-
return fmt.Errorf("collector ID is not set")
250-
}
251-
252-
id, err := strconv.Atoi(rs.Primary.ID)
253-
if err != nil {
254-
return fmt.Errorf("collector id should be int; got %s", rs.Primary.ID)
255-
}
256-
c := testAccProvider.Meta().(*Client)
257-
*collector, err = c.GetCollector(id)
258-
if err != nil {
259-
return fmt.Errorf("collector %d not found", id)
260-
}
261-
return nil
262-
}
263-
}
264-
265-
// Returns a function checking that the attributes in the state match that attributes of the actual resource created
266-
func testAccCheckCollectorAttributes(name string, expected **Collector) resource.TestCheckFunc {
267-
return func(s *terraform.State) error {
268-
f := resource.ComposeTestCheckFunc(
269-
resource.TestCheckResourceAttr(name, "name", (**expected).Name),
270-
resource.TestCheckResourceAttr(name, "description", (**expected).Description),
271-
resource.TestCheckResourceAttr(name, "category", (**expected).Category),
272-
resource.TestCheckResourceAttr(name, "timezone", (**expected).TimeZone),
273-
)
274-
return f(s)
275-
}
276-
}
277-
27886
func testAccCheckCollectorDestroy(s *terraform.State) error {
27987
client := testAccProvider.Meta().(*Client)
28088

@@ -303,14 +111,12 @@ func testAccCheckCollectorDestroy(s *terraform.State) error {
303111
}
304112

305113
var testAccSumologicCollectorConfigMinimal = `
306-
307114
resource "sumologic_collector" "test" {
308115
name = "MyTerraformCollector1"
309116
}
310117
`
311118

312119
var testAccSumologicCollectorConfig = `
313-
314120
resource "sumologic_collector" "test" {
315121
name = "MyTerraformCollector2"
316122
description = "MyCollectorDesc"
@@ -319,24 +125,11 @@ resource "sumologic_collector" "test" {
319125
}
320126
`
321127

322-
var testAccSumologicCollectorConfigLookupByName = `
323-
128+
var testAccSumologicCollectorConfigUpdate = `
324129
resource "sumologic_collector" "test" {
325-
name = "MyOtherCollector"
326-
description = "MyCollectorDesc"
130+
name = "MyTerraformCollector2Updated"
131+
description = "MyCollectorDescUpdated"
327132
category = "Cat"
328-
timezone = "Europe/Berlin"
329-
lookup_by_name=true
330-
}
331-
`
332-
333-
var testAccSumologicCollectorConfigAll = `
334-
resource "sumologic_collector" "test" {
335-
name="CollectorName"
336-
description="CollectorDesc"
337-
category="Category"
338-
timezone="Europe/Berlin"
339-
lookup_by_name=true
340-
destroy=true
133+
timezone = "Etc/UTC"
341134
}
342135
`

0 commit comments

Comments
 (0)