From 8a8ffc3384a59340f47efe97f18611b6672da9bd Mon Sep 17 00:00:00 2001 From: Mauricio Alvarez Leon <65101411+BBBmau@users.noreply.github.com> Date: Thu, 21 Dec 2023 16:27:59 -0800 Subject: [PATCH] Add `google_compute_reservation` datasource (#9339) * WIP: initial data source go file * WIP: initial data source creation * WIP: error shows project is required even though its marked as optional * WIP: some attributes are not working as expected * use read function from resource for data source * fix reservationparsingid test * add data source to provider_mmv1_resources handwritten list * remove ParseComputeReservationId function * remove testAccCheckDataSourceComputeReservationDestroy * add google_compute_reservation data source docs * lint fix * Update mmv1/third_party/terraform/website/docs/d/compute_reservation.html.markdown Co-authored-by: Stephen Lewis (Burrows) * simplify data source test * add ReservationIdParsing Test * remove parsing Test --------- Co-authored-by: Stephen Lewis (Burrows) --- .../provider/provider_mmv1_resources.go.erb | 1 + .../data_source_google_compute_reservation.go | 47 +++++++++++++++ ..._source_google_compute_reservation_test.go | 59 +++++++++++++++++++ .../docs/d/compute_reservation.html.markdown | 30 ++++++++++ 4 files changed, 137 insertions(+) create mode 100644 mmv1/third_party/terraform/services/compute/data_source_google_compute_reservation.go create mode 100644 mmv1/third_party/terraform/services/compute/data_source_google_compute_reservation_test.go create mode 100644 mmv1/third_party/terraform/website/docs/d/compute_reservation.html.markdown diff --git a/mmv1/third_party/terraform/provider/provider_mmv1_resources.go.erb b/mmv1/third_party/terraform/provider/provider_mmv1_resources.go.erb index d0bfb7e99f0d..1b1f0f7f7d74 100644 --- a/mmv1/third_party/terraform/provider/provider_mmv1_resources.go.erb +++ b/mmv1/third_party/terraform/provider/provider_mmv1_resources.go.erb @@ -97,6 +97,7 @@ var handwrittenDatasources = map[string]*schema.Resource{ "google_compute_region_instance_template": compute.DataSourceGoogleComputeRegionInstanceTemplate(), "google_compute_region_network_endpoint_group": compute.DataSourceGoogleComputeRegionNetworkEndpointGroup(), "google_compute_region_ssl_certificate": compute.DataSourceGoogleRegionComputeSslCertificate(), + "google_compute_reservation": compute.DataSourceGoogleComputeReservation(), "google_compute_resource_policy": compute.DataSourceGoogleComputeResourcePolicy(), "google_compute_router": compute.DataSourceGoogleComputeRouter(), "google_compute_router_nat": compute.DataSourceGoogleComputeRouterNat(), diff --git a/mmv1/third_party/terraform/services/compute/data_source_google_compute_reservation.go b/mmv1/third_party/terraform/services/compute/data_source_google_compute_reservation.go new file mode 100644 index 000000000000..f5eca3204939 --- /dev/null +++ b/mmv1/third_party/terraform/services/compute/data_source_google_compute_reservation.go @@ -0,0 +1,47 @@ +package compute + +import ( + "fmt" + + "github.com/hashicorp/terraform-provider-google/google/tpgresource" + transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func DataSourceGoogleComputeReservation() *schema.Resource { + // Generate datasource schema from resource + dsSchema := tpgresource.DatasourceSchemaFromResourceSchema(ResourceComputeReservation().Schema) + + // Set 'Required' schema elements + tpgresource.AddRequiredFieldsToSchema(dsSchema, "name") + tpgresource.AddRequiredFieldsToSchema(dsSchema, "zone") + + tpgresource.AddOptionalFieldsToSchema(dsSchema, "project") + + return &schema.Resource{ + Read: dataSourceGoogleComputeReservationRead, + Schema: dsSchema, + } +} + +func dataSourceGoogleComputeReservationRead(d *schema.ResourceData, meta interface{}) error { + config := meta.(*transport_tpg.Config) + err := resourceComputeReservationRead(d, meta) + if err != nil { + return err + } + + project, err := tpgresource.GetProject(d, config) + if err != nil { + return err + } + zone, err := tpgresource.GetZone(d, config) + if err != nil { + return err + } + name := d.Get("name").(string) + + d.SetId(fmt.Sprintf("projects/%s/zones/%s/reservations/%s", project, zone, name)) + return nil +} diff --git a/mmv1/third_party/terraform/services/compute/data_source_google_compute_reservation_test.go b/mmv1/third_party/terraform/services/compute/data_source_google_compute_reservation_test.go new file mode 100644 index 000000000000..8cf06e5974af --- /dev/null +++ b/mmv1/third_party/terraform/services/compute/data_source_google_compute_reservation_test.go @@ -0,0 +1,59 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 +package compute_test + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-provider-google/google/acctest" +) + +func TestAccDataSourceComputeReservation(t *testing.T) { + t.Parallel() + + reservationName := fmt.Sprintf("tf-test-%s", acctest.RandString(t, 10)) + + rsName := "foobar" + dsName := "my_reservation" + rsFullName := fmt.Sprintf("google_compute_reservation.%s", rsName) + dsFullName := fmt.Sprintf("data.google_compute_reservation.%s", dsName) + + acctest.VcrTest(t, resource.TestCase{ + PreCheck: func() { acctest.AccTestPreCheck(t) }, + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), + CheckDestroy: testAccCheckComputeReservationDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testAccDataSourceComputeReservationConfig(reservationName, rsName, dsName), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(dsFullName, "status", "READY"), + acctest.CheckDataSourceStateMatchesResourceState(dsFullName, rsFullName), + ), + }, + }, + }) +} + +func testAccDataSourceComputeReservationConfig(reservationName, rsName, dsName string) string { + return fmt.Sprintf(` +resource "google_compute_reservation" "%s" { + name = "%s" + zone = "us-west1-a" + + specific_reservation { + count = 1 + instance_properties { + min_cpu_platform = "Intel Cascade Lake" + machine_type = "n2-standard-2" + } + } +} + +data "google_compute_reservation" "%s" { + name = google_compute_reservation.%s.name + zone = "us-west1-a" +} +`, rsName, reservationName, dsName, rsName) +} diff --git a/mmv1/third_party/terraform/website/docs/d/compute_reservation.html.markdown b/mmv1/third_party/terraform/website/docs/d/compute_reservation.html.markdown new file mode 100644 index 000000000000..2e16f0c0dc2e --- /dev/null +++ b/mmv1/third_party/terraform/website/docs/d/compute_reservation.html.markdown @@ -0,0 +1,30 @@ +--- +subcategory: "Compute Engine" +description: |- + Provide access to a Reservation's attributes +--- + +# google\_compute\_reservation + +Provides access to available Google Compute Reservation Resources for a given project. +See more about [Reservations of Compute Engine resources](https://cloud.google.com/compute/docs/instances/reservations-overview) in the upstream docs. + +```hcl +data "google_compute_reservation" "reservation" { + name = "gce-reservation" + zone = "us-central1-a" +} + +``` + +## Argument Reference + +The following arguments are supported: + +* `name` (Required) - The name of the Compute Reservation. +* `zone` (Required) - Zone where the Compute Reservation resides. +* `project` (Optional) - Project from which to list the Compute Reservation. Defaults to project declared in the provider. + +## Attributes Reference + +See [google_compute_reservation](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_reservation) resource for details of the available attributes.