|
| 1 | +package ns1 |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" |
| 10 | + ns1 "gopkg.in/ns1/ns1-go.v2/rest" |
| 11 | + "gopkg.in/ns1/ns1-go.v2/rest/model/account" |
| 12 | +) |
| 13 | + |
| 14 | +func TestAccAccountWhitelist_basic(t *testing.T) { |
| 15 | + var wl account.IPWhitelist |
| 16 | + var name = fmt.Sprintf("it-%s", acctest.RandStringFromCharSet(15, acctest.CharSetAlphaNum)) |
| 17 | + |
| 18 | + resource.Test(t, resource.TestCase{ |
| 19 | + PreCheck: func() { testAccPreCheck(t) }, |
| 20 | + Providers: testAccProviders, |
| 21 | + CheckDestroy: testAccAccountWhitelistDestroy, |
| 22 | + Steps: []resource.TestStep{ |
| 23 | + { |
| 24 | + Config: testAccAccountWhitelistBasic(name), |
| 25 | + Check: resource.ComposeTestCheckFunc( |
| 26 | + testAccCheckAccountWhitelistExists(fmt.Sprintf("ns1_account_whitelist.%s", name), &wl), |
| 27 | + ), |
| 28 | + }, |
| 29 | + }, |
| 30 | + }) |
| 31 | +} |
| 32 | + |
| 33 | +func TestAccAccountWhitelist_ManualDelete(t *testing.T) { |
| 34 | + var wl account.IPWhitelist |
| 35 | + var name = fmt.Sprintf("it-%s", acctest.RandStringFromCharSet(15, acctest.CharSetAlphaNum)) |
| 36 | + |
| 37 | + resource.Test(t, resource.TestCase{ |
| 38 | + PreCheck: func() { testAccPreCheck(t) }, |
| 39 | + Providers: testAccProviders, |
| 40 | + CheckDestroy: testAccAccountWhitelistDestroy, |
| 41 | + Steps: []resource.TestStep{ |
| 42 | + { |
| 43 | + Config: testAccAccountWhitelistBasic(name), |
| 44 | + Check: testAccCheckAccountWhitelistExists(fmt.Sprintf("ns1_account_whitelist.%s", name), &wl), |
| 45 | + }, |
| 46 | + // Simulate a manual deletion of the whitelist and verify that the plan has a diff. |
| 47 | + { |
| 48 | + PreConfig: testAccManualDeleteAccountWhitelist(t, &wl), |
| 49 | + Config: testAccAccountWhitelistBasic(name), |
| 50 | + PlanOnly: true, |
| 51 | + ExpectNonEmptyPlan: true, |
| 52 | + }, |
| 53 | + // Then re-create and make sure it is there again. |
| 54 | + { |
| 55 | + Config: testAccAccountWhitelistBasic(name), |
| 56 | + Check: testAccCheckAccountWhitelistExists(fmt.Sprintf("ns1_account_whitelist.%s", name), &wl), |
| 57 | + }, |
| 58 | + }, |
| 59 | + }) |
| 60 | +} |
| 61 | + |
| 62 | +func TestAccAccountWhitelist_updated(t *testing.T) { |
| 63 | + var wl account.IPWhitelist |
| 64 | + var name = fmt.Sprintf("it-%s", acctest.RandStringFromCharSet(15, acctest.CharSetAlphaNum)) |
| 65 | + |
| 66 | + resource.Test(t, resource.TestCase{ |
| 67 | + PreCheck: func() { testAccPreCheck(t) }, |
| 68 | + Providers: testAccProviders, |
| 69 | + CheckDestroy: testAccAccountWhitelistDestroy, |
| 70 | + Steps: []resource.TestStep{ |
| 71 | + { |
| 72 | + Config: testAccAccountWhitelistBasic(name), |
| 73 | + Check: resource.ComposeTestCheckFunc( |
| 74 | + testAccCheckAccountWhitelistExists(fmt.Sprintf("ns1_account_whitelist.%s", name), &wl), |
| 75 | + testAccCheckAccountWhitelistValues(&wl, 5), |
| 76 | + ), |
| 77 | + }, |
| 78 | + { |
| 79 | + Config: testAccAccountWhitelistUpdated(name), |
| 80 | + Check: resource.ComposeTestCheckFunc( |
| 81 | + testAccCheckAccountWhitelistExists(fmt.Sprintf("ns1_account_whitelist.%s", name), &wl), |
| 82 | + testAccCheckAccountWhitelistValues(&wl, 1), |
| 83 | + ), |
| 84 | + }, |
| 85 | + }, |
| 86 | + }) |
| 87 | +} |
| 88 | + |
| 89 | +// Other stuff passed here |
| 90 | + |
| 91 | +func testAccCheckAccountWhitelistExists(n string, accountWhitelist *account.IPWhitelist) resource.TestCheckFunc { |
| 92 | + return func(s *terraform.State) error { |
| 93 | + rs, ok := s.RootModule().Resources[n] |
| 94 | + |
| 95 | + if !ok { |
| 96 | + return fmt.Errorf("not found: %s", n) |
| 97 | + } |
| 98 | + |
| 99 | + if rs.Primary.ID == "" { |
| 100 | + return fmt.Errorf("no ID is set") |
| 101 | + } |
| 102 | + |
| 103 | + client := testAccProvider.Meta().(*ns1.Client) |
| 104 | + |
| 105 | + foundAccountwhitelist, _, err := client.GlobalIPWhitelist.Get(rs.Primary.Attributes["id"]) |
| 106 | + |
| 107 | + p := rs.Primary |
| 108 | + |
| 109 | + if err != nil { |
| 110 | + return err |
| 111 | + } |
| 112 | + |
| 113 | + if foundAccountwhitelist.ID != p.Attributes["id"] { |
| 114 | + return fmt.Errorf("Global allow list not found") |
| 115 | + } |
| 116 | + |
| 117 | + *accountWhitelist = *foundAccountwhitelist |
| 118 | + |
| 119 | + return nil |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +func testAccManualDeleteAccountWhitelist(t *testing.T, wl *account.IPWhitelist) func() { |
| 124 | + return func() { |
| 125 | + client := testAccProvider.Meta().(*ns1.Client) |
| 126 | + _, err := client.GlobalIPWhitelist.Delete(wl.ID) |
| 127 | + |
| 128 | + // Not a big deal if this fails, it will get caught in the test conditions and fail the test. |
| 129 | + if err != nil { |
| 130 | + t.Logf("failed to delete global white list: %v", err) |
| 131 | + } |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +func testAccAccountWhitelistDestroy(s *terraform.State) error { |
| 136 | + client := testAccProvider.Meta().(*ns1.Client) |
| 137 | + |
| 138 | + for _, rs := range s.RootModule().Resources { |
| 139 | + if rs.Type != "ns1_account_whitelist" { |
| 140 | + continue |
| 141 | + } |
| 142 | + |
| 143 | + wl, _, err := client.GlobalIPWhitelist.Get(rs.Primary.Attributes["id"]) |
| 144 | + if err == nil { |
| 145 | + return fmt.Errorf("global ip allow list still exists %#v: %#v", err, wl) |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + return nil |
| 150 | +} |
| 151 | + |
| 152 | +func testAccCheckAccountWhitelistValues(wl *account.IPWhitelist, expected int) resource.TestCheckFunc { |
| 153 | + return func(s *terraform.State) error { |
| 154 | + values := len(wl.Values) |
| 155 | + |
| 156 | + if values != expected { |
| 157 | + return fmt.Errorf("IPWhitelist.Values: got: %v want: %v", values, expected) |
| 158 | + } |
| 159 | + |
| 160 | + return nil |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +func testAccAccountWhitelistBasic(name string) string { |
| 165 | + return fmt.Sprintf(`resource "ns1_account_whitelist" "%s" { |
| 166 | + name = "%s" |
| 167 | + values = [ |
| 168 | + "0.0.0.0/0", |
| 169 | + "192.0.0.0/8", |
| 170 | + "192.168.0.0/16", |
| 171 | + "192.168.1.0/24", |
| 172 | + "192.168.1.1" |
| 173 | + ] |
| 174 | + }`, name, name) |
| 175 | +} |
| 176 | + |
| 177 | +func testAccAccountWhitelistUpdated(name string) string { |
| 178 | + return fmt.Sprintf(`resource "ns1_account_whitelist" "%s" { |
| 179 | + name = "%s" |
| 180 | + values = [ |
| 181 | + "0.0.0.0/0", |
| 182 | + ] |
| 183 | + }`, name, name) |
| 184 | +} |
0 commit comments