|
| 1 | +package sumologic |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "strconv" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/terraform" |
| 11 | +) |
| 12 | + |
| 13 | +func TestAccSumologicS3ArchiveSource_create(t *testing.T) { |
| 14 | + var s3Source PollingSource |
| 15 | + var collector Collector |
| 16 | + cName, cDescription, cCategory := getRandomizedParams() |
| 17 | + sName, sDescription, sCategory := getRandomizedParams() |
| 18 | + s3ResourceName := "sumologic_s3_archive_source.s3_archive" |
| 19 | + testAwsRoleArn := os.Getenv("SUMOLOGIC_TEST_ROLE_ARN") |
| 20 | + testAwsBucket := os.Getenv("SUMOLOGIC_TEST_BUCKET_NAME") |
| 21 | + resource.Test(t, resource.TestCase{ |
| 22 | + PreCheck: func() { testAccPreCheckWithAWS(t) }, |
| 23 | + Providers: testAccProviders, |
| 24 | + CheckDestroy: testAccCheckS3ArchiveSourceDestroy, |
| 25 | + Steps: []resource.TestStep{ |
| 26 | + { |
| 27 | + Config: testAccSumologicS3ArchiveSourceConfig(cName, cDescription, cCategory, sName, sDescription, sCategory, testAwsRoleArn, testAwsBucket), |
| 28 | + Check: resource.ComposeTestCheckFunc( |
| 29 | + testAccCheckS3SourceExists(s3ResourceName, &s3Source), |
| 30 | + testAccCheckS3SourceValues(&s3Source, sName, sDescription, sCategory), |
| 31 | + testAccCheckCollectorExists("sumologic_collector.test", &collector), |
| 32 | + testAccCheckCollectorValues(&collector, cName, cDescription, cCategory, "Etc/UTC", ""), |
| 33 | + resource.TestCheckResourceAttrSet(s3ResourceName, "id"), |
| 34 | + resource.TestCheckResourceAttr(s3ResourceName, "name", sName), |
| 35 | + resource.TestCheckResourceAttr(s3ResourceName, "description", sDescription), |
| 36 | + resource.TestCheckResourceAttr(s3ResourceName, "category", sCategory), |
| 37 | + resource.TestCheckResourceAttr(s3ResourceName, "content_type", "AwsS3ArchiveBucket"), |
| 38 | + resource.TestCheckResourceAttr(s3ResourceName, "path.0.type", "S3BucketPathExpression"), |
| 39 | + ), |
| 40 | + }, |
| 41 | + }, |
| 42 | + }) |
| 43 | +} |
| 44 | +func TestAccSumologicS3ArchiveSource_update(t *testing.T) { |
| 45 | + var s3Source PollingSource |
| 46 | + cName, cDescription, cCategory := getRandomizedParams() |
| 47 | + sName, sDescription, sCategory := getRandomizedParams() |
| 48 | + sNameUpdated, sDescriptionUpdated, sCategoryUpdated := getRandomizedParams() |
| 49 | + s3ResourceName := "sumologic_s3_archive_source.s3_archive" |
| 50 | + testAwsRoleArn := os.Getenv("SUMOLOGIC_TEST_ROLE_ARN") |
| 51 | + testAwsBucket := os.Getenv("SUMOLOGIC_TEST_BUCKET_NAME") |
| 52 | + resource.Test(t, resource.TestCase{ |
| 53 | + PreCheck: func() { testAccPreCheckWithAWS(t) }, |
| 54 | + Providers: testAccProviders, |
| 55 | + CheckDestroy: testAccCheckHTTPSourceDestroy, |
| 56 | + Steps: []resource.TestStep{ |
| 57 | + { |
| 58 | + Config: testAccSumologicS3ArchiveSourceConfig(cName, cDescription, cCategory, sName, sDescription, sCategory, testAwsRoleArn, testAwsBucket), |
| 59 | + Check: resource.ComposeTestCheckFunc( |
| 60 | + testAccCheckS3SourceExists(s3ResourceName, &s3Source), |
| 61 | + testAccCheckS3SourceValues(&s3Source, sName, sDescription, sCategory), |
| 62 | + resource.TestCheckResourceAttrSet(s3ResourceName, "id"), |
| 63 | + resource.TestCheckResourceAttr(s3ResourceName, "name", sName), |
| 64 | + resource.TestCheckResourceAttr(s3ResourceName, "description", sDescription), |
| 65 | + resource.TestCheckResourceAttr(s3ResourceName, "category", sCategory), |
| 66 | + resource.TestCheckResourceAttr(s3ResourceName, "content_type", "AwsS3ArchiveBucket"), |
| 67 | + resource.TestCheckResourceAttr(s3ResourceName, "path.0.type", "S3BucketPathExpression"), |
| 68 | + ), |
| 69 | + }, |
| 70 | + { |
| 71 | + Config: testAccSumologicS3ArchiveSourceConfig(cName, cDescription, cCategory, sNameUpdated, sDescriptionUpdated, sCategoryUpdated, testAwsRoleArn, testAwsBucket), |
| 72 | + Check: resource.ComposeTestCheckFunc( |
| 73 | + testAccCheckS3SourceExists(s3ResourceName, &s3Source), |
| 74 | + testAccCheckS3SourceValues(&s3Source, sNameUpdated, sDescriptionUpdated, sCategoryUpdated), |
| 75 | + resource.TestCheckResourceAttrSet(s3ResourceName, "id"), |
| 76 | + resource.TestCheckResourceAttr(s3ResourceName, "name", sNameUpdated), |
| 77 | + resource.TestCheckResourceAttr(s3ResourceName, "description", sDescriptionUpdated), |
| 78 | + resource.TestCheckResourceAttr(s3ResourceName, "category", sCategoryUpdated), |
| 79 | + resource.TestCheckResourceAttr(s3ResourceName, "content_type", "AwsS3ArchiveBucket"), |
| 80 | + resource.TestCheckResourceAttr(s3ResourceName, "path.0.type", "S3BucketPathExpression"), |
| 81 | + ), |
| 82 | + }, |
| 83 | + }, |
| 84 | + }) |
| 85 | +} |
| 86 | +func testAccCheckS3ArchiveSourceDestroy(s *terraform.State) error { |
| 87 | + client := testAccProvider.Meta().(*Client) |
| 88 | + for _, rs := range s.RootModule().Resources { |
| 89 | + if rs.Type != "sumologic_s3_source" && rs.Type != "sumologic_cloudwatch_source" { |
| 90 | + continue |
| 91 | + } |
| 92 | + if rs.Primary.ID == "" { |
| 93 | + return fmt.Errorf("HTTP Source destruction check: HTTP Source ID is not set") |
| 94 | + } |
| 95 | + id, err := strconv.Atoi(rs.Primary.ID) |
| 96 | + if err != nil { |
| 97 | + return fmt.Errorf("Encountered an error: " + err.Error()) |
| 98 | + } |
| 99 | + collectorID, err := strconv.Atoi(rs.Primary.Attributes["collector_id"]) |
| 100 | + if err != nil { |
| 101 | + return fmt.Errorf("Encountered an error: " + err.Error()) |
| 102 | + } |
| 103 | + s, err := client.GetPollingSource(collectorID, id) |
| 104 | + if err != nil { |
| 105 | + return fmt.Errorf("Encountered an error: " + err.Error()) |
| 106 | + } |
| 107 | + if s != nil { |
| 108 | + return fmt.Errorf("Polling Source still exists") |
| 109 | + } |
| 110 | + } |
| 111 | + return nil |
| 112 | +} |
| 113 | +func testAccCheckS3ArchiveSourceExists(n string, pollingSource *PollingSource) resource.TestCheckFunc { |
| 114 | + return func(s *terraform.State) error { |
| 115 | + rs, ok := s.RootModule().Resources[n] |
| 116 | + if !ok { |
| 117 | + return fmt.Errorf("not found: %s", n) |
| 118 | + } |
| 119 | + if rs.Primary.ID == "" { |
| 120 | + return fmt.Errorf("Polling Source ID is not set") |
| 121 | + } |
| 122 | + id, err := strconv.Atoi(rs.Primary.ID) |
| 123 | + if err != nil { |
| 124 | + return fmt.Errorf("Polling Source id should be int; got %s", rs.Primary.ID) |
| 125 | + } |
| 126 | + collectorID, err := strconv.Atoi(rs.Primary.Attributes["collector_id"]) |
| 127 | + if err != nil { |
| 128 | + return fmt.Errorf("Encountered an error: " + err.Error()) |
| 129 | + } |
| 130 | + c := testAccProvider.Meta().(*Client) |
| 131 | + pollingSourceResp, err := c.GetPollingSource(collectorID, id) |
| 132 | + if err != nil { |
| 133 | + return err |
| 134 | + } |
| 135 | + *pollingSource = *pollingSourceResp |
| 136 | + return nil |
| 137 | + } |
| 138 | +} |
| 139 | +func testAccCheckS3ArchiveSourceValues(pollingSource *PollingSource, name, description, category string) resource.TestCheckFunc { |
| 140 | + return func(s *terraform.State) error { |
| 141 | + if pollingSource.Name != name { |
| 142 | + return fmt.Errorf("bad name, expected \"%s\", got: %#v", name, pollingSource.Name) |
| 143 | + } |
| 144 | + if pollingSource.Description != description { |
| 145 | + return fmt.Errorf("bad description, expected \"%s\", got: %#v", description, pollingSource.Description) |
| 146 | + } |
| 147 | + if pollingSource.Category != category { |
| 148 | + return fmt.Errorf("bad category, expected \"%s\", got: %#v", category, pollingSource.Category) |
| 149 | + } |
| 150 | + return nil |
| 151 | + } |
| 152 | +} |
| 153 | +func testAccSumologicS3ArchiveSourceConfig(cName, cDescription, cCategory, sName, sDescription, sCategory, testAwsRoleArn, testAwsBucket string) string { |
| 154 | + return fmt.Sprintf(` |
| 155 | +resource "sumologic_collector" "test" { |
| 156 | + name = "%s" |
| 157 | + description = "%s" |
| 158 | + category = "%s" |
| 159 | +} |
| 160 | +resource "sumologic_s3_archive_source" "s3_archive" { |
| 161 | + name = "%s" |
| 162 | + description = "%s" |
| 163 | + category = "%s" |
| 164 | + content_type = "AwsS3ArchiveBucket" |
| 165 | + scan_interval = 300000 |
| 166 | + paused = false |
| 167 | + collector_id = "${sumologic_collector.test.id}" |
| 168 | + authentication { |
| 169 | + type = "AWSRoleBasedAuthentication" |
| 170 | + role_arn = "%s" |
| 171 | + } |
| 172 | + path { |
| 173 | + type = "S3BucketPathExpression" |
| 174 | + bucket_name = "%s" |
| 175 | + path_expression = "*" |
| 176 | + } |
| 177 | + } |
| 178 | +
|
| 179 | +`, cName, cDescription, cCategory, sName, sDescription, sCategory, testAwsRoleArn, testAwsBucket) |
| 180 | +} |
0 commit comments