Skip to content

Commit 07034c2

Browse files
Add google_compute_reservation datasource (#9339) (#6791)
* 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 * simplify data source test * add ReservationIdParsing Test * remove parsing Test --------- [upstream:8a8ffc3384a59340f47efe97f18611b6672da9bd] Signed-off-by: Modular Magician <[email protected]>
1 parent d80105a commit 07034c2

File tree

5 files changed

+144
-0
lines changed

5 files changed

+144
-0
lines changed

.changelog/9339.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-datasource
2+
`google_compute_reservation`
3+
```

google-beta/provider/provider_mmv1_resources.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ var handwrittenDatasources = map[string]*schema.Resource{
195195
"google_compute_region_instance_template": compute.DataSourceGoogleComputeRegionInstanceTemplate(),
196196
"google_compute_region_network_endpoint_group": compute.DataSourceGoogleComputeRegionNetworkEndpointGroup(),
197197
"google_compute_region_ssl_certificate": compute.DataSourceGoogleRegionComputeSslCertificate(),
198+
"google_compute_reservation": compute.DataSourceGoogleComputeReservation(),
198199
"google_compute_resource_policy": compute.DataSourceGoogleComputeResourcePolicy(),
199200
"google_compute_router": compute.DataSourceGoogleComputeRouter(),
200201
"google_compute_router_nat": compute.DataSourceGoogleComputeRouterNat(),
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
package compute
4+
5+
import (
6+
"fmt"
7+
8+
"github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource"
9+
transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport"
10+
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12+
)
13+
14+
func DataSourceGoogleComputeReservation() *schema.Resource {
15+
// Generate datasource schema from resource
16+
dsSchema := tpgresource.DatasourceSchemaFromResourceSchema(ResourceComputeReservation().Schema)
17+
18+
// Set 'Required' schema elements
19+
tpgresource.AddRequiredFieldsToSchema(dsSchema, "name")
20+
tpgresource.AddRequiredFieldsToSchema(dsSchema, "zone")
21+
22+
tpgresource.AddOptionalFieldsToSchema(dsSchema, "project")
23+
24+
return &schema.Resource{
25+
Read: dataSourceGoogleComputeReservationRead,
26+
Schema: dsSchema,
27+
}
28+
}
29+
30+
func dataSourceGoogleComputeReservationRead(d *schema.ResourceData, meta interface{}) error {
31+
config := meta.(*transport_tpg.Config)
32+
err := resourceComputeReservationRead(d, meta)
33+
if err != nil {
34+
return err
35+
}
36+
37+
project, err := tpgresource.GetProject(d, config)
38+
if err != nil {
39+
return err
40+
}
41+
zone, err := tpgresource.GetZone(d, config)
42+
if err != nil {
43+
return err
44+
}
45+
name := d.Get("name").(string)
46+
47+
d.SetId(fmt.Sprintf("projects/%s/zones/%s/reservations/%s", project, zone, name))
48+
return nil
49+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
// Copyright (c) HashiCorp, Inc.
4+
// SPDX-License-Identifier: MPL-2.0
5+
package compute_test
6+
7+
import (
8+
"fmt"
9+
"testing"
10+
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
12+
"github.com/hashicorp/terraform-provider-google-beta/google-beta/acctest"
13+
)
14+
15+
func TestAccDataSourceComputeReservation(t *testing.T) {
16+
t.Parallel()
17+
18+
reservationName := fmt.Sprintf("tf-test-%s", acctest.RandString(t, 10))
19+
20+
rsName := "foobar"
21+
dsName := "my_reservation"
22+
rsFullName := fmt.Sprintf("google_compute_reservation.%s", rsName)
23+
dsFullName := fmt.Sprintf("data.google_compute_reservation.%s", dsName)
24+
25+
acctest.VcrTest(t, resource.TestCase{
26+
PreCheck: func() { acctest.AccTestPreCheck(t) },
27+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
28+
CheckDestroy: testAccCheckComputeReservationDestroyProducer(t),
29+
Steps: []resource.TestStep{
30+
{
31+
Config: testAccDataSourceComputeReservationConfig(reservationName, rsName, dsName),
32+
Check: resource.ComposeTestCheckFunc(
33+
resource.TestCheckResourceAttr(dsFullName, "status", "READY"),
34+
acctest.CheckDataSourceStateMatchesResourceState(dsFullName, rsFullName),
35+
),
36+
},
37+
},
38+
})
39+
}
40+
41+
func testAccDataSourceComputeReservationConfig(reservationName, rsName, dsName string) string {
42+
return fmt.Sprintf(`
43+
resource "google_compute_reservation" "%s" {
44+
name = "%s"
45+
zone = "us-west1-a"
46+
47+
specific_reservation {
48+
count = 1
49+
instance_properties {
50+
min_cpu_platform = "Intel Cascade Lake"
51+
machine_type = "n2-standard-2"
52+
}
53+
}
54+
}
55+
56+
data "google_compute_reservation" "%s" {
57+
name = google_compute_reservation.%s.name
58+
zone = "us-west1-a"
59+
}
60+
`, rsName, reservationName, dsName, rsName)
61+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
subcategory: "Compute Engine"
3+
description: |-
4+
Provide access to a Reservation's attributes
5+
---
6+
7+
# google\_compute\_reservation
8+
9+
Provides access to available Google Compute Reservation Resources for a given project.
10+
See more about [Reservations of Compute Engine resources](https://cloud.google.com/compute/docs/instances/reservations-overview) in the upstream docs.
11+
12+
```hcl
13+
data "google_compute_reservation" "reservation" {
14+
name = "gce-reservation"
15+
zone = "us-central1-a"
16+
}
17+
18+
```
19+
20+
## Argument Reference
21+
22+
The following arguments are supported:
23+
24+
* `name` (Required) - The name of the Compute Reservation.
25+
* `zone` (Required) - Zone where the Compute Reservation resides.
26+
* `project` (Optional) - Project from which to list the Compute Reservation. Defaults to project declared in the provider.
27+
28+
## Attributes Reference
29+
30+
See [google_compute_reservation](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_reservation) resource for details of the available attributes.

0 commit comments

Comments
 (0)