Skip to content

Commit bc08043

Browse files
authored
Merge pull request #137 from SumoLogic/ssain-fix-http-source
Ssain fix http source
2 parents 552de35 + 2ad203c commit bc08043

File tree

4 files changed

+152
-72
lines changed

4 files changed

+152
-72
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ BUG FIXES:
1717
DEPRECATIONS:
1818

1919
* resource/sumologic_collector_ingest_budget_assignment: Deprecated in favor of assigning ingest budgets through the _budget field attribute of collectors [GH-135]
20-
* resource/sumologic_collector: Deprecated `lookup_by_name` and `destroy` attributes
20+
* resource/sumologic_collector: Deprecated `lookup_by_name` and `destroy` attributes [GH-136]
21+
* resource/sumologic_sources: Deprecated `lookup_by_name` and `destroy` attributes [GH-137]

sumologic/resource_sumologic_collector_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ func TestAccSumologicCollector_basic(t *testing.T) {
1515
rname := acctest.RandomWithPrefix("tf-acc-test")
1616
resourceName := "sumologic_collector.test"
1717
resource.Test(t, resource.TestCase{
18+
PreCheck: func() { testAccPreCheck(t) },
1819
Providers: testAccProviders,
1920
CheckDestroy: testAccCheckCollectorDestroy,
2021
Steps: []resource.TestStep{
@@ -47,6 +48,7 @@ func TestAccSumologicCollector_create(t *testing.T) {
4748
rcategory := acctest.RandomWithPrefix("tf-acc-test")
4849
resourceName := "sumologic_collector.test"
4950
resource.Test(t, resource.TestCase{
51+
PreCheck: func() { testAccPreCheck(t) },
5052
Providers: testAccProviders,
5153
CheckDestroy: testAccCheckCollectorDestroy,
5254
Steps: []resource.TestStep{
@@ -76,6 +78,7 @@ func TestAccSumologicCollector_update(t *testing.T) {
7678
rcategory := acctest.RandomWithPrefix("tf-acc-test")
7779
resourceName := "sumologic_collector.test"
7880
resource.Test(t, resource.TestCase{
81+
PreCheck: func() { testAccPreCheck(t) },
7982
Providers: testAccProviders,
8083
CheckDestroy: testAccCheckCollectorDestroy,
8184
Steps: []resource.TestStep{

sumologic/resource_sumologic_http_source_test.go

Lines changed: 137 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,114 +1,188 @@
11
package sumologic
22

33
import (
4+
"fmt"
5+
"strconv"
46
"testing"
57

8+
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
69
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
710
"github.com/hashicorp/terraform-plugin-sdk/terraform"
811
)
912

10-
func TestAccSumologicHTTPSource_basic(t *testing.T) {
13+
func TestAccSumologicHTTPSource_create(t *testing.T) {
14+
var httpSource HTTPSource
15+
var collector Collector
16+
cName := acctest.RandomWithPrefix("tf-acc-test")
17+
cDescription := acctest.RandomWithPrefix("tf-acc-test")
18+
cCategory := acctest.RandomWithPrefix("tf-acc-test")
19+
sName := acctest.RandomWithPrefix("tf-acc-test")
20+
sDescription := acctest.RandomWithPrefix("tf-acc-test")
21+
sCategory := acctest.RandomWithPrefix("tf-acc-test")
1122
resource.Test(t, resource.TestCase{
12-
Providers: testAccProviders,
23+
PreCheck: func() { testAccPreCheck(t) },
24+
Providers: testAccProviders,
25+
CheckDestroy: testAccCheckHTTPSourceDestroy,
1326
Steps: []resource.TestStep{
1427
{
15-
Config: testAccSumologicHTTPSourceConfig,
28+
Config: testAccSumologicHTTPSourceConfig(cName, cDescription, cCategory, sName, sDescription, sCategory),
1629
Check: resource.ComposeTestCheckFunc(
17-
testAccCheckHTTPSourceExists("sumologic_http_source.http", t),
30+
testAccCheckHTTPSourceExists("sumologic_http_source.http", &httpSource),
31+
testAccCheckHTTPSourceValues(&httpSource, sName, sDescription, sCategory),
32+
testAccCheckCollectorExists("sumologic_collector.test", &collector),
33+
testAccCheckCollectorValues(&collector, cName, cDescription, cCategory, "Etc/UTC", ""),
1834
resource.TestCheckResourceAttrSet("sumologic_http_source.http", "id"),
1935
resource.TestCheckResourceAttrSet("sumologic_http_source.http", "url"),
36+
resource.TestCheckResourceAttr("sumologic_http_source.http", "name", sName),
37+
resource.TestCheckResourceAttr("sumologic_http_source.http", "description", sDescription),
38+
resource.TestCheckResourceAttr("sumologic_http_source.http", "message_per_request", "false"),
39+
resource.TestCheckResourceAttr("sumologic_http_source.http", "category", sCategory),
2040
),
2141
},
2242
},
2343
})
2444
}
2545

2646
func TestAccSumologicHTTPSource_update(t *testing.T) {
47+
var httpSource HTTPSource
48+
cName := acctest.RandomWithPrefix("tf-acc-test")
49+
cDescription := acctest.RandomWithPrefix("tf-acc-test")
50+
cCategory := acctest.RandomWithPrefix("tf-acc-test")
51+
sName := acctest.RandomWithPrefix("tf-acc-test")
52+
sDescription := acctest.RandomWithPrefix("tf-acc-test")
53+
sCategory := acctest.RandomWithPrefix("tf-acc-test")
54+
sNameUpdated := acctest.RandomWithPrefix("tf-acc-test")
55+
sDescriptionUpdated := acctest.RandomWithPrefix("tf-acc-test")
56+
sCategoryUpdated := acctest.RandomWithPrefix("tf-acc-test")
2757
resource.Test(t, resource.TestCase{
28-
Providers: testAccProviders,
58+
PreCheck: func() { testAccPreCheck(t) },
59+
Providers: testAccProviders,
60+
CheckDestroy: testAccCheckHTTPSourceDestroy,
2961
Steps: []resource.TestStep{
3062
{
31-
Config: testAccSumologicHTTPSourceConfig,
63+
Config: testAccSumologicHTTPSourceConfig(cName, cDescription, cCategory, sName, sDescription, sCategory),
3264
Check: resource.ComposeTestCheckFunc(
33-
testAccCheckHTTPSourceExists("sumologic_http_source.http", t),
65+
testAccCheckHTTPSourceExists("sumologic_http_source.http", &httpSource),
66+
testAccCheckHTTPSourceValues(&httpSource, sName, sDescription, sCategory),
3467
resource.TestCheckResourceAttrSet("sumologic_http_source.http", "id"),
35-
resource.TestCheckResourceAttr("sumologic_http_source.http", "name", "test_http"),
36-
resource.TestCheckResourceAttr("sumologic_http_source.http", "description", "test_desc"),
68+
resource.TestCheckResourceAttrSet("sumologic_http_source.http", "url"),
69+
resource.TestCheckResourceAttr("sumologic_http_source.http", "name", sName),
70+
resource.TestCheckResourceAttr("sumologic_http_source.http", "description", sDescription),
3771
resource.TestCheckResourceAttr("sumologic_http_source.http", "message_per_request", "false"),
38-
resource.TestCheckResourceAttr("sumologic_http_source.http", "category", "source/category"),
72+
resource.TestCheckResourceAttr("sumologic_http_source.http", "category", sCategory),
3973
),
4074
},
4175
{
42-
Config: testAccSumologicHTTPSourceConfigUpdate,
76+
Config: testAccSumologicHTTPSourceConfig(cName, cDescription, cCategory, sNameUpdated, sDescriptionUpdated, sCategoryUpdated),
4377
Check: resource.ComposeTestCheckFunc(
44-
testAccCheckHTTPSourceExists("sumologic_http_source.http", t),
78+
testAccCheckHTTPSourceExists("sumologic_http_source.http", &httpSource),
79+
testAccCheckHTTPSourceValues(&httpSource, sNameUpdated, sDescriptionUpdated, sCategoryUpdated),
4580
resource.TestCheckResourceAttrSet("sumologic_http_source.http", "id"),
46-
resource.TestCheckResourceAttr("sumologic_http_source.http", "name", "test_http"),
47-
resource.TestCheckResourceAttr("sumologic_http_source.http", "description", "desc_test"),
48-
resource.TestCheckResourceAttr("sumologic_http_source.http", "category", "category/source"),
81+
resource.TestCheckResourceAttrSet("sumologic_http_source.http", "url"),
82+
resource.TestCheckResourceAttr("sumologic_http_source.http", "name", sNameUpdated),
83+
resource.TestCheckResourceAttr("sumologic_http_source.http", "description", sDescriptionUpdated),
84+
resource.TestCheckResourceAttr("sumologic_http_source.http", "category", sCategoryUpdated),
4985
),
5086
},
5187
},
5288
})
5389
}
5490

55-
// func TestAccSumologicHTTPSourceImport(t *testing.T) {
56-
// resource.Test(t, resource.TestCase{
57-
// Providers: testAccProviders,
58-
// Steps: []resource.TestStep{
59-
// {
60-
// Config: testAccSumologicHTTPSourceConfig,
61-
// ResourceName: "sumologic_http_source.http",
62-
// ImportState: true,
63-
// ImportStateId: "123/456",
64-
// ImportStateVerify: true,
65-
// Check: resource.ComposeTestCheckFunc(
66-
// testAccCheckHTTPSourceExists("sumologic_http_source.http", t),
67-
// resource.TestCheckResourceAttr("sumologic_http_source.http", "id", "123"),
68-
// resource.TestCheckResourceAttr("sumologic_http_source.http", "collector_id", "456"),
69-
// ),
70-
// },
71-
// },
72-
// })
73-
// }
74-
75-
func testAccCheckHTTPSourceExists(n string, t *testing.T) resource.TestCheckFunc {
76-
return func(s *terraform.State) error {
77-
return nil
91+
func testAccCheckHTTPSourceDestroy(s *terraform.State) error {
92+
client := testAccProvider.Meta().(*Client)
93+
94+
for _, rs := range s.RootModule().Resources {
95+
if rs.Type != "sumologic_http_source" {
96+
continue
97+
}
98+
99+
if rs.Primary.ID == "" {
100+
return fmt.Errorf("HTTP Source destruction check: HTTP Source ID is not set")
101+
}
102+
103+
id, err := strconv.Atoi(rs.Primary.ID)
104+
if err != nil {
105+
return fmt.Errorf("Encountered an error: " + err.Error())
106+
}
107+
108+
collectorID, err := strconv.Atoi(rs.Primary.Attributes["collector_id"])
109+
if err != nil {
110+
return fmt.Errorf("Encountered an error: " + err.Error())
111+
}
112+
113+
s, err := client.GetHTTPSource(collectorID, id)
114+
if err != nil {
115+
return fmt.Errorf("Encountered an error: " + err.Error())
116+
}
117+
if s != nil {
118+
return fmt.Errorf("HTTP Source still exists")
119+
}
78120
}
121+
return nil
79122
}
80123

81-
var testAccSumologicHTTPSourceConfig = `
124+
func testAccCheckHTTPSourceExists(n string, httpSource *HTTPSource) resource.TestCheckFunc {
125+
return func(s *terraform.State) error {
126+
rs, ok := s.RootModule().Resources[n]
127+
if !ok {
128+
return fmt.Errorf("not found: %s", n)
129+
}
82130

83-
resource "sumologic_collector" "test" {
84-
name = "MyCollector"
85-
description = "MyCollectorDesc"
86-
category = "Cat"
87-
}
131+
if rs.Primary.ID == "" {
132+
return fmt.Errorf("HTTP Source ID is not set")
133+
}
88134

89-
resource "sumologic_http_source" "http" {
90-
name = "test_http"
91-
description = "test_desc"
92-
message_per_request = false
93-
category = "source/category"
94-
collector_id = "${sumologic_collector.test.id}"
135+
id, err := strconv.Atoi(rs.Primary.ID)
136+
if err != nil {
137+
return fmt.Errorf("HTTP Source id should be int; got %s", rs.Primary.ID)
138+
}
139+
140+
collectorID, err := strconv.Atoi(rs.Primary.Attributes["collector_id"])
141+
if err != nil {
142+
return fmt.Errorf("Encountered an error: " + err.Error())
143+
}
144+
145+
c := testAccProvider.Meta().(*Client)
146+
httpSourceResp, err := c.GetHTTPSource(collectorID, id)
147+
if err != nil {
148+
return err
149+
}
150+
151+
*httpSource = *httpSourceResp
152+
153+
return nil
154+
}
95155
}
96-
`
97156

98-
var testAccSumologicHTTPSourceConfigUpdate = `
157+
func testAccCheckHTTPSourceValues(httpSource *HTTPSource, name, description, category string) resource.TestCheckFunc {
158+
return func(s *terraform.State) error {
159+
if httpSource.Name != name {
160+
return fmt.Errorf("bad name, expected \"%s\", got: %#v", name, httpSource.Name)
161+
}
162+
if httpSource.Description != description {
163+
return fmt.Errorf("bad description, expected \"%s\", got: %#v", description, httpSource.Description)
164+
}
165+
if httpSource.Category != category {
166+
return fmt.Errorf("bad category, expected \"%s\", got: %#v", category, httpSource.Category)
167+
}
168+
return nil
169+
}
170+
}
99171

172+
func testAccSumologicHTTPSourceConfig(cName, cDescription, cCategory, sName, sDescription, sCategory string) string {
173+
return fmt.Sprintf(`
100174
resource "sumologic_collector" "test" {
101-
name = "MyCollector"
102-
description = "MyCollectorDesc"
103-
category = "Cat"
175+
name = "%s"
176+
description = "%s"
177+
category = "%s"
104178
}
105-
179+
106180
resource "sumologic_http_source" "http" {
107-
name = "test_http"
108-
description = "desc_test"
109-
message_per_request = false
110-
category = "category/source"
111-
collector_id = "${sumologic_collector.test.id}"
112-
lookup_by_name = true
181+
name = "%s"
182+
description = "%s"
183+
message_per_request = false
184+
category = "%s"
185+
collector_id = "${sumologic_collector.test.id}"
186+
}
187+
`, cName, cDescription, cCategory, sName, sDescription, sCategory)
113188
}
114-
`

sumologic/sumologic_sources.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -179,16 +179,18 @@ func resourceSumologicSource() *schema.Resource {
179179
ForceNew: true,
180180
},
181181
"lookup_by_name": {
182-
Type: schema.TypeBool,
183-
Optional: true,
184-
ForceNew: false,
185-
Default: false,
182+
Deprecated: "We are deprecating the lookup_by_name attribute as sources can be imported using the format collectorID/sourceID.",
183+
Type: schema.TypeBool,
184+
Optional: true,
185+
ForceNew: false,
186+
Default: false,
186187
},
187188
"destroy": {
188-
Type: schema.TypeBool,
189-
Optional: true,
190-
ForceNew: false,
191-
Default: true,
189+
Deprecated: "We are deprecating the destroy attribute as all resources support lifecycle attribute prevent_destroy",
190+
Type: schema.TypeBool,
191+
Optional: true,
192+
ForceNew: false,
193+
Default: true,
192194
},
193195
},
194196
}

0 commit comments

Comments
 (0)