Merge branch 'main' into feat/appautoscaling

main
Elie 2021-09-27 16:15:46 +02:00 committed by GitHub
commit c4db6fa761
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 943 additions and 1 deletions

View File

@ -316,6 +316,8 @@ func TestTerraformStateReader_Google_Resources(t *testing.T) {
wantErr bool
}{
{name: "compute firewall", dirName: "google_compute_firewall", wantErr: false},
{name: "compute router", dirName: "google_compute_router", wantErr: false},
{name: "compute instance", dirName: "google_compute_instance", wantErr: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

View File

@ -0,0 +1,63 @@
[
{
"Id": "projects/cloudskiff-dev-elie/zones/us-central1-a/instances/test",
"Type": "google_compute_instance",
"Attrs": {
"boot_disk": [
{
"auto_delete": true,
"device_name": "persistent-disk-0",
"disk_encryption_key_raw": "",
"disk_encryption_key_sha256": "",
"initialize_params": [
{
"image": "https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-9-stretch-v20210916",
"size": 10,
"type": "pd-standard"
}
],
"kms_key_self_link": "",
"mode": "READ_WRITE",
"source": "https://www.googleapis.com/compute/v1/projects/cloudskiff-dev-elie/zones/us-central1-a/disks/test"
}
],
"can_ip_forward": false,
"cpu_platform": "Intel Haswell",
"current_status": "RUNNING",
"deletion_protection": false,
"description": "",
"enable_display": false,
"hostname": "",
"id": "projects/cloudskiff-dev-elie/zones/us-central1-a/instances/test",
"instance_id": "4993131743765937902",
"label_fingerprint": "42WmSpB8rSM=",
"machine_type": "e2-medium",
"metadata_fingerprint": "Av7l3BNGSpc=",
"metadata_startup_script": "",
"min_cpu_platform": "",
"name": "test",
"network_interface": [
{
"name": "nic0",
"network": "https://www.googleapis.com/compute/v1/projects/cloudskiff-dev-elie/global/networks/default",
"network_ip": "10.128.0.2",
"nic_type": "",
"subnetwork": "https://www.googleapis.com/compute/v1/projects/cloudskiff-dev-elie/regions/us-central1/subnetworks/default",
"subnetwork_project": "cloudskiff-dev-elie"
}
],
"project": "cloudskiff-dev-elie",
"scheduling": [
{
"automatic_restart": true,
"min_node_cpus": 0,
"on_host_maintenance": "MIGRATE",
"preemptible": false
}
],
"self_link": "https://www.googleapis.com/compute/v1/projects/cloudskiff-dev-elie/zones/us-central1-a/instances/test",
"tags_fingerprint": "42WmSpB8rSM=",
"zone": "us-central1-a"
}
}
]

View File

@ -0,0 +1,98 @@
{
"version": 4,
"terraform_version": "0.15.0",
"serial": 3,
"lineage": "aacd8e7b-193c-c11b-8d66-0340f4043eac",
"outputs": {},
"resources": [
{
"mode": "managed",
"type": "google_compute_instance",
"name": "default",
"provider": "provider[\"registry.terraform.io/hashicorp/google\"]",
"instances": [
{
"schema_version": 6,
"attributes": {
"advanced_machine_features": [],
"allow_stopping_for_update": null,
"attached_disk": [],
"boot_disk": [
{
"auto_delete": true,
"device_name": "persistent-disk-0",
"disk_encryption_key_raw": "",
"disk_encryption_key_sha256": "",
"initialize_params": [
{
"image": "https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-9-stretch-v20210916",
"labels": {},
"size": 10,
"type": "pd-standard"
}
],
"kms_key_self_link": "",
"mode": "READ_WRITE",
"source": "https://www.googleapis.com/compute/v1/projects/cloudskiff-dev-elie/zones/us-central1-a/disks/test"
}
],
"can_ip_forward": false,
"confidential_instance_config": [],
"cpu_platform": "Intel Haswell",
"current_status": "RUNNING",
"deletion_protection": false,
"description": "",
"desired_status": null,
"enable_display": false,
"guest_accelerator": [],
"hostname": "",
"id": "projects/cloudskiff-dev-elie/zones/us-central1-a/instances/test",
"instance_id": "4993131743765937902",
"label_fingerprint": "42WmSpB8rSM=",
"labels": null,
"machine_type": "e2-medium",
"metadata": null,
"metadata_fingerprint": "Av7l3BNGSpc=",
"metadata_startup_script": "",
"min_cpu_platform": "",
"name": "test",
"network_interface": [
{
"access_config": [],
"alias_ip_range": [],
"name": "nic0",
"network": "https://www.googleapis.com/compute/v1/projects/cloudskiff-dev-elie/global/networks/default",
"network_ip": "10.128.0.2",
"nic_type": "",
"subnetwork": "https://www.googleapis.com/compute/v1/projects/cloudskiff-dev-elie/regions/us-central1/subnetworks/default",
"subnetwork_project": "cloudskiff-dev-elie"
}
],
"project": "cloudskiff-dev-elie",
"reservation_affinity": [],
"resource_policies": null,
"scheduling": [
{
"automatic_restart": true,
"min_node_cpus": 0,
"node_affinities": [],
"on_host_maintenance": "MIGRATE",
"preemptible": false
}
],
"scratch_disk": [],
"self_link": "https://www.googleapis.com/compute/v1/projects/cloudskiff-dev-elie/zones/us-central1-a/instances/test",
"service_account": [],
"shielded_instance_config": [],
"tags": null,
"tags_fingerprint": "42WmSpB8rSM=",
"timeouts": null,
"zone": "us-central1-a"
},
"sensitive_attributes": [],
"private": "eyJlMmJmYjczMC1lY2FhLTExZTYtOGY4OC0zNDM2M2JjN2M0YzAiOnsiY3JlYXRlIjoxMjAwMDAwMDAwMDAwLCJkZWxldGUiOjEyMDAwMDAwMDAwMDAsInVwZGF0ZSI6MTIwMDAwMDAwMDAwMH0sInNjaGVtYV92ZXJzaW9uIjoiNiJ9"
}
]
}
]
}

View File

@ -0,0 +1,101 @@
[
{
"Id": "projects/cloudskiff-dev-raphael/regions/us-central1/routers/test-router-2",
"Type": "google_compute_router",
"Attrs": {
"bgp": [
{
"advertise_mode": "CUSTOM",
"advertised_groups": [
"ALL_SUBNETS"
],
"advertised_ip_ranges": [
{
"description": "",
"range": "1.2.3.4"
},
{
"description": "",
"range": "6.7.0.0/16"
}
],
"asn": 64514
}
],
"creation_timestamp": "2021-09-16T07:40:45.385-07:00",
"description": "",
"id": "projects/cloudskiff-dev-raphael/regions/us-central1/routers/test-router-2",
"name": "test-router-2",
"network": "https://www.googleapis.com/compute/v1/projects/cloudskiff-dev-raphael/global/networks/my-network",
"project": "cloudskiff-dev-raphael",
"region": "us-central1",
"self_link": "https://www.googleapis.com/compute/v1/projects/cloudskiff-dev-raphael/regions/us-central1/routers/test-router-2"
}
},
{
"Id": "projects/cloudskiff-dev-raphael/regions/us-central1/routers/test-router-0",
"Type": "google_compute_router",
"Attrs": {
"bgp": [
{
"advertise_mode": "CUSTOM",
"advertised_groups": [
"ALL_SUBNETS"
],
"advertised_ip_ranges": [
{
"description": "",
"range": "1.2.3.4"
},
{
"description": "",
"range": "6.7.0.0/16"
}
],
"asn": 64514
}
],
"creation_timestamp": "2021-09-16T07:40:46.101-07:00",
"description": "",
"id": "projects/cloudskiff-dev-raphael/regions/us-central1/routers/test-router-0",
"name": "test-router-0",
"network": "https://www.googleapis.com/compute/v1/projects/cloudskiff-dev-raphael/global/networks/my-network",
"project": "cloudskiff-dev-raphael",
"region": "us-central1",
"self_link": "https://www.googleapis.com/compute/v1/projects/cloudskiff-dev-raphael/regions/us-central1/routers/test-router-0"
}
},
{
"Id": "projects/cloudskiff-dev-raphael/regions/us-central1/routers/test-router-1",
"Type": "google_compute_router",
"Attrs": {
"bgp": [
{
"advertise_mode": "CUSTOM",
"advertised_groups": [
"ALL_SUBNETS"
],
"advertised_ip_ranges": [
{
"description": "",
"range": "1.2.3.4"
},
{
"description": "",
"range": "6.7.0.0/16"
}
],
"asn": 64514
}
],
"creation_timestamp": "2021-09-16T07:40:43.732-07:00",
"description": "",
"id": "projects/cloudskiff-dev-raphael/regions/us-central1/routers/test-router-1",
"name": "test-router-1",
"network": "https://www.googleapis.com/compute/v1/projects/cloudskiff-dev-raphael/global/networks/my-network",
"project": "cloudskiff-dev-raphael",
"region": "us-central1",
"self_link": "https://www.googleapis.com/compute/v1/projects/cloudskiff-dev-raphael/regions/us-central1/routers/test-router-1"
}
}
]

View File

@ -0,0 +1,147 @@
{
"version": 4,
"terraform_version": "0.15.5",
"serial": 6,
"lineage": "5dbb3a54-27a7-9a8d-0c76-8d44f1873015",
"outputs": {},
"resources": [
{
"mode": "managed",
"type": "google_compute_router",
"name": "router-0",
"provider": "provider[\"registry.terraform.io/hashicorp/google\"]",
"instances": [
{
"schema_version": 0,
"attributes": {
"bgp": [
{
"advertise_mode": "CUSTOM",
"advertised_groups": [
"ALL_SUBNETS"
],
"advertised_ip_ranges": [
{
"description": "",
"range": "1.2.3.4"
},
{
"description": "",
"range": "6.7.0.0/16"
}
],
"asn": 64514
}
],
"creation_timestamp": "2021-09-16T07:40:46.101-07:00",
"description": "",
"id": "projects/cloudskiff-dev-raphael/regions/us-central1/routers/test-router-0",
"name": "test-router-0",
"network": "https://www.googleapis.com/compute/v1/projects/cloudskiff-dev-raphael/global/networks/my-network",
"project": "cloudskiff-dev-raphael",
"region": "us-central1",
"self_link": "https://www.googleapis.com/compute/v1/projects/cloudskiff-dev-raphael/regions/us-central1/routers/test-router-0",
"timeouts": null
},
"sensitive_attributes": [],
"private": "eyJlMmJmYjczMC1lY2FhLTExZTYtOGY4OC0zNDM2M2JjN2M0YzAiOnsiY3JlYXRlIjoyNDAwMDAwMDAwMDAsImRlbGV0ZSI6MjQwMDAwMDAwMDAwLCJ1cGRhdGUiOjI0MDAwMDAwMDAwMH19",
"dependencies": [
"google_compute_network.foobar"
]
}
]
},
{
"mode": "managed",
"type": "google_compute_router",
"name": "router-1",
"provider": "provider[\"registry.terraform.io/hashicorp/google\"]",
"instances": [
{
"schema_version": 0,
"attributes": {
"bgp": [
{
"advertise_mode": "CUSTOM",
"advertised_groups": [
"ALL_SUBNETS"
],
"advertised_ip_ranges": [
{
"description": "",
"range": "1.2.3.4"
},
{
"description": "",
"range": "6.7.0.0/16"
}
],
"asn": 64514
}
],
"creation_timestamp": "2021-09-16T07:40:43.732-07:00",
"description": "",
"id": "projects/cloudskiff-dev-raphael/regions/us-central1/routers/test-router-1",
"name": "test-router-1",
"network": "https://www.googleapis.com/compute/v1/projects/cloudskiff-dev-raphael/global/networks/my-network",
"project": "cloudskiff-dev-raphael",
"region": "us-central1",
"self_link": "https://www.googleapis.com/compute/v1/projects/cloudskiff-dev-raphael/regions/us-central1/routers/test-router-1",
"timeouts": null
},
"sensitive_attributes": [],
"private": "eyJlMmJmYjczMC1lY2FhLTExZTYtOGY4OC0zNDM2M2JjN2M0YzAiOnsiY3JlYXRlIjoyNDAwMDAwMDAwMDAsImRlbGV0ZSI6MjQwMDAwMDAwMDAwLCJ1cGRhdGUiOjI0MDAwMDAwMDAwMH19",
"dependencies": [
"google_compute_network.foobar"
]
}
]
},
{
"mode": "managed",
"type": "google_compute_router",
"name": "router-2",
"provider": "provider[\"registry.terraform.io/hashicorp/google\"]",
"instances": [
{
"schema_version": 0,
"attributes": {
"bgp": [
{
"advertise_mode": "CUSTOM",
"advertised_groups": [
"ALL_SUBNETS"
],
"advertised_ip_ranges": [
{
"description": "",
"range": "1.2.3.4"
},
{
"description": "",
"range": "6.7.0.0/16"
}
],
"asn": 64514
}
],
"creation_timestamp": "2021-09-16T07:40:45.385-07:00",
"description": "",
"id": "projects/cloudskiff-dev-raphael/regions/us-central1/routers/test-router-2",
"name": "test-router-2",
"network": "https://www.googleapis.com/compute/v1/projects/cloudskiff-dev-raphael/global/networks/my-network",
"project": "cloudskiff-dev-raphael",
"region": "us-central1",
"self_link": "https://www.googleapis.com/compute/v1/projects/cloudskiff-dev-raphael/regions/us-central1/routers/test-router-2",
"timeouts": null
},
"sensitive_attributes": [],
"private": "eyJlMmJmYjczMC1lY2FhLTExZTYtOGY4OC0zNDM2M2JjN2M0YzAiOnsiY3JlYXRlIjoyNDAwMDAwMDAwMDAsImRlbGV0ZSI6MjQwMDAwMDAwMDAwLCJ1cGRhdGUiOjI0MDAwMDAwMDAwMH19",
"dependencies": [
"google_compute_network.foobar"
]
}
]
}
]
}

View File

@ -46,7 +46,7 @@ func (e *GoogleComputeFirewallEnumerator) Enumerate() ([]*resource.Resource, err
results,
e.factory.CreateAbstractResource(
string(e.SupportedType()),
strings.TrimPrefix(res.GetName(), "//compute.googleapis.com/"),
trimResourceName(res.GetName()),
map[string]interface{}{
"name": res.DisplayName,
"project": project,

View File

@ -0,0 +1,47 @@
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 GoogleComputeInstanceEnumerator struct {
repository repository.AssetRepository
factory resource.ResourceFactory
}
func NewGoogleComputeInstanceEnumerator(repo repository.AssetRepository, factory resource.ResourceFactory) *GoogleComputeInstanceEnumerator {
return &GoogleComputeInstanceEnumerator{
repository: repo,
factory: factory,
}
}
func (e *GoogleComputeInstanceEnumerator) SupportedType() resource.ResourceType {
return google.GoogleComputeInstanceResourceType
}
func (e *GoogleComputeInstanceEnumerator) Enumerate() ([]*resource.Resource, error) {
resources, err := e.repository.SearchAllInstances()
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{}{},
),
)
}
return results, err
}

View File

@ -0,0 +1,46 @@
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 GoogleComputeRouterEnumerator struct {
repository repository.AssetRepository
factory resource.ResourceFactory
}
func NewGoogleComputeRouterEnumerator(repo repository.AssetRepository, factory resource.ResourceFactory) *GoogleComputeRouterEnumerator {
return &GoogleComputeRouterEnumerator{
repository: repo,
factory: factory,
}
}
func (e *GoogleComputeRouterEnumerator) SupportedType() resource.ResourceType {
return google.GoogleComputeRouterResourceType
}
func (e *GoogleComputeRouterEnumerator) Enumerate() ([]*resource.Resource, error) {
resources, err := e.repository.SearchAllRouters()
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{}{},
),
)
}
return results, err
}

