|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | +package certificatemanager_test |
| 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 | +) |
| 13 | + |
| 14 | +func TestAccDataSourceGoogleCertificateManagerCertificateMap_basic(t *testing.T) { |
| 15 | + t.Parallel() |
| 16 | + |
| 17 | + project := envvar.GetTestProjectFromEnv() |
| 18 | + |
| 19 | + description := "My acceptance data source test certificate map" |
| 20 | + name := fmt.Sprintf("tf-test-certificate-map-%d", acctest.RandInt(t)) |
| 21 | + id := fmt.Sprintf("projects/%s/locations/global/certificateMaps/%s", project, name) |
| 22 | + |
| 23 | + acctest.VcrTest(t, resource.TestCase{ |
| 24 | + PreCheck: func() { acctest.AccTestPreCheck(t) }, |
| 25 | + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), |
| 26 | + Steps: []resource.TestStep{ |
| 27 | + { |
| 28 | + Config: testAccDataSourceGoogleCertificateManagerCertificateMap_basic(name, description), |
| 29 | + Check: resource.ComposeTestCheckFunc( |
| 30 | + resource.TestCheckResourceAttr("data.google_certificate_manager_certificate_map.cert_map_data", "id", id), |
| 31 | + resource.TestCheckResourceAttr("data.google_certificate_manager_certificate_map.cert_map_data", "description", description), |
| 32 | + resource.TestCheckResourceAttr("data.google_certificate_manager_certificate_map.cert_map_data", "name", name), |
| 33 | + ), |
| 34 | + }, |
| 35 | + }, |
| 36 | + }) |
| 37 | +} |
| 38 | + |
| 39 | +func testAccDataSourceGoogleCertificateManagerCertificateMap_basic(certificateMapName, certificateMapDescription string) string { |
| 40 | + return fmt.Sprintf(` |
| 41 | +resource "google_certificate_manager_certificate_map" "cert_map" { |
| 42 | + name = "%s" |
| 43 | + description = "%s" |
| 44 | + labels = { |
| 45 | + "terraform" : true, |
| 46 | + "acc-test" : true, |
| 47 | + } |
| 48 | +} |
| 49 | +data "google_certificate_manager_certificate_map" "cert_map_data" { |
| 50 | + name = google_certificate_manager_certificate_map.cert_map.name |
| 51 | +} |
| 52 | +`, certificateMapName, certificateMapDescription) |
| 53 | +} |
| 54 | + |
| 55 | +func TestAccDataSourceGoogleCertificateManagerCertificateMap_certificateMapEntryUsingMapDatasource(t *testing.T) { |
| 56 | + t.Parallel() |
| 57 | + |
| 58 | + project := envvar.GetTestProjectFromEnv() |
| 59 | + |
| 60 | + certName := fmt.Sprintf("tf-test-certificate-%d", acctest.RandInt(t)) |
| 61 | + mapEntryName := fmt.Sprintf("tf-test-certificate-map-entry-%d", acctest.RandInt(t)) |
| 62 | + mapName := fmt.Sprintf("tf-test-certificate-map-%d", acctest.RandInt(t)) |
| 63 | + id := fmt.Sprintf("projects/%s/locations/global/certificateMaps/%s", project, mapName) |
| 64 | + |
| 65 | + acctest.VcrTest(t, resource.TestCase{ |
| 66 | + PreCheck: func() { acctest.AccTestPreCheck(t) }, |
| 67 | + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), |
| 68 | + Steps: []resource.TestStep{ |
| 69 | + { |
| 70 | + Config: testAccDataSourceGoogleCertificateManagerCertificateMap_certificateMapEntryUsingMapDatasource(mapName, mapEntryName, certName), |
| 71 | + Check: resource.ComposeTestCheckFunc( |
| 72 | + resource.TestCheckResourceAttr("data.google_certificate_manager_certificate_map.cert_map_data", "id", id), |
| 73 | + resource.TestCheckResourceAttr("data.google_certificate_manager_certificate_map.cert_map_data", "name", mapName), |
| 74 | + resource.TestCheckResourceAttr("google_certificate_manager_certificate_map_entry.cert_map_entry", "map", mapName), // check that the certificate map entry is referencing the data source |
| 75 | + |
| 76 | + ), |
| 77 | + }, |
| 78 | + }, |
| 79 | + }) |
| 80 | +} |
| 81 | + |
| 82 | +func testAccDataSourceGoogleCertificateManagerCertificateMap_certificateMapEntryUsingMapDatasource(certificateMapName, certificateMapEntryName, certificateName string) string { |
| 83 | + return fmt.Sprintf(` |
| 84 | +resource "google_certificate_manager_certificate_map" "cert_map" { |
| 85 | + name = "%s" |
| 86 | + description = "certificate map example created for testing data sources in TF" |
| 87 | + labels = { |
| 88 | + "terraform" : true, |
| 89 | + "acc-test" : true, |
| 90 | + } |
| 91 | +} |
| 92 | +data "google_certificate_manager_certificate_map" "cert_map_data" { |
| 93 | + name = google_certificate_manager_certificate_map.cert_map.name |
| 94 | +} |
| 95 | +resource "google_certificate_manager_certificate" "certificate" { |
| 96 | + name = "%s" |
| 97 | + description = "Global cert" |
| 98 | + self_managed { |
| 99 | + pem_certificate = file("test-fixtures/cert.pem") |
| 100 | + pem_private_key = file("test-fixtures/private-key.pem") |
| 101 | + } |
| 102 | +} |
| 103 | +resource "google_certificate_manager_certificate_map_entry" "cert_map_entry" { |
| 104 | + name = "%s" |
| 105 | + description = "certificate map entry that reference a data source of certificate map and a self managed certificate" |
| 106 | + map = data.google_certificate_manager_certificate_map.cert_map_data.name |
| 107 | + certificates = [google_certificate_manager_certificate.certificate.id] |
| 108 | + matcher = "PRIMARY" |
| 109 | +} |
| 110 | +`, certificateMapName, certificateName, certificateMapEntryName) |
| 111 | +} |
0 commit comments