|
| 1 | +package sumologic |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "strconv" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 12 | + "github.com/hashicorp/terraform-plugin-sdk/terraform" |
| 13 | +) |
| 14 | + |
| 15 | +func shouldTestGcpMetricsSource() bool { |
| 16 | + return !strings.EqualFold(os.Getenv("SUMOLOGIC_ENABLE_GCP_METRICS_ACC_TESTS"), "false") |
| 17 | +} |
| 18 | + |
| 19 | +func TestAccSumologicGcpMetricsSource_create(t *testing.T) { |
| 20 | + if shouldTestGcpMetricsSource() { |
| 21 | + var GcpMetricsSource PollingSource |
| 22 | + var collector Collector |
| 23 | + cName, cDescription, cCategory := getRandomizedParams() |
| 24 | + sName, sDescription, sCategory := getRandomizedParams() |
| 25 | + GcpMetricsResourceName := "sumologic_gcp_metrics_source.gcp_metrics_source" |
| 26 | + resource.Test(t, resource.TestCase{ |
| 27 | + PreCheck: func() { getServiceAccountCreds(t) }, |
| 28 | + Providers: testAccProviders, |
| 29 | + CheckDestroy: testAccCheckGcpMetricsSourceDestroy, |
| 30 | + Steps: []resource.TestStep{ |
| 31 | + { |
| 32 | + Config: testAccSumologicGcpMetricsSourceConfig(t, cName, cDescription, cCategory, sName, sDescription, sCategory), |
| 33 | + Check: resource.ComposeTestCheckFunc( |
| 34 | + testAccCheckGcpMetricsSourceExists(GcpMetricsResourceName, &GcpMetricsSource), |
| 35 | + testAccCheckGcpMetricsSourceValues(&GcpMetricsSource, sName, sDescription, sCategory), |
| 36 | + testAccCheckCollectorExists("sumologic_collector.test", &collector), |
| 37 | + testAccCheckCollectorValues(&collector, cName, cDescription, cCategory, "Etc/UTC", ""), |
| 38 | + resource.TestCheckResourceAttrSet(GcpMetricsResourceName, "id"), |
| 39 | + resource.TestCheckResourceAttr(GcpMetricsResourceName, "name", sName), |
| 40 | + resource.TestCheckResourceAttr(GcpMetricsResourceName, "description", sDescription), |
| 41 | + resource.TestCheckResourceAttr(GcpMetricsResourceName, "category", sCategory), |
| 42 | + resource.TestCheckResourceAttr(GcpMetricsResourceName, "content_type", "GcpMetrics"), |
| 43 | + resource.TestCheckResourceAttr(GcpMetricsResourceName, "path.0.type", "GcpMetricsPath"), |
| 44 | + ), |
| 45 | + }, |
| 46 | + }, |
| 47 | + }) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func TestAccSumologicGcpMetricsSource_update(t *testing.T) { |
| 52 | + if shouldTestGcpMetricsSource() { |
| 53 | + var GcpMetricsSource PollingSource |
| 54 | + cName, cDescription, cCategory := getRandomizedParams() |
| 55 | + sName, sDescription, sCategory := getRandomizedParams() |
| 56 | + sNameUpdated, sDescriptionUpdated, sCategoryUpdated := getRandomizedParams() |
| 57 | + GcpMetricsResourceName := "sumologic_gcp_metrics_source.gcp_metrics_source" |
| 58 | + resource.Test(t, resource.TestCase{ |
| 59 | + PreCheck: func() { getServiceAccountCreds(t) }, |
| 60 | + Providers: testAccProviders, |
| 61 | + CheckDestroy: testAccCheckHTTPSourceDestroy, |
| 62 | + Steps: []resource.TestStep{ |
| 63 | + { |
| 64 | + Config: testAccSumologicGcpMetricsSourceConfig(t, cName, cDescription, cCategory, sName, sDescription, sCategory), |
| 65 | + Check: resource.ComposeTestCheckFunc( |
| 66 | + testAccCheckGcpMetricsSourceExists(GcpMetricsResourceName, &GcpMetricsSource), |
| 67 | + testAccCheckGcpMetricsSourceValues(&GcpMetricsSource, sName, sDescription, sCategory), |
| 68 | + resource.TestCheckResourceAttrSet(GcpMetricsResourceName, "id"), |
| 69 | + resource.TestCheckResourceAttr(GcpMetricsResourceName, "name", sName), |
| 70 | + resource.TestCheckResourceAttr(GcpMetricsResourceName, "description", sDescription), |
| 71 | + resource.TestCheckResourceAttr(GcpMetricsResourceName, "category", sCategory), |
| 72 | + resource.TestCheckResourceAttr(GcpMetricsResourceName, "content_type", "GcpMetrics"), |
| 73 | + resource.TestCheckResourceAttr(GcpMetricsResourceName, "path.0.type", "GcpMetricsPath"), |
| 74 | + ), |
| 75 | + }, |
| 76 | + { |
| 77 | + Config: testAccSumologicGcpMetricsSourceConfig(t, cName, cDescription, cCategory, sNameUpdated, sDescriptionUpdated, sCategoryUpdated), |
| 78 | + Check: resource.ComposeTestCheckFunc( |
| 79 | + testAccCheckGcpMetricsSourceExists(GcpMetricsResourceName, &GcpMetricsSource), |
| 80 | + testAccCheckGcpMetricsSourceValues(&GcpMetricsSource, sNameUpdated, sDescriptionUpdated, sCategoryUpdated), |
| 81 | + resource.TestCheckResourceAttrSet(GcpMetricsResourceName, "id"), |
| 82 | + resource.TestCheckResourceAttr(GcpMetricsResourceName, "name", sNameUpdated), |
| 83 | + resource.TestCheckResourceAttr(GcpMetricsResourceName, "description", sDescriptionUpdated), |
| 84 | + resource.TestCheckResourceAttr(GcpMetricsResourceName, "category", sCategoryUpdated), |
| 85 | + resource.TestCheckResourceAttr(GcpMetricsResourceName, "content_type", "GcpMetrics"), |
| 86 | + resource.TestCheckResourceAttr(GcpMetricsResourceName, "path.0.type", "GcpMetricsPath"), |
| 87 | + ), |
| 88 | + }, |
| 89 | + }, |
| 90 | + }) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func testAccCheckGcpMetricsSourceDestroy(s *terraform.State) error { |
| 95 | + client := testAccProvider.Meta().(*Client) |
| 96 | + for _, rs := range s.RootModule().Resources { |
| 97 | + if rs.Type != "sumologic_gcp_metrics_source" { |
| 98 | + continue |
| 99 | + } |
| 100 | + if rs.Primary.ID == "" { |
| 101 | + return fmt.Errorf("HTTP Source destruction check: HTTP Source ID is not set") |
| 102 | + } |
| 103 | + id, err := strconv.Atoi(rs.Primary.ID) |
| 104 | + if err != nil { |
| 105 | + return fmt.Errorf("Encountered an error: " + err.Error()) |
| 106 | + } |
| 107 | + collectorID, err := strconv.Atoi(rs.Primary.Attributes["collector_id"]) |
| 108 | + if err != nil { |
| 109 | + return fmt.Errorf("Encountered an error: " + err.Error()) |
| 110 | + } |
| 111 | + s, err := client.GetPollingSource(collectorID, id) |
| 112 | + if err != nil { |
| 113 | + return fmt.Errorf("Encountered an error: " + err.Error()) |
| 114 | + } |
| 115 | + if s != nil { |
| 116 | + return fmt.Errorf("Polling Source still exists") |
| 117 | + } |
| 118 | + } |
| 119 | + return nil |
| 120 | +} |
| 121 | + |
| 122 | +func testAccCheckGcpMetricsSourceExists(n string, pollingSource *PollingSource) resource.TestCheckFunc { |
| 123 | + return func(s *terraform.State) error { |
| 124 | + rs, ok := s.RootModule().Resources[n] |
| 125 | + if !ok { |
| 126 | + return fmt.Errorf("not found: %s", n) |
| 127 | + } |
| 128 | + if rs.Primary.ID == "" { |
| 129 | + return fmt.Errorf("Polling Source ID is not set") |
| 130 | + } |
| 131 | + id, err := strconv.Atoi(rs.Primary.ID) |
| 132 | + if err != nil { |
| 133 | + return fmt.Errorf("Polling Source id should be int; got %s", rs.Primary.ID) |
| 134 | + } |
| 135 | + collectorID, err := strconv.Atoi(rs.Primary.Attributes["collector_id"]) |
| 136 | + if err != nil { |
| 137 | + return fmt.Errorf("Encountered an error: " + err.Error()) |
| 138 | + } |
| 139 | + c := testAccProvider.Meta().(*Client) |
| 140 | + pollingSourceResp, err := c.GetPollingSource(collectorID, id) |
| 141 | + if err != nil { |
| 142 | + return err |
| 143 | + } |
| 144 | + *pollingSource = *pollingSourceResp |
| 145 | + return nil |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +func testAccCheckGcpMetricsSourceValues(pollingSource *PollingSource, name, description, category string) resource.TestCheckFunc { |
| 150 | + return func(s *terraform.State) error { |
| 151 | + if pollingSource.Name != name { |
| 152 | + return fmt.Errorf("bad name, expected \"%s\", got: %#v", name, pollingSource.Name) |
| 153 | + } |
| 154 | + if pollingSource.Description != description { |
| 155 | + return fmt.Errorf("bad description, expected \"%s\", got: %#v", description, pollingSource.Description) |
| 156 | + } |
| 157 | + if pollingSource.Category != category { |
| 158 | + return fmt.Errorf("bad category, expected \"%s\", got: %#v", category, pollingSource.Category) |
| 159 | + } |
| 160 | + return nil |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +type ServiceAccountCreds struct { |
| 165 | + Type string `json:"type"` |
| 166 | + ProjectId string `json:"project_id"` |
| 167 | + PrivateKeyId string `json:"private_key_id"` |
| 168 | + PrivateKey string `json:"private_key"` |
| 169 | + ClientEmail string `json:"client_email"` |
| 170 | + ClientId string `json:"client_id"` |
| 171 | + AuthUri string `json:"auth_uri"` |
| 172 | + TokenUri string `json:"token_uri"` |
| 173 | + AuthProviderX509CertUrl string `json:"auth_provider_x509_cert_url"` |
| 174 | + ClientX509CertUrl string `json:"client_x509_cert_url"` |
| 175 | +} |
| 176 | + |
| 177 | +func getServiceAccountCreds(t *testing.T) ServiceAccountCreds { |
| 178 | + var err error |
| 179 | + contentBytes := []byte("") |
| 180 | + serviceAccountDetailsJsonEnvName := "SUMOLOGIC_TEST_GOOGLE_APPLICATION_CREDENTIALS" |
| 181 | + |
| 182 | + serviceAccountDetailsJson, isEnvVarDefined := os.LookupEnv(serviceAccountDetailsJsonEnvName) |
| 183 | + if !isEnvVarDefined { |
| 184 | + t.Fatal(fmt.Sprintf("Environment variable %#v has to be defined", serviceAccountDetailsJsonEnvName)) |
| 185 | + } else if len(serviceAccountDetailsJson) == 0 { |
| 186 | + t.Fatal(fmt.Sprintf("Environment variable %#v can not be empty string", serviceAccountDetailsJsonEnvName)) |
| 187 | + } |
| 188 | + |
| 189 | + contentBytes = []byte(serviceAccountDetailsJson) |
| 190 | + var serviceAccountCreds ServiceAccountCreds |
| 191 | + err = json.Unmarshal(contentBytes, &serviceAccountCreds) |
| 192 | + if err != nil { |
| 193 | + t.Fatal(fmt.Sprintf("Failed to parse content pointed by environment variable %#v", serviceAccountDetailsJsonEnvName)) |
| 194 | + } |
| 195 | + return serviceAccountCreds |
| 196 | +} |
| 197 | + |
| 198 | +func testAccSumologicGcpMetricsSourceConfig(t *testing.T, cName, cDescription, cCategory, sName, sDescription, sCategory string) string { |
| 199 | + cred := getServiceAccountCreds(t) |
| 200 | + srcStr := fmt.Sprintf(` |
| 201 | + resource "sumologic_collector" "test" { |
| 202 | + name = "%s" |
| 203 | + description = "%s" |
| 204 | + category = "%s" |
| 205 | + } |
| 206 | + resource "sumologic_gcp_metrics_source" "gcp_metrics_source" { |
| 207 | + name = "%s" |
| 208 | + description = "%s" |
| 209 | + category = "%s" |
| 210 | + content_type = "GcpMetrics" |
| 211 | + scan_interval = 300000 |
| 212 | + paused = false |
| 213 | + collector_id = "${sumologic_collector.test.id}" |
| 214 | + authentication { |
| 215 | + type = "%s" |
| 216 | + project_id = "%s" |
| 217 | + private_key_id = "%s" |
| 218 | + private_key = <<EOPK |
| 219 | +%sEOPK |
| 220 | + client_email = "%s" |
| 221 | + client_id = "%s" |
| 222 | + auth_uri = "%s" |
| 223 | + token_uri = "%s" |
| 224 | + auth_provider_x509_cert_url = "%s" |
| 225 | + client_x509_cert_url = "%s" |
| 226 | + } |
| 227 | + path { |
| 228 | + type = "GcpMetricsPath" |
| 229 | + limit_to_regions = ["asia-south1"] |
| 230 | + limit_to_services = ["Compute Engine", "CloudSQL"] |
| 231 | + } |
| 232 | + lifecycle { |
| 233 | + ignore_changes = [authentication[0].private_key] |
| 234 | + } |
| 235 | + } |
| 236 | + `, cName, cDescription, cCategory, sName, sDescription, sCategory, |
| 237 | + cred.Type, cred.ProjectId, cred.PrivateKeyId, cred.PrivateKey, cred.ClientEmail, cred.ClientId, cred.AuthUri, cred.TokenUri, cred.AuthProviderX509CertUrl, cred.ClientX509CertUrl) |
| 238 | + return srcStr |
| 239 | +} |
0 commit comments