-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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) <[email protected]> * simplify data source test * add ReservationIdParsing Test * remove parsing Test --------- Co-authored-by: Stephen Lewis (Burrows) <[email protected]>
- Loading branch information
Showing
4 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
mmv1/third_party/terraform/services/compute/data_source_google_compute_reservation.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
59 changes: 59 additions & 0 deletions
59
mmv1/third_party/terraform/services/compute/data_source_google_compute_reservation_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
30 changes: 30 additions & 0 deletions
30
mmv1/third_party/terraform/website/docs/d/compute_reservation.html.markdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |