|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | +package google |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 10 | + "github.com/hashicorp/terraform-provider-google-beta/google-beta/acctest" |
| 11 | + "github.com/hashicorp/terraform-provider-google-beta/google-beta/envvar" |
| 12 | + "github.com/hashicorp/terraform-provider-google-beta/google-beta/services/monitoring" |
| 13 | + "github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource" |
| 14 | +) |
| 15 | + |
| 16 | +func TestAccMonitoringMonitoredProject_projectNumLongForm(t *testing.T) { |
| 17 | + t.Parallel() |
| 18 | + |
| 19 | + context := map[string]interface{}{ |
| 20 | + "org_id": envvar.GetTestOrgFromEnv(t), |
| 21 | + "project_id": envvar.GetTestProjectFromEnv(), |
| 22 | + "random_suffix": acctest.RandString(t, 10), |
| 23 | + } |
| 24 | + |
| 25 | + acctest.VcrTest(t, resource.TestCase{ |
| 26 | + PreCheck: func() { acctest.AccTestPreCheck(t) }, |
| 27 | + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), |
| 28 | + CheckDestroy: testAccCheckMonitoringMonitoredProjectDestroyProducer(t), |
| 29 | + Steps: []resource.TestStep{ |
| 30 | + { |
| 31 | + Config: testAccMonitoringMonitoredProject_projectNumLongForm(context), |
| 32 | + }, |
| 33 | + { |
| 34 | + ResourceName: "google_monitoring_monitored_project.primary", |
| 35 | + ImportState: true, |
| 36 | + ImportStateVerify: true, |
| 37 | + ImportStateVerifyIgnore: []string{"metrics_scope"}, |
| 38 | + }, |
| 39 | + }, |
| 40 | + }) |
| 41 | +} |
| 42 | + |
| 43 | +func TestAccMonitoringMonitoredProject_projectNumShortForm(t *testing.T) { |
| 44 | + t.Parallel() |
| 45 | + |
| 46 | + context := map[string]interface{}{ |
| 47 | + "org_id": envvar.GetTestOrgFromEnv(t), |
| 48 | + "project_id": envvar.GetTestProjectFromEnv(), |
| 49 | + "random_suffix": acctest.RandString(t, 10), |
| 50 | + } |
| 51 | + |
| 52 | + acctest.VcrTest(t, resource.TestCase{ |
| 53 | + PreCheck: func() { acctest.AccTestPreCheck(t) }, |
| 54 | + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), |
| 55 | + CheckDestroy: testAccCheckMonitoringMonitoredProjectDestroyProducer(t), |
| 56 | + Steps: []resource.TestStep{ |
| 57 | + { |
| 58 | + Config: testAccMonitoringMonitoredProject_projectNumShortForm(context), |
| 59 | + }, |
| 60 | + { |
| 61 | + ResourceName: "google_monitoring_monitored_project.primary", |
| 62 | + ImportState: true, |
| 63 | + ImportStateVerify: true, |
| 64 | + ImportStateVerifyIgnore: []string{"metrics_scope"}, |
| 65 | + }, |
| 66 | + }, |
| 67 | + }) |
| 68 | +} |
| 69 | + |
| 70 | +func testAccMonitoringMonitoredProject_projectNumLongForm(context map[string]interface{}) string { |
| 71 | + return acctest.Nprintf(` |
| 72 | +resource "google_monitoring_monitored_project" "primary" { |
| 73 | + metrics_scope = "%{project_id}" |
| 74 | + name = "locations/global/metricsScopes/%{project_id}/projects/${google_project.basic.number}" |
| 75 | +} |
| 76 | +
|
| 77 | +resource "google_project" "basic" { |
| 78 | + project_id = "tf-test-m-id%{random_suffix}" |
| 79 | + name = "tf-test-m-id%{random_suffix}-display" |
| 80 | + org_id = "%{org_id}" |
| 81 | +} |
| 82 | +`, context) |
| 83 | +} |
| 84 | + |
| 85 | +func testAccMonitoringMonitoredProject_projectNumShortForm(context map[string]interface{}) string { |
| 86 | + return acctest.Nprintf(` |
| 87 | +resource "google_monitoring_monitored_project" "primary" { |
| 88 | + metrics_scope = "%{project_id}" |
| 89 | + name = "${google_project.basic.number}" |
| 90 | +} |
| 91 | +
|
| 92 | +resource "google_project" "basic" { |
| 93 | + project_id = "tf-test-m-id%{random_suffix}" |
| 94 | + name = "tf-test-m-id%{random_suffix}-display" |
| 95 | + org_id = "%{org_id}" |
| 96 | +} |
| 97 | +`, context) |
| 98 | +} |
| 99 | + |
| 100 | +func TestUnitMonitoringMonitoredProject_nameDiffSuppress(t *testing.T) { |
| 101 | + for _, tc := range monitoringMonitoredProjectDiffSuppressTestCases { |
| 102 | + tc.Test(t) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +type MonitoringMonitoredProjectDiffSuppressTestCase struct { |
| 107 | + Name string |
| 108 | + KeysToSuppress []string |
| 109 | + Before map[string]interface{} |
| 110 | + After map[string]interface{} |
| 111 | +} |
| 112 | + |
| 113 | +var monitoringMonitoredProjectDiffSuppressTestCases = []MonitoringMonitoredProjectDiffSuppressTestCase{ |
| 114 | + // Project Id -> project Id |
| 115 | + { |
| 116 | + Name: "short project id to long project id suppressed", |
| 117 | + KeysToSuppress: []string{"name"}, |
| 118 | + Before: map[string]interface{}{ |
| 119 | + "name": "sameId", |
| 120 | + }, |
| 121 | + After: map[string]interface{}{ |
| 122 | + "name": "locations/global/metricsScopes/projectId/projects/sameId", |
| 123 | + }, |
| 124 | + }, |
| 125 | + { |
| 126 | + Name: "long project id to short project id suppressed", |
| 127 | + KeysToSuppress: []string{"name"}, |
| 128 | + Before: map[string]interface{}{ |
| 129 | + "name": "locations/global/metricsScopes/projectId/projects/sameId", |
| 130 | + }, |
| 131 | + After: map[string]interface{}{ |
| 132 | + "name": "sameId", |
| 133 | + }, |
| 134 | + }, |
| 135 | + { |
| 136 | + Name: "short project id to long project id show diff", |
| 137 | + KeysToSuppress: []string{}, |
| 138 | + Before: map[string]interface{}{ |
| 139 | + "name": "oldId", |
| 140 | + }, |
| 141 | + After: map[string]interface{}{ |
| 142 | + "name": "locations/global/metricsScopes/projectId/projects/newId", |
| 143 | + }, |
| 144 | + }, |
| 145 | + { |
| 146 | + Name: "long project id to short project id show diff", |
| 147 | + KeysToSuppress: []string{}, |
| 148 | + Before: map[string]interface{}{ |
| 149 | + "name": "locations/global/metricsScopes/projectId/projects/oldId", |
| 150 | + }, |
| 151 | + After: map[string]interface{}{ |
| 152 | + "name": "newId", |
| 153 | + }, |
| 154 | + }, |
| 155 | + |
| 156 | + // Project Num -> Project Num |
| 157 | + { |
| 158 | + Name: "short project num to long project num suppressed", |
| 159 | + KeysToSuppress: []string{"name"}, |
| 160 | + Before: map[string]interface{}{ |
| 161 | + "name": "000000000000", |
| 162 | + }, |
| 163 | + After: map[string]interface{}{ |
| 164 | + "name": "locations/global/metricsScopes/projectId/projects/000000000000", |
| 165 | + }, |
| 166 | + }, |
| 167 | + { |
| 168 | + Name: "long project num to short project num suppressed", |
| 169 | + KeysToSuppress: []string{"name"}, |
| 170 | + Before: map[string]interface{}{ |
| 171 | + "name": "locations/global/metricsScopes/projectId/projects/000000000000", |
| 172 | + }, |
| 173 | + After: map[string]interface{}{ |
| 174 | + "name": "000000000000", |
| 175 | + }, |
| 176 | + }, |
| 177 | + { |
| 178 | + Name: "short project num to long project num show diff", |
| 179 | + KeysToSuppress: []string{}, |
| 180 | + Before: map[string]interface{}{ |
| 181 | + "name": "000000000000", |
| 182 | + }, |
| 183 | + After: map[string]interface{}{ |
| 184 | + "name": "locations/global/metricsScopes/projectId/projects/111111111111", |
| 185 | + }, |
| 186 | + }, |
| 187 | + { |
| 188 | + Name: "long project num to short project num show diff", |
| 189 | + KeysToSuppress: []string{}, |
| 190 | + Before: map[string]interface{}{ |
| 191 | + "name": "locations/global/metricsScopes/projectId/projects/000000000000", |
| 192 | + }, |
| 193 | + After: map[string]interface{}{ |
| 194 | + "name": "111111111111", |
| 195 | + }, |
| 196 | + }, |
| 197 | + |
| 198 | + // Project id <--> Project num |
| 199 | + // Every variation of this should be suppressed. We cannot detect |
| 200 | + // if the project number matches the id within a diff suppress |
| 201 | + { |
| 202 | + Name: "short project id to long project num suppressed", |
| 203 | + KeysToSuppress: []string{"name"}, |
| 204 | + Before: map[string]interface{}{ |
| 205 | + "name": "oldId", |
| 206 | + }, |
| 207 | + After: map[string]interface{}{ |
| 208 | + "name": "locations/global/metricsScopes/projectId/projects/111111111111", |
| 209 | + }, |
| 210 | + }, |
| 211 | + { |
| 212 | + Name: "long project id to short project num suppressed", |
| 213 | + KeysToSuppress: []string{"name"}, |
| 214 | + Before: map[string]interface{}{ |
| 215 | + "name": "locations/global/metricsScopes/projectId/projects/oldId", |
| 216 | + }, |
| 217 | + After: map[string]interface{}{ |
| 218 | + "name": "111111111111", |
| 219 | + }, |
| 220 | + }, |
| 221 | + { |
| 222 | + Name: "short project num to long project id suppressed", |
| 223 | + KeysToSuppress: []string{"name"}, |
| 224 | + Before: map[string]interface{}{ |
| 225 | + "name": "000000000000", |
| 226 | + }, |
| 227 | + After: map[string]interface{}{ |
| 228 | + "name": "locations/global/metricsScopes/projectId/projects/newId", |
| 229 | + }, |
| 230 | + }, |
| 231 | + { |
| 232 | + Name: "long project num to short project id suppressed", |
| 233 | + KeysToSuppress: []string{"name"}, |
| 234 | + Before: map[string]interface{}{ |
| 235 | + "name": "locations/global/metricsScopes/projectId/projects/000000000000", |
| 236 | + }, |
| 237 | + After: map[string]interface{}{ |
| 238 | + "name": "newId", |
| 239 | + }, |
| 240 | + }, |
| 241 | + |
| 242 | + // Empty -> anything (resource creation) |
| 243 | + { |
| 244 | + Name: "empty name to anything shows diff", |
| 245 | + KeysToSuppress: []string{}, |
| 246 | + Before: map[string]interface{}{ |
| 247 | + "name": "", |
| 248 | + }, |
| 249 | + After: map[string]interface{}{ |
| 250 | + "name": "newId", |
| 251 | + }, |
| 252 | + }, |
| 253 | +} |
| 254 | + |
| 255 | +func (tc *MonitoringMonitoredProjectDiffSuppressTestCase) Test(t *testing.T) { |
| 256 | + mockResourceDiff := &tpgresource.ResourceDiffMock{ |
| 257 | + Before: tc.Before, |
| 258 | + After: tc.After, |
| 259 | + } |
| 260 | + |
| 261 | + keySuppressionMap := map[string]bool{} |
| 262 | + for key := range tc.Before { |
| 263 | + keySuppressionMap[key] = false |
| 264 | + } |
| 265 | + for key := range tc.After { |
| 266 | + keySuppressionMap[key] = false |
| 267 | + } |
| 268 | + |
| 269 | + for _, key := range tc.KeysToSuppress { |
| 270 | + keySuppressionMap[key] = true |
| 271 | + } |
| 272 | + |
| 273 | + for key, tcSuppress := range keySuppressionMap { |
| 274 | + oldValue, ok := tc.Before[key] |
| 275 | + if !ok { |
| 276 | + oldValue = "" |
| 277 | + } |
| 278 | + newValue, ok := tc.After[key] |
| 279 | + if !ok { |
| 280 | + newValue = "" |
| 281 | + } |
| 282 | + suppressed := monitoring.ResourceMonitoringMonitoredProjectNameDiffSuppressFunc(key, fmt.Sprintf("%v", oldValue), fmt.Sprintf("%v", newValue), mockResourceDiff) |
| 283 | + if suppressed != tcSuppress { |
| 284 | + var expectation string |
| 285 | + if tcSuppress { |
| 286 | + expectation = "be" |
| 287 | + } else { |
| 288 | + expectation = "not be" |
| 289 | + } |
| 290 | + t.Errorf("Test %s: expected key `%s` to %s suppressed", tc.Name, key, expectation) |
| 291 | + } |
| 292 | + } |
| 293 | +} |
0 commit comments