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