|
| 1 | +package sumologic |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strconv" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/terraform" |
| 12 | +) |
| 13 | + |
| 14 | +func getRandomizedParamsForToken() (string, string, string, string) { |
| 15 | + name := acctest.RandomWithPrefix("tf-acc-test") |
| 16 | + description := acctest.RandomWithPrefix("tf-acc-test") |
| 17 | + status := "Active" |
| 18 | + tokenType := "CollectorRegistration" |
| 19 | + return name, description, status, tokenType |
| 20 | +} |
| 21 | + |
| 22 | +func TestAccSumologicToken_basic(t *testing.T) { |
| 23 | + var token Token |
| 24 | + |
| 25 | + testName, testDescription, testStatus, _ := getRandomizedParamsForToken() |
| 26 | + |
| 27 | + resource.Test(t, resource.TestCase{ |
| 28 | + PreCheck: func() { testAccPreCheck(t) }, |
| 29 | + Providers: testAccProviders, |
| 30 | + CheckDestroy: testAccCheckTokenDestroy(token), |
| 31 | + Steps: []resource.TestStep{ |
| 32 | + { |
| 33 | + Config: testAccCheckSumologicTokenConfigImported(testName, testDescription, testStatus), |
| 34 | + }, |
| 35 | + { |
| 36 | + ResourceName: "sumologic_token.foo", |
| 37 | + ImportState: true, |
| 38 | + ImportStateVerify: false, |
| 39 | + }, |
| 40 | + }, |
| 41 | + }) |
| 42 | +} |
| 43 | + |
| 44 | +func TestAccSumologicToken_create(t *testing.T) { |
| 45 | + var token Token |
| 46 | + testName, testDescription, testStatus, testType := getRandomizedParamsForToken() |
| 47 | + |
| 48 | + resource.Test(t, resource.TestCase{ |
| 49 | + PreCheck: func() { testAccPreCheck(t) }, |
| 50 | + Providers: testAccProviders, |
| 51 | + CheckDestroy: testAccCheckTokenDestroy(token), |
| 52 | + Steps: []resource.TestStep{ |
| 53 | + { |
| 54 | + Config: testAccSumologicToken(testName, testDescription, testStatus), |
| 55 | + Check: resource.ComposeTestCheckFunc( |
| 56 | + testAccCheckTokenExists("sumologic_token.test", &token, t), |
| 57 | + testAccCheckTokenAttributes("sumologic_token.test"), |
| 58 | + resource.TestCheckResourceAttr("sumologic_token.test", "name", testName), |
| 59 | + resource.TestCheckResourceAttr("sumologic_token.test", "description", testDescription), |
| 60 | + resource.TestCheckResourceAttr("sumologic_token.test", "status", testStatus), |
| 61 | + resource.TestCheckResourceAttr("sumologic_token.test", "type", testType), |
| 62 | + ), |
| 63 | + }, |
| 64 | + }, |
| 65 | + }) |
| 66 | +} |
| 67 | + |
| 68 | +func testAccCheckTokenDestroy(token Token) resource.TestCheckFunc { |
| 69 | + return func(s *terraform.State) error { |
| 70 | + client := testAccProvider.Meta().(*Client) |
| 71 | + for _, r := range s.RootModule().Resources { |
| 72 | + id := r.Primary.ID |
| 73 | + u, err := client.GetToken(id) |
| 74 | + if err != nil { |
| 75 | + return fmt.Errorf("Encountered an error: " + err.Error()) |
| 76 | + } |
| 77 | + if u != nil { |
| 78 | + return fmt.Errorf("Token %s still exists", id) |
| 79 | + } |
| 80 | + } |
| 81 | + return nil |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func testAccCheckTokenExists(name string, token *Token, t *testing.T) resource.TestCheckFunc { |
| 86 | + return func(s *terraform.State) error { |
| 87 | + rs, ok := s.RootModule().Resources[name] |
| 88 | + if !ok { |
| 89 | + //need this so that we don't get an unused import error for strconv in some cases |
| 90 | + return fmt.Errorf("Error = %s. Token not found: %s", strconv.FormatBool(ok), name) |
| 91 | + } |
| 92 | + |
| 93 | + //need this so that we don't get an unused import error for strings in some cases |
| 94 | + if strings.EqualFold(rs.Primary.ID, "") { |
| 95 | + return fmt.Errorf("Token ID is not set") |
| 96 | + } |
| 97 | + |
| 98 | + id := rs.Primary.ID |
| 99 | + c := testAccProvider.Meta().(*Client) |
| 100 | + newToken, err := c.GetToken(id) |
| 101 | + if err != nil { |
| 102 | + return fmt.Errorf("Token %s not found", id) |
| 103 | + } |
| 104 | + token = newToken |
| 105 | + return nil |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +func TestAccSumologicToken_update(t *testing.T) { |
| 110 | + var token Token |
| 111 | + testName, testDescription, testStatus, testType := getRandomizedParamsForToken() |
| 112 | + |
| 113 | + testUpdatedName, testUpdatedDescription, _, _ := getRandomizedParamsForToken() |
| 114 | + testUpdatedStatus := "Inactive" |
| 115 | + |
| 116 | + resource.Test(t, resource.TestCase{ |
| 117 | + PreCheck: func() { testAccPreCheck(t) }, |
| 118 | + Providers: testAccProviders, |
| 119 | + CheckDestroy: testAccCheckTokenDestroy(token), |
| 120 | + Steps: []resource.TestStep{ |
| 121 | + { |
| 122 | + Config: testAccSumologicToken(testName, testDescription, testStatus), |
| 123 | + Check: resource.ComposeTestCheckFunc( |
| 124 | + testAccCheckTokenExists("sumologic_token.test", &token, t), |
| 125 | + testAccCheckTokenAttributes("sumologic_token.test"), |
| 126 | + resource.TestCheckResourceAttr("sumologic_token.test", "name", testName), |
| 127 | + resource.TestCheckResourceAttr("sumologic_token.test", "description", testDescription), |
| 128 | + resource.TestCheckResourceAttr("sumologic_token.test", "status", testStatus), |
| 129 | + resource.TestCheckResourceAttr("sumologic_token.test", "type", testType), |
| 130 | + ), |
| 131 | + }, |
| 132 | + { |
| 133 | + Config: testAccSumologicTokenUpdate(testUpdatedName, testUpdatedDescription, testUpdatedStatus), |
| 134 | + Check: resource.ComposeTestCheckFunc( |
| 135 | + testAccCheckTokenExists("sumologic_token.test", &token, t), |
| 136 | + testAccCheckTokenAttributes("sumologic_token.test"), |
| 137 | + resource.TestCheckResourceAttr("sumologic_token.test", "name", testUpdatedName), |
| 138 | + resource.TestCheckResourceAttr("sumologic_token.test", "description", testUpdatedDescription), |
| 139 | + resource.TestCheckResourceAttr("sumologic_token.test", "status", testUpdatedStatus), |
| 140 | + resource.TestCheckResourceAttr("sumologic_token.test", "type", testType), |
| 141 | + ), |
| 142 | + }, |
| 143 | + }, |
| 144 | + }) |
| 145 | +} |
| 146 | + |
| 147 | +func testAccCheckSumologicTokenConfigImported(name string, description string, status string) string { |
| 148 | + return fmt.Sprintf(` |
| 149 | +resource "sumologic_token" "foo" { |
| 150 | + name = "%s" |
| 151 | + description = "%s" |
| 152 | + status = "%s" |
| 153 | + type = "CollectorRegistration" |
| 154 | +} |
| 155 | +`, name, description, status) |
| 156 | +} |
| 157 | + |
| 158 | +func testAccSumologicToken(name string, description string, status string) string { |
| 159 | + return fmt.Sprintf(` |
| 160 | +resource "sumologic_token" "test" { |
| 161 | + name = "%s" |
| 162 | + description = "%s" |
| 163 | + status = "%s" |
| 164 | + type = "CollectorRegistration" |
| 165 | +} |
| 166 | +`, name, description, status) |
| 167 | +} |
| 168 | + |
| 169 | +func testAccSumologicTokenUpdate(name string, description string, status string) string { |
| 170 | + return fmt.Sprintf(` |
| 171 | +resource "sumologic_token" "test" { |
| 172 | + name = "%s" |
| 173 | + description = "%s" |
| 174 | + status = "%s" |
| 175 | + type = "CollectorRegistration" |
| 176 | +} |
| 177 | +`, name, description, status) |
| 178 | +} |
| 179 | + |
| 180 | +func testAccCheckTokenAttributes(name string) resource.TestCheckFunc { |
| 181 | + return func(s *terraform.State) error { |
| 182 | + f := resource.ComposeTestCheckFunc( |
| 183 | + resource.TestCheckResourceAttrSet(name, "name"), |
| 184 | + resource.TestCheckResourceAttrSet(name, "description"), |
| 185 | + resource.TestCheckResourceAttrSet(name, "status"), |
| 186 | + resource.TestCheckResourceAttrSet(name, "type"), |
| 187 | + resource.TestCheckResourceAttrSet(name, "version"), |
| 188 | + ) |
| 189 | + return f(s) |
| 190 | + } |
| 191 | +} |
0 commit comments