Merge pull request #1024 from cloudskiff/feat/support_google_compute_network

Add google_compute_network resource
main
Elie 2021-09-29 15:59:48 +02:00 committed by GitHub
commit 44f0a98c96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 378 additions and 0 deletions

View File

@ -0,0 +1,48 @@
package google
import (
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
"github.com/cloudskiff/driftctl/pkg/remote/google/repository"
"github.com/cloudskiff/driftctl/pkg/resource"
"github.com/cloudskiff/driftctl/pkg/resource/google"
)
type GoogleComputeNetworkEnumerator struct {
repository repository.AssetRepository
factory resource.ResourceFactory
}
func NewGoogleComputeNetworkEnumerator(repo repository.AssetRepository, factory resource.ResourceFactory) *GoogleComputeNetworkEnumerator {
return &GoogleComputeNetworkEnumerator{
repository: repo,
factory: factory,
}
}
func (e *GoogleComputeNetworkEnumerator) SupportedType() resource.ResourceType {
return google.GoogleComputeNetworkResourceType
}
func (e *GoogleComputeNetworkEnumerator) Enumerate() ([]*resource.Resource, error) {
resources, err := e.repository.SearchAllNetworks()
if err != nil {
return nil, remoteerror.NewResourceListingError(err, string(e.SupportedType()))
}
results := make([]*resource.Resource, 0, len(resources))
for _, res := range resources {
results = append(
results,
e.factory.CreateAbstractResource(
string(e.SupportedType()),
trimResourceName(res.GetName()),
map[string]interface{}{
"display_name": res.DisplayName,
},
),
)
}
return results, err
}

View File

