Skip to content

Commit

Permalink
Global forwarding rule and forwarding rule DCL conversion. (#5138) (#…
Browse files Browse the repository at this point in the history
…10030)

* Add forwarding rule to tpgtools.

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Sep 8, 2021
1 parent 78a700f commit f860da3
Show file tree
Hide file tree
Showing 12 changed files with 727 additions and 1,363 deletions.
2 changes: 2 additions & 0 deletions .changelog/5138.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
```release-note:none
```
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module github.com/hashicorp/terraform-provider-google
require (
cloud.google.com/go/bigtable v1.10.1
github.com/GoogleCloudPlatform/declarative-resource-client-library v0.0.0-20210824221031-bbef03d2748a
github.com/GoogleCloudPlatform/declarative-resource-client-library v0.0.0-20210901222754-f6c1204eeae9
github.com/apparentlymart/go-cidr v1.1.0
github.com/client9/misspell v0.3.4
github.com/davecgh/go-spew v1.1.1
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802 h1:1BDTz0u9nC3//pOC
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/Djarvur/go-err113 v0.0.0-20210108212216-aea10b59be24 h1:sHglBQTwgx+rWPdisA5ynNEsoARbiCBOyGcJM4/OzsM=
github.com/Djarvur/go-err113 v0.0.0-20210108212216-aea10b59be24/go.mod h1:4UJr5HIiMZrwgkSPdsjy2uOQExX/WEILpIrO9UPGuXs=
github.com/GoogleCloudPlatform/declarative-resource-client-library v0.0.0-20210901222754-f6c1204eeae9 h1:b4+4SWkgDcBSTpKYvl+SaUDZ/PwEMfnuI1z41qPy+z8=
github.com/GoogleCloudPlatform/declarative-resource-client-library v0.0.0-20210901222754-f6c1204eeae9/go.mod h1:oEeBHikdF/NrnUy0ornVaY1OT+jGvTqm+LQS0+ZDKzU=
github.com/GoogleCloudPlatform/declarative-resource-client-library v0.0.0-20210830202814-90d607e5aba1 h1:Cq6wsP8cz8yoyIqVe9qXiuLv6myB3jY5aCobrvTN5WQ=
github.com/GoogleCloudPlatform/declarative-resource-client-library v0.0.0-20210830202814-90d607e5aba1/go.mod h1:oEeBHikdF/NrnUy0ornVaY1OT+jGvTqm+LQS0+ZDKzU=
github.com/GoogleCloudPlatform/declarative-resource-client-library v0.0.0-20210828005957-5e300a425b22 h1:r1uW7IkHP90/jxMW8Br+DzEK2wuZQs0JFYC6T5foazk=
github.com/GoogleCloudPlatform/declarative-resource-client-library v0.0.0-20210828005957-5e300a425b22/go.mod h1:oEeBHikdF/NrnUy0ornVaY1OT+jGvTqm+LQS0+ZDKzU=
github.com/GoogleCloudPlatform/declarative-resource-client-library v0.0.0-20210824221031-bbef03d2748a h1:k71tcjN3d1+YrSB1cN2d9T787JjbwbfNFM6HarBrn2o=
github.com/GoogleCloudPlatform/declarative-resource-client-library v0.0.0-20210824221031-bbef03d2748a/go.mod h1:oEeBHikdF/NrnUy0ornVaY1OT+jGvTqm+LQS0+ZDKzU=
github.com/GoogleCloudPlatform/declarative-resource-client-library v0.0.0-20210623224421-7f5af5003bbb h1:9HBI5n77Z2ReBlOFk1h2JFLpY/HobH9Xaq90E7IjCpw=
Expand Down
2 changes: 1 addition & 1 deletion google/data_source_google_compute_forwarding_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestAccDataSourceGoogleForwardingRule(t *testing.T) {
Steps: []resource.TestStep{
{
Config: testAccDataSourceGoogleForwardingRuleConfig(poolName, ruleName),
Check: checkDataSourceStateMatchesResourceState("data.google_compute_forwarding_rule.my_forwarding_rule", "google_compute_forwarding_rule.foobar-fr"),
Check: checkDataSourceStateMatchesResourceStateWithIgnores("data.google_compute_forwarding_rule.my_forwarding_rule", "google_compute_forwarding_rule.foobar-fr", map[string]struct{}{"port_range": {}, "target": {}}),
},
},
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestAccDataSourceGoogleGlobalForwardingRule(t *testing.T) {
Steps: []resource.TestStep{
{
Config: testAccDataSourceGoogleGlobalForwardingRuleConfig(poolName, ruleName),
Check: checkDataSourceStateMatchesResourceState("data.google_compute_global_forwarding_rule.my_forwarding_rule", "google_compute_global_forwarding_rule.foobar-fr"),
Check: checkDataSourceStateMatchesResourceStateWithIgnores("data.google_compute_global_forwarding_rule.my_forwarding_rule", "google_compute_global_forwarding_rule.foobar-fr", map[string]struct{}{"port_range": {}, "target": {}}),
},
},
})
Expand Down
1,002 changes: 308 additions & 694 deletions google/resource_compute_forwarding_rule.go

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions google/resource_compute_forwarding_rule_generated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func TestAccComputeForwardingRule_forwardingRuleBasicExample(t *testing.T) {
ResourceName: "google_compute_forwarding_rule.default",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"backend_service", "network", "subnetwork", "region"},
ImportStateVerifyIgnore: []string{"backend_service", "network", "subnetwork", "region", "port_range", "target"},
},
},
})
Expand Down Expand Up @@ -145,7 +145,7 @@ func TestAccComputeForwardingRule_forwardingRuleInternallbExample(t *testing.T)
ResourceName: "google_compute_forwarding_rule.default",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"backend_service", "network", "subnetwork", "region"},
ImportStateVerifyIgnore: []string{"backend_service", "network", "subnetwork", "region", "port_range", "target"},
},
},
})
Expand Down
104 changes: 26 additions & 78 deletions google/resource_compute_forwarding_rule_sweeper_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
// ----------------------------------------------------------------------------
//
// *** AUTO GENERATED CODE *** Type: MMv1 ***
// *** AUTO GENERATED CODE *** Type: DCL ***
//
// ----------------------------------------------------------------------------
//
// This file is automatically generated by Magic Modules and manual
// changes will be clobbered when the file is regenerated.
// This file is managed by Magic Modules (https://github.com/GoogleCloudPlatform/magic-modules)
// and is based on the DCL (https://github.com/GoogleCloudPlatform/declarative-resource-client-library).
// Changes will need to be made to the DCL or Magic Modules instead of here.
//
// Please read more about how to change this file in
// .github/CONTRIBUTING.md.
// We are not currently able to accept contributions to this file. If changes
// are required, please file an issue at https://github.com/hashicorp/terraform-provider-google/issues/new/choose
//
// ----------------------------------------------------------------------------

