test: azurerm_postgresql_database
parent
fd3a0601db
commit
b4b04f89f9
|
@ -1,10 +1,12 @@
|
|||
package azurerm
|
||||
|
||||
import (
|
||||
"github.com/Azure/go-autorest/autorest/azure"
|
||||
"github.com/cloudskiff/driftctl/pkg/remote/azurerm/repository"
|
||||
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
|
||||
"github.com/cloudskiff/driftctl/pkg/resource"
|
||||
"github.com/cloudskiff/driftctl/pkg/resource/azurerm"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type AzurermPostgresqlDatabaseEnumerator struct {
|
||||
|
@ -31,7 +33,16 @@ func (e *AzurermPostgresqlDatabaseEnumerator) Enumerate() ([]*resource.Resource,
|
|||
|
||||
results := make([]*resource.Resource, 0)
|
||||
for _, server := range servers {
|
||||
databases, err := e.repository.ListAllDatabasesByServer(trimResourceGroupName(*server.ID), *server.Name)
|
||||
res, err := azure.ParseResourceID(*server.ID)
|
||||
if err != nil {
|
||||
logrus.WithFields(map[string]interface{}{
|
||||
"type": azurerm.AzurePostgresqlServerResourceType,
|
||||
"id": *server.ID,
|
||||
}).Errorf("Error listing %s: failed to parse resource ID", string(e.SupportedType()))
|
||||
continue
|
||||
}
|
||||
|
||||
databases, err := e.repository.ListAllDatabasesByServer(res.ResourceGroup, server)
|
||||
if err != nil {
|
||||
return nil, remoteerror.NewResourceListingError(err, string(e.SupportedType()))
|
||||
}
|
||||
|
|
|
@ -12,13 +12,13 @@ type MockPostgresqlRespository struct {
|
|||
mock.Mock
|
||||
}
|
||||
|
||||
// ListAllDatabasesByServer provides a mock function with given fields: resGroup, serverName
|
||||
func (_m *MockPostgresqlRespository) ListAllDatabasesByServer(resGroup string, serverName string) ([]*armpostgresql.Database, error) {
|
||||
ret := _m.Called(resGroup, serverName)
|
||||
// ListAllDatabasesByServer provides a mock function with given fields: resGroup, server
|
||||
func (_m *MockPostgresqlRespository) ListAllDatabasesByServer(resGroup string, server *armpostgresql.Server) ([]*armpostgresql.Database, error) {
|
||||
ret := _m.Called(resGroup, server)
|
||||
|
||||
var r0 []*armpostgresql.Database
|
||||
if rf, ok := ret.Get(0).(func(string, string) []*armpostgresql.Database); ok {
|
||||
r0 = rf(resGroup, serverName)
|
||||
if rf, ok := ret.Get(0).(func(string, *armpostgresql.Server) []*armpostgresql.Database); ok {
|
||||
r0 = rf(resGroup, server)
|
||||
} else {
|
||||
if ret.Get(0) != nil {
|
||||
r0 = ret.Get(0).([]*armpostgresql.Database)
|
||||
|
@ -26,8 +26,8 @@ func (_m *MockPostgresqlRespository) ListAllDatabasesByServer(resGroup string, s
|
|||
}
|
||||
|
||||
var r1 error
|
||||
if rf, ok := ret.Get(1).(func(string, string) error); ok {
|
||||
r1 = rf(resGroup, serverName)
|
||||
if rf, ok := ret.Get(1).(func(string, *armpostgresql.Server) error); ok {
|
||||
r1 = rf(resGroup, server)
|
||||
} else {
|
||||
r1 = ret.Error(1)
|
||||
}
|
||||
|
|
|
@ -12,7 +12,7 @@ import (
|
|||
|
||||
type PostgresqlRespository interface {
|
||||
ListAllServers() ([]*armpostgresql.Server, error)
|
||||
ListAllDatabasesByServer(resGroup string, serverName string) ([]*armpostgresql.Database, error)
|
||||
ListAllDatabasesByServer(resGroup string, server *armpostgresql.Server) ([]*armpostgresql.Database, error)
|
||||
}
|
||||
|
||||
type postgresqlServersClientImpl struct {
|
||||
|
@ -70,13 +70,13 @@ func (s *postgresqlRepository) ListAllServers() ([]*armpostgresql.Server, error)
|
|||
return res.Value, nil
|
||||
}
|
||||
|
||||
func (s *postgresqlRepository) ListAllDatabasesByServer(resGroup string, serverName string) ([]*armpostgresql.Database, error) {
|
||||
cacheKey := fmt.Sprintf("postgresqlListAllDatabases_%s_%s", resGroup, serverName)
|
||||
func (s *postgresqlRepository) ListAllDatabasesByServer(resGroup string, server *armpostgresql.Server) ([]*armpostgresql.Database, error) {
|
||||
cacheKey := fmt.Sprintf("postgresqlListAllDatabases_%s_%s", resGroup, *server.Name)
|
||||
if v := s.cache.Get(cacheKey); v != nil {
|
||||
return v.([]*armpostgresql.Database), nil
|
||||
}
|
||||
|
||||
res, err := s.databaseClient.ListByServer(context.Background(), resGroup, serverName, &armpostgresql.DatabasesListByServerOptions{})
|
||||
res, err := s.databaseClient.ListByServer(context.Background(), resGroup, *server.Name, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -129,7 +129,7 @@ func Test_Postgresql_ListAllDatabases(t *testing.T) {
|
|||
{
|
||||
name: "should return postgres servers",
|
||||
mocks: func(client *mockPostgresqlDatabaseClient, mockCache *cache.MockCache) {
|
||||
client.On("ListByServer", context.Background(), "res-group", "server", &armpostgresql.DatabasesListByServerOptions{}).Return(armpostgresql.DatabasesListByServerResponse{
|
||||
client.On("ListByServer", context.Background(), "res-group", "server", (*armpostgresql.DatabasesListByServerOptions)(nil)).Return(armpostgresql.DatabasesListByServerResponse{
|
||||
DatabasesListByServerResult: armpostgresql.DatabasesListByServerResult{
|
||||
DatabaseListResult: armpostgresql.DatabaseListResult{
|
||||
Value: expectedResults,
|
||||
|
@ -154,7 +154,7 @@ func Test_Postgresql_ListAllDatabases(t *testing.T) {
|
|||
mocks: func(client *mockPostgresqlDatabaseClient, mockCache *cache.MockCache) {
|
||||
mockCache.On("Get", "postgresqlListAllDatabases_res-group_server").Return(nil).Times(1)
|
||||
|
||||
client.On("ListByServer", context.Background(), "res-group", "server", &armpostgresql.DatabasesListByServerOptions{}).Return(armpostgresql.DatabasesListByServerResponse{}, errors.New("remote error")).Times(1)
|
||||
client.On("ListByServer", context.Background(), "res-group", "server", (*armpostgresql.DatabasesListByServerOptions)(nil)).Return(armpostgresql.DatabasesListByServerResponse{}, errors.New("remote error")).Times(1)
|
||||
},
|
||||
wantErr: "remote error",
|
||||
},
|
||||
|
@ -171,7 +171,14 @@ func Test_Postgresql_ListAllDatabases(t *testing.T) {
|
|||
databaseClient: fakeClient,
|
||||
cache: mockCache,
|
||||
}
|
||||
got, err := s.ListAllDatabasesByServer("res-group", "server")
|
||||
got, err := s.ListAllDatabasesByServer("res-group", &armpostgresql.Server{
|
||||
TrackedResource: armpostgresql.TrackedResource{
|
||||
Resource: armpostgresql.Resource{
|
||||
ID: to.StringPtr("server"),
|
||||
Name: to.StringPtr("server"),
|
||||
},
|
||||
},
|
||||
})
|
||||
if tt.wantErr != "" {
|
||||
assert.EqualError(t, err, tt.wantErr)
|
||||
} else {
|
||||
|
|
|
@ -1,11 +0,0 @@
|
|||
package azurerm
|
||||
|
||||
import "regexp"
|
||||
|
||||
func trimResourceGroupName(name string) string {
|
||||
re, _ := regexp.Compile(`\/resourceGroups\/(?P<group>[\w-_]+)\/?`)
|
||||
if m := re.FindStringSubmatch(name); len(m) > 0 {
|
||||
return m[len(m)-1]
|
||||
}
|
||||
return ""
|
||||
}
|
|
@ -1,43 +0,0 @@
|
|||
package azurerm
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestAzurermUtil_trimResourceGroupName(t *testing.T) {
|
||||
testcases := []struct {
|
||||
name string
|
||||
resourceId string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
name: "should return resource's group name",
|
||||
resourceId: "/subscriptions/7bfb2c5c-7308-46ed-8ae4-fffa356eb406/resourceGroups/api-rg-pro/providers/Microsoft.DBforPostgreSQL/servers/postgresql-server-8791542",
|
||||
expected: "api-rg-pro",
|
||||
},
|
||||
{
|
||||
name: "should return resource's group name",
|
||||
resourceId: "/subscriptions/7bfb2c5c-7308-46ed-8ae4-fffa356eb406/resourceGroups/api-rg-pro",
|
||||
expected: "api-rg-pro",
|
||||
},
|
||||
{
|
||||
name: "should return resource's group name",
|
||||
resourceId: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/foobar/providers/Microsoft.Storage/storageAccounts/testeliedriftctl",
|
||||
expected: "foobar",
|
||||
},
|
||||
{
|
||||
name: "should return resource's group name",
|
||||
resourceId: "/subscriptions/00000000-0000-0000-0000-000000000000/test/foobar/providers/Microsoft.Storage/storageAccounts/testeliedriftctl",
|
||||
expected: "",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range testcases {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := trimResourceGroupName(tt.resourceId)
|
||||
assert.Equal(t, tt.expected, got)
|
||||
})
|
||||
}
|
||||
}
|
|
@ -10,7 +10,7 @@ import (
|
|||
"github.com/cloudskiff/driftctl/pkg/remote/azurerm"
|
||||
"github.com/cloudskiff/driftctl/pkg/remote/azurerm/repository"
|
||||
"github.com/cloudskiff/driftctl/pkg/remote/common"
|
||||
error2 "github.com/cloudskiff/driftctl/pkg/remote/error"
|
||||
remoteerr "github.com/cloudskiff/driftctl/pkg/remote/error"
|
||||
"github.com/cloudskiff/driftctl/pkg/resource"
|
||||
resourceazure "github.com/cloudskiff/driftctl/pkg/resource/azurerm"
|
||||
"github.com/cloudskiff/driftctl/pkg/terraform"
|
||||
|
@ -44,7 +44,7 @@ func TestAzurermPostgresqlServer(t *testing.T) {
|
|||
mocks: func(repository *repository.MockPostgresqlRespository, alerter *mocks.AlerterInterface) {
|
||||
repository.On("ListAllServers").Return(nil, dummyError)
|
||||
},
|
||||
wantErr: error2.NewResourceListingError(dummyError, resourceazure.AzurePostgresqlServerResourceType),
|
||||
wantErr: remoteerr.NewResourceListingError(dummyError, resourceazure.AzurePostgresqlServerResourceType),
|
||||
},
|
||||
{
|
||||
test: "multiple postgres servers",
|
||||
|
@ -141,7 +141,7 @@ func TestAzurermPostgresqlDatabase(t *testing.T) {
|
|||
mocks: func(repository *repository.MockPostgresqlRespository, alerter *mocks.AlerterInterface) {
|
||||
repository.On("ListAllServers").Return(nil, dummyError)
|
||||
},
|
||||
wantErr: error2.NewResourceListingErrorWithType(dummyError, resourceazure.AzurePostgresqlDatabaseResourceType, resourceazure.AzurePostgresqlServerResourceType),
|
||||
wantErr: remoteerr.NewResourceListingErrorWithType(dummyError, resourceazure.AzurePostgresqlDatabaseResourceType, resourceazure.AzurePostgresqlServerResourceType),
|
||||
},
|
||||
{
|
||||
test: "error listing postgres databases",
|
||||
|
@ -157,9 +157,9 @@ func TestAzurermPostgresqlDatabase(t *testing.T) {
|
|||
},
|
||||
}, nil).Once()
|
||||
|
||||
repository.On("ListAllDatabasesByServer", "api-rg-pro", "postgresql-server-8791542").Return(nil, dummyError).Once()
|
||||
repository.On("ListAllDatabasesByServer", "api-rg-pro", mock.IsType(&armpostgresql.Server{})).Return(nil, dummyError).Once()
|
||||
},
|
||||
wantErr: error2.NewResourceListingError(dummyError, resourceazure.AzurePostgresqlDatabaseResourceType),
|
||||
wantErr: remoteerr.NewResourceListingError(dummyError, resourceazure.AzurePostgresqlDatabaseResourceType),
|
||||
},
|
||||
{
|
||||
test: "multiple postgres databases",
|
||||
|
@ -175,7 +175,7 @@ func TestAzurermPostgresqlDatabase(t *testing.T) {
|
|||
},
|
||||
}, nil).Once()
|
||||
|
||||
repository.On("ListAllDatabasesByServer", "api-rg-pro", "postgresql-server-8791542").Return([]*armpostgresql.Database{
|
||||
repository.On("ListAllDatabasesByServer", "api-rg-pro", mock.IsType(&armpostgresql.Server{})).Return([]*armpostgresql.Database{
|
||||
{
|
||||
ProxyResource: armpostgresql.ProxyResource{
|
||||
Resource: armpostgresql.Resource{
|
||||
|
|
|
@ -14,7 +14,7 @@ func TestAcc_Azure_PostgresqlDatabase(t *testing.T) {
|
|||
Args: []string{
|
||||
"scan",
|
||||
"--to", "azure+tf",
|
||||
"--filter", "Type=='azurerm_postgresql_database' && contains(Id, 'acc-test-db')",
|
||||
"--filter", "contains(Id, 'acc-test-db')",
|
||||
},
|
||||
Checks: []acceptance.AccCheck{
|
||||
{
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
*
|
||||
!azurerm_postgresql_database
|
|
@ -12,7 +12,7 @@ provider "azurerm" {
|
|||
}
|
||||
|
||||
data "azurerm_resource_group" "qa1" {
|
||||
name = "raphael-dev" // driftctl-qa-1
|
||||
name = "driftctl-qa-1"
|
||||
}
|
||||
|
||||
resource "random_string" "suffix" {
|
||||
|
|
Loading…
Reference in New Issue