forked from GoogleCloudPlatform/magic-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding Data Source Forwarding Rules (GoogleCloudPlatform#10004)
- Loading branch information
Showing
4 changed files
with
198 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
102 changes: 102 additions & 0 deletions
102
mmv1/third_party/terraform/services/compute/data_source_google_compute_forwarding_rules.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,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 | ||
} |
53 changes: 53 additions & 0 deletions
53
...hird_party/terraform/services/compute/data_source_google_compute_forwarding_rules_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,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) | ||
} |
42 changes: 42 additions & 0 deletions
42
mmv1/third_party/terraform/website/docs/d/compute_forwarding_rules.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,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. |