Skip to content

Commit caf761f

Browse files
authored
Merge pull request #1453 from snyk/feat/google_compute_global_forwarding_rule
feat: google compute global forwarding rule
2 parents 28dffc6 + d4515f8 commit caf761f

File tree

15 files changed

+393
-24
lines changed

15 files changed

+393
-24
lines changed

pkg/iac/terraform/state/terraform_state_reader_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@ func TestTerraformStateReader_Google_Resources(t *testing.T) {
379379
{name: "compute node group", dirName: "google_compute_node_group", wantErr: false},
380380
{name: "compute forwarding rule", dirName: "google_compute_forwarding_rule", wantErr: false},
381381
{name: "compute instance group manager", dirName: "google_compute_instance_group_manager", wantErr: false},
382+
{name: "compute global forwarding rule", dirName: "google_compute_global_forwarding_rule", wantErr: false},
382383
}
383384
for _, tt := range tests {
384385
t.Run(tt.name, func(t *testing.T) {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[
2+
{
3+
"Id": "projects/driftctl-qa-1/global/forwardingRules/global-rule",
4+
"Type": "google_compute_global_forwarding_rule",
5+
"Attrs": {
6+
"description": "",
7+
"id": "projects/driftctl-qa-1/global/forwardingRules/global-rule",
8+
"ip_address": "34.120.169.13",
9+
"ip_protocol": "TCP",
10+
"ip_version": "",
11+
"load_balancing_scheme": "EXTERNAL",
12+
"name": "global-rule",
13+
"port_range": "80",
14+
"project": "driftctl-qa-1",
15+
"self_link": "https://www.googleapis.com/compute/v1/projects/driftctl-qa-1/global/forwardingRules/global-rule",
16+
"target": "projects/driftctl-qa-1/global/targetHttpProxies/target-proxy"
17+
}
18+
}
19+
]
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"version": 4,
3+
"terraform_version": "0.14.6",
4+
"serial": 35,
5+
"lineage": "49d84423-0f98-9ac4-ffe0-84ef3126d36f",
6+
"outputs": {},
7+
"resources": [
8+
{
9+
"mode": "managed",
10+
"type": "google_compute_global_forwarding_rule",
11+
"name": "default",
12+
"provider": "provider[\"registry.terraform.io/hashicorp/google\"]",
13+
"instances": [
14+
{
15+
"schema_version": 0,
16+
"attributes": {
17+
"description": "",
18+
"id": "projects/driftctl-qa-1/global/forwardingRules/global-rule",
19+
"ip_address": "34.120.169.13",
20+
"ip_protocol": "TCP",
21+
"ip_version": "",
22+
"label_fingerprint": "42WmSpB8rSM=",
23+
"labels": null,
24+
"load_balancing_scheme": "EXTERNAL",
25+
"metadata_filters": [],
26+
"name": "global-rule",
27+
"network": "",
28+
"port_range": "80",
29+
"project": "driftctl-qa-1",
30+
"self_link": "https://www.googleapis.com/compute/v1/projects/driftctl-qa-1/global/forwardingRules/global-rule",
31+
"target": "projects/driftctl-qa-1/global/targetHttpProxies/target-proxy",
32+
"timeouts": null
33+
},
34+
"sensitive_attributes": [],
35+
"private": "eyJlMmJmYjczMC1lY2FhLTExZTYtOGY4OC0zNDM2M2JjN2M0YzAiOnsiY3JlYXRlIjoxMjAwMDAwMDAwMDAwLCJkZWxldGUiOjEyMDAwMDAwMDAwMDAsInVwZGF0ZSI6MTIwMDAwMDAwMDAwMH19",
36+
"dependencies": [
37+
"google_compute_backend_service.default",
38+
"google_compute_http_health_check.default",
39+
"google_compute_target_http_proxy.default",
40+
"google_compute_url_map.default"
41+
]
42+
}
43+
]
44+
}
45+
]
46+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package google
2+
3+
import (
4+
remoteerror "github.com/snyk/driftctl/pkg/remote/error"
5+
"github.com/snyk/driftctl/pkg/remote/google/repository"
6+
"github.com/snyk/driftctl/pkg/resource"
7+
"github.com/snyk/driftctl/pkg/resource/google"
8+
)
9+
10+
type GoogleComputeGlobalForwardingRuleEnumerator struct {
11+
repository repository.AssetRepository
12+
factory resource.ResourceFactory
13+
}
14+
15+
func NewGoogleComputeGlobalForwardingRuleEnumerator(repo repository.AssetRepository, factory resource.ResourceFactory) *GoogleComputeGlobalForwardingRuleEnumerator {
16+
return &GoogleComputeGlobalForwardingRuleEnumerator{
17+
repository: repo,
18+
factory: factory,
19+
}
20+
}
21+
22+
func (e *GoogleComputeGlobalForwardingRuleEnumerator) SupportedType() resource.ResourceType {
23+
return google.GoogleComputeGlobalForwardingRuleResourceType
24+
}
25+
26+
func (e *GoogleComputeGlobalForwardingRuleEnumerator) Enumerate() ([]*resource.Resource, error) {
27+
globalForwardingRules, err := e.repository.SearchAllGlobalForwardingRules()
28+
if err != nil {
29+
return nil, remoteerror.NewResourceListingError(err, string(e.SupportedType()))
30+
}
31+
32+
results := make([]*resource.Resource, 0, len(globalForwardingRules))
33+
34+
for _, res := range globalForwardingRules {
35+
results = append(
36+
results,
37+
e.factory.CreateAbstractResource(
38+
string(e.SupportedType()),
39+
trimResourceName(res.GetName()),
40+
map[string]interface{}{},
41+
),
42+
)
43+
}
44+
45+
return results, err
46+
}

pkg/remote/google/init.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ func Init(version string, alerter *alerter.Alerter,
101101
remoteLibrary.AddEnumerator(NewGoogleComputeNodeGroupEnumerator(assetRepository, factory))
102102
remoteLibrary.AddEnumerator(NewGoogleComputeForwardingRuleEnumerator(assetRepository, factory))
103103
remoteLibrary.AddEnumerator(NewGoogleComputeInstanceGroupManagerEnumerator(assetRepository, factory))
104+
remoteLibrary.AddEnumerator(NewGoogleComputeGlobalForwardingRuleEnumerator(assetRepository, factory))
104105

105106
err = resourceSchemaRepository.Init(terraform.GOOGLE, provider.Version(), provider.Schema())
106107
if err != nil {

pkg/remote/google/repository/asset.go

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,30 @@ import (
1313

1414
// https://cloud.google.com/asset-inventory/docs/supported-asset-types#supported_resource_types
1515
const (
16-
storageBucketAssetType = "storage.googleapis.com/Bucket"
17-
computeFirewallAssetType = "compute.googleapis.com/Firewall"
18-
computeRouterAssetType = "compute.googleapis.com/Router"
19-
computeInstanceAssetType = "compute.googleapis.com/Instance"
20-
computeNetworkAssetType = "compute.googleapis.com/Network"
21-
computeSubnetworkAssetType = "compute.googleapis.com/Subnetwork"
22-
computeDiskAssetType = "compute.googleapis.com/Disk"
23-
computeImageAssetType = "compute.googleapis.com/Image"
24-
dnsManagedZoneAssetType = "dns.googleapis.com/ManagedZone"
25-
computeInstanceGroupAssetType = "compute.googleapis.com/InstanceGroup"
26-
bigqueryDatasetAssetType = "bigquery.googleapis.com/Dataset"
27-
bigqueryTableAssetType = "bigquery.googleapis.com/Table"
28-
computeAddressAssetType = "compute.googleapis.com/Address"
29-
computeGlobalAddressAssetType = "compute.googleapis.com/GlobalAddress"
30-
cloudFunctionsFunction = "cloudfunctions.googleapis.com/CloudFunction"
31-
bigtableInstanceAssetType = "bigtableadmin.googleapis.com/Instance"
32-
bigtableTableAssetType = "bigtableadmin.googleapis.com/Table"
33-
sqlDatabaseInstanceAssetType = "sqladmin.googleapis.com/Instance"
34-
healthCheckAssetType = "compute.googleapis.com/HealthCheck"
35-
cloudRunServiceAssetType = "run.googleapis.com/Service"
36-
nodeGroupAssetType = "compute.googleapis.com/NodeGroup"
37-
computeForwardingRuleAssetType = "compute.googleapis.com/ForwardingRule"
38-
instanceGroupManagerAssetType = "compute.googleapis.com/InstanceGroupManager"
16+
storageBucketAssetType = "storage.googleapis.com/Bucket"
17+
computeFirewallAssetType = "compute.googleapis.com/Firewall"
18+
computeRouterAssetType = "compute.googleapis.com/Router"
19+
computeInstanceAssetType = "compute.googleapis.com/Instance"
20+
computeNetworkAssetType = "compute.googleapis.com/Network"
21+
computeSubnetworkAssetType = "compute.googleapis.com/Subnetwork"
22+
computeDiskAssetType = "compute.googleapis.com/Disk"
23+
computeImageAssetType = "compute.googleapis.com/Image"
24+
dnsManagedZoneAssetType = "dns.googleapis.com/ManagedZone"
25+
computeInstanceGroupAssetType = "compute.googleapis.com/InstanceGroup"
26+
bigqueryDatasetAssetType = "bigquery.googleapis.com/Dataset"
27+
bigqueryTableAssetType = "bigquery.googleapis.com/Table"
28+
computeAddressAssetType = "compute.googleapis.com/Address"
29+
computeGlobalAddressAssetType = "compute.googleapis.com/GlobalAddress"
30+
cloudFunctionsFunction = "cloudfunctions.googleapis.com/CloudFunction"
31+
bigtableInstanceAssetType = "bigtableadmin.googleapis.com/Instance"
32+
bigtableTableAssetType = "bigtableadmin.googleapis.com/Table"
33+
sqlDatabaseInstanceAssetType = "sqladmin.googleapis.com/Instance"
34+
healthCheckAssetType = "compute.googleapis.com/HealthCheck"
35+
cloudRunServiceAssetType = "run.googleapis.com/Service"
36+
nodeGroupAssetType = "compute.googleapis.com/NodeGroup"
37+
computeForwardingRuleAssetType = "compute.googleapis.com/ForwardingRule"
38+
instanceGroupManagerAssetType = "compute.googleapis.com/InstanceGroupManager"
39+
computeGlobalForwardingRuleAssetType = "compute.googleapis.com/GlobalForwardingRule"
3940
)
4041

4142
type AssetRepository interface {
@@ -62,6 +63,7 @@ type AssetRepository interface {
6263
SearchAllNodeGroups() ([]*assetpb.Asset, error)
6364
SearchAllForwardingRules() ([]*assetpb.Asset, error)
6465
SearchAllInstanceGroupManagers() ([]*assetpb.Asset, error)
66+
SearchAllGlobalForwardingRules() ([]*assetpb.Asset, error)
6567
}
6668

6769
type assetRepository struct {
@@ -91,6 +93,7 @@ func (s assetRepository) listAllResources(ty string) ([]*assetpb.Asset, error) {
9193
nodeGroupAssetType,
9294
computeForwardingRuleAssetType,
9395
instanceGroupManagerAssetType,
96+
computeGlobalForwardingRuleAssetType,
9497
},
9598
}
9699
var results []*assetpb.Asset
@@ -273,3 +276,7 @@ func (s assetRepository) SearchAllForwardingRules() ([]*assetpb.Asset, error) {
273276
func (s assetRepository) SearchAllInstanceGroupManagers() ([]*assetpb.Asset, error) {
274277
return s.listAllResources(instanceGroupManagerAssetType)
275278
}
279+
280+
func (s assetRepository) SearchAllGlobalForwardingRules() ([]*assetpb.Asset, error) {
281+
return s.listAllResources(computeGlobalForwardingRuleAssetType)
282+
}

pkg/remote/google/repository/mock_AssetRepository.go

Lines changed: 24 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/remote/google_compute_scanner_test.go

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1652,3 +1652,112 @@ func TestGoogleComputeInstanceGroupManager(t *testing.T) {
16521652
})
16531653
}
16541654
}
1655+
1656+
func TestGoogleComputeGlobalForwardingRule(t *testing.T) {
1657+
cases := []struct {
1658+
test string
1659+
assertExpected func(t *testing.T, got []*resource.Resource)
1660+
response []*assetpb.Asset
1661+
responseErr error
1662+
setupAlerterMock func(alerter *mocks.AlerterInterface)
1663+
wantErr error
1664+
}{
1665+
{
1666+
test: "no compute global forwarding rules",
1667+
response: []*assetpb.Asset{},
1668+
assertExpected: func(t *testing.T, got []*resource.Resource) {
1669+
assert.Len(t, got, 0)
1670+
},
1671+
},
1672+
{
1673+
test: "multiple compute global forwarding rules",
1674+
assertExpected: func(t *testing.T, got []*resource.Resource) {
1675+
assert.Len(t, got, 2)
1676+
assert.Equal(t, "//projects/driftctl-qa-1/global/forwardingRules/global-rule-foo", got[0].ResourceId())
1677+
assert.Equal(t, "google_compute_global_forwarding_rule", got[0].ResourceType())
1678+
1679+
assert.Equal(t, "//projects/driftctl-qa-1/global/forwardingRules/global-rule-bar", got[1].ResourceId())
1680+
assert.Equal(t, "google_compute_global_forwarding_rule", got[1].ResourceType())
1681+
},
1682+
response: []*assetpb.Asset{
1683+
{
1684+
AssetType: "compute.googleapis.com/GlobalForwardingRule",
1685+
Name: "//projects/driftctl-qa-1/global/forwardingRules/global-rule-foo",
1686+
},
1687+
{
1688+
AssetType: "compute.googleapis.com/GlobalForwardingRule",
1689+
Name: "//projects/driftctl-qa-1/global/forwardingRules/global-rule-bar",
1690+
},
1691+
},
1692+
},
1693+
{
1694+
test: "cannot list compute global forwarding rules",
1695+
assertExpected: func(t *testing.T, got []*resource.Resource) {
1696+
assert.Len(t, got, 0)
1697+
},
1698+
responseErr: status.Error(codes.PermissionDenied, "The caller does not have permission"),
1699+
setupAlerterMock: func(alerter *mocks.AlerterInterface) {
1700+
alerter.On(
1701+
"SendAlert",
1702+
"google_compute_global_forwarding_rule",
1703+
alerts.NewRemoteAccessDeniedAlert(
1704+
common.RemoteGoogleTerraform,
1705+
remoteerr.NewResourceListingError(
1706+
status.Error(codes.PermissionDenied, "The caller does not have permission"),
1707+
"google_compute_global_forwarding_rule",
1708+
),
1709+
alerts.EnumerationPhase,
1710+
),
1711+
).Once()
1712+
},
1713+
},
1714+
}
1715+
1716+
providerVersion := "3.78.0"
1717+
schemaRepository := testresource.InitFakeSchemaRepository(terraform.GOOGLE, providerVersion)
1718+
googleresource.InitResourcesMetadata(schemaRepository)
1719+
factory := terraform.NewTerraformResourceFactory(schemaRepository)
1720+
1721+
for _, c := range cases {
1722+
t.Run(c.test, func(tt *testing.T) {
1723+
scanOptions := ScannerOptions{}
1724+
providerLibrary := terraform.NewProviderLibrary()
1725+
remoteLibrary := common.NewRemoteLibrary()
1726+
1727+
// Initialize mocks
1728+
alerter := &mocks.AlerterInterface{}
1729+
if c.setupAlerterMock != nil {
1730+
c.setupAlerterMock(alerter)
1731+
}
1732+
1733+
assetClient, err := testgoogle.NewFakeAssertServerWithList(c.response, c.responseErr)
1734+
if err != nil {
1735+
tt.Fatal(err)
1736+
}
1737+
1738+
realProvider, err := terraform2.InitTestGoogleProvider(providerLibrary, providerVersion)
1739+
if err != nil {
1740+
tt.Fatal(err)
1741+
}
1742+
1743+
repo := repository.NewAssetRepository(assetClient, realProvider.GetConfig(), cache.New(0))
1744+
1745+
remoteLibrary.AddEnumerator(google.NewGoogleComputeGlobalForwardingRuleEnumerator(repo, factory))
1746+
1747+
testFilter := &filter.MockFilter{}
1748+
testFilter.On("IsTypeIgnored", mock.Anything).Return(false)
1749+
1750+
s := NewScanner(remoteLibrary, alerter, scanOptions, testFilter)
1751+
got, err := s.Resources()
1752+
assert.Equal(tt, err, c.wantErr)
1753+
if err != nil {
1754+
return
1755+
}
1756+
alerter.AssertExpectations(tt)
1757+
testFilter.AssertExpectations(tt)
1758+
if c.assertExpected != nil {
1759+
c.assertExpected(t, got)
1760+
}
1761+
})
1762+
}
1763+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package google
2+
3+
const GoogleComputeGlobalForwardingRuleResourceType = "google_compute_global_forwarding_rule"
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package google_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/snyk/driftctl/test"
7+
"github.com/snyk/driftctl/test/acceptance"
8+
)
9+
10+
func TestAcc_Google_ComputeGlobalForwardingRule(t *testing.T) {
11+
acceptance.Run(t, acceptance.AccTestCase{
12+
TerraformVersion: "0.15.5",
13+
Paths: []string{"./testdata/acc/google_compute_global_forwarding_rule"},
14+
Args: []string{
15+
"scan",
16+
"--to", "gcp+tf",
17+
},
18+
Checks: []acceptance.AccCheck{
19+
{
20+
Check: func(result *test.ScanResult, stdout string, err error) {
21+
if err != nil {
22+
t.Fatal(err)
23+
}
24+
result.AssertInfrastructureIsInSync()
25+
result.AssertManagedCount(1)
26+
},
27+
},
28+
},
29+
})
30+
}

0 commit comments

Comments
 (0)