View File

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

View File

@ -15,11 +15,15 @@ import (
const (
storageBucketAssetType = "storage.googleapis.com/Bucket"
computeFirewallAssetType = "compute.googleapis.com/Firewall"
computeRouterAssetType = "compute.googleapis.com/Router"
computeInstanceAssetType = "compute.googleapis.com/Instance"
)
type AssetRepository interface {
SearchAllBuckets() ([]*assetpb.ResourceSearchResult, error)
SearchAllFirewalls() ([]*assetpb.ResourceSearchResult, error)
SearchAllRouters() ([]*assetpb.ResourceSearchResult, error)
SearchAllInstances() ([]*assetpb.ResourceSearchResult, error)
}
type assetRepository struct {
@ -44,6 +48,8 @@ func (s assetRepository) searchAllResources(ty string) ([]*assetpb.ResourceSearc
AssetTypes: []string{
storageBucketAssetType,
computeFirewallAssetType,
computeRouterAssetType,
computeInstanceAssetType,
},
}
var results []*assetpb.ResourceSearchResult
@ -86,3 +92,11 @@ func (s assetRepository) SearchAllBuckets() ([]*assetpb.ResourceSearchResult, er
func (s assetRepository) SearchAllFirewalls() ([]*assetpb.ResourceSearchResult, error) {
return s.searchAllResources(computeFirewallAssetType)
}
func (s assetRepository) SearchAllRouters() ([]*assetpb.ResourceSearchResult, error) {
return s.searchAllResources(computeRouterAssetType)
}
func (s assetRepository) SearchAllInstances() ([]*assetpb.ResourceSearchResult, error) {
return s.searchAllResources(computeInstanceAssetType)
}

10
pkg/remote/google/util.go Normal file
View File

@ -0,0 +1,10 @@
package google
import (
"regexp"
)
func trimResourceName(name string) string {
re, _ := regexp.Compile(`^\/\/[\w]+.googleapis.com\/`)
return re.ReplaceAllString(name, "")
}

View File

@ -148,3 +148,231 @@ func TestGoogleComputeFirewall(t *testing.T) {
})
}
}
func TestGoogleComputeRouter(t *testing.T) {
cases := []struct {
test string
response []*assetpb.ResourceSearchResult
responseErr error
setupAlerterMock func(alerter *mocks.AlerterInterface)
wantErr error
assertExpected func(t *testing.T, got []*resource.Resource)
}{
{
test: "no compute router",
response: []*assetpb.ResourceSearchResult{},
wantErr: nil,
assertExpected: func(t *testing.T, got []*resource.Resource) {
assert.Len(t, got, 0)
},
},
{
test: "multiples compute routers",
response: []*assetpb.ResourceSearchResult{
{
AssetType: "compute.googleapis.com/Router",
DisplayName: "test-router-0",
Name: "//compute.googleapis.com/projects/cloudskiff-dev-raphael/regions/us-central1/routers/test-router-0",
},
{
AssetType: "compute.googleapis.com/Router",
DisplayName: "test-router-1",
Name: "//compute.googleapis.com/projects/cloudskiff-dev-raphael/regions/us-central1/routers/test-router-1",
},
{
AssetType: "compute.googleapis.com/Router",
DisplayName: "test-router-2",
Name: "//compute.googleapis.com/projects/cloudskiff-dev-raphael/regions/us-central1/routers/test-router-2",
},
},
wantErr: nil,
assertExpected: func(t *testing.T, got []*resource.Resource) {
assert.Len(t, got, 3)
assert.Equal(t, got[0].ResourceId(), "projects/cloudskiff-dev-raphael/regions/us-central1/routers/test-router-0")
assert.Equal(t, got[0].ResourceType(), googleresource.GoogleComputeRouterResourceType)
assert.Equal(t, got[1].ResourceId(), "projects/cloudskiff-dev-raphael/regions/us-central1/routers/test-router-1")
assert.Equal(t, got[1].ResourceType(), googleresource.GoogleComputeRouterResourceType)
assert.Equal(t, got[2].ResourceId(), "projects/cloudskiff-dev-raphael/regions/us-central1/routers/test-router-2")
assert.Equal(t, got[2].ResourceType(), googleresource.GoogleComputeRouterResourceType)
},
},
{
test: "should return access denied error",
wantErr: nil,
responseErr: status.Error(codes.PermissionDenied, "The caller does not have permission"),
setupAlerterMock: func(alerter *mocks.AlerterInterface) {
alerter.On(
"SendAlert",
googleresource.GoogleComputeRouterResourceType,
alerts.NewRemoteAccessDeniedAlert(
common.RemoteGoogleTerraform,
remoteerr.NewResourceListingError(
status.Error(codes.PermissionDenied, "The caller does not have permission"),
googleresource.GoogleComputeRouterResourceType,
),
alerts.EnumerationPhase,
),
).Once()
},
assertExpected: func(t *testing.T, got []*resource.Resource) {
assert.Len(t, got, 0)
},
},
}
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.NewFakeAssetServer(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.NewGoogleComputeRouterEnumerator(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, c.wantErr, err)
if err != nil {
return
}
alerter.AssertExpectations(tt)
testFilter.AssertExpectations(tt)
if c.assertExpected != nil {
c.assertExpected(t, got)
}
})
}
}
func TestGoogleComputeInstance(t *testing.T) {
cases := []struct {
test string
assertExpected func(t *testing.T, got []*resource.Resource)
response []*assetpb.ResourceSearchResult
responseErr error
setupAlerterMock func(alerter *mocks.AlerterInterface)
wantErr error
}{
{
test: "no compute instance",
response: []*assetpb.ResourceSearchResult{},
assertExpected: func(t *testing.T, got []*resource.Resource) {
assert.Len(t, got, 0)
},
},
{
test: "multiples compute instances",
assertExpected: func(t *testing.T, got []*resource.Resource) {
assert.Len(t, got, 1)
assert.Equal(t, "projects/cloudskiff-dev-elie/zones/us-central1-a/instances/test", got[0].ResourceId())
assert.Equal(t, "google_compute_instance", got[0].ResourceType())
},
response: []*assetpb.ResourceSearchResult{
{
AssetType: "compute.googleapis.com/Instance",
Name: "//compute.googleapis.com/projects/cloudskiff-dev-elie/zones/us-central1-a/instances/test",
},
},
},
{
test: "cannot list compute firewall",
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_instance",
alerts.NewRemoteAccessDeniedAlert(
common.RemoteGoogleTerraform,
remoteerr.NewResourceListingError(
status.Error(codes.PermissionDenied, "The caller does not have permission"),
"google_compute_instance",
),
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{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)
}
repo := repository.NewAssetRepository(assetClient, realProvider.GetConfig(), cache.New(0))
remoteLibrary.AddEnumerator(google.NewGoogleComputeInstanceEnumerator(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)
}
})
}
}