@ -57,6 +57,9 @@ func Init(version string, alerter *alerter.Alerter,
remoteLibrary.AddEnumerator(NewGoogleComputeInstanceEnumerator(assetRepository, factory))
remoteLibrary.AddEnumerator(NewGoogleComputeNetworkEnumerator(assetRepository, factory))
remoteLibrary.AddDetailsFetcher(google.GoogleComputeNetworkResourceType, common.NewGenericDetailsFetcher(google.GoogleComputeNetworkResourceType, provider, deserializer))
err = resourceSchemaRepository.Init(terraform.GOOGLE, version, provider.Schema())
if err != nil {
return err

View File

@ -17,6 +17,7 @@ const (
computeFirewallAssetType = "compute.googleapis.com/Firewall"
computeRouterAssetType = "compute.googleapis.com/Router"
computeInstanceAssetType = "compute.googleapis.com/Instance"
computeNetworkAssetType = "compute.googleapis.com/Network"
)
type AssetRepository interface {
@ -24,6 +25,7 @@ type AssetRepository interface {
SearchAllFirewalls() ([]*assetpb.ResourceSearchResult, error)
SearchAllRouters() ([]*assetpb.ResourceSearchResult, error)
SearchAllInstances() ([]*assetpb.ResourceSearchResult, error)
SearchAllNetworks() ([]*assetpb.ResourceSearchResult, error)
}
type assetRepository struct {
@ -50,6 +52,7 @@ func (s assetRepository) searchAllResources(ty string) ([]*assetpb.ResourceSearc
computeFirewallAssetType,
computeRouterAssetType,
computeInstanceAssetType,
computeNetworkAssetType,
},
}
var results []*assetpb.ResourceSearchResult
@ -100,3 +103,7 @@ func (s assetRepository) SearchAllRouters() ([]*assetpb.ResourceSearchResult, er
func (s assetRepository) SearchAllInstances() ([]*assetpb.ResourceSearchResult, error) {
return s.searchAllResources(computeInstanceAssetType)
}
func (s assetRepository) SearchAllNetworks() ([]*assetpb.ResourceSearchResult, error) {
return s.searchAllResources(computeNetworkAssetType)
}

View File

@ -376,3 +376,126 @@ func TestGoogleComputeInstance(t *testing.T) {
})
}
}
func TestGoogleComputeNetwork(t *testing.T) {
cases := []struct {
test string
dirName string
response []*assetpb.ResourceSearchResult
responseErr error
setupAlerterMock func(alerter *mocks.AlerterInterface)
wantErr error
}{
{
test: "no network",
dirName: "google_compute_network_empty",
response: []*assetpb.ResourceSearchResult{},
wantErr: nil,
},
{
test: "multiple networks",
dirName: "google_compute_network",
response: []*assetpb.ResourceSearchResult{
{
AssetType: "compute.googleapis.com/Network",
DisplayName: "driftctl-unittest-1",
Name: "//compute.googleapis.com/projects/driftctl-qa-1/global/networks/driftctl-unittest-1",
},
{
AssetType: "compute.googleapis.com/Network",
DisplayName: "driftctl-unittest-2",
Name: "//compute.googleapis.com/projects/driftctl-qa-1/global/networks/driftctl-unittest-2",
},
{
AssetType: "compute.googleapis.com/Network",
DisplayName: "driftctl-unittest-3",
Name: "//compute.googleapis.com/projects/driftctl-qa-1/global/networks/driftctl-unittest-3",
},
},
wantErr: nil,
},
{
test: "cannot list compute networks",
dirName: "google_compute_network_empty",
responseErr: status.Error(codes.PermissionDenied, "The caller does not have permission"),
setupAlerterMock: func(alerter *mocks.AlerterInterface) {
alerter.On(
"SendAlert",
"google_compute_network",
alerts.NewRemoteAccessDeniedAlert(
common.RemoteGoogleTerraform,
remoteerr.NewResourceListingError(
status.Error(codes.PermissionDenied, "The caller does not have permission"),
"google_compute_network",
),
alerts.EnumerationPhase,
),
).Once()
},
wantErr: nil,
},
}
providerVersion := "3.78.0"
resType := resource.ResourceType(googleresource.GoogleComputeNetworkResourceType)
schemaRepository := testresource.InitFakeSchemaRepository("google", providerVersion)
googleresource.InitResourcesMetadata(schemaRepository)
factory := terraform.NewTerraformResourceFactory(schemaRepository)
deserializer := resource.NewDeserializer(factory)
for _, c := range cases {
t.Run(c.test, func(tt *testing.T) {
shouldUpdate := c.dirName == *goldenfile.Update
scanOptions := ScannerOptions{Deep: true}
providerLibrary := terraform.NewProviderLibrary()
remoteLibrary := common.NewRemoteLibrary()
// Initialize mocks
alerter := &mocks.AlerterInterface{}
if c.setupAlerterMock != nil {
c.setupAlerterMock(alerter)
}
assetClient, err := testgoogle.NewFakeAssetServer(c.response, c.responseErr)
if err != nil {
tt.Fatal(err)
}
realProvider, err := terraform2.InitTestGoogleProvider(providerLibrary, providerVersion)
if err != nil {
tt.Fatal(err)
}
provider := terraform2.NewFakeTerraformProvider(realProvider)
provider.WithResponse(c.dirName)
// Replace mock by real resources if we are in update mode
if shouldUpdate {
err = realProvider.Init()
if err != nil {
tt.Fatal(err)
}
provider.ShouldUpdate()
}
repo := repository.NewAssetRepository(assetClient, realProvider.GetConfig(), cache.New(0))
remoteLibrary.AddEnumerator(google.NewGoogleComputeNetworkEnumerator(repo, factory))
remoteLibrary.AddDetailsFetcher(resType, common.NewGenericDetailsFetcher(resType, provider, deserializer))
testFilter := &filter.MockFilter{}
testFilter.On("IsTypeIgnored", mock.Anything).Return(false)
s := NewScanner(remoteLibrary, alerter, scanOptions, testFilter)
got, err := s.Resources()
assert.Equal(tt, err, c.wantErr)
if err != nil {
return
}
alerter.AssertExpectations(tt)
testFilter.AssertExpectations(tt)
test.TestAgainstGoldenFile(got, resType.String(), c.dirName, provider, deserializer, shouldUpdate, tt)
})
}
}

View File

@ -0,0 +1,5 @@
{
"Typ": "WyJvYmplY3QiLHsiYXV0b19jcmVhdGVfc3VibmV0d29ya3MiOiJib29sIiwiZGVsZXRlX2RlZmF1bHRfcm91dGVzX29uX2NyZWF0ZSI6ImJvb2wiLCJkZXNjcmlwdGlvbiI6InN0cmluZyIsImdhdGV3YXlfaXB2NCI6InN0cmluZyIsImlkIjoic3RyaW5nIiwibXR1IjoibnVtYmVyIiwibmFtZSI6InN0cmluZyIsInByb2plY3QiOiJzdHJpbmciLCJyb3V0aW5nX21vZGUiOiJzdHJpbmciLCJzZWxmX2xpbmsiOiJzdHJpbmciLCJ0aW1lb3V0cyI6WyJvYmplY3QiLHsiY3JlYXRlIjoic3RyaW5nIiwiZGVsZXRlIjoic3RyaW5nIiwidXBkYXRlIjoic3RyaW5nIn1dfV0=",
"Val": "eyJhdXRvX2NyZWF0ZV9zdWJuZXR3b3JrcyI6ZmFsc2UsImRlbGV0ZV9kZWZhdWx0X3JvdXRlc19vbl9jcmVhdGUiOmZhbHNlLCJkZXNjcmlwdGlvbiI6IiIsImdhdGV3YXlfaXB2NCI6IiIsImlkIjoicHJvamVjdHMvZHJpZnRjdGwtcWEtMS9nbG9iYWwvbmV0d29ya3MvZHJpZnRjdGwtdW5pdHRlc3QtMSIsIm10dSI6MTQ2MCwibmFtZSI6ImRyaWZ0Y3RsLXVuaXR0ZXN0LTEiLCJwcm9qZWN0IjoiZHJpZnRjdGwtcWEtMSIsInJvdXRpbmdfbW9kZSI6IlJFR0lPTkFMIiwic2VsZl9saW5rIjoiaHR0cHM6Ly93d3cuZ29vZ2xlYXBpcy5jb20vY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1xYS0xL2dsb2JhbC9uZXR3b3Jrcy9kcmlmdGN0bC11bml0dGVzdC0zIiwidGltZW91dHMiOnsiY3JlYXRlIjpudWxsLCJkZWxldGUiOm51bGwsInVwZGF0ZSI6bnVsbH19",
"Err": null
}

View File

@ -0,0 +1,41 @@
[
{
"auto_create_subnetworks": false,
"delete_default_routes_on_create": false,
"description": "",
"gateway_ipv4": null,
"id": "projects/driftctl-qa-1/global/networks/driftctl-unittest-3",
"mtu": 1460,
"name": "driftctl-unittest-3",
"project": "driftctl-qa-1",
"routing_mode": "REGIONAL",
"self_link": null,
"timeouts": null
},
{
"auto_create_subnetworks": true,
"delete_default_routes_on_create": false,
"description": "",
"gateway_ipv4": null,
"id": "projects/driftctl-qa-1/global/networks/driftctl-unittest-2",
"mtu": 1460,
"name": "driftctl-unittest-2",
"project": "driftctl-qa-1",
"routing_mode": "REGIONAL",
"self_link": null,
"timeouts": null
},
{
"auto_create_subnetworks": false,
"delete_default_routes_on_create": false,
"description": "",
"gateway_ipv4": null,
"id": "projects/driftctl-qa-1/global/networks/driftctl-unittest-1",
"mtu": 1460,
"name": "driftctl-unittest-1",
"project": "driftctl-qa-1",
"routing_mode": "REGIONAL",
"self_link": null,
"timeouts": null
}
]

View File

@ -0,0 +1,19 @@
package google
import "github.com/cloudskiff/driftctl/pkg/resource"
const GoogleComputeNetworkResourceType = "google_compute_network"
func initGoogleComputeNetworkMetadata(resourceSchemaRepository resource.SchemaRepositoryInterface) {
resourceSchemaRepository.SetNormalizeFunc(GoogleComputeNetworkResourceType, func(res *resource.Resource) {
res.Attributes().SafeDelete([]string{"timeouts"})
res.Attributes().SafeDelete([]string{"self_link"})
res.Attributes().SafeDelete([]string{"gateway_ipv4"})
res.Attributes().SafeDelete([]string{"delete_default_routes_on_create"})
})
resourceSchemaRepository.SetResolveReadAttributesFunc(GoogleComputeNetworkResourceType, func(res *resource.Resource) map[string]string {
return map[string]string{
"name": *res.Attributes().GetString("display_name"),
}
})
}

View File

@ -0,0 +1,33 @@
package google_test
import (
"testing"
"github.com/cloudskiff/driftctl/test"
"github.com/cloudskiff/driftctl/test/acceptance"
)
func TestAcc_Google_ComputeNetwork(t *testing.T) {
acceptance.Run(t, acceptance.AccTestCase{
TerraformVersion: "0.15.5",
Paths: []string{"./testdata/acc/google_compute_network"},
Args: []string{
"scan",
"--to", "gcp+tf",
"--filter", "Type=='google_compute_network'",
"--deep",
},
Checks: []acceptance.AccCheck{
{
Check: func(result *test.ScanResult, stdout string, err error) {
if err != nil {
t.Fatal(err)
}
result.AssertInfrastructureIsInSync()
result.AssertManagedCount(3)
result.AssertDriftCountTotal(0)
},
},
},
})
}

View File

@ -6,4 +6,5 @@ func InitResourcesMetadata(resourceSchemaRepository resource.SchemaRepositoryInt
initGoogleStorageBucketMetadata(resourceSchemaRepository)
initGoogleComputeFirewallMetadata(resourceSchemaRepository)
initGoogleComputeRouterMetadata(resourceSchemaRepository)
initGoogleComputeNetworkMetadata(resourceSchemaRepository)
}

View File

@ -0,0 +1,39 @@
# This file is maintained automatically by "terraform init".
# Manual edits may be lost in future updates.
provider "registry.terraform.io/hashicorp/google" {
version = "3.78.0"
constraints = "3.78.0"
hashes = [
"h1:iCyTW8BWdr6Bvd5B89wkxlrB8xLxqHvT1CPmGuKembU=",
"zh:027971c4689b6130619827fe57ce260aaca060db3446817d3a92869dba7cc07f",
"zh:0876dbecc0d441bf2479edd17fe9141d77274b5071ea5f68ac26a2994bff66f3",
"zh:2a5363ed6b1b880f5284e604567cfdabecca809584c30bbe7f19ff568d1ea4cd",
"zh:2f5af69b70654bda91199f6393253e3e479107deebfeddc3fe5850b3a1e83dfb",
"zh:52e6816ef11f5f799a6626dfff384e2153b37450d8320f1ef1eee8f71a2a87b2",
"zh:59ae534607db13db35c0015c06d1ae6d4886f01f7e8fd4e07bc120236a01c494",
"zh:65ab2ed1746ea02d0b1bbd8a22ff3a95d09dc8bdb3841fbc17e45e9feccfb327",
"zh:877a71d24ff65ede3f0c5973168acfeaea0f2fea3757cab5600efcddfd3171d5",
"zh:8b10c9643a4a53148f6758bfd60804b33c2b838482f2c39ed210b729e6b1e2e8",
"zh:ba682648d9f6c11a6d04a250ac79eec39271f615f3ff60c5ae73ebfcc2cdb450",
"zh:e946561921e0279450e9b9f705de9354ce35562ed4cc0d4cd3512aa9eb1f6486",
]
}
provider "registry.terraform.io/hashicorp/random" {
version = "3.1.0"
hashes = [
"h1:BZMEPucF+pbu9gsPk0G0BHx7YP04+tKdq2MrRDF1EDM=",
"zh:2bbb3339f0643b5daa07480ef4397bd23a79963cc364cdfbb4e86354cb7725bc",
"zh:3cd456047805bf639fbf2c761b1848880ea703a054f76db51852008b11008626",
"zh:4f251b0eda5bb5e3dc26ea4400dba200018213654b69b4a5f96abee815b4f5ff",
"zh:7011332745ea061e517fe1319bd6c75054a314155cb2c1199a5b01fe1889a7e2",
"zh:738ed82858317ccc246691c8b85995bc125ac3b4143043219bd0437adc56c992",
"zh:7dbe52fac7bb21227acd7529b487511c91f4107db9cc4414f50d04ffc3cab427",
"zh:a3a9251fb15f93e4cfc1789800fc2d7414bbc18944ad4c5c98f466e6477c42bc",
"zh:a543ec1a3a8c20635cf374110bd2f87c07374cf2c50617eee2c669b3ceeeaa9f",
"zh:d9ab41d556a48bd7059f0810cf020500635bfc696c9fc3adab5ea8915c1d886b",
"zh:d9e13427a7d011dbd654e591b0337e6074eef8c3b9bb11b2e39eaaf257044fd7",
"zh:f7605bd1437752114baf601bdf6931debe6dc6bfe3006eb7e9bb9080931dca8a",
]
}

View File

@ -0,0 +1,48 @@
provider "google" {}
terraform {
required_version = "~> 0.15.0"
required_providers {
google = {
version = "3.78.0"
}
}
}
resource "random_string" "driftctl-unittest-1" {
length = 12
upper = false
special = false
}
resource "random_string" "driftctl-unittest-2" {
length = 12
upper = false
special = false
}
resource "random_string" "driftctl-unittest-3" {
length = 12
upper = false
special = false
}
resource "google_compute_network" "driftctl-unittest-1" {
name = "driftctl-unittest-${random_string.driftctl-unittest-1.result}"
}
resource "google_compute_network" "driftctl-unittest-2" {
name = "driftctl-unittest-${random_string.driftctl-unittest-2.result}"
auto_create_subnetworks = true
mtu = 1460
routing_mode = "GLOBAL"
}
resource "google_compute_network" "driftctl-unittest-3" {
name = "driftctl-unittest-${random_string.driftctl-unittest-3.result}"
description = "driftctl test"
auto_create_subnetworks = false
mtu = 1500
delete_default_routes_on_create = true
routing_mode = "REGIONAL"
}

View File

@ -70,6 +70,7 @@ var supportedTypes = map[string]struct{}{
"google_compute_firewall": {},
"google_compute_router": {},
"google_compute_instance": {},
"google_compute_network": {},
"azurerm_storage_account": {},
"azurerm_storage_container": {},