Skip to content

Commit

Permalink
Add google_compute_reservation datasource (#9339)
Browse files Browse the repository at this point in the history
* 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) <[email protected]>

* simplify data source test

* add ReservationIdParsing Test

* remove parsing Test

---------

Co-authored-by: Stephen Lewis (Burrows) <[email protected]>
  • Loading branch information
BBBmau and melinath authored Dec 22, 2023
1 parent a7305c8 commit 8a8ffc3
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit 8a8ffc3

Please sign in to comment.