|
| 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 TestAccSumologicAzureMetricsSource_create(t *testing.T) { |
| 14 | + var azureMetricsSource PollingSource |
| 15 | + var collector Collector |
| 16 | + cName, cDescription, cCategory := getRandomizedParams() |
| 17 | + sName, sDescription, sCategory := getRandomizedParams() |
| 18 | + azureMetricsResourceName := "sumologic_azure_metrics_source.azure" |
| 19 | + testTenantId := os.Getenv("SUMOLOGIC_TEST_AZURE_TENANT_ID") |
| 20 | + testClientId := os.Getenv("SUMOLOGIC_TEST_AZURE_CLIENT_ID") |
| 21 | + testClientSecret := os.Getenv("SUMOLOGIC_TEST_AZURE_CLIENT_SECRET") |
| 22 | + |
| 23 | + resource.Test(t, resource.TestCase{ |
| 24 | + PreCheck: func() { testAccPreCheck(t) }, |
| 25 | + Providers: testAccProviders, |
| 26 | + CheckDestroy: testAccCheckAzureMetricsSourceDestroy, |
| 27 | + Steps: []resource.TestStep{ |
| 28 | + { |
| 29 | + Config: testAccSumologicAzureMetricsSourceConfig(cName, cDescription, cCategory, sName, sDescription, sCategory, testTenantId, testClientId, testClientSecret), |
| 30 | + Check: resource.ComposeTestCheckFunc( |
| 31 | + testAccCheckAzureMetricsSourceExists(azureMetricsResourceName, &azureMetricsSource), |
| 32 | + testAccCheckAzureMetricsSourceValues(&azureMetricsSource, sName, sDescription, sCategory), |
| 33 | + testAccCheckCollectorExists("sumologic_collector.test", &collector), |
| 34 | + testAccCheckCollectorValues(&collector, cName, cDescription, cCategory, "Etc/UTC", ""), |
| 35 | + resource.TestCheckResourceAttrSet(azureMetricsResourceName, "id"), |
| 36 | + resource.TestCheckResourceAttr(azureMetricsResourceName, "name", sName), |
| 37 | + resource.TestCheckResourceAttr(azureMetricsResourceName, "description", sDescription), |
| 38 | + resource.TestCheckResourceAttr(azureMetricsResourceName, "category", sCategory), |
| 39 | + resource.TestCheckResourceAttr(azureMetricsResourceName, "content_type", "AzureMetrics"), |
| 40 | + resource.TestCheckResourceAttr(azureMetricsResourceName, "path.0.type", "AzureMetricsPath"), |
| 41 | + ), |
| 42 | + ExpectNonEmptyPlan: true, |
| 43 | + }, |
| 44 | + }, |
| 45 | + }) |
| 46 | +} |
| 47 | + |
| 48 | +func TestAccSumologicAzureMetricsSource_update(t *testing.T) { |
| 49 | + var azureMetricsSource PollingSource |
| 50 | + cName, cDescription, cCategory := getRandomizedParams() |
| 51 | + sName, sDescription, sCategory := getRandomizedParams() |
| 52 | + sNameUpdated, sDescriptionUpdated, sCategoryUpdated := getRandomizedParams() |
| 53 | + azureMetricsResourceName := "sumologic_azure_metrics_source.azure" |
| 54 | + testTenantId := os.Getenv("SUMOLOGIC_TEST_AZURE_TENANT_ID") |
| 55 | + testClientId := os.Getenv("SUMOLOGIC_TEST_AZURE_CLIENT_ID") |
| 56 | + testClientSecret := os.Getenv("SUMOLOGIC_TEST_AZURE_CLIENT_SECRET") |
| 57 | + |
| 58 | + resource.Test(t, resource.TestCase{ |
| 59 | + PreCheck: func() { testAccPreCheck(t) }, |
| 60 | + Providers: testAccProviders, |
| 61 | + CheckDestroy: testAccCheckAzureMetricsSourceDestroy, |
| 62 | + Steps: []resource.TestStep{ |
| 63 | + { |
| 64 | + Config: testAccSumologicAzureMetricsSourceConfig(cName, cDescription, cCategory, sName, sDescription, sCategory, testTenantId, testClientId, testClientSecret), |
| 65 | + Check: resource.ComposeTestCheckFunc( |
| 66 | + testAccCheckAzureMetricsSourceExists(azureMetricsResourceName, &azureMetricsSource), |
| 67 | + testAccCheckAzureMetricsSourceValues(&azureMetricsSource, sName, sDescription, sCategory), |
| 68 | + resource.TestCheckResourceAttrSet(azureMetricsResourceName, "id"), |
| 69 | + resource.TestCheckResourceAttr(azureMetricsResourceName, "name", sName), |
| 70 | + resource.TestCheckResourceAttr(azureMetricsResourceName, "description", sDescription), |
| 71 | + resource.TestCheckResourceAttr(azureMetricsResourceName, "category", sCategory), |
| 72 | + resource.TestCheckResourceAttr(azureMetricsResourceName, "content_type", "AzureMetrics"), |
| 73 | + resource.TestCheckResourceAttr(azureMetricsResourceName, "path.0.type", "AzureMetricsPath"), |
| 74 | + ), |
| 75 | + ExpectNonEmptyPlan: true, |
| 76 | + }, |
| 77 | + { |
| 78 | + Config: testAccSumologicAzureMetricsSourceConfig(cName, cDescription, cCategory, sNameUpdated, sDescriptionUpdated, sCategoryUpdated, testTenantId, testClientId, testClientSecret), |
| 79 | + Check: resource.ComposeTestCheckFunc( |
| 80 | + testAccCheckAzureMetricsSourceExists(azureMetricsResourceName, &azureMetricsSource), |
| 81 | + testAccCheckAzureMetricsSourceValues(&azureMetricsSource, sNameUpdated, sDescriptionUpdated, sCategoryUpdated), |
| 82 | + resource.TestCheckResourceAttrSet(azureMetricsResourceName, "id"), |
| 83 | + resource.TestCheckResourceAttr(azureMetricsResourceName, "name", sNameUpdated), |
| 84 | + resource.TestCheckResourceAttr(azureMetricsResourceName, "description", sDescriptionUpdated), |
| 85 | + resource.TestCheckResourceAttr(azureMetricsResourceName, "category", sCategoryUpdated), |
| 86 | + resource.TestCheckResourceAttr(azureMetricsResourceName, "content_type", "AzureMetrics"), |
| 87 | + resource.TestCheckResourceAttr(azureMetricsResourceName, "path.0.type", "AzureMetricsPath"), |
| 88 | + ), |
| 89 | + ExpectNonEmptyPlan: true, |
| 90 | + }, |
| 91 | + }, |
| 92 | + }) |
| 93 | + |
| 94 | +} |
| 95 | + |
| 96 | +func testAccCheckAzureMetricsSourceDestroy(s *terraform.State) error { |
| 97 | + client := testAccProvider.Meta().(*Client) |
| 98 | + for _, rs := range s.RootModule().Resources { |
| 99 | + if rs.Type != "sumologic_azure_metrics_source" { |
| 100 | + continue |
| 101 | + } |
| 102 | + if rs.Primary.ID == "" { |
| 103 | + return fmt.Errorf("Azure Event Hub Log Source destruction check: Azure Event Hub Log Source ID is not set") |
| 104 | + } |
| 105 | + id, err := strconv.Atoi(rs.Primary.ID) |
| 106 | + if err != nil { |
| 107 | + return fmt.Errorf("Encountered an error: " + err.Error()) |
| 108 | + } |
| 109 | + collectorID, err := strconv.Atoi(rs.Primary.Attributes["collector_id"]) |
| 110 | + if err != nil { |
| 111 | + return fmt.Errorf("Encountered an error: " + err.Error()) |
| 112 | + } |
| 113 | + s, err := client.GetPollingSource(collectorID, id) |
| 114 | + if err != nil { |
| 115 | + return fmt.Errorf("Encountered an error: " + err.Error()) |
| 116 | + } |
| 117 | + if s != nil { |
| 118 | + return fmt.Errorf("Polling Source still exists") |
| 119 | + } |
| 120 | + } |
| 121 | + return nil |
| 122 | +} |
| 123 | + |
| 124 | +func testAccCheckAzureMetricsSourceExists(n string, pollingSource *PollingSource) resource.TestCheckFunc { |
| 125 | + return func(s *terraform.State) error { |
| 126 | + rs, ok := s.RootModule().Resources[n] |
| 127 | + if !ok { |
| 128 | + return fmt.Errorf("not found: %s", n) |
| 129 | + } |
| 130 | + if rs.Primary.ID == "" { |
| 131 | + return fmt.Errorf("Polling Source ID is not set") |
| 132 | + } |
| 133 | + id, err := strconv.Atoi(rs.Primary.ID) |
| 134 | + if err != nil { |
| 135 | + return fmt.Errorf("Polling Source id should be int; got %s", rs.Primary.ID) |
| 136 | + } |
| 137 | + collectorID, err := strconv.Atoi(rs.Primary.Attributes["collector_id"]) |
| 138 | + if err != nil { |
| 139 | + return fmt.Errorf("Encountered an error: " + err.Error()) |
| 140 | + } |
| 141 | + c := testAccProvider.Meta().(*Client) |
| 142 | + pollingSourceResp, err := c.GetPollingSource(collectorID, id) |
| 143 | + if err != nil { |
| 144 | + return err |
| 145 | + } |
| 146 | + *pollingSource = *pollingSourceResp |
| 147 | + return nil |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +func testAccCheckAzureMetricsSourceValues(pollingSource *PollingSource, name, description, category string) resource.TestCheckFunc { |
| 152 | + return func(s *terraform.State) error { |
| 153 | + if pollingSource.Name != name { |
| 154 | + return fmt.Errorf("bad name, expected \"%s\", got: %#v", name, pollingSource.Name) |
| 155 | + } |
| 156 | + if pollingSource.Description != description { |
| 157 | + return fmt.Errorf("bad description, expected \"%s\", got: %#v", description, pollingSource.Description) |
| 158 | + } |
| 159 | + if pollingSource.Category != category { |
| 160 | + return fmt.Errorf("bad category, expected \"%s\", got: %#v", category, pollingSource.Category) |
| 161 | + } |
| 162 | + return nil |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +func testAccSumologicAzureMetricsSourceConfig(cName, cDescription, cCategory, sName, sDescription, sCategory, testTenantId, testClientId, testClientSecret string) string { |
| 167 | + return fmt.Sprintf(` |
| 168 | +resource "sumologic_collector" "test" { |
| 169 | + name = "%s" |
| 170 | + description = "%s" |
| 171 | + category = "%s" |
| 172 | +} |
| 173 | +resource "sumologic_azure_metrics_source" "azure" { |
| 174 | + name = "%s" |
| 175 | + description = "%s" |
| 176 | + category = "%s" |
| 177 | + content_type = "AzureMetrics" |
| 178 | + collector_id = "${sumologic_collector.test.id}" |
| 179 | +
|
| 180 | + authentication { |
| 181 | + type = "AzureClientSecretAuthentication" |
| 182 | + tenant_id = "%s" |
| 183 | + client_id = "%s" |
| 184 | + client_secret = "%s" |
| 185 | + } |
| 186 | +
|
| 187 | + path { |
| 188 | + type = "AzureMetricsPath" |
| 189 | + environment = "Azure" |
| 190 | + limit_to_namespaces = ["Microsoft.ClassicStorage/storageAccounts"] |
| 191 | + azure_tag_filters { |
| 192 | + type = "AzureTagFilters" |
| 193 | + namespace = "Microsoft.ClassicStorage/storageAccounts" |
| 194 | + tags { |
| 195 | + name = "test-name-1" |
| 196 | + values = ["value1"] |
| 197 | + } |
| 198 | + tags { |
| 199 | + name = "test-name-2" |
| 200 | + values = ["value2"] |
| 201 | + } |
| 202 | + } |
| 203 | + } |
| 204 | +
|
| 205 | + lifecycle { |
| 206 | + ignore_changes = [authentication.0.client_secret] |
| 207 | + } |
| 208 | +}`, cName, cDescription, cCategory, sName, sDescription, sCategory, testTenantId, testClientId, testClientSecret) |
| 209 | +} |
0 commit comments