|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "regexp" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" |
| 11 | +) |
| 12 | + |
| 13 | +func TestAccAmplitudeConnection(t *testing.T) { |
| 14 | + name := "TestAcc" |
| 15 | + resource.Test(t, resource.TestCase{ |
| 16 | + PreCheck: func() { testAccPreCheck(t) }, |
| 17 | + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, |
| 18 | + Steps: []resource.TestStep{ |
| 19 | + // Missing configuration |
| 20 | + { |
| 21 | + Config: testAccAmplitudeConnectionResourceNoConfig(name), |
| 22 | + ExpectError: regexp.MustCompile("The argument \"configuration\" is required, but no definition was found."), |
| 23 | + }, |
| 24 | + // Empty configuration |
| 25 | + { |
| 26 | + Config: testAccAmplitudeConnectionResourceMissingRequired(name), |
| 27 | + ExpectError: regexp.MustCompile("Inappropriate value for attribute \"configuration\":"), |
| 28 | + }, |
| 29 | + { |
| 30 | + Config: testAccAmplitudeConnectionResource(name), |
| 31 | + Check: resource.ComposeTestCheckFunc( |
| 32 | + // Check if the resource exists |
| 33 | + testAccAmplitudeConnectionExists(name), |
| 34 | + // Check the name |
| 35 | + resource.TestCheckResourceAttr("polytomic_amplitude_connection.test", "name", name), |
| 36 | + ), |
| 37 | + }, |
| 38 | + }, |
| 39 | + }) |
| 40 | +} |
| 41 | + |
| 42 | +func testAccAmplitudeConnectionExists(name string) resource.TestCheckFunc { |
| 43 | + return func(s *terraform.State) error { |
| 44 | + _, ok := s.RootModule().Resources["polytomic_amplitude_connection.test"] |
| 45 | + if !ok { |
| 46 | + return fmt.Errorf("not found: %s", "polytomic_amplitude_connection.test") |
| 47 | + } |
| 48 | + |
| 49 | + client := testClient() |
| 50 | + connections, err := client.Connections().List(context.TODO()) |
| 51 | + if err != nil { |
| 52 | + return err |
| 53 | + } |
| 54 | + var found bool |
| 55 | + for _, connection := range connections { |
| 56 | + if connection.Name == name { |
| 57 | + found = true |
| 58 | + break |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + if !found { |
| 63 | + return fmt.Errorf("connection %s not found", name) |
| 64 | + } |
| 65 | + |
| 66 | + return nil |
| 67 | + |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func testAccAmplitudeConnectionResource(name string) string { |
| 72 | + return fmt.Sprintf(` |
| 73 | +resource "polytomic_amplitude_connection" "test" { |
| 74 | + name = "%s" |
| 75 | + configuration = { |
| 76 | + api_key = "my-api-key" |
| 77 | + secret_key = "my-secret-key" |
| 78 | + } |
| 79 | +} |
| 80 | +`, name) |
| 81 | +} |
| 82 | + |
| 83 | +func testAccAmplitudeConnectionResourceNoConfig(name string) string { |
| 84 | + return fmt.Sprintf(` |
| 85 | +resource "polytomic_amplitude_connection" "test" { |
| 86 | + name = "%s" |
| 87 | +} |
| 88 | +`, name) |
| 89 | +} |
| 90 | + |
| 91 | +func testAccAmplitudeConnectionResourceMissingRequired(name string) string { |
| 92 | + return fmt.Sprintf(` |
| 93 | +resource "polytomic_amplitude_connection" "test" { |
| 94 | + name = "%s" |
| 95 | + configuration = {} |
| 96 | +}`, name) |
| 97 | +} |
0 commit comments