Skip to content

Commit 9df10ae

Browse files
Add PrivateCA CertificateAuthority data source (#5750) (#4087)
* Add CA ds * Provider * Fmt * Add CA datasource * Write docs * Check err Signed-off-by: Modular Magician <[email protected]>
1 parent c963805 commit 9df10ae

File tree

6 files changed

+231
-0
lines changed

6 files changed

+231
-0
lines changed

.changelog/5750.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_privateca_certificate_authority
3+
```
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package google
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
7+
)
8+
9+
func dataSourcePrivatecaCertificateAuthority() *schema.Resource {
10+
dsSchema := datasourceSchemaFromResourceSchema(resourcePrivatecaCertificateAuthority().Schema)
11+
addOptionalFieldsToSchema(dsSchema, "project")
12+
addOptionalFieldsToSchema(dsSchema, "location")
13+
addOptionalFieldsToSchema(dsSchema, "pool")
14+
addOptionalFieldsToSchema(dsSchema, "certificate_authority_id")
15+
16+
dsSchema["pem_csr"] = &schema.Schema{
17+
Type: schema.TypeString,
18+
Computed: true,
19+
}
20+
21+
return &schema.Resource{
22+
Read: dataSourcePrivatecaCertificateAuthorityRead,
23+
Schema: dsSchema,
24+
}
25+
}
26+
27+
func dataSourcePrivatecaCertificateAuthorityRead(d *schema.ResourceData, meta interface{}) error {
28+
config := meta.(*Config)
29+
userAgent, err := generateUserAgentString(d, config.userAgent)
30+
if err != nil {
31+
return fmt.Errorf("Error generating user agent: %s", err)
32+
}
33+
34+
id, err := replaceVars(d, config, "projects/{{project}}/locations/{{location}}/caPools/{{pool}}/certificateAuthorities/{{certificate_authority_id}}")
35+
if err != nil {
36+
return fmt.Errorf("Error constructing id: %s", err)
37+
}
38+
39+
d.SetId(id)
40+
41+
err = resourcePrivatecaCertificateAuthorityRead(d, meta)
42+
if err != nil {
43+
return err
44+
}
45+
46+
// pem_csr is only applicable for SUBORDINATE CertificateAuthorities
47+
if d.Get("type") == "SUBORDINATE" {
48+
url, err := replaceVars(d, config, "{{PrivatecaBasePath}}projects/{{project}}/locations/{{location}}/caPools/{{pool}}/certificateAuthorities/{{certificate_authority_id}}:fetch")
49+
if err != nil {
50+
return err
51+
}
52+
53+
billingProject := ""
54+
55+
project, err := getProject(d, config)
56+
if err != nil {
57+
return fmt.Errorf("Error fetching project for CertificateAuthority: %s", err)
58+
}
59+
billingProject = project
60+
61+
// err == nil indicates that the billing_project value was found
62+
if bp, err := getBillingProject(d, config); err == nil {
63+
billingProject = bp
64+
}
65+
66+
res, err := sendRequest(config, "GET", billingProject, url, userAgent, nil)
67+
if err != nil {
68+
return handleNotFoundError(err, d, fmt.Sprintf("PrivatecaCertificateAuthority %q", d.Id()))
69+
}
70+
if err := d.Set("pem_csr", res["pemCsr"]); err != nil {
71+
return fmt.Errorf("Error fetching CertificateAuthority: %s", err)
72+
}
73+
}
74+
75+
return nil
76+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package google
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
)
8+
9+
func TestAccDataSourcePrivatecaCertificateAuthority_privatecaCertificateAuthorityBasicExample(t *testing.T) {
10+
t.Parallel()
11+
12+
context := map[string]interface{}{
13+
"pool_name": BootstrapSharedCaPoolInLocation(t, "us-central1"),
14+
"pool_location": "us-central1",
15+
"random_suffix": randString(t, 10),
16+
}
17+
18+
vcrTest(t, resource.TestCase{
19+
PreCheck: func() { testAccPreCheck(t) },
20+
Providers: testAccProviders,
21+
CheckDestroy: testAccCheckPrivatecaCertificateAuthorityDestroyProducer(t),
22+
Steps: []resource.TestStep{
23+
{
24+
Config: testAccDataSourcePrivatecaCertificateAuthority_privatecaCertificateAuthorityBasicExample(context),
25+
Check: resource.ComposeTestCheckFunc(
26+
resource.TestCheckResourceAttrSet("data.google_privateca_certificate_authority.default", "pem_csr"),
27+
),
28+
},
29+
},
30+
})
31+
}
32+
33+
func testAccDataSourcePrivatecaCertificateAuthority_privatecaCertificateAuthorityBasicExample(context map[string]interface{}) string {
34+
return Nprintf(`
35+
resource "google_privateca_certificate_authority" "default" {
36+
// This example assumes this pool already exists.
37+
// Pools cannot be deleted in normal test circumstances, so we depend on static pools
38+
pool = "%{pool_name}"
39+
certificate_authority_id = "tf-test-my-certificate-authority%{random_suffix}"
40+
location = "%{pool_location}"
41+
type = "SUBORDINATE"
42+
config {
43+
subject_config {
44+
subject {
45+
organization = "HashiCorp"
46+
common_name = "my-certificate-authority"
47+
}
48+
subject_alt_name {
49+
dns_names = ["hashicorp.com"]
50+
}
51+
}
52+
x509_config {
53+
ca_options {
54+
is_ca = true
55+
max_issuer_path_length = 10
56+
}
57+
key_usage {
58+
base_key_usage {
59+
digital_signature = true
60+
content_commitment = true
61+
key_encipherment = false
62+
data_encipherment = true
63+
key_agreement = true
64+
cert_sign = true
65+
crl_sign = true
66+
decipher_only = true
67+
}
68+
extended_key_usage {
69+
server_auth = true
70+
client_auth = false
71+
email_protection = true
72+
code_signing = true
73+
time_stamping = true
74+
}
75+
}
76+
}
77+
}
78+
lifetime = "86400s"
79+
key_spec {
80+
algorithm = "RSA_PKCS1_4096_SHA256"
81+
}
82+
}
83+
84+
data "google_privateca_certificate_authority" "default" {
85+
location = google_privateca_certificate_authority.default.location
86+
pool = google_privateca_certificate_authority.default.pool
87+
certificate_authority_id = google_privateca_certificate_authority.default.certificate_authority_id
88+
}
89+
90+
output "csr" {
91+
value = data.google_privateca_certificate_authority.default.pem_csr
92+
}
93+
`, context)
94+
}

google-beta/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,7 @@ func Provider() *schema.Provider {
864864
"google_monitoring_uptime_check_ips": dataSourceGoogleMonitoringUptimeCheckIps(),
865865
"google_netblock_ip_ranges": dataSourceGoogleNetblockIpRanges(),
866866
"google_organization": dataSourceGoogleOrganization(),
867+
"google_privateca_certificate_authority": dataSourcePrivatecaCertificateAuthority(),
867868
"google_project": dataSourceGoogleProject(),
868869
"google_projects": dataSourceGoogleProjects(),
869870
"google_project_organization_policy": dataSourceGoogleProjectOrganizationPolicy(),
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
subcategory: "Certificate Authority Service"
3+
layout: "google"
4+
page_title: "Google: google_privateca_certificate_authority"
5+
sidebar_current: "docs-google-datasource-privateca-certificate-authority"
6+
description: |-
7+
Contains the data that describes a Certificate Authority
8+
---
9+
# google_privateca_certificate_authority
10+
11+
Get info about a Google Cloud IAP Client.
12+
13+
## Example Usage
14+
15+
```tf
16+
data "google_privateca_certificate_authority" "default" {
17+
location = "us-west1"
18+
pool = "pool-name"
19+
certificate_authority_id = "ca-id"
20+
}
21+
22+
output "csr" {
23+
value = data.google_privateca_certificate_authority.default.pem_csr
24+
}
25+
26+
```
27+
28+
## Argument Reference
29+
30+
The following arguments are supported:
31+
32+
* `location` - (Required) The location the certificate authority exists in.
33+
34+
* `pool` - (Required) The name of the pool the certificate authority belongs to.
35+
36+
* `certificate_authority_id` - (Required) ID of the certificate authority.
37+
38+
- - -
39+
40+
* `project` - (Optional) The ID of the project in which the resource belongs. If it
41+
is not provided, the provider project is used.
42+
43+
## Attributes Reference
44+
45+
See [google_privateca_certificate_authority](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/privateca_certificate_authority) resource for details of the available attributes.
46+
47+
* `pem_csr` - The PEM-encoded signed certificate signing request (CSR). This is only set on subordinate certificate authorities.

website/google.erb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,16 @@
409409
<li>
410410
<a href="#">Certificate Authority Service</a>
411411
<ul class="nav">
412+
<li>
413+
<a href="#">Data Sources</a>
414+
<ul class="nav nav-auto-expand">
415+
416+
<li>
417+
<a href="/docs/providers/google/d/privateca_certificate_authority.html">google_privateca_certificate_authority</a>
418+
</li>
419+
420+
</ul>
421+
</li>
412422
<li>
413423
<a href="#">Resources</a>
414424
<ul class="nav nav-auto-expand">

0 commit comments

Comments
 (0)