Skip to content

Commit dc3a8ec

Browse files
modular-magicianHamza Hassan
andauthored
Add support for certificate map datasource (#8972) (#6316)
Signed-off-by: Modular Magician <[email protected]> Co-authored-by: Hamza Hassan <[email protected]>
1 parent b58487b commit dc3a8ec

File tree

5 files changed

+191
-0
lines changed

5 files changed

+191
-0
lines changed

.changelog/8972.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-datasource
2+
google_certificate_manager_certificate_map
3+
```

google-beta/provider/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,7 @@ func DatasourceMapWithErrors() (map[string]*schema.Resource, error) {
877877
"google_beyondcorp_app_gateway": beyondcorp.DataSourceGoogleBeyondcorpAppGateway(),
878878
"google_billing_account": billing.DataSourceGoogleBillingAccount(),
879879
"google_bigquery_default_service_account": bigquery.DataSourceGoogleBigqueryDefaultServiceAccount(),
880+
"google_certificate_manager_certificate_map": certificatemanager.DataSourceGoogleCertificateManagerCertificateMap(),
880881
"google_cloudbuild_trigger": cloudbuild.DataSourceGoogleCloudBuildTrigger(),
881882
"google_cloudfunctions_function": cloudfunctions.DataSourceGoogleCloudFunctionsFunction(),
882883
"google_cloudfunctions2_function": cloudfunctions2.DataSourceGoogleCloudFunctions2Function(),
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
package certificatemanager
4+
5+
import (
6+
"fmt"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
"github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource"
10+
transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport"
11+
)
12+
13+
func DataSourceGoogleCertificateManagerCertificateMap() *schema.Resource {
14+
15+
dsSchema := tpgresource.DatasourceSchemaFromResourceSchema(ResourceCertificateManagerCertificateMap().Schema)
16+
tpgresource.AddRequiredFieldsToSchema(dsSchema, "name")
17+
tpgresource.AddOptionalFieldsToSchema(dsSchema, "project")
18+
19+
return &schema.Resource{
20+
Read: dataSourceGoogleCertificateManagerCertificateMapRead,
21+
Schema: dsSchema,
22+
}
23+
}
24+
25+
func dataSourceGoogleCertificateManagerCertificateMapRead(d *schema.ResourceData, meta interface{}) error {
26+
config := meta.(*transport_tpg.Config)
27+
28+
name := d.Get("name").(string)
29+
30+
project, err := tpgresource.GetProject(d, config)
31+
if err != nil {
32+
return err
33+
}
34+
35+
id := fmt.Sprintf("projects/%s/locations/global/certificateMaps/%s", project, name)
36+
d.SetId(id)
37+
err = resourceCertificateManagerCertificateMapRead(d, meta)
38+
if err != nil {
39+
return err
40+
}
41+
42+
if d.Id() == "" {
43+
return fmt.Errorf("%s not found", id)
44+
}
45+
return nil
46+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
subcategory: "Certificate manager"
3+
description: |-
4+
Contains the data that describes a Certificate Map
5+
---
6+
# google_certificate_manager_certificate_map
7+
8+
Get info about a Google Certificate Manager Certificate Map resource.
9+
10+
## Example Usage
11+
12+
```tf
13+
data "google_certificate_manager_certificate_map" "default" {
14+
name = "cert-map"
15+
}
16+
```
17+
18+
## Argument Reference
19+
20+
The following arguments are supported:
21+
22+
* `name` - (Required) The name of the certificate map.
23+
24+
- - -
25+
* `project` - (Optional) The ID of the project in which the resource belongs. If it
26+
is not provided, the provider project is used.
27+
28+
## Attributes Reference
29+
30+
See [google_certificate_manager_certificate_map](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/certificate_manager_certificate_map) resource for details of the available attributes.

0 commit comments

Comments
 (0)