Skip to content

Commit a419f40

Browse files
author
Sean Sain
committed
fix test errors in connection
1 parent 33d58ab commit a419f40

File tree

3 files changed

+29
-15
lines changed

3 files changed

+29
-15
lines changed

sumologic/resource_sumologic_connection.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func resourceSumologicConnection() *schema.Resource {
5252
},
5353
"url": {
5454
Type: schema.TypeString,
55-
Optional: true,
55+
Required: true,
5656
ForceNew: false,
5757
ValidateFunc: validation.IsURLWithHTTPorHTTPS,
5858
},

sumologic/resource_sumologic_connection_test.go

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ func TestAccConnection_create(t *testing.T) {
1414
connectionType := "WebhookConnection"
1515
name := acctest.RandomWithPrefix("tf-connection-test-name")
1616
description := acctest.RandomWithPrefix("tf-connection-test-description")
17-
url := acctest.RandomWithPrefix("https://")
18-
defaultPayload := `{"eventType" : "{{SearchName}}"}`
17+
url := "https://example.com"
18+
defaultPayload := "{\"eventType\" : \"{{SearchName}}\"}"
1919
webhookType := "Webhook"
2020

2121
var connection Connection
2222

2323
resource.Test(t, resource.TestCase{
2424
PreCheck: func() { testAccPreCheck(t) },
2525
Providers: testAccProviders,
26-
CheckDestroy: testAccCheckConnectionDestroy(connection),
26+
CheckDestroy: testAccCheckConnectionDestroy,
2727
Steps: []resource.TestStep{
2828
{
2929
Config: createConnectionConfig(name, connectionType, description, url, webhookType, defaultPayload),
@@ -34,7 +34,7 @@ func TestAccConnection_create(t *testing.T) {
3434
resource.TestCheckResourceAttr("sumologic_connection.test", "name", name),
3535
resource.TestCheckResourceAttr("sumologic_connection.test", "description", description),
3636
resource.TestCheckResourceAttr("sumologic_connection.test", "url", url),
37-
resource.TestCheckResourceAttr("sumologic_connection.test", "default_payload", defaultPayload),
37+
resource.TestCheckResourceAttr("sumologic_connection.test", "default_payload", defaultPayload+"\n"),
3838
resource.TestCheckResourceAttr("sumologic_connection.test", "webhook_type", webhookType),
3939
),
4040
},
@@ -55,7 +55,7 @@ func TestAccConnection_update(t *testing.T) {
5555
resource.Test(t, resource.TestCase{
5656
PreCheck: func() { testAccPreCheck(t) },
5757
Providers: testAccProviders,
58-
CheckDestroy: testAccCheckConnectionDestroy(connection),
58+
CheckDestroy: testAccCheckConnectionDestroy,
5959
Steps: []resource.TestStep{
6060
{
6161
Config: createConnectionConfig(name, connectionType, fDescription, url, webhookType, defaultPayload),
@@ -66,7 +66,7 @@ func TestAccConnection_update(t *testing.T) {
6666
resource.TestCheckResourceAttr("sumologic_connection.test", "name", name),
6767
resource.TestCheckResourceAttr("sumologic_connection.test", "description", fDescription),
6868
resource.TestCheckResourceAttr("sumologic_connection.test", "url", url),
69-
resource.TestCheckResourceAttr("sumologic_connection.test", "default_payload", defaultPayload),
69+
resource.TestCheckResourceAttr("sumologic_connection.test", "default_payload", defaultPayload+"\n"),
7070
resource.TestCheckResourceAttr("sumologic_connection.test", "webhook_type", webhookType),
7171
),
7272
}, {
@@ -78,7 +78,7 @@ func TestAccConnection_update(t *testing.T) {
7878
resource.TestCheckResourceAttr("sumologic_connection.test", "name", name),
7979
resource.TestCheckResourceAttr("sumologic_connection.test", "description", sDescription),
8080
resource.TestCheckResourceAttr("sumologic_connection.test", "url", url),
81-
resource.TestCheckResourceAttr("sumologic_connection.test", "default_payload", defaultPayload),
81+
resource.TestCheckResourceAttr("sumologic_connection.test", "default_payload", defaultPayload+"\n"),
8282
resource.TestCheckResourceAttr("sumologic_connection.test", "webhook_type", webhookType),
8383
),
8484
},
@@ -117,15 +117,24 @@ func testAccCheckConnectionAttributes(name string) resource.TestCheckFunc {
117117
}
118118
}
119119

120-
func testAccCheckConnectionDestroy(connection Connection) resource.TestCheckFunc {
121-
return func(s *terraform.State) error {
122-
client := testAccProvider.Meta().(*Client)
123-
conn, err := client.GetConnection(connection.ID)
124-
if err == nil && conn == nil {
125-
return nil
120+
func testAccCheckConnectionDestroy(s *terraform.State) error {
121+
client := testAccProvider.Meta().(*Client)
122+
123+
for _, rs := range s.RootModule().Resources {
124+
if rs.Type != "sumologic_connection" {
125+
continue
126+
}
127+
128+
id := rs.Primary.ID
129+
c, err := client.GetConnection(id)
130+
if err != nil {
131+
return fmt.Errorf("Encountered an error: " + err.Error())
132+
}
133+
if c != nil {
134+
return fmt.Errorf("Connection still exists")
126135
}
127-
return fmt.Errorf("Connection still exists")
128136
}
137+
return nil
129138
}
130139

131140
func createConnectionConfig(name, connectionType, desc, url, webhookType, defaultPayload string) string {

sumologic/sumologic_connection.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"fmt"
66
"log"
7+
"strings"
78
)
89

910
// GetConnection returns connection information for given id
@@ -15,6 +16,10 @@ func (s *Client) GetConnection(id string) (*Connection, error) {
1516

1617
rawConnection, _, err := s.Get(url)
1718
if err != nil {
19+
log.Printf("SSAIN: The err: %s", err.Error())
20+
if strings.Contains(err.Error(), "Connection with given ID does not exist.") {
21+
return nil, nil
22+
}
1823
return nil, err
1924
}
2025

0 commit comments

Comments
 (0)