Add azurerm_virtual_network
parent
5c6a1e7106
commit
1fa17d2591
2
go.mod
2
go.mod
|
@ -6,8 +6,8 @@ require (
|
|||
cloud.google.com/go/asset v0.1.0
|
||||
github.com/Azure/azure-sdk-for-go/sdk/azcore v0.19.0
|
||||
github.com/Azure/azure-sdk-for-go/sdk/azidentity v0.11.0
|
||||
github.com/Azure/azure-sdk-for-go/sdk/network/armnetwork v0.3.0
|
||||
github.com/Azure/azure-sdk-for-go/sdk/storage/armstorage v0.2.0
|
||||
github.com/Azure/azure-sdk-for-go/sdk/to v0.1.4
|
||||
github.com/Azure/go-autorest/autorest v0.11.3
|
||||
github.com/aws/aws-sdk-go v1.38.68
|
||||
github.com/bmatcuk/doublestar/v4 v4.0.1
|
||||
|
|
4
go.sum
4
go.sum
|
@ -57,10 +57,10 @@ github.com/Azure/azure-sdk-for-go/sdk/azidentity v0.11.0 h1:OYa9vmRX2XC5GXRAzegg
|
|||
github.com/Azure/azure-sdk-for-go/sdk/azidentity v0.11.0/go.mod h1:HcM1YX14R7CJcghJGOYCgdezslRSVzqwLf/q+4Y2r/0=
|
||||
github.com/Azure/azure-sdk-for-go/sdk/internal v0.7.0 h1:v9p9TfTbf7AwNb5NYQt7hI41IfPoLFiFkLtb+bmGjT0=
|
||||
github.com/Azure/azure-sdk-for-go/sdk/internal v0.7.0/go.mod h1:yqy467j36fJxcRV2TzfVZ1pCb5vxm4BtZPUdYWe/Xo8=
|
||||
github.com/Azure/azure-sdk-for-go/sdk/network/armnetwork v0.3.0 h1:3ICM5L/XRaknp4DUNqdcNtiOzs6Mc3VKeyQp81+JS2Y=
|
||||
github.com/Azure/azure-sdk-for-go/sdk/network/armnetwork v0.3.0/go.mod h1:YSO+0IW+22kuLybFl2GAYaTDh1VWxNid83hqY/DkpGQ=
|
||||
github.com/Azure/azure-sdk-for-go/sdk/storage/armstorage v0.2.0 h1:LOq4ZG6rMgTAZTyGbYHyxL1EVfZdngpUDRY/KvBToMs=
|
||||
github.com/Azure/azure-sdk-for-go/sdk/storage/armstorage v0.2.0/go.mod h1:mIFJgQ93RCQPBsN2jBDzDOfwJpLacGwXIxmirNQMiq4=
|
||||
github.com/Azure/azure-sdk-for-go/sdk/to v0.1.4 h1:3w4gk+uYOwplGhID1fDP305/8bI5Aug3URoC1V493KU=
|
||||
github.com/Azure/azure-sdk-for-go/sdk/to v0.1.4/go.mod h1:UL/d4lvWAzSJUuX+19uKdN0ktyjoOyQhgY+HWNgtIYI=
|
||||
github.com/Azure/go-autorest v14.2.0+incompatible h1:V5VMDjClD3GiElqLWO7mz2MxNAK/vTfRHdAubSIPRgs=
|
||||
github.com/Azure/go-autorest v14.2.0+incompatible/go.mod h1:r+4oMnoxhatjLLJ6zxSWATqVooLgysK6ZNox3g/xq24=
|
||||
github.com/Azure/go-autorest/autorest v0.11.3 h1:fyYnmYujkIXUgv88D9/Wo2ybE4Zwd/TmQd5sSI5u2Ws=
|
||||
|
|
|
@ -0,0 +1,46 @@
|
|||
package azurerm
|
||||
|
||||
import (
|
||||
"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"
|
||||
)
|
||||
|
||||
type AzurermVirtualNetworkEnumerator struct {
|
||||
repository repository.NetworkRepository
|
||||
factory resource.ResourceFactory
|
||||
}
|
||||
|
||||
func NewAzurermVirtualNetworkEnumerator(repo repository.NetworkRepository, factory resource.ResourceFactory) *AzurermVirtualNetworkEnumerator {
|
||||
return &AzurermVirtualNetworkEnumerator{
|
||||
repository: repo,
|
||||
factory: factory,
|
||||
}
|
||||
}
|
||||
|
||||
func (e *AzurermVirtualNetworkEnumerator) SupportedType() resource.ResourceType {
|
||||
return azurerm.AzureVirtualNetworkResourceType
|
||||
}
|
||||
|
||||
func (e *AzurermVirtualNetworkEnumerator) Enumerate() ([]*resource.Resource, error) {
|
||||
resources, err := e.repository.ListAllVirtualNetworks()
|
||||
if err != nil {
|
||||
return nil, remoteerror.NewResourceListingError(err, string(e.SupportedType()))
|
||||
}
|
||||
|
||||
results := make([]*resource.Resource, len(resources))
|
||||
|
||||
for _, res := range resources {
|
||||
results = append(
|
||||
results,
|
||||
e.factory.CreateAbstractResource(
|
||||
string(e.SupportedType()),
|
||||
*res.ID,
|
||||
map[string]interface{}{},
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
return results, err
|
||||
}
|
|
@ -46,11 +46,13 @@ func Init(
|
|||
c := cache.New(100)
|
||||
|
||||
storageAccountRepo := repository.NewStorageRepository(con, providerConfig, c)
|
||||
networkRepo := repository.NewNetworkRepository(con, providerConfig, c)
|
||||
|
||||
providerLibrary.AddProvider(terraform.AZURE, provider)
|
||||
|
||||
remoteLibrary.AddEnumerator(NewAzurermStorageAccountEnumerator(storageAccountRepo, factory))
|
||||
remoteLibrary.AddEnumerator(NewAzurermStorageContainerEnumerator(storageAccountRepo, factory))
|
||||
remoteLibrary.AddEnumerator(NewAzurermVirtualNetworkEnumerator(networkRepo, factory))
|
||||
|
||||
err = resourceSchemaRepository.Init(terraform.AZURE, version, provider.Schema())
|
||||
if err != nil {
|
||||
|
|
|
@ -0,0 +1,36 @@
|
|||
// Code generated by mockery v0.0.0-dev. DO NOT EDIT.
|
||||
|
||||
package repository
|
||||
|
||||
import (
|
||||
armnetwork "github.com/Azure/azure-sdk-for-go/sdk/network/armnetwork"
|
||||
mock "github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
// MockNetworkRepository is an autogenerated mock type for the NetworkRepository type
|
||||
type MockNetworkRepository struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
// ListAllVirtualNetwork provides a mock function with given fields:
|
||||
func (_m *MockNetworkRepository) ListAllVirtualNetworks() ([]*armnetwork.VirtualNetwork, error) {
|
||||
ret := _m.Called()
|
||||
|
||||
var r0 []*armnetwork.VirtualNetwork
|
||||
if rf, ok := ret.Get(0).(func() []*armnetwork.VirtualNetwork); ok {
|
||||
r0 = rf()
|
||||
} else {
|
||||
if ret.Get(0) != nil {
|
||||
r0 = ret.Get(0).([]*armnetwork.VirtualNetwork)
|
||||
}
|
||||
}
|
||||
|
||||
var r1 error
|
||||
if rf, ok := ret.Get(1).(func() error); ok {
|
||||
r1 = rf()
|
||||
} else {
|
||||
r1 = ret.Error(1)
|
||||
}
|
||||
|
||||
return r0, r1
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
// Code generated by mockery v0.0.0-dev. DO NOT EDIT.
|
||||
|
||||
package repository
|
||||
|
||||
import (
|
||||
armnetwork "github.com/Azure/azure-sdk-for-go/sdk/network/armnetwork"
|
||||
mock "github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
// mockVirtualNetworkClient is an autogenerated mock type for the virtualNetworksClient type
|
||||
type mockVirtualNetworkClient struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
// ListAll provides a mock function with given fields: options
|
||||
func (_m *mockVirtualNetworkClient) ListAll(options *armnetwork.VirtualNetworksListAllOptions) virtualNetworksListAllPager {
|
||||
ret := _m.Called(options)
|
||||
|
||||
var r0 virtualNetworksListAllPager
|
||||
if rf, ok := ret.Get(0).(func(*armnetwork.VirtualNetworksListAllOptions) virtualNetworksListAllPager); ok {
|
||||
r0 = rf(options)
|
||||
} else {
|
||||
if ret.Get(0) != nil {
|
||||
r0 = ret.Get(0).(virtualNetworksListAllPager)
|
||||
}
|
||||
}
|
||||
|
||||
return r0
|
||||
}
|
|
@ -0,0 +1,58 @@
|
|||
// Code generated by mockery v0.0.0-dev. DO NOT EDIT.
|
||||
|
||||
package repository
|
||||
|
||||
import (
|
||||
context "context"
|
||||
|
||||
armnetwork "github.com/Azure/azure-sdk-for-go/sdk/network/armnetwork"
|
||||
|
||||
mock "github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
// mockVirtualNetworksListAllPager is an autogenerated mock type for the virtualNetworksListAllPager type
|
||||
type mockVirtualNetworksListAllPager struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
// Err provides a mock function with given fields:
|
||||
func (_m *mockVirtualNetworksListAllPager) Err() error {
|
||||
ret := _m.Called()
|
||||
|
||||
var r0 error
|
||||
if rf, ok := ret.Get(0).(func() error); ok {
|
||||
r0 = rf()
|
||||
} else {
|
||||
r0 = ret.Error(0)
|
||||
}
|
||||
|
||||
return r0
|
||||
}
|
||||
|
||||
// NextPage provides a mock function with given fields: ctx
|
||||
func (_m *mockVirtualNetworksListAllPager) NextPage(ctx context.Context) bool {
|
||||
ret := _m.Called(ctx)
|
||||
|
||||
var r0 bool
|
||||
if rf, ok := ret.Get(0).(func(context.Context) bool); ok {
|
||||
r0 = rf(ctx)
|
||||
} else {
|
||||
r0 = ret.Get(0).(bool)
|
||||
}
|
||||
|
||||
return r0
|
||||
}
|
||||
|
||||
// PageResponse provides a mock function with given fields:
|
||||
func (_m *mockVirtualNetworksListAllPager) PageResponse() armnetwork.VirtualNetworksListAllResponse {
|
||||
ret := _m.Called()
|
||||
|
||||
var r0 armnetwork.VirtualNetworksListAllResponse
|
||||
if rf, ok := ret.Get(0).(func() armnetwork.VirtualNetworksListAllResponse); ok {
|
||||
r0 = rf()
|
||||
} else {
|
||||
r0 = ret.Get(0).(armnetwork.VirtualNetworksListAllResponse)
|
||||
}
|
||||
|
||||
return r0
|
||||
}
|
|
@ -0,0 +1,68 @@
|
|||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
|
||||
"github.com/Azure/azure-sdk-for-go/sdk/network/armnetwork"
|
||||
"github.com/cloudskiff/driftctl/pkg/remote/azurerm/common"
|
||||
"github.com/cloudskiff/driftctl/pkg/remote/cache"
|
||||
)
|
||||
|
||||
type NetworkRepository interface {
|
||||
ListAllVirtualNetworks() ([]*armnetwork.VirtualNetwork, error)
|
||||
}
|
||||
|
||||
type virtualNetworksListAllPager interface {
|
||||
pager
|
||||
PageResponse() armnetwork.VirtualNetworksListAllResponse
|
||||
}
|
||||
|
||||
type virtualNetworksClient interface {
|
||||
ListAll(options *armnetwork.VirtualNetworksListAllOptions) virtualNetworksListAllPager
|
||||
}
|
||||
|
||||
type virtualNetworksClientImpl struct {
|
||||
client *armnetwork.VirtualNetworksClient
|
||||
}
|
||||
|
||||
func (c virtualNetworksClientImpl) ListAll(options *armnetwork.VirtualNetworksListAllOptions) virtualNetworksListAllPager {
|
||||
return c.client.ListAll(options)
|
||||
}
|
||||
|
||||
type networkRepository struct {
|
||||
virtualNetworksClient virtualNetworksClient
|
||||
cache cache.Cache
|
||||
}
|
||||
|
||||
func NewNetworkRepository(con *arm.Connection, config common.AzureProviderConfig, cache cache.Cache) *networkRepository {
|
||||
return &networkRepository{
|
||||
&virtualNetworksClientImpl{client: armnetwork.NewVirtualNetworksClient(con, config.SubscriptionID)},
|
||||
cache,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *networkRepository) ListAllVirtualNetworks() ([]*armnetwork.VirtualNetwork, error) {
|
||||
|
||||
if v := s.cache.Get("ListAllVirtualNetworks"); v != nil {
|
||||
return v.([]*armnetwork.VirtualNetwork), nil
|
||||
}
|
||||
|
||||
pager := s.virtualNetworksClient.ListAll(nil)
|
||||
results := make([]*armnetwork.VirtualNetwork, 0)
|
||||
for pager.NextPage(context.Background()) {
|
||||
resp := pager.PageResponse()
|
||||
if err := pager.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
results = append(results, resp.VirtualNetworksListAllResult.VirtualNetworkListResult.Value...)
|
||||
}
|
||||
|
||||
if err := pager.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
s.cache.Put("ListAllVirtualNetworks", results)
|
||||
|
||||
return results, nil
|
||||
}
|
|
@ -0,0 +1,188 @@
|
|||
package repository
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
|
||||
"github.com/Azure/azure-sdk-for-go/sdk/network/armnetwork"
|
||||
"github.com/cloudskiff/driftctl/pkg/remote/cache"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
func Test_ListAllVirtualNetwork_MultiplesResults(t *testing.T) {
|
||||
|
||||
expected := []*armnetwork.VirtualNetwork{
|
||||
{
|
||||
Resource: armnetwork.Resource{
|
||||
ID: to.StringPtr("network1"),
|
||||
},
|
||||
},
|
||||
{
|
||||
Resource: armnetwork.Resource{
|
||||
ID: to.StringPtr("network2"),
|
||||
},
|
||||
},
|
||||
{
|
||||
Resource: armnetwork.Resource{
|
||||
ID: to.StringPtr("network3"),
|
||||
},
|
||||
},
|
||||
{
|
||||
Resource: armnetwork.Resource{
|
||||
ID: to.StringPtr("network4"),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
fakeClient := &mockVirtualNetworkClient{}
|
||||
|
||||
mockPager := &mockVirtualNetworksListAllPager{}
|
||||
mockPager.On("Err").Return(nil).Times(3)
|
||||
mockPager.On("NextPage", mock.Anything).Return(true).Times(2)
|
||||
mockPager.On("NextPage", mock.Anything).Return(false).Times(1)
|
||||
mockPager.On("PageResponse").Return(armnetwork.VirtualNetworksListAllResponse{
|
||||
VirtualNetworksListAllResult: armnetwork.VirtualNetworksListAllResult{
|
||||
VirtualNetworkListResult: armnetwork.VirtualNetworkListResult{
|
||||
Value: []*armnetwork.VirtualNetwork{
|
||||
{
|
||||
Resource: armnetwork.Resource{
|
||||
ID: to.StringPtr("network1"),
|
||||
},
|
||||
},
|
||||
{
|
||||
Resource: armnetwork.Resource{
|
||||
ID: to.StringPtr("network2"),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}).Times(1)
|
||||
mockPager.On("PageResponse").Return(armnetwork.VirtualNetworksListAllResponse{
|
||||
VirtualNetworksListAllResult: armnetwork.VirtualNetworksListAllResult{
|
||||
VirtualNetworkListResult: armnetwork.VirtualNetworkListResult{
|
||||
Value: []*armnetwork.VirtualNetwork{
|
||||
{
|
||||
Resource: armnetwork.Resource{
|
||||
ID: to.StringPtr("network3"),
|
||||
},
|
||||
},
|
||||
{
|
||||
Resource: armnetwork.Resource{
|
||||
ID: to.StringPtr("network4"),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}).Times(1)
|
||||
|
||||
fakeClient.On("ListAll", mock.Anything).Return(mockPager)
|
||||
|
||||
c := &cache.MockCache{}
|
||||
c.On("Get", "ListAllVirtualNetworks").Return(nil).Times(1)
|
||||
c.On("Put", "ListAllVirtualNetworks", expected).Return(true).Times(1)
|
||||
s := &networkRepository{
|
||||
virtualNetworksClient: fakeClient,
|
||||
cache: c,
|
||||
}
|
||||
got, err := s.ListAllVirtualNetworks()
|
||||
if err != nil {
|
||||
t.Errorf("ListAllVirtualNetworks() error = %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
mockPager.AssertExpectations(t)
|
||||
fakeClient.AssertExpectations(t)
|
||||
c.AssertExpectations(t)
|
||||
|
||||
if !reflect.DeepEqual(got, expected) {
|
||||
t.Errorf("ListAllVirtualNetworks() got = %v, want %v", got, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_ListAllVirtualNetwork_MultiplesResults_WithCache(t *testing.T) {
|
||||
|
||||
expected := []*armnetwork.VirtualNetwork{
|
||||
{
|
||||
Resource: armnetwork.Resource{
|
||||
ID: to.StringPtr("network3"),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
fakeClient := &mockVirtualNetworkClient{}
|
||||
|
||||
c := &cache.MockCache{}
|
||||
c.On("Get", "ListAllVirtualNetworks").Return(expected).Times(1)
|
||||
s := &networkRepository{
|
||||
virtualNetworksClient: fakeClient,
|
||||
cache: c,
|
||||
}
|
||||
got, err := s.ListAllVirtualNetworks()
|
||||
if err != nil {
|
||||
t.Errorf("ListAllVirtualNetworks() error = %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
fakeClient.AssertExpectations(t)
|
||||
c.AssertExpectations(t)
|
||||
|
||||
if !reflect.DeepEqual(got, expected) {
|
||||
t.Errorf("ListAllVirtualNetworks() got = %v, want %v", got, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_ListAllVirtualNetwork_Error_OnPageResponse(t *testing.T) {
|
||||
|
||||
fakeClient := &mockVirtualNetworkClient{}
|
||||
|
||||
expectedErr := errors.New("unexpected error")
|
||||
|
||||
mockPager := &mockVirtualNetworksListAllPager{}
|
||||
mockPager.On("Err").Return(expectedErr).Times(1)
|
||||
mockPager.On("NextPage", mock.Anything).Return(true).Times(1)
|
||||
mockPager.On("PageResponse").Return(armnetwork.VirtualNetworksListAllResponse{}).Times(1)
|
||||
|
||||
fakeClient.On("ListAll", mock.Anything).Return(mockPager)
|
||||
|
||||
s := &networkRepository{
|
||||
virtualNetworksClient: fakeClient,
|
||||
cache: cache.New(0),
|
||||
}
|
||||
got, err := s.ListAllVirtualNetworks()
|
||||
|
||||
mockPager.AssertExpectations(t)
|
||||
fakeClient.AssertExpectations(t)
|
||||
|
||||
assert.Equal(t, expectedErr, err)
|
||||
assert.Nil(t, got)
|
||||
}
|
||||
|
||||
func Test_ListAllVirtualNetwork_Error(t *testing.T) {
|
||||
|
||||
fakeClient := &mockVirtualNetworkClient{}
|
||||
|
||||
expectedErr := errors.New("unexpected error")
|
||||
|
||||
mockPager := &mockVirtualNetworksListAllPager{}
|
||||
mockPager.On("Err").Return(expectedErr).Times(1)
|
||||
mockPager.On("NextPage", mock.Anything).Return(false).Times(1)
|
||||
|
||||
fakeClient.On("ListAll", mock.Anything).Return(mockPager)
|
||||
|
||||
s := &networkRepository{
|
||||
virtualNetworksClient: fakeClient,
|
||||
cache: cache.New(0),
|
||||
}
|
||||
got, err := s.ListAllVirtualNetworks()
|
||||
|
||||
mockPager.AssertExpectations(t)
|
||||
fakeClient.AssertExpectations(t)
|
||||
|
||||
assert.Equal(t, expectedErr, err)
|
||||
assert.Nil(t, got)
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package repository
|
||||
|
||||
import "context"
|
||||
|
||||
type pager interface {
|
||||
Err() error
|
||||
NextPage(ctx context.Context) bool
|
||||
}
|
|
@ -18,8 +18,7 @@ type StorageRespository interface {
|
|||
}
|
||||
|
||||
type blobContainerListPager interface {
|
||||
Err() error
|
||||
NextPage(ctx context.Context) bool
|
||||
pager
|
||||
PageResponse() armstorage.BlobContainersListResponse
|
||||
}
|
||||
|
||||
|
@ -37,8 +36,7 @@ func (c blobContainerClientImpl) List(resourceGroupName string, accountName stri
|
|||
}
|
||||
|
||||
type storageAccountListPager interface {
|
||||
Err() error
|
||||
NextPage(ctx context.Context) bool
|
||||
pager
|
||||
PageResponse() armstorage.StorageAccountsListResponse
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,112 @@
|
|||
package remote
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
|
||||
"github.com/Azure/azure-sdk-for-go/sdk/network/armnetwork"
|
||||
"github.com/cloudskiff/driftctl/mocks"
|
||||
"github.com/cloudskiff/driftctl/pkg/filter"
|
||||
"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"
|
||||
"github.com/cloudskiff/driftctl/pkg/resource"
|
||||
resourceazure "github.com/cloudskiff/driftctl/pkg/resource/azurerm"
|
||||
"github.com/cloudskiff/driftctl/pkg/terraform"
|
||||
testresource "github.com/cloudskiff/driftctl/test/resource"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
func TestAzurermVirtualNetwork(t *testing.T) {
|
||||
|
||||
dummyError := errors.New("this is an error")
|
||||
|
||||
tests := []struct {
|
||||
test string
|
||||
mocks func(*repository.MockNetworkRepository, *mocks.AlerterInterface)
|
||||
assertExpected func(t *testing.T, got []*resource.Resource)
|
||||
wantErr error
|
||||
}{
|
||||
{
|
||||
test: "no virtual network",
|
||||
mocks: func(repository *repository.MockNetworkRepository, alerter *mocks.AlerterInterface) {
|
||||
repository.On("ListAllVirtualNetworks").Return([]*armnetwork.VirtualNetwork{}, nil)
|
||||
},
|
||||
assertExpected: func(t *testing.T, got []*resource.Resource) {
|
||||
assert.Len(t, got, 0)
|
||||
},
|
||||
},
|
||||
{
|
||||
test: "error listing virtual network",
|
||||
mocks: func(repository *repository.MockNetworkRepository, alerter *mocks.AlerterInterface) {
|
||||
repository.On("ListAllVirtualNetworks").Return(nil, dummyError)
|
||||
},
|
||||
wantErr: error2.NewResourceListingError(dummyError, resourceazure.AzureVirtualNetworkResourceType),
|
||||
},
|
||||
{
|
||||
test: "multiple virtual network",
|
||||
mocks: func(repository *repository.MockNetworkRepository, alerter *mocks.AlerterInterface) {
|
||||
repository.On("ListAllVirtualNetworks").Return([]*armnetwork.VirtualNetwork{
|
||||
{
|
||||
Resource: armnetwork.Resource{
|
||||
ID: to.StringPtr("network1"),
|
||||
},
|
||||
},
|
||||
{
|
||||
Resource: armnetwork.Resource{
|
||||
ID: to.StringPtr("network2"),
|
||||
},
|
||||
},
|
||||
}, nil)
|
||||
},
|
||||
assertExpected: func(t *testing.T, got []*resource.Resource) {
|
||||
assert.Len(t, got, 2)
|
||||
|
||||
assert.Equal(t, got[0].ResourceId(), "network1")
|
||||
assert.Equal(t, got[0].ResourceType(), resourceazure.AzureVirtualNetworkResourceType)
|
||||
|
||||
assert.Equal(t, got[1].ResourceId(), "network2")
|
||||
assert.Equal(t, got[1].ResourceType(), resourceazure.AzureVirtualNetworkResourceType)
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
providerVersion := "2.71.0"
|
||||
schemaRepository := testresource.InitFakeSchemaRepository("azurerm", providerVersion)
|
||||
resourceazure.InitResourcesMetadata(schemaRepository)
|
||||
factory := terraform.NewTerraformResourceFactory(schemaRepository)
|
||||
|
||||
for _, c := range tests {
|
||||
t.Run(c.test, func(tt *testing.T) {
|
||||
|
||||
scanOptions := ScannerOptions{}
|
||||
remoteLibrary := common.NewRemoteLibrary()
|
||||
|
||||
// Initialize mocks
|
||||
alerter := &mocks.AlerterInterface{}
|
||||
fakeRepo := &repository.MockNetworkRepository{}
|
||||
c.mocks(fakeRepo, alerter)
|
||||
|
||||
var repo repository.NetworkRepository = fakeRepo
|
||||
|
||||
remoteLibrary.AddEnumerator(azurerm.NewAzurermVirtualNetworkEnumerator(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
|
||||
}
|
||||
|
||||
c.assertExpected(tt, got)
|
||||
alerter.AssertExpectations(tt)
|
||||
fakeRepo.AssertExpectations(tt)
|
||||
})
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
package azurerm
|
||||
|
||||
const AzureVirtualNetworkResourceType = "azurerm_virtual_network"
|
|
@ -0,0 +1,31 @@
|
|||
package azurerm_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/cloudskiff/driftctl/test"
|
||||
"github.com/cloudskiff/driftctl/test/acceptance"
|
||||
)
|
||||
|
||||
func TestAcc_Azure_VirtualNetwork(t *testing.T) {
|
||||
acceptance.Run(t, acceptance.AccTestCase{
|
||||
TerraformVersion: "0.15.5",
|
||||
Paths: []string{"./testdata/acc/azurerm_virtual_network"},
|
||||
Args: []string{
|
||||
"scan",
|
||||
"--to", "azure+tf",
|
||||
"--filter", "Type=='azurerm_virtual_network'",
|
||||
},
|
||||
Checks: []acceptance.AccCheck{
|
||||
{
|
||||
Check: func(result *test.ScanResult, stdout string, err error) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
result.AssertInfrastructureIsInSync()
|
||||
result.AssertManagedCount(1)
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
21
pkg/resource/azurerm/testdata/acc/azurerm_virtual_network/.terraform.lock.hcl
vendored
Executable file
21
pkg/resource/azurerm/testdata/acc/azurerm_virtual_network/.terraform.lock.hcl
vendored
Executable 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/azurerm" {
|
||||
version = "2.71.0"
|
||||
constraints = "~> 2.71.0"
|
||||
hashes = [
|
||||
"h1:RiFIxNI4Yr9CqleqEdgg1ydLAZ5JiYiz6l5iTD3WcuU=",
|
||||
"zh:2b9d8a703a0222f72cbceb8d2bdb580066afdcd7f28b6ad65d5ed935319b5433",
|
||||
"zh:332988f4c1747bcc8ebd32734bf8de2bea4c13a6fbd08d7eb97d0c43d335b15e",
|
||||
"zh:3a902470276ba48e23ad4dd6baff16a9ce3b60b29c0b07064dbe96ce4640a31c",
|
||||
"zh:5eaa0d0c2c6554913421be10fbf4bb6a9ef98fbbd750d3d1f02c99798aae2c22",
|
||||
"zh:67859f40ed2f770f33ace9d3911e8b9c9be505947b38a0578e6d097f5db1d4bf",
|
||||
"zh:7cd9bf4899fe383fc7eeede03cad138d637244878cd295a7a1044ca20ca0652c",
|
||||
"zh:afcb82c1382a1a9d63a41137321e077144aad768e4e46057a7ea604d067b4181",
|
||||
"zh:c6e358759ed00a628dcfe7adb0906b2c98576ac3056fdd70930786d404e1da66",
|
||||
"zh:cb3390c34f6790ad656929d0268ab3bc082678e8cbe2add0a177cf7896068844",
|
||||
"zh:cc213dbf59cf41506e86b83492ccfef6ef5f34d4d00d9e49fc8a01fee253f4ee",
|
||||
"zh:d1e8c9b507e2d187ea2447ae156028ba3f76db2164674761987c14217d04fee5",
|
||||
]
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
terraform {
|
||||
required_providers {
|
||||
azurerm = {
|
||||
source = "hashicorp/azurerm"
|
||||
version = "~> 2.71.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
provider "azurerm" {
|
||||
features {}
|
||||
}
|
||||
|
||||
data "azurerm_resource_group" "qa1" {
|
||||
name = "driftctl-qa-1"
|
||||
}
|
||||
|
||||
resource "azurerm_virtual_network" "test" {
|
||||
name = "network1"
|
||||
location = data.azurerm_resource_group.qa1.location
|
||||
resource_group_name = data.azurerm_resource_group.qa1.name
|
||||
address_space = ["10.0.0.0/16"]
|
||||
dns_servers = ["10.0.0.4", "10.0.0.5"]
|
||||
}
|
|
@ -78,6 +78,7 @@ var supportedTypes = map[string]struct{}{
|
|||
|
||||
"azurerm_storage_account": {},
|
||||
"azurerm_storage_container": {},
|
||||
"azurerm_virtual_network": {},
|
||||
}
|
||||
|
||||
func IsResourceTypeSupported(ty string) bool {
|
||||
|
|
Loading…
Reference in New Issue