Skip to content

Commit

Permalink
Adding Data Source Forwarding Rules (GoogleCloudPlatform#10004)
Browse files Browse the repository at this point in the history
  • Loading branch information
jesse-l authored and Charles Leon committed Mar 11, 2024
1 parent 98bb4ea commit ce13ea1
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ var handwrittenDatasources = map[string]*schema.Resource{
"google_compute_default_service_account": compute.DataSourceGoogleComputeDefaultServiceAccount(),
"google_compute_disk": compute.DataSourceGoogleComputeDisk(),
"google_compute_forwarding_rule": compute.DataSourceGoogleComputeForwardingRule(),
"google_compute_forwarding_rules": compute.DataSourceGoogleComputeForwardingRules(),
"google_compute_global_address": compute.DataSourceGoogleComputeGlobalAddress(),
"google_compute_global_forwarding_rule": compute.DataSourceGoogleComputeGlobalForwardingRule(),
"google_compute_ha_vpn_gateway": compute.DataSourceGoogleComputeHaVpnGateway(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package compute

import (
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
)

func DataSourceGoogleComputeForwardingRules() *schema.Resource {
return &schema.Resource{
Read: dataSourceGoogleComputeForwardingRulesRead,

Schema: map[string]*schema.Schema{

"project": {
Type: schema.TypeString,
Optional: true,
},

"region": {
Type: schema.TypeString,
Optional: true,
},

"rules": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: tpgresource.DatasourceSchemaFromResourceSchema(ResourceComputeForwardingRule().Schema),
},
},
},
}
}

func dataSourceGoogleComputeForwardingRulesRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*transport_tpg.Config)
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
if err != nil {
return err
}

project, err := tpgresource.GetProject(d, config)
if err != nil {
return err
}

region, err := tpgresource.GetRegion(d, config)
if err != nil {
return err
}

id := fmt.Sprintf("projects/%s/regions/%s/forwardingRules", project, region)
d.SetId(id)

forwardingRulesAggregatedList, err := config.NewComputeClient(userAgent).ForwardingRules.List(project, region).Do()
if err != nil {
return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("Forwarding Rules Not Found : %s", project))
}

forwardingRules := make([]map[string]interface{}, 0, len(forwardingRulesAggregatedList.Items))

for i := 0; i < len(forwardingRulesAggregatedList.Items); i++ {
rule := forwardingRulesAggregatedList.Items[i]
mappedData := map[string]interface{}{
"name": rule.Name,
"network": rule.Network,
"subnetwork": rule.Subnetwork,
"backend_service": rule.BackendService,
"ip_address": rule.IPAddress,
"service_name": rule.ServiceName,
"service_label": rule.ServiceLabel,
"description": rule.Description,
"self_link": rule.SelfLink,
"labels": rule.Labels,
"ports": rule.Ports,
"region": rule.Region,
"target": rule.Target,
"ip_version": rule.IpVersion,
"network_tier": rule.NetworkTier,
"base_forwarding_rule": rule.BaseForwardingRule,
"port_range": rule.PortRange,
}
forwardingRules = append(forwardingRules, mappedData)
}

if err := d.Set("rules", forwardingRules); err != nil {
return fmt.Errorf("Error setting the forwarding rules names: %s", err)
}

if err := d.Set("project", project); err != nil {
return fmt.Errorf("Error setting the network names: %s", err)
}

if err := d.Set("region", region); err != nil {
return fmt.Errorf("Error setting the region: %s", err)
}

return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package compute_test

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-provider-google/google/acctest"
)

func TestAccDataSourceGoogleForwardingRules(t *testing.T) {
t.Parallel()

poolName := fmt.Sprintf("tf-%s", acctest.RandString(t, 10))
ruleName := fmt.Sprintf("tf-%s", acctest.RandString(t, 10))

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
Steps: []resource.TestStep{
{
Config: testAccDataSourceGoogleForwardingRuleConfig(poolName, ruleName),
Check: acctest.CheckDataSourceStateMatchesResourceState("data.google_compute_forwarding_rule.my_forwarding_rule", "google_compute_forwarding_rule.foobar-fr"),
},
},
})
}

func testAccDataSourceGoogleForwardingRulesConfig(poolName, ruleName string) string {
return fmt.Sprintf(`
resource "google_compute_target_pool" "foobar-tp" {
description = "Resource created for Terraform acceptance testing"
instances = ["us-central1-a/foo", "us-central1-b/bar"]
name = "%s"
}
resource "google_compute_forwarding_rule" "foobar-fr" {
description = "Resource created for Terraform acceptance testing"
ip_protocol = "UDP"
name = "%s"
port_range = "80-81"
target = google_compute_target_pool.foobar-tp.self_link
labels = {
my-label = "my-label-value"
}
}
data "google_compute_forwarding_rules" "my_forwarding_rule" {
project = google_compute_forwarding_rule.foobar-fr.project
region = google_compute_forwarding_rule.foobar-fr.region
}
`, poolName, ruleName)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
subcategory: "Compute Engine"
description: |-
List forwarding rules in a region of a Google Cloud project.
---

# google\_compute\_forwarding\_rules

List all networks in a specified Google Cloud project.

## Example Usage

```tf
data "google_compute_forwarding_rules" "my-forwarding-rules" {
project = "my-cloud-project"
region = "us-central1"
}
```

## Argument Reference

The following arguments are supported:

* `project` - (Optional) The name of the project.

* `region` - (Optional) The region you want to get the forwarding rules from.

These arguments must be set in either the provider or the resouce in order for the information to be queried.

## Attributes Reference

In addition to the arguments listed above, the following attributes are exported:

* `id` - an identifier for the resource with format projects/{{project}}/region/{{region}}/forwardingRules

* `project` - The project name being queried.

* `region` - The region being queried.

* `rules` - This is a list of the forwarding rules in the project. Each forwarding rule will list the backend, description, ip address. name, network, self link, service label, service name, and subnet.

* `self_link` - The URI of the resource.

0 comments on commit ce13ea1

Please sign in to comment.