|
| 1 | +package sumologic |
| 2 | + |
| 3 | +// Todo: (ngoyal, 09-19-2024) - Uncomment the test cases once the issue with PUT permissions on SUMOLOGIC_TEST_BUCKET_NAME is fixed |
| 4 | + |
| 5 | +/* |
| 6 | +
|
| 7 | +import ( |
| 8 | + "fmt" |
| 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 | + "os" |
| 13 | + "strings" |
| 14 | + "testing" |
| 15 | +) |
| 16 | +
|
| 17 | +func getTestParams() (string, string, string, string, string, string) { |
| 18 | + dataForwardingDestinationResourceName := "sumologic_data_forwarding_destination.test" |
| 19 | + destinationName, description := getRandomizedDataForwardingDestinationParams() |
| 20 | + testAwsRoleArn := os.Getenv("SUMOLOGIC_TEST_ROLE_ARN") |
| 21 | + testAwsBucket := os.Getenv("SUMOLOGIC_TEST_BUCKET_NAME") |
| 22 | + testAwsRegion := os.Getenv("SUMOLOGIC_TEST_AWS_REGION") |
| 23 | + println("AWS Test Bucket: ", testAwsBucket) |
| 24 | + println("AWS Test ARN: ", testAwsRoleArn) |
| 25 | + println("AWS Test Region: ", testAwsRegion) |
| 26 | + return dataForwardingDestinationResourceName, destinationName, description, testAwsRegion, testAwsRoleArn, testAwsBucket |
| 27 | +} |
| 28 | +
|
| 29 | +func getRandomizedDataForwardingDestinationParams() (string, string) { |
| 30 | + destinationName := acctest.RandomWithPrefix("tf-acc-test") |
| 31 | + description := acctest.RandomWithPrefix("tf-acc-test") |
| 32 | + return destinationName, description |
| 33 | +} |
| 34 | +
|
| 35 | +func TestAccSumologicDataForwarding_create(t *testing.T) { |
| 36 | + dataForwardingDestinationResourceName, destinationName, description, testAwsRegion, testAwsRoleArn, testAwsBucket := getTestParams() |
| 37 | + resource.Test(t, resource.TestCase{ |
| 38 | + PreCheck: func() { testAccPreCheckWithAWS(t) }, |
| 39 | + Providers: testAccProviders, |
| 40 | + CheckDestroy: testAccCheckDataForwardingDestinationDestroy(), |
| 41 | + Steps: []resource.TestStep{ |
| 42 | + { |
| 43 | + Config: testAccSumologicDataForwardingCreateConfig(destinationName, description, testAwsBucket, testAwsRoleArn, testAwsRegion), |
| 44 | + Check: resource.ComposeTestCheckFunc( |
| 45 | + testAccCheckDataForwardingDestinationExists(), |
| 46 | + resource.TestCheckResourceAttr(dataForwardingDestinationResourceName, "destination_name", destinationName), |
| 47 | + resource.TestCheckResourceAttr(dataForwardingDestinationResourceName, "description", description), |
| 48 | + resource.TestCheckResourceAttr(dataForwardingDestinationResourceName, "s3_region", testAwsRegion), |
| 49 | + resource.TestCheckResourceAttr(dataForwardingDestinationResourceName, "s3_server_side_encryption", "false"), |
| 50 | + ), |
| 51 | + }, |
| 52 | + }, |
| 53 | + }) |
| 54 | +} |
| 55 | +
|
| 56 | +func TestAccSumologicDataForwarding_read(t *testing.T) { |
| 57 | + dataForwardingResourceName, destinationName, description, testAwsRegion, testAwsRoleArn, testAwsBucket := getTestParams() |
| 58 | + resource.Test(t, resource.TestCase{ |
| 59 | + PreCheck: func() { testAccPreCheckWithAWS(t) }, |
| 60 | + Providers: testAccProviders, |
| 61 | + CheckDestroy: testAccCheckDataForwardingDestinationDestroy(), |
| 62 | + Steps: []resource.TestStep{ |
| 63 | + { |
| 64 | + Config: testAccSumologicDataForwardingCreateConfig(destinationName, description, testAwsBucket, testAwsRoleArn, testAwsRegion), |
| 65 | + Check: resource.ComposeTestCheckFunc( |
| 66 | + testAccCheckDataForwardingDestinationExists(), |
| 67 | + resource.TestCheckResourceAttr(dataForwardingResourceName, "destination_name", destinationName), |
| 68 | + resource.TestCheckResourceAttr(dataForwardingResourceName, "description", description), |
| 69 | + resource.TestCheckResourceAttr(dataForwardingResourceName, "bucket_name", testAwsBucket), |
| 70 | + resource.TestCheckResourceAttr(dataForwardingResourceName, "s3_region", testAwsRegion), |
| 71 | + resource.TestCheckResourceAttr(dataForwardingResourceName, "s3_server_side_encryption", "false"), |
| 72 | + ), |
| 73 | + }, |
| 74 | + { |
| 75 | + ResourceName: dataForwardingResourceName, |
| 76 | + ImportState: true, |
| 77 | + }, |
| 78 | + }, |
| 79 | + }) |
| 80 | +} |
| 81 | +
|
| 82 | +func TestAccSumologicDataForwarding_update(t *testing.T) { |
| 83 | + dataForwardingResourceName, destinationName, description, testAwsRegion, testAwsRoleArn, testAwsBucket := getTestParams() |
| 84 | + resource.Test(t, resource.TestCase{ |
| 85 | + PreCheck: func() { testAccPreCheckWithAWS(t) }, |
| 86 | + Providers: testAccProviders, |
| 87 | + CheckDestroy: testAccCheckDataForwardingDestinationDestroy(), |
| 88 | + Steps: []resource.TestStep{ |
| 89 | + { |
| 90 | + Config: testAccSumologicDataForwardingCreateConfig(destinationName, description, testAwsBucket, testAwsRoleArn, testAwsRegion), |
| 91 | + Check: resource.ComposeTestCheckFunc( |
| 92 | + testAccCheckDataForwardingDestinationExists(), |
| 93 | + resource.TestCheckResourceAttr(dataForwardingResourceName, "destination_name", destinationName), |
| 94 | + resource.TestCheckResourceAttr(dataForwardingResourceName, "description", description), |
| 95 | + resource.TestCheckResourceAttr(dataForwardingResourceName, "s3_region", testAwsRegion), |
| 96 | + resource.TestCheckResourceAttr(dataForwardingResourceName, "s3_server_side_encryption", "false"), |
| 97 | + ), |
| 98 | + }, { |
| 99 | + Config: testAccSumologicDataForwardingUpdateConfig(destinationName, description, testAwsBucket, testAwsRoleArn, testAwsRegion), |
| 100 | + Check: resource.ComposeTestCheckFunc( |
| 101 | + testAccCheckDataForwardingDestinationExists(), |
| 102 | + resource.TestCheckResourceAttr(dataForwardingResourceName, "destination_name", destinationName), |
| 103 | + resource.TestCheckResourceAttr(dataForwardingResourceName, "description", description), |
| 104 | + resource.TestCheckResourceAttr(dataForwardingResourceName, "s3_region", testAwsRegion), |
| 105 | + resource.TestCheckResourceAttr(dataForwardingResourceName, "s3_server_side_encryption", "true"), |
| 106 | + ), |
| 107 | + }, |
| 108 | + }, |
| 109 | + }) |
| 110 | +
|
| 111 | +} |
| 112 | +
|
| 113 | +func TestAccSumologicDataForwarding_delete(t *testing.T) { |
| 114 | + dataForwardingResourceName, destinationName, description, testAwsRegion, testAwsRoleArn, testAwsBucket := getTestParams() |
| 115 | + resource.Test(t, resource.TestCase{ |
| 116 | + PreCheck: func() { testAccPreCheckWithAWS(t) }, |
| 117 | + Providers: testAccProviders, |
| 118 | + CheckDestroy: testAccCheckDataForwardingDestinationDestroy(), |
| 119 | + Steps: []resource.TestStep{ |
| 120 | + { |
| 121 | + Config: testAccSumologicDataForwardingCreateConfig(destinationName, description, testAwsBucket, testAwsRoleArn, testAwsRegion), |
| 122 | + Check: resource.ComposeTestCheckFunc( |
| 123 | + testAccCheckDataForwardingDestinationExists(), |
| 124 | + resource.TestCheckResourceAttr(dataForwardingResourceName, "destination_name", destinationName), |
| 125 | + resource.TestCheckResourceAttr(dataForwardingResourceName, "description", description), |
| 126 | + resource.TestCheckResourceAttr(dataForwardingResourceName, "s3_region", testAwsRegion), |
| 127 | + resource.TestCheckResourceAttr(dataForwardingResourceName, "s3_server_side_encryption", "false"), |
| 128 | + ), |
| 129 | + }, { |
| 130 | + Config: testAccSumologicDataForwardingDeleteConfig(), |
| 131 | + Check: resource.ComposeTestCheckFunc(testAccCheckDataForwardingDestinationDestroy()), |
| 132 | + }, |
| 133 | + }, |
| 134 | + }) |
| 135 | +} |
| 136 | +
|
| 137 | +func testAccCheckDataForwardingDestinationExists() resource.TestCheckFunc { |
| 138 | + return func(s *terraform.State) error { |
| 139 | + client := testAccProvider.Meta().(*Client) |
| 140 | + for _, r := range s.RootModule().Resources { |
| 141 | + id := r.Primary.ID |
| 142 | + if _, err := client.getDataForwardingDestination(id); err != nil { |
| 143 | + return fmt.Errorf("Received an error retrieving data forwarding destination %s", err) |
| 144 | + } |
| 145 | + } |
| 146 | + return nil |
| 147 | + } |
| 148 | +} |
| 149 | +
|
| 150 | +func testAccCheckDataForwardingDestinationDestroy() resource.TestCheckFunc { |
| 151 | + return func(s *terraform.State) error { |
| 152 | + client := testAccProvider.Meta().(*Client) |
| 153 | + for _, r := range s.RootModule().Resources { |
| 154 | + id := r.Primary.ID |
| 155 | + p, err := client.getDataForwardingDestination(id) |
| 156 | +
|
| 157 | + if err != nil { |
| 158 | + if strings.Contains(err.Error(), "Data forwarding Destination doesn't exists") { |
| 159 | + continue |
| 160 | + } |
| 161 | +
|
| 162 | + return fmt.Errorf("Encountered an error: " + err.Error()) |
| 163 | + } |
| 164 | +
|
| 165 | + if p != nil { |
| 166 | + return fmt.Errorf("Data Forwarding Destination still exists!") |
| 167 | + } |
| 168 | + } |
| 169 | + return nil |
| 170 | + } |
| 171 | +} |
| 172 | +
|
| 173 | +func testAccSumologicDataForwardingCreateConfig(destinationName string, description string, testAwsBucket string, testAwsRoleArn string, testAwsRegion string) string { |
| 174 | + return fmt.Sprintf(` |
| 175 | +resource "sumologic_data_forwarding_destination" "test" { |
| 176 | + destination_name = "%s" |
| 177 | + description = "%s" |
| 178 | + bucket_name = "%s" |
| 179 | + authentication { |
| 180 | + type = "RoleBased" |
| 181 | + role_arn = "%s" |
| 182 | + } |
| 183 | + s3_region = "%s" |
| 184 | + s3_server_side_encryption = false |
| 185 | +} |
| 186 | +`, destinationName, description, testAwsBucket, testAwsRoleArn, testAwsRegion) |
| 187 | +} |
| 188 | +
|
| 189 | +func testAccSumologicDataForwardingUpdateConfig(destinationName string, description string, testAwsBucket string, testAwsRoleArn string, testAwsRegion string) string { |
| 190 | + return fmt.Sprintf(` |
| 191 | +resource "sumologic_data_forwarding_destination" "test" { |
| 192 | + destination_name = "%s" |
| 193 | + description = "%s" |
| 194 | + bucket_name = "%s" |
| 195 | + authentication { |
| 196 | + type = "RoleBased" |
| 197 | + role_arn = "%s" |
| 198 | + } |
| 199 | + s3_region = "%s" |
| 200 | + s3_server_side_encryption = true |
| 201 | +} |
| 202 | +`, destinationName, description, testAwsBucket, testAwsRoleArn, testAwsRegion) |
| 203 | +} |
| 204 | +
|
| 205 | +func testAccSumologicDataForwardingDeleteConfig() string { |
| 206 | + return fmt.Sprintf(` `) |
| 207 | +} |
| 208 | +*/ |
0 commit comments