feat: add google_compute_health_check

main
sundowndev 2021-10-27 17:05:23 +02:00
parent 9ae26aba17
commit 6f117efdb2
15 changed files with 378 additions and 0 deletions

View File

@ -355,6 +355,7 @@ func TestTerraformStateReader_Google_Resources(t *testing.T) {
{name: "bigtable instance", dirName: "google_bigtable_instance", wantErr: false},
{name: "bigtable table", dirName: "google_bigtable_table", wantErr: false},
{name: "sql database instance", dirName: "google_sql_database_instance", wantErr: false},
{name: "health check", dirName: "google_compute_health_check", wantErr: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

View File

@ -0,0 +1,35 @@
[
{
"Id": "projects/cloudskiff-dev-raphael/global/healthChecks/https-health-check",
"Type": "google_compute_health_check",
"Attrs": {
"check_interval_sec": 3,
"creation_timestamp": "2021-10-26T06:05:03.005-07:00",
"description": "Health check via https",
"healthy_threshold": 4,
"https_health_check": [
{
"host": "crvx.fr",
"port": 0,
"port_name": "health-check-port",
"port_specification": "USE_NAMED_PORT",
"proxy_header": "NONE",
"request_path": "/",
"response": "I AM HEALTHY"
}
],
"id": "projects/cloudskiff-dev-raphael/global/healthChecks/https-health-check",
"log_config": [
{
"enable": false
}
],
"name": "https-health-check",
"project": "cloudskiff-dev-raphael",
"self_link": "https://www.googleapis.com/compute/v1/projects/cloudskiff-dev-raphael/global/healthChecks/https-health-check",
"timeout_sec": 1,
"type": "HTTPS",
"unhealthy_threshold": 5
}
}
]

View File

@ -0,0 +1,57 @@
{
"version": 4,
"terraform_version": "0.15.5",
"serial": 492,
"lineage": "9566e18d-6080-4aa8-e9a6-4c38905cf68f",
"outputs": {},
"resources": [
{
"mode": "managed",
"type": "google_compute_health_check",
"name": "https-health-check",
"provider": "provider[\"registry.terraform.io/hashicorp/google\"]",
"instances": [
{
"schema_version": 0,
"attributes": {
"check_interval_sec": 3,
"creation_timestamp": "2021-10-26T06:05:03.005-07:00",
"description": "Health check via https",
"grpc_health_check": [],
"healthy_threshold": 4,
"http2_health_check": [],
"http_health_check": [],
"https_health_check": [
{
"host": "crvx.fr",
"port": 0,
"port_name": "health-check-port",
"port_specification": "USE_NAMED_PORT",
"proxy_header": "NONE",
"request_path": "/",
"response": "I AM HEALTHY"
}
],
"id": "projects/cloudskiff-dev-raphael/global/healthChecks/https-health-check",
"log_config": [
{
"enable": false
}
],
"name": "https-health-check",
"project": "cloudskiff-dev-raphael",
"self_link": "https://www.googleapis.com/compute/v1/projects/cloudskiff-dev-raphael/global/healthChecks/https-health-check",
"ssl_health_check": [],
"tcp_health_check": [],
"timeout_sec": 1,
"timeouts": null,
"type": "HTTPS",
"unhealthy_threshold": 5
},
"sensitive_attributes": [],
"private": "eyJlMmJmYjczMC1lY2FhLTExZTYtOGY4OC0zNDM2M2JjN2M0YzAiOnsiY3JlYXRlIjoyNDAwMDAwMDAwMDAsImRlbGV0ZSI6MjQwMDAwMDAwMDAwLCJ1cGRhdGUiOjI0MDAwMDAwMDAwMH19"
}
]
}
]
}

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 GoogleComputeHealthCheckEnumerator struct {
repository repository.AssetRepository
factory resource.ResourceFactory
}
func NewGoogleComputeHealthCheckEnumerator(repo repository.AssetRepository, factory resource.ResourceFactory) *GoogleComputeHealthCheckEnumerator {
return &GoogleComputeHealthCheckEnumerator{
repository: repo,
factory: factory,
}
}
func (e *GoogleComputeHealthCheckEnumerator) SupportedType() resource.ResourceType {
return google.GoogleComputeHealthCheckResourceType
}
func (e *GoogleComputeHealthCheckEnumerator) Enumerate() ([]*resource.Resource, error) {
checks, err := e.repository.SearchAllHealthChecks()
if err != nil {
return nil, remoteerror.NewResourceListingError(err, string(e.SupportedType()))
}
results := make([]*resource.Resource, 0, len(checks))
for _, res := range checks {
results = append(
results,
e.factory.CreateAbstractResource(
string(e.SupportedType()),
trimResourceName(res.GetName()),
map[string]interface{}{
"display_name": res.GetDisplayName(),
},
),
)
}
return results, err
}

View File

@ -95,6 +95,7 @@ func Init(version string, alerter *alerter.Alerter,
remoteLibrary.AddEnumerator(NewGoogleBigTableInstanceEnumerator(assetRepository, factory))
remoteLibrary.AddEnumerator(NewGoogleBigtableTableEnumerator(assetRepository, factory))
remoteLibrary.AddEnumerator(NewGoogleSQLDatabaseInstanceEnumerator(assetRepository, factory))
remoteLibrary.AddEnumerator(NewGoogleComputeHealthCheckEnumerator(assetRepository, factory))
err = resourceSchemaRepository.Init(terraform.GOOGLE, provider.Version(), provider.Schema())
if err != nil {

View File

@ -30,6 +30,7 @@ const (
bigtableInstanceAssetType = "bigtableadmin.googleapis.com/Instance"
bigtableTableAssetType = "bigtableadmin.googleapis.com/Table"
sqlDatabaseInstanceAssetType = "sqladmin.googleapis.com/Instance"
healthCheckAssetType = "compute.googleapis.com/HealthCheck"
)
type AssetRepository interface {
@ -50,6 +51,7 @@ type AssetRepository interface {
SearchAllBigtableInstances() ([]*assetpb.Asset, error)
SearchAllBigtableTables() ([]*assetpb.Asset, error)
SearchAllSQLDatabaseInstances() ([]*assetpb.Asset, error)
SearchAllHealthChecks() ([]*assetpb.ResourceSearchResult, error)
}
type assetRepository struct {
@ -128,6 +130,7 @@ func (s assetRepository) searchAllResources(ty string) ([]*assetpb.ResourceSearc
computeAddressAssetType,
computeDiskAssetType,
computeImageAssetType,
healthCheckAssetType,
},
}
var results []*assetpb.ResourceSearchResult
@ -231,3 +234,7 @@ func (s assetRepository) SearchAllBigtableTables() ([]*assetpb.Asset, error) {
func (s assetRepository) SearchAllSQLDatabaseInstances() ([]*assetpb.Asset, error) {
return s.listAllResources(sqlDatabaseInstanceAssetType)
}
func (s assetRepository) SearchAllHealthChecks() ([]*assetpb.ResourceSearchResult, error) {
return s.searchAllResources(healthCheckAssetType)
}

View File

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

View File

@ -1083,3 +1083,113 @@ func TestGoogleComputeImage(t *testing.T) {
})
}
}
func TestGoogleComputeHealthCheck(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 health check",
response: []*assetpb.ResourceSearchResult{},
assertExpected: func(t *testing.T, got []*resource.Resource) {
assert.Len(t, got, 0)
},
},
{
test: "multiples compute health checks",
assertExpected: func(t *testing.T, got []*resource.Resource) {
assert.Len(t, got, 2)
assert.Equal(t, "projects/cloudskiff-dev-raphael/global/healthChecks/test-health-check-1", got[0].ResourceId())
assert.Equal(t, "google_compute_health_check", got[0].ResourceType())
assert.Equal(t, "projects/cloudskiff-dev-raphael/global/healthChecks/test-health-check-2", got[1].ResourceId())
assert.Equal(t, "google_compute_health_check", got[1].ResourceType())
},
response: []*assetpb.ResourceSearchResult{
{
AssetType: "compute.googleapis.com/HealthCheck",
Name: "//compute.googleapis.com/projects/cloudskiff-dev-raphael/global/healthChecks/test-health-check-1",
},
{
AssetType: "compute.googleapis.com/HealthCheck",
Name: "//compute.googleapis.com/projects/cloudskiff-dev-raphael/global/healthChecks/test-health-check-2",
},
},
},
{
test: "cannot list compute health checks",
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_health_check",
alerts.NewRemoteAccessDeniedAlert(
common.RemoteGoogleTerraform,
remoteerr.NewResourceListingError(
status.Error(codes.PermissionDenied, "The caller does not have permission"),
"google_compute_health_check",
),
alerts.EnumerationPhase,
),
).Once()
},
},
}
providerVersion := "3.78.0"
schemaRepository := testresource.InitFakeSchemaRepository(terraform.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.NewGoogleComputeHealthCheckEnumerator(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,13 @@
package google
import "github.com/cloudskiff/driftctl/pkg/resource"
const GoogleComputeHealthCheckResourceType = "google_compute_health_check"
func initGoogleComputeHealthCheckMetadata(resourceSchemaRepository resource.SchemaRepositoryInterface) {
resourceSchemaRepository.SetHumanReadableAttributesFunc(GoogleComputeHealthCheckResourceType, func(res *resource.Resource) map[string]string {
return map[string]string{
"Name": *res.Attributes().GetString("display_name"),
}
})
}

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_ComputeHealthCheck(t *testing.T) {
acceptance.Run(t, acceptance.AccTestCase{
TerraformVersion: "0.15.5",
Paths: []string{"./testdata/acc/google_compute_health_check"},
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(1)
},
},
},
})
}

View File

@ -16,4 +16,5 @@ func InitResourcesMetadata(resourceSchemaRepository resource.SchemaRepositoryInt
initGoogleComputeSubnetworkMetadata(resourceSchemaRepository)
initGoogleComputeDiskMetadata(resourceSchemaRepository)
initGoogleComputeImageMetadata(resourceSchemaRepository)
initGoogleComputeHealthCheckMetadata(resourceSchemaRepository)
}

View File

@ -0,0 +1,2 @@
*
!google_compute_health_check

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,29 @@
provider "google" {}
terraform {
required_version = "~> 0.15.0"
required_providers {
google = {
version = "3.78.0"
}
}
}
resource "google_compute_health_check" "https-health-check" {
name = "https-health-check"
description = "Health check via https"
timeout_sec = 1
check_interval_sec = 3
healthy_threshold = 4
unhealthy_threshold = 5
https_health_check {
port_name = "health-check-port"
port_specification = "USE_NAMED_PORT"
host = "google.com"
request_path = "/"
proxy_header = "NONE"
response = "I AM HEALTHY"
}
}

View File

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