Merge pull request #1178 from cloudskiff/add_google_compute_global_address

Add google_compute_global_address
main
Elie 2021-10-29 10:50:19 +02:00 committed by GitHub
commit b87f11938a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 373 additions and 0 deletions

View File

@ -348,6 +348,7 @@ func TestTerraformStateReader_Google_Resources(t *testing.T) {
{name: "bigquery dataset", dirName: "google_bigquery_dataset", wantErr: false},
{name: "bigquery table", dirName: "google_bigquery_table", wantErr: false},
{name: "compute address", dirName: "google_compute_address", wantErr: false},
{name: "compute global address", dirName: "google_compute_global_address", wantErr: false},
{name: "cloudfunctions function", dirName: "google_cloudfunctions_function", wantErr: false},
{name: "compute subnetwork", dirName: "google_compute_subnetwork", wantErr: false},
{name: "compute disk", dirName: "google_compute_disk", wantErr: false},

View File

@ -0,0 +1,20 @@
[
{
"Id": "projects/cloudskiff-dev-elie/global/addresses/global-appserver-ip",
"Type": "google_compute_global_address",
"Attrs": {
"address": "107.178.250.143",
"address_type": "EXTERNAL",
"creation_timestamp": "2021-10-26T07:43:18.117-07:00",
"description": "",
"id": "projects/cloudskiff-dev-elie/global/addresses/global-appserver-ip",
"ip_version": "",
"name": "global-appserver-ip",
"network": "",
"prefix_length": 0,
"project": "cloudskiff-dev-elie",
"purpose": "",
"self_link": "https://www.googleapis.com/compute/v1/projects/cloudskiff-dev-elie/global/addresses/global-appserver-ip"
}
}
]

View File

@ -0,0 +1,37 @@
{
"version": 4,
"terraform_version": "0.15.5",
"serial": 84,
"lineage": "0738cef4-9d69-9ccc-aebd-1177cafa0aa9",
"outputs": {},
"resources": [
{
"mode": "managed",
"type": "google_compute_global_address",
"name": "default",
"provider": "provider[\"registry.terraform.io/hashicorp/google\"]",
"instances": [
{
"schema_version": 0,
"attributes": {
"address": "107.178.250.143",
"address_type": "EXTERNAL",
"creation_timestamp": "2021-10-26T07:43:18.117-07:00",
"description": "",
"id": "projects/cloudskiff-dev-elie/global/addresses/global-appserver-ip",
"ip_version": "",
"name": "global-appserver-ip",
"network": "",
"prefix_length": 0,
"project": "cloudskiff-dev-elie",
"purpose": "",
"self_link": "https://www.googleapis.com/compute/v1/projects/cloudskiff-dev-elie/global/addresses/global-appserver-ip",
"timeouts": null
},
"sensitive_attributes": [],
"private": "eyJlMmJmYjczMC1lY2FhLTExZTYtOGY4OC0zNDM2M2JjN2M0YzAiOnsiY3JlYXRlIjoyNDAwMDAwMDAwMDAsImRlbGV0ZSI6MjQwMDAwMDAwMDAwfX0="
}
]
}
]
}

View File

@ -33,6 +33,10 @@ func (e *GoogleComputeAddressEnumerator) Enumerate() ([]*resource.Resource, erro
results := make([]*resource.Resource, 0, len(resources))
for _, res := range resources {
// Global addresses are handled as a dedicated resource
if res.GetLocation() == "global" {
continue
}
address := ""
if addr, exist := res.GetAdditionalAttributes().GetFields()["address"]; exist {
address = addr.GetStringValue()

View File

@ -0,0 +1,60 @@
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"
"github.com/sirupsen/logrus"
)
type GoogleComputeGlobalAddressEnumerator struct {
repository repository.AssetRepository
factory resource.ResourceFactory
}
func NewGoogleComputeGlobalAddressEnumerator(repo repository.AssetRepository, factory resource.ResourceFactory) *GoogleComputeGlobalAddressEnumerator {
return &GoogleComputeGlobalAddressEnumerator{
repository: repo,
factory: factory,
}
}
func (e *GoogleComputeGlobalAddressEnumerator) SupportedType() resource.ResourceType {
return google.GoogleComputeGlobalAddressResourceType
}
func (e *GoogleComputeGlobalAddressEnumerator) Enumerate() ([]*resource.Resource, error) {
resources, err := e.repository.SearchAllGlobalAddresses()
if err != nil {
return nil, remoteerror.NewResourceListingError(err, string(e.SupportedType()))
}
results := make([]*resource.Resource, 0, len(resources))
for _, res := range resources {
name, exist := res.GetResource().GetData().GetFields()["name"]
if !exist || name.GetStringValue() == "" {
logrus.WithField("name", res.GetName()).Warn("Unable to retrieve resource name")
continue
}
address := ""
if addr, exist := res.GetResource().GetData().GetFields()["address"]; exist {
address = addr.GetStringValue()
}
results = append(
results,
e.factory.CreateAbstractResource(
string(e.SupportedType()),
trimResourceName(res.GetName()),
map[string]interface{}{
"name": name.GetStringValue(),
"address": address,
},
),
)
}
return results, err
}

View File

@ -89,6 +89,7 @@ func Init(version string, alerter *alerter.Alerter,
remoteLibrary.AddEnumerator(NewGoogleBigqueryTableEnumerator(assetRepository, factory))
remoteLibrary.AddEnumerator(NewGoogleComputeAddressEnumerator(assetRepository, factory))
remoteLibrary.AddEnumerator(NewGoogleComputeGlobalAddressEnumerator(assetRepository, factory))
remoteLibrary.AddEnumerator(NewGoogleCloudFunctionsFunctionEnumerator(assetRepository, factory))
remoteLibrary.AddEnumerator(NewGoogleComputeDiskEnumerator(assetRepository, factory))
remoteLibrary.AddEnumerator(NewGoogleComputeImageEnumerator(assetRepository, factory))

View File

@ -26,6 +26,7 @@ const (
bigqueryDatasetAssetType = "bigquery.googleapis.com/Dataset"
bigqueryTableAssetType = "bigquery.googleapis.com/Table"
computeAddressAssetType = "compute.googleapis.com/Address"
computeGlobalAddressAssetType = "compute.googleapis.com/GlobalAddress"
cloudFunctionsFunction = "cloudfunctions.googleapis.com/CloudFunction"
bigtableInstanceAssetType = "bigtableadmin.googleapis.com/Instance"
bigtableTableAssetType = "bigtableadmin.googleapis.com/Table"
@ -46,6 +47,7 @@ type AssetRepository interface {
SearchAllDatasets() ([]*assetpb.ResourceSearchResult, error)
SearchAllTables() ([]*assetpb.ResourceSearchResult, error)
SearchAllAddresses() ([]*assetpb.ResourceSearchResult, error)
SearchAllGlobalAddresses() ([]*assetpb.Asset, error)
SearchAllFunctions() ([]*assetpb.Asset, error)
SearchAllSubnetworks() ([]*assetpb.ResourceSearchResult, error)
SearchAllBigtableInstances() ([]*assetpb.Asset, error)
@ -77,6 +79,7 @@ func (s assetRepository) listAllResources(ty string) ([]*assetpb.Asset, error) {
bigtableInstanceAssetType,
bigtableTableAssetType,
sqlDatabaseInstanceAssetType,
computeGlobalAddressAssetType,
},
}
var results []*assetpb.Asset
@ -207,6 +210,10 @@ func (s assetRepository) SearchAllAddresses() ([]*assetpb.ResourceSearchResult,
return s.searchAllResources(computeAddressAssetType)
}
func (s assetRepository) SearchAllGlobalAddresses() ([]*assetpb.Asset, error) {
return s.listAllResources(computeGlobalAddressAssetType)
}
func (s assetRepository) SearchAllFunctions() ([]*assetpb.Asset, error) {
return s.listAllResources(cloudFunctionsFunction)
}

View File

@ -219,6 +219,29 @@ func (_m *MockAssetRepository) SearchAllFunctions() ([]*asset.Asset, error) {
return r0, r1
}
// SearchAllGlobalAddresses provides a mock function with given fields:
func (_m *MockAssetRepository) SearchAllGlobalAddresses() ([]*asset.Asset, error) {
ret := _m.Called()
var r0 []*asset.Asset
if rf, ok := ret.Get(0).(func() []*asset.Asset); ok {
r0 = rf()
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).([]*asset.Asset)
}
}
var r1 error
if rf, ok := ret.Get(1).(func() error); ok {
r1 = rf()
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// SearchAllHealthChecks provides a mock function with given fields:
func (_m *MockAssetRepository) SearchAllHealthChecks() ([]*asset.ResourceSearchResult, error) {
ret := _m.Called()

View File

@ -657,6 +657,10 @@ func TestGoogleComputeAddress(t *testing.T) {
AssetType: "compute.googleapis.com/Address",
Name: "//compute.googleapis.com/projects/cloudskiff-dev-elie/regions/us-central1/addresses/my-address",
},
{
AssetType: "compute.googleapis.com/Address",
Location: "global", // Global addresses should be ignored
},
{
AssetType: "compute.googleapis.com/Address",
Name: "//compute.googleapis.com/projects/cloudskiff-dev-elie/regions/us-central1/addresses/my-address-2",
@ -741,6 +745,132 @@ func TestGoogleComputeAddress(t *testing.T) {
}
}
func TestGoogleComputeGlobalAddress(t *testing.T) {
cases := []struct {
test string
assertExpected func(t *testing.T, got []*resource.Resource)
response []*assetpb.Asset
responseErr error
setupAlerterMock func(alerter *mocks.AlerterInterface)
wantErr error
}{
{
test: "no resource",
response: []*assetpb.Asset{},
assertExpected: func(t *testing.T, got []*resource.Resource) {
assert.Len(t, got, 0)
},
},
{
test: "one resource returned",
assertExpected: func(t *testing.T, got []*resource.Resource) {
assert.Len(t, got, 1)
assert.Equal(t, "projects/cloudskiff-dev-elie/global/addresses/global-appserver-ip", got[0].ResourceId())
assert.Equal(t, "google_compute_global_address", got[0].ResourceType())
},
response: []*assetpb.Asset{
{
AssetType: "compute.googleapis.com/GlobalAddress",
Name: "//compute.googleapis.com/projects/cloudskiff-dev-elie/global/addresses/global-appserver-ip",
Resource: &assetpb.Resource{
Data: func() *structpb.Struct {
v, err := structpb.NewStruct(map[string]interface{}{
"name": "projects/cloudskiff-dev-elie/global/addresses/global-appserver-ip",
})
if err != nil {
t.Fatal(err)
}
return v
}(),
},
},
},
},
{
test: "one resource without resource data",
assertExpected: func(t *testing.T, got []*resource.Resource) {
assert.Len(t, got, 0)
},
response: []*assetpb.Asset{
{
AssetType: "compute.googleapis.com/GlobalAddress",
Name: "//compute.googleapis.com/projects/cloudskiff-dev-elie/global/addresses/global-appserver-ip",
},
},
},
{
test: "cannot list cloud functions",
assertExpected: func(t *testing.T, got []*resource.Resource) {
assert.Len(t, got, 0)
},
responseErr: status.Error(codes.PermissionDenied, "The caller does not have permission"),
setupAlerterMock: func(alerter *mocks.AlerterInterface) {
alerter.On(
"SendAlert",
"google_compute_global_address",
alerts.NewRemoteAccessDeniedAlert(
common.RemoteGoogleTerraform,
remoteerr.NewResourceListingError(
status.Error(codes.PermissionDenied, "The caller does not have permission"),
"google_compute_global_address",
),
alerts.EnumerationPhase,
),
).Once()
},
},
}
providerVersion := "3.78.0"
schemaRepository := testresource.InitFakeSchemaRepository("google", providerVersion)
googleresource.InitResourcesMetadata(schemaRepository)
factory := terraform.NewTerraformResourceFactory(schemaRepository)
for _, c := range cases {
t.Run(c.test, func(tt *testing.T) {
scanOptions := ScannerOptions{}
providerLibrary := terraform.NewProviderLibrary()
remoteLibrary := common.NewRemoteLibrary()
// Initialize mocks
alerter := &mocks.AlerterInterface{}
if c.setupAlerterMock != nil {
c.setupAlerterMock(alerter)
}
assetClient, err := testgoogle.NewFakeAssertServerWithList(c.response, c.responseErr)
if err != nil {
tt.Fatal(err)
}
realProvider, err := terraform2.InitTestGoogleProvider(providerLibrary, providerVersion)
if err != nil {
tt.Fatal(err)
}
repo := repository.NewAssetRepository(assetClient, realProvider.GetConfig(), cache.New(0))
remoteLibrary.AddEnumerator(google.NewGoogleComputeGlobalAddressEnumerator(repo, factory))
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)
if c.assertExpected != nil {
c.assertExpected(t, got)
}
})
}
}
func TestGoogleComputeSubnetwork(t *testing.T) {
cases := []struct {

View File

@ -0,0 +1,14 @@
package google
import "github.com/cloudskiff/driftctl/pkg/resource"
const GoogleComputeGlobalAddressResourceType = "google_compute_global_address"
func initGoogleComputeGlobalAddressMetadata(resourceSchemaRepository resource.SchemaRepositoryInterface) {
resourceSchemaRepository.SetHumanReadableAttributesFunc(GoogleComputeGlobalAddressResourceType, func(res *resource.Resource) map[string]string {
return map[string]string{
"Name": *res.Attributes().GetString("name"),
"Address": *res.Attributes().GetString("address"),
}
})
}

View File

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

View File

@ -28,6 +28,7 @@ func TestGoogle_Metadata_Flags(t *testing.T) {
GoogleBigqueryTableResourceType: {},
GoogleComputeDiskResourceType: {},
GoogleBigTableInstanceResourceType: {},
GoogleComputeGlobalAddressResourceType: {},
}
schemaRepository := testresource.InitFakeSchemaRepository(tf.GOOGLE, "3.78.0")

View File

@ -13,6 +13,7 @@ func InitResourcesMetadata(resourceSchemaRepository resource.SchemaRepositoryInt
initGoogleBigqueryTableMetadata(resourceSchemaRepository)
initGoogleProjectIAMMemberMetadata(resourceSchemaRepository)
initGoogleComputeAddressMetadata(resourceSchemaRepository)
initGoogleComputeGlobalAddressMetadata(resourceSchemaRepository)
initGoogleComputeSubnetworkMetadata(resourceSchemaRepository)
initGoogleComputeDiskMetadata(resourceSchemaRepository)
initGoogleComputeImageMetadata(resourceSchemaRepository)

View File

@ -0,0 +1,3 @@
*
!google_compute_address
!google_compute_global_address

View File

@ -0,0 +1,21 @@
# 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",
]
}

View File

@ -0,0 +1,19 @@
provider "google" {}
terraform {
required_version = "~> 0.15.0"
required_providers {
google = {
version = "3.78.0"
}
}
}
resource "google_compute_global_address" "default" {
name = "global-appserver-ip"
}
resource "google_compute_address" "ip_address" {
name = "my-address"
region = "us-central1"
}

View File

@ -175,6 +175,7 @@ var supportedTypes = map[string]ResourceTypeMeta{
"google_sql_database_instance": {},
"google_compute_image": {},
"google_compute_health_check": {},
"google_compute_global_address": {},
"azurerm_storage_account": {},
"azurerm_storage_container": {},