|
| 1 | +// ---------------------------------------------------------------------------- |
| 2 | +// |
| 3 | +// *** AUTO GENERATED CODE *** AUTO GENERATED CODE *** |
| 4 | +// |
| 5 | +// ---------------------------------------------------------------------------- |
| 6 | +// |
| 7 | +// This file is automatically generated by Sumo Logic and manual |
| 8 | +// changes will be clobbered when the file is regenerated. Do not submit |
| 9 | +// changes to this file. |
| 10 | +// |
| 11 | +// ---------------------------------------------------------------------------- |
| 12 | +package sumologic |
| 13 | + |
| 14 | +import ( |
| 15 | + "fmt" |
| 16 | + "strconv" |
| 17 | + "strings" |
| 18 | + "testing" |
| 19 | + |
| 20 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 21 | + "github.com/hashicorp/terraform-plugin-sdk/terraform" |
| 22 | +) |
| 23 | + |
| 24 | +func TestAccSumologicSubdomain_basic(t *testing.T) { |
| 25 | + var subdomain Subdomain |
| 26 | + testSubdomain := "my-company" |
| 27 | + |
| 28 | + resource.Test(t, resource.TestCase{ |
| 29 | + PreCheck: func() { testAccPreCheck(t) }, |
| 30 | + Providers: testAccProviders, |
| 31 | + CheckDestroy: testAccCheckSubdomainDestroy(subdomain), |
| 32 | + Steps: []resource.TestStep{ |
| 33 | + { |
| 34 | + Config: testAccCheckSumologicSubdomainConfigImported(testSubdomain), |
| 35 | + }, |
| 36 | + { |
| 37 | + ResourceName: "sumologic_subdomain.foo", |
| 38 | + ImportState: true, |
| 39 | + ImportStateVerify: true, |
| 40 | + }, |
| 41 | + }, |
| 42 | + }) |
| 43 | +} |
| 44 | + |
| 45 | +func TestAccSubdomain_create(t *testing.T) { |
| 46 | + var subdomain Subdomain |
| 47 | + testSubdomain := "my-company" |
| 48 | + resource.Test(t, resource.TestCase{ |
| 49 | + PreCheck: func() { testAccPreCheck(t) }, |
| 50 | + Providers: testAccProviders, |
| 51 | + CheckDestroy: testAccCheckSubdomainDestroy(subdomain), |
| 52 | + Steps: []resource.TestStep{ |
| 53 | + { |
| 54 | + Config: testAccSumologicSubdomain(testSubdomain), |
| 55 | + Check: resource.ComposeTestCheckFunc( |
| 56 | + testAccCheckSubdomainExists("sumologic_subdomain.test", &subdomain, t), |
| 57 | + testAccCheckSubdomainAttributes("sumologic_subdomain.test"), |
| 58 | + resource.TestCheckResourceAttr("sumologic_subdomain.test", "subdomain", testSubdomain), |
| 59 | + ), |
| 60 | + }, |
| 61 | + }, |
| 62 | + }) |
| 63 | +} |
| 64 | + |
| 65 | +func TestAccSubdomain_update(t *testing.T) { |
| 66 | + var subdomain Subdomain |
| 67 | + testSubdomain := "my-company" |
| 68 | + |
| 69 | + testUpdatedSubdomain := "my-new-company" |
| 70 | + |
| 71 | + resource.Test(t, resource.TestCase{ |
| 72 | + PreCheck: func() { testAccPreCheck(t) }, |
| 73 | + Providers: testAccProviders, |
| 74 | + CheckDestroy: testAccCheckSubdomainDestroy(subdomain), |
| 75 | + Steps: []resource.TestStep{ |
| 76 | + { |
| 77 | + Config: testAccSumologicSubdomain(testSubdomain), |
| 78 | + Check: resource.ComposeTestCheckFunc( |
| 79 | + testAccCheckSubdomainExists("sumologic_subdomain.test", &subdomain, t), |
| 80 | + testAccCheckSubdomainAttributes("sumologic_subdomain.test"), |
| 81 | + resource.TestCheckResourceAttr("sumologic_subdomain.test", "subdomain", testSubdomain), |
| 82 | + ), |
| 83 | + }, |
| 84 | + { |
| 85 | + Config: testAccSumologicSubdomainUpdate(testUpdatedSubdomain), |
| 86 | + Check: resource.ComposeTestCheckFunc( |
| 87 | + resource.TestCheckResourceAttr("sumologic_subdomain.test", "subdomain", testUpdatedSubdomain), |
| 88 | + ), |
| 89 | + }, |
| 90 | + }, |
| 91 | + }) |
| 92 | +} |
| 93 | + |
| 94 | +func testAccCheckSubdomainDestroy(subdomain Subdomain) resource.TestCheckFunc { |
| 95 | + return func(s *terraform.State) error { |
| 96 | + client := testAccProvider.Meta().(*Client) |
| 97 | + for _, r := range s.RootModule().Resources { |
| 98 | + id := r.Primary.ID |
| 99 | + u, err := client.GetSubdomain() |
| 100 | + if err != nil { |
| 101 | + return fmt.Errorf("Encountered an error: " + err.Error()) |
| 102 | + } |
| 103 | + if u != nil { |
| 104 | + return fmt.Errorf("Subdomain %s still exists", id) |
| 105 | + } |
| 106 | + } |
| 107 | + return nil |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +func testAccCheckSubdomainExists(name string, subdomain *Subdomain, t *testing.T) resource.TestCheckFunc { |
| 112 | + return func(s *terraform.State) error { |
| 113 | + rs, ok := s.RootModule().Resources[name] |
| 114 | + if !ok { |
| 115 | + //need this so that we don't get an unused import error for strconv in some cases |
| 116 | + return fmt.Errorf("Error = %s. Subdomain not found: %s", strconv.FormatBool(ok), name) |
| 117 | + } |
| 118 | + |
| 119 | + //need this so that we don't get an unused import error for strings in some cases |
| 120 | + if strings.EqualFold(rs.Primary.ID, "") { |
| 121 | + return fmt.Errorf("Subdomain ID is not set") |
| 122 | + } |
| 123 | + |
| 124 | + id := rs.Primary.ID |
| 125 | + client := testAccProvider.Meta().(*Client) |
| 126 | + newSubdomain, err := client.GetSubdomain() |
| 127 | + if err != nil { |
| 128 | + return fmt.Errorf("Subdomain %s not found", id) |
| 129 | + } |
| 130 | + subdomain = newSubdomain |
| 131 | + return nil |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +func testAccCheckSumologicSubdomainConfigImported(subdomain string) string { |
| 136 | + return fmt.Sprintf(` |
| 137 | +resource "sumologic_subdomain" "foo" { |
| 138 | + subdomain = "%s" |
| 139 | +} |
| 140 | +`, subdomain) |
| 141 | +} |
| 142 | + |
| 143 | +func testAccSumologicSubdomain(subdomain string) string { |
| 144 | + return fmt.Sprintf(` |
| 145 | +resource "sumologic_subdomain" "test" { |
| 146 | + subdomain = "%s" |
| 147 | +} |
| 148 | +`, subdomain) |
| 149 | +} |
| 150 | + |
| 151 | +func testAccSumologicSubdomainUpdate(subdomain string) string { |
| 152 | + return fmt.Sprintf(` |
| 153 | +resource "sumologic_subdomain" "test" { |
| 154 | + subdomain = "%s" |
| 155 | +} |
| 156 | +`, subdomain) |
| 157 | +} |
| 158 | + |
| 159 | +func testAccCheckSubdomainAttributes(name string) resource.TestCheckFunc { |
| 160 | + return func(s *terraform.State) error { |
| 161 | + f := resource.ComposeTestCheckFunc( |
| 162 | + resource.TestCheckResourceAttrSet(name, "subdomain"), |
| 163 | + ) |
| 164 | + return f(s) |
| 165 | + } |
| 166 | +} |
0 commit comments