View File

@ -0,0 +1,3 @@
package google
const GoogleComputeInstanceResourceType = "google_compute_instance"

View File

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

View File

@ -0,0 +1,15 @@
package google
import "github.com/cloudskiff/driftctl/pkg/resource"
const GoogleComputeRouterResourceType = "google_compute_router"
func initGoogleComputeRouterMetadata(resourceSchemaRepository resource.SchemaRepositoryInterface) {
resourceSchemaRepository.SetResolveReadAttributesFunc(GoogleComputeRouterResourceType, func(res *resource.Resource) map[string]string {
return map[string]string{
"name": *res.Attrs.GetString("name"),
"region": *res.Attrs.GetString("region"),
"project": *res.Attrs.GetString("project"),
}
})
}

View File

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

View File

@ -5,4 +5,5 @@ import "github.com/cloudskiff/driftctl/pkg/resource"
func InitResourcesMetadata(resourceSchemaRepository resource.SchemaRepositoryInterface) {
initGoogleStorageBucketMetadata(resourceSchemaRepository)
initGoogleComputeFirewallMetadata(resourceSchemaRepository)
initGoogleComputeRouterMetadata(resourceSchemaRepository)
}

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,26 @@
provider "google" {}
terraform {
required_version = "~> 0.15.0"
required_providers {
google = {
version = "3.78.0"
}
}
}
resource "google_compute_instance" "default" {
name = "test"
machine_type = "e2-medium"
zone = "us-central1-a"
boot_disk {
initialize_params {
image = "debian-cloud/debian-9"
}
}
network_interface {
network = "default"
}
}

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,31 @@
provider "google" {}
terraform {
required_version = "~> 0.15.0"
required_providers {
google = {
version = "3.78.0"
}
}
}
resource "google_compute_network" "default" {
name = "compute-firewall"
}
resource "google_compute_router" "router-0" {
name = "test-router-0"
region = "us-central1"
network = google_compute_network.default.name
bgp {
asn = 64514
advertise_mode = "CUSTOM"
advertised_groups = ["ALL_SUBNETS"]
advertised_ip_ranges {
range = "1.2.3.4"
}
advertised_ip_ranges {
range = "6.7.0.0/16"
}
}
}

View File

@ -68,6 +68,8 @@ var supportedTypes = map[string]struct{}{
"google_storage_bucket": {},
"google_compute_firewall": {},
"google_compute_router": {},
"google_compute_instance": {},
"azurerm_storage_account": {},
"azurerm_storage_container": {},