Expand All @@ -17,22 +18,21 @@ package google
import (
"context"
"log"
"strings"
"testing"

compute "github.com/GoogleCloudPlatform/declarative-resource-client-library/services/google/compute"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func init() {
resource.AddTestSweepers("ComputeForwardingRule", &resource.Sweeper{
Name: "ComputeForwardingRule",
F: testSweepComputeForwardingRule,
resource.AddTestSweepers("ComputeForwarding_rule", &resource.Sweeper{
Name: "ComputeForwarding_rule",
F: testSweepComputeForwarding_rule,
})
}

// At the time of writing, the CI only passes us-central1 as the region
func testSweepComputeForwardingRule(region string) error {
resourceName := "ComputeForwardingRule"
func testSweepComputeForwarding_rule(region string) error {
resourceName := "ComputeForwarding_rule"
log.Printf("[INFO][SWEEPER_LOG] Starting sweeper for %s", resourceName)

config, err := sharedConfigForRegion(region)
Expand All @@ -50,75 +50,23 @@ func testSweepComputeForwardingRule(region string) error {
t := &testing.T{}
billingId := getTestBillingAccountFromEnv(t)

// Setup variables to replace in list template
d := &ResourceDataMock{
FieldsInSchema: map[string]interface{}{
"project": config.Project,
"region": region,
"location": region,
"zone": "-",
"billing_account": billingId,
},
// Setup variables to be used for Delete arguments.
d := map[string]string{
"project": config.Project,
"region": region,
"location": region,
"zone": "-",
"billing_account": billingId,
}

listTemplate := strings.Split("https://compute.googleapis.com/compute/v1/projects/{{project}}/regions/{{region}}/forwardingRules", "?")[0]
listUrl, err := replaceVars(d, config, listTemplate)
client := NewDCLComputeClient(config, config.userAgent, "")
err = client.DeleteAllForwardingRule(context.Background(), d["project"], d["location"], isDeletableComputeForwarding_rule)
if err != nil {
log.Printf("[INFO][SWEEPER_LOG] error preparing sweeper list url: %s", err)
return nil
}

res, err := sendRequest(config, "GET", config.Project, listUrl, config.userAgent, nil)
if err != nil {
log.Printf("[INFO][SWEEPER_LOG] Error in response from request %s: %s", listUrl, err)
return nil
}

resourceList, ok := res["items"]
if !ok {
log.Printf("[INFO][SWEEPER_LOG] Nothing found in response.")
return nil
}

rl := resourceList.([]interface{})

log.Printf("[INFO][SWEEPER_LOG] Found %d items in %s list response.", len(rl), resourceName)
// Keep count of items that aren't sweepable for logging.
nonPrefixCount := 0
for _, ri := range rl {
obj := ri.(map[string]interface{})
if obj["name"] == nil {
log.Printf("[INFO][SWEEPER_LOG] %s resource name was nil", resourceName)
return nil
}

name := GetResourceNameFromSelfLink(obj["name"].(string))
// Skip resources that shouldn't be sweeped
if !isSweepableTestResource(name) {
nonPrefixCount++
continue
}

deleteTemplate := "https://compute.googleapis.com/compute/v1/projects/{{project}}/regions/{{region}}/forwardingRules/{{name}}"
deleteUrl, err := replaceVars(d, config, deleteTemplate)
if err != nil {
log.Printf("[INFO][SWEEPER_LOG] error preparing delete url: %s", err)
return nil
}
deleteUrl = deleteUrl + name

// Don't wait on operations as we may have a lot to delete
_, err = sendRequest(config, "DELETE", config.Project, deleteUrl, config.userAgent, nil)
if err != nil {
log.Printf("[INFO][SWEEPER_LOG] Error deleting for url %s : %s", deleteUrl, err)
} else {
log.Printf("[INFO][SWEEPER_LOG] Sent delete request for %s resource: %s", resourceName, name)
}
}

if nonPrefixCount > 0 {
log.Printf("[INFO][SWEEPER_LOG] %d items were non-sweepable and skipped.", nonPrefixCount)
return err
}

return nil
}

func isDeletableComputeForwarding_rule(r *compute.ForwardingRule) bool {
return isSweepableTestResource(*r.Name)
}
Loading

0 comments on commit f860da3

Please sign in to comment.