|
1 | 1 | package sumologic |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
| 5 | + "strconv" |
4 | 6 | "testing" |
5 | 7 |
|
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" |
6 | 9 | "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
7 | 10 | "github.com/hashicorp/terraform-plugin-sdk/terraform" |
8 | 11 | ) |
9 | 12 |
|
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") |
11 | 22 | resource.Test(t, resource.TestCase{ |
12 | | - Providers: testAccProviders, |
| 23 | + PreCheck: func() { testAccPreCheck(t) }, |
| 24 | + Providers: testAccProviders, |
| 25 | + CheckDestroy: testAccCheckHTTPSourceDestroy, |
13 | 26 | Steps: []resource.TestStep{ |
14 | 27 | { |
15 | | - Config: testAccSumologicHTTPSourceConfig, |
| 28 | + Config: testAccSumologicHTTPSourceConfig(cName, cDescription, cCategory, sName, sDescription, sCategory), |
16 | 29 | 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", ""), |
18 | 34 | resource.TestCheckResourceAttrSet("sumologic_http_source.http", "id"), |
19 | 35 | 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), |
20 | 40 | ), |
21 | 41 | }, |
22 | 42 | }, |
23 | 43 | }) |
24 | 44 | } |
25 | 45 |
|
26 | 46 | 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") |
27 | 57 | resource.Test(t, resource.TestCase{ |
28 | | - Providers: testAccProviders, |
| 58 | + PreCheck: func() { testAccPreCheck(t) }, |
| 59 | + Providers: testAccProviders, |
| 60 | + CheckDestroy: testAccCheckHTTPSourceDestroy, |
29 | 61 | Steps: []resource.TestStep{ |
30 | 62 | { |
31 | | - Config: testAccSumologicHTTPSourceConfig, |
| 63 | + Config: testAccSumologicHTTPSourceConfig(cName, cDescription, cCategory, sName, sDescription, sCategory), |
32 | 64 | Check: resource.ComposeTestCheckFunc( |
33 | | - testAccCheckHTTPSourceExists("sumologic_http_source.http", t), |
| 65 | + testAccCheckHTTPSourceExists("sumologic_http_source.http", &httpSource), |
| 66 | + testAccCheckHTTPSourceValues(&httpSource, sName, sDescription, sCategory), |
34 | 67 | 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), |
37 | 71 | 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), |
39 | 73 | ), |
40 | 74 | }, |
41 | 75 | { |
42 | | - Config: testAccSumologicHTTPSourceConfigUpdate, |
| 76 | + Config: testAccSumologicHTTPSourceConfig(cName, cDescription, cCategory, sNameUpdated, sDescriptionUpdated, sCategoryUpdated), |
43 | 77 | Check: resource.ComposeTestCheckFunc( |
44 | | - testAccCheckHTTPSourceExists("sumologic_http_source.http", t), |
| 78 | + testAccCheckHTTPSourceExists("sumologic_http_source.http", &httpSource), |
| 79 | + testAccCheckHTTPSourceValues(&httpSource, sNameUpdated, sDescriptionUpdated, sCategoryUpdated), |
45 | 80 | 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), |
49 | 85 | ), |
50 | 86 | }, |
51 | 87 | }, |
52 | 88 | }) |
53 | 89 | } |
54 | 90 |
|
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 | + } |
78 | 120 | } |
| 121 | + return nil |
79 | 122 | } |
80 | 123 |
|
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 | + } |
82 | 130 |
|
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 | + } |
88 | 134 |
|
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 | + } |
95 | 155 | } |
96 | | -` |
97 | 156 |
|
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 | +} |
99 | 171 |
|
| 172 | +func testAccSumologicHTTPSourceConfig(cName, cDescription, cCategory, sName, sDescription, sCategory string) string { |
| 173 | + return fmt.Sprintf(` |
100 | 174 | resource "sumologic_collector" "test" { |
101 | | - name = "MyCollector" |
102 | | - description = "MyCollectorDesc" |
103 | | - category = "Cat" |
| 175 | + name = "%s" |
| 176 | + description = "%s" |
| 177 | + category = "%s" |
104 | 178 | } |
105 | | -
|
| 179 | + |
106 | 180 | 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) |
113 | 188 | } |
114 | | -` |
|
0 commit comments