Skip to content

Commit

Permalink
Add Regional SSL Certificate datasource (#3969) (#7252)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician authored Sep 11, 2020
1 parent 60c951c commit 7e26bd6
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/3969.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-datasource
compute: added `google_compute_region_ssl_certificate` datasource
```
37 changes: 37 additions & 0 deletions google/data_source_google_compute_region_ssl_certificate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package google

import (
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

func dataSourceGoogleRegionComputeSslCertificate() *schema.Resource {
// Generate datasource schema from resource
dsSchema := datasourceSchemaFromResourceSchema(resourceComputeRegionSslCertificate().Schema)

// Set 'Required' schema elements
addRequiredFieldsToSchema(dsSchema, "name")

// Set 'Optional' schema elements
addOptionalFieldsToSchema(dsSchema, "project")
addOptionalFieldsToSchema(dsSchema, "region")

return &schema.Resource{
Read: dataSourceComputeRegionSslCertificateRead,
Schema: dsSchema,
}
}

func dataSourceComputeRegionSslCertificateRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

project, region, name, err := GetRegionalResourcePropertiesFromSelfLinkOrSchema(d, config)
if err != nil {
return err
}

d.SetId(fmt.Sprintf("projects/%s/regions/%s/sslCertificates/%s", project, region, name))

return resourceComputeRegionSslCertificateRead(d, meta)
}
47 changes: 47 additions & 0 deletions google/data_source_google_compute_region_ssl_certificate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package google

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func TestAccDataSourceComputeRegionSslCertificate(t *testing.T) {
t.Parallel()

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceComputeRegionSslCertificateConfig(randString(t, 10)),
Check: resource.ComposeTestCheckFunc(
checkDataSourceStateMatchesResourceStateWithIgnores(
"data.google_compute_region_ssl_certificate.cert",
"google_compute_region_ssl_certificate.foobar",
map[string]struct{}{
"private_key": {},
},
),
),
},
},
})
}

func testAccDataSourceComputeRegionSslCertificateConfig(certName string) string {
return fmt.Sprintf(`
resource "google_compute_region_ssl_certificate" "foobar" {
name = "cert-test-%s"
region = "us-central1"
description = "really descriptive"
private_key = file("test-fixtures/ssl_cert/test.key")
certificate = file("test-fixtures/ssl_cert/test.crt")
}
data "google_compute_region_ssl_certificate" "cert" {
name = google_compute_region_ssl_certificate.foobar.name
}
`, certName)
}
1 change: 1 addition & 0 deletions google/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,7 @@ func Provider() terraform.ResourceProvider {
"google_compute_node_types": dataSourceGoogleComputeNodeTypes(),
"google_compute_regions": dataSourceGoogleComputeRegions(),
"google_compute_region_instance_group": dataSourceGoogleComputeRegionInstanceGroup(),
"google_compute_region_ssl_certificate": dataSourceGoogleRegionComputeSslCertificate(),
"google_compute_router": dataSourceGoogleComputeRouter(),
"google_compute_ssl_certificate": dataSourceGoogleComputeSslCertificate(),
"google_compute_ssl_policy": dataSourceGoogleComputeSslPolicy(),
Expand Down
50 changes: 50 additions & 0 deletions website/docs/d/compute_region_ssl_certificate.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---
subcategory: "Compute Engine"
layout: "google"
page_title: "Google: google_compute_region_ssl_certificate"
sidebar_current: "docs-google-datasource-compute-region-ssl-certificate"
description: |-
Get info about a Regional Google Compute SSL Certificate.
---

# google\_compute\_region\_ssl\_certificate

Get info about a Region Google Compute SSL Certificate from its name.

## Example Usage

```tf
data "google_compute_region_ssl_certificate" "my_cert" {
name = "my-cert"
}
output "certificate" {
value = data.google_compute_region_ssl_certificate.my_cert.certificate
}
output "certificate_id" {
value = data.google_compute_region_ssl_certificate.my_cert.certificate_id
}
output "self_link" {
value = data.google_compute_region_ssl_certificate.my_cert.self_link
}
```

## Argument Reference

The following arguments are supported:

* `name` (Required) - The name of the certificate.

- - -

* `project` - (Optional) The project in which the resource belongs. If it
is not provided, the provider project is used.

* `region` - (Optional) The region in which the resource belongs. If it
is not provided, the provider region is used.

## Attributes Reference

See [google_compute_region_ssl_certificate](https://www.terraform.io/docs/providers/google/r/compute_region_ssl_certificate.html) resource for details of the available attributes.
4 changes: 4 additions & 0 deletions website/google.erb
Original file line number Diff line number Diff line change
Expand Up @@ -1214,6 +1214,10 @@
<a href="/docs/providers/google/d/compute_region_instance_group.html">google_compute_region_instance_group</a>
</li>

<li>
<a href="/docs/providers/google/d/compute_region_ssl_certificate.html">google_compute_region_ssl_certificate</a>
</li>

<li>
<a href="/docs/providers/google/d/compute_regions.html">google_compute_regions</a>
</li>
Expand Down

0 comments on commit 7e26bd6

Please sign in to comment.