From 7e26bd65df0f9d29756e0b4aa01fbbcb83f49733 Mon Sep 17 00:00:00 2001 From: The Magician Date: Fri, 11 Sep 2020 14:27:48 -0700 Subject: [PATCH] Add Regional SSL Certificate datasource (#3969) (#7252) Signed-off-by: Modular Magician --- .changelog/3969.txt | 3 ++ ...e_google_compute_region_ssl_certificate.go | 37 ++++++++++++++ ...gle_compute_region_ssl_certificate_test.go | 47 +++++++++++++++++ google/provider.go | 1 + ...mpute_region_ssl_certificate.html.markdown | 50 +++++++++++++++++++ website/google.erb | 4 ++ 6 files changed, 142 insertions(+) create mode 100644 .changelog/3969.txt create mode 100644 google/data_source_google_compute_region_ssl_certificate.go create mode 100644 google/data_source_google_compute_region_ssl_certificate_test.go create mode 100644 website/docs/d/compute_region_ssl_certificate.html.markdown diff --git a/.changelog/3969.txt b/.changelog/3969.txt new file mode 100644 index 00000000000..c02f87f337d --- /dev/null +++ b/.changelog/3969.txt @@ -0,0 +1,3 @@ +```release-note:new-datasource +compute: added `google_compute_region_ssl_certificate` datasource +``` diff --git a/google/data_source_google_compute_region_ssl_certificate.go b/google/data_source_google_compute_region_ssl_certificate.go new file mode 100644 index 00000000000..ccb9ac9ce99 --- /dev/null +++ b/google/data_source_google_compute_region_ssl_certificate.go @@ -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) +} diff --git a/google/data_source_google_compute_region_ssl_certificate_test.go b/google/data_source_google_compute_region_ssl_certificate_test.go new file mode 100644 index 00000000000..b2a022dd4a1 --- /dev/null +++ b/google/data_source_google_compute_region_ssl_certificate_test.go @@ -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) +} diff --git a/google/provider.go b/google/provider.go index 8f78a122aba..44d2fa3f8e3 100644 --- a/google/provider.go +++ b/google/provider.go @@ -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(), diff --git a/website/docs/d/compute_region_ssl_certificate.html.markdown b/website/docs/d/compute_region_ssl_certificate.html.markdown new file mode 100644 index 00000000000..7fe3725d3a6 --- /dev/null +++ b/website/docs/d/compute_region_ssl_certificate.html.markdown @@ -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. diff --git a/website/google.erb b/website/google.erb index a9affedbcf9..2318f4778db 100644 --- a/website/google.erb +++ b/website/google.erb @@ -1214,6 +1214,10 @@ google_compute_region_instance_group +
  • + google_compute_region_ssl_certificate +
  • +
  • google_compute_regions