Merge branch 'main' into feat/support_aws_appautoscaling_policy

main
Raphaël 2021-10-05 10:55:38 +02:00 committed by GitHub
commit 583345f7c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
40 changed files with 2063 additions and 19 deletions

View File

@ -1,6 +1,7 @@
version: 2.1 version: 2.1
orbs: orbs:
go: circleci/go@1.5.0 go: circleci/go@1.5.0
codecov: codecov/codecov@3.1.0
jobs: jobs:
test_acc: test_acc:
parameters: parameters:
@ -31,6 +32,9 @@ jobs:
name: Run acceptance tests name: Run acceptance tests
command: make acc command: make acc
no_output_timeout: 30m no_output_timeout: 30m
- codecov/upload:
flags: << parameters.pattern >>
file: cover-acc.out
- run: - run:
name: Discord notification name: Discord notification
when: on_fail when: on_fail
@ -72,17 +76,8 @@ jobs:
-w /app\ -w /app\
golang:1.16\ golang:1.16\
bash -c 'make install-tools && make test' bash -c 'make install-tools && make test'
- run: - codecov/upload:
name: Check Codecov flags: unit
command: |
curl -s -o codecov https://codecov.io/bash \
&& VERSION=$(grep 'VERSION=\".*\"' codecov | cut -d'"' -f2) \
&& shasum -a 512 -c --ignore-missing <(curl -s https://raw.githubusercontent.com/codecov/codecov-bash/${VERSION}/SHA512SUM)
- run:
name: Codecov upload
command: |
chmod +x codecov
./codecov
- store_test_results: - store_test_results:
path: ./ path: ./
release: release:

1
go.mod
View File

@ -8,6 +8,7 @@ require (
github.com/Azure/azure-sdk-for-go/sdk/azcore v0.19.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/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/network/armnetwork v0.3.0
github.com/Azure/azure-sdk-for-go/sdk/resources/armresources v0.3.0
github.com/Azure/azure-sdk-for-go/sdk/storage/armstorage v0.2.0 github.com/Azure/azure-sdk-for-go/sdk/storage/armstorage v0.2.0
github.com/Azure/go-autorest/autorest v0.11.3 github.com/Azure/go-autorest/autorest v0.11.3
github.com/aws/aws-sdk-go v1.38.68 github.com/aws/aws-sdk-go v1.38.68

3
go.sum
View File

@ -48,6 +48,7 @@ cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
github.com/AndreasBriese/bbloom v0.0.0-20190306092124-e2d15f34fcf9/go.mod h1:bOvUY6CB00SOBii9/FifXqc0awNKxLFCL/+pkDPuyl8= github.com/AndreasBriese/bbloom v0.0.0-20190306092124-e2d15f34fcf9/go.mod h1:bOvUY6CB00SOBii9/FifXqc0awNKxLFCL/+pkDPuyl8=
github.com/Azure/azure-sdk-for-go v45.0.0+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc= github.com/Azure/azure-sdk-for-go v45.0.0+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc=
github.com/Azure/azure-sdk-for-go v57.0.0+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc=
github.com/Azure/azure-sdk-for-go v57.1.0+incompatible h1:TKQ3ieyB0vVKkF6t9dsWbMjq56O1xU3eh3Ec09v6ajM= github.com/Azure/azure-sdk-for-go v57.1.0+incompatible h1:TKQ3ieyB0vVKkF6t9dsWbMjq56O1xU3eh3Ec09v6ajM=
github.com/Azure/azure-sdk-for-go v57.1.0+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc= github.com/Azure/azure-sdk-for-go v57.1.0+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc=
github.com/Azure/azure-sdk-for-go/sdk/azcore v0.19.0 h1:lhSJz9RMbJcTgxifR1hUNJnn6CNYtbgEDtQV22/9RBA= github.com/Azure/azure-sdk-for-go/sdk/azcore v0.19.0 h1:lhSJz9RMbJcTgxifR1hUNJnn6CNYtbgEDtQV22/9RBA=
@ -59,6 +60,8 @@ github.com/Azure/azure-sdk-for-go/sdk/internal v0.7.0 h1:v9p9TfTbf7AwNb5NYQt7hI4
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/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 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/network/armnetwork v0.3.0/go.mod h1:YSO+0IW+22kuLybFl2GAYaTDh1VWxNid83hqY/DkpGQ=
github.com/Azure/azure-sdk-for-go/sdk/resources/armresources v0.3.0 h1:I1cONUC2nKiGU3JXm2jRB4+QIs06lGqkplVpwy4ie9o=
github.com/Azure/azure-sdk-for-go/sdk/resources/armresources v0.3.0/go.mod h1:LdmyxRi5+2XPnbuv0X9c6ymGle+UkoNvqsBvG+oG53M=
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 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/storage/armstorage v0.2.0/go.mod h1:mIFJgQ93RCQPBsN2jBDzDOfwJpLacGwXIxmirNQMiq4=
github.com/Azure/go-autorest v14.2.0+incompatible h1:V5VMDjClD3GiElqLWO7mz2MxNAK/vTfRHdAubSIPRgs= github.com/Azure/go-autorest v14.2.0+incompatible h1:V5VMDjClD3GiElqLWO7mz2MxNAK/vTfRHdAubSIPRgs=

View File

@ -111,6 +111,7 @@ func (d DriftCTL) Run() (*analyser.Analysis, error) {
middlewares.NewEipAssociationExpander(d.resourceFactory), middlewares.NewEipAssociationExpander(d.resourceFactory),
middlewares.NewRDSClusterInstanceExpander(d.resourceFactory), middlewares.NewRDSClusterInstanceExpander(d.resourceFactory),
middlewares.NewGoogleLegacyBucketIAMBindings(), middlewares.NewGoogleLegacyBucketIAMBindings(),
middlewares.NewAzurermSubnetExpander(d.resourceFactory),
) )
if !d.opts.StrictMode { if !d.opts.StrictMode {

View File

@ -0,0 +1,62 @@
package middlewares
import (
"github.com/cloudskiff/driftctl/pkg/resource"
"github.com/cloudskiff/driftctl/pkg/resource/azurerm"
)
// Explodes subnet found in azurerm_virtual_network.subnet from state resources to dedicated resources
type AzurermSubnetExpander struct {
resourceFactory resource.ResourceFactory
}
func NewAzurermSubnetExpander(resourceFactory resource.ResourceFactory) AzurermSubnetExpander {
return AzurermSubnetExpander{
resourceFactory: resourceFactory,
}
}
func (m AzurermSubnetExpander) Execute(_, resourcesFromState *[]*resource.Resource) error {
newList := make([]*resource.Resource, 0)
for _, res := range *resourcesFromState {
newList = append(newList, res)
// Ignore all resources other than azurerm_virtual_network
if res.ResourceType() != azurerm.AzureVirtualNetworkResourceType {
continue
}
subnets, exist := res.Attributes().Get("subnet")
if !exist || subnets == nil {
continue
}
for _, subnet := range subnets.([]interface{}) {
subnet := subnet.(map[string]interface{})
id := subnet["id"].(string)
exist := false
for _, resFromState := range *resourcesFromState {
if resFromState.ResourceType() == azurerm.AzureSubnetResourceType &&
resFromState.ResourceId() == id {
exist = true
break
}
}
if exist {
continue
}
res := m.resourceFactory.CreateAbstractResource(
azurerm.AzureSubnetResourceType,
id,
map[string]interface{}{},
)
newList = append(newList, res)
}
res.Attrs.SafeDelete([]string{"subnet"})
}
*resourcesFromState = newList
return nil
}

View File

@ -0,0 +1,174 @@
package middlewares
import (
"strings"
"testing"
"github.com/aws/aws-sdk-go/aws/awsutil"
"github.com/cloudskiff/driftctl/pkg/resource"
"github.com/cloudskiff/driftctl/pkg/resource/azurerm"
"github.com/cloudskiff/driftctl/pkg/terraform"
"github.com/r3labs/diff/v2"
)
func TestAzurermSubnetExpander_Execute(t *testing.T) {
tests := []struct {
name string
input []*resource.Resource
expected []*resource.Resource
mock func(factory *terraform.MockResourceFactory)
}{
{
name: "test with nil subnet attribute",
input: []*resource.Resource{
{
Id: "network1",
Type: azurerm.AzureVirtualNetworkResourceType,
Attrs: &resource.Attributes{
"subnet": nil,
},
},
},
expected: []*resource.Resource{
{
Id: "network1",
Type: azurerm.AzureVirtualNetworkResourceType,
Attrs: &resource.Attributes{
"subnet": nil,
},
},
},
},
{
name: "test with empty subnet attributes",
input: []*resource.Resource{
{
Id: "network1",
Type: azurerm.AzureVirtualNetworkResourceType,
Attrs: &resource.Attributes{
"subnet": []interface{}{},
},
},
},
expected: []*resource.Resource{
{
Id: "network1",
Type: azurerm.AzureVirtualNetworkResourceType,
Attrs: &resource.Attributes{},
},
},
},
{
name: "test that resource will not be expanded if it already exist",
input: []*resource.Resource{
{
Id: "exist",
Type: azurerm.AzureSubnetResourceType,
Attrs: &resource.Attributes{},
},
{
Id: "network1",
Type: azurerm.AzureVirtualNetworkResourceType,
Attrs: &resource.Attributes{
"subnet": []interface{}{
map[string]interface{}{
"id": "exist",
},
},
},
},
},
expected: []*resource.Resource{
{
Id: "exist",
Type: azurerm.AzureSubnetResourceType,
Attrs: &resource.Attributes{},
},
{
Id: "network1",
Type: azurerm.AzureVirtualNetworkResourceType,
Attrs: &resource.Attributes{},
},
},
},
{
name: "test subnet are expanded",
input: []*resource.Resource{
{
Id: "fake_resource",
},
{
Id: "network1",
Type: azurerm.AzureVirtualNetworkResourceType,
Attrs: &resource.Attributes{
"subnet": []interface{}{
map[string]interface{}{
"id": "subnet1",
},
map[string]interface{}{
"id": "subnet2",
},
},
},
},
},
expected: []*resource.Resource{
{
Id: "fake_resource",
},
{
Id: "network1",
Type: azurerm.AzureVirtualNetworkResourceType,
Attrs: &resource.Attributes{},
},
{
Id: "subnet1",
Type: azurerm.AzureSubnetResourceType,
Attrs: &resource.Attributes{},
},
{
Id: "subnet2",
Type: azurerm.AzureSubnetResourceType,
Attrs: &resource.Attributes{},
},
},
mock: func(factory *terraform.MockResourceFactory) {
factory.On("CreateAbstractResource", azurerm.AzureSubnetResourceType, "subnet1", map[string]interface{}{}).Times(1).Return(&resource.Resource{
Id: "subnet1",
Type: azurerm.AzureSubnetResourceType,
Attrs: &resource.Attributes{},
}, nil)
factory.On("CreateAbstractResource", azurerm.AzureSubnetResourceType, "subnet2", map[string]interface{}{}).Times(1).Return(&resource.Resource{
Id: "subnet2",
Type: azurerm.AzureSubnetResourceType,
Attrs: &resource.Attributes{},
}, nil)
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
factory := &terraform.MockResourceFactory{}
if tt.mock != nil {
tt.mock(factory)
}
m := NewAzurermSubnetExpander(factory)
err := m.Execute(&[]*resource.Resource{}, &tt.input)
if err != nil {
t.Fatal(err)
}
changelog, err := diff.Diff(tt.expected, tt.input)
if err != nil {
t.Fatal(err)
}
if len(changelog) > 0 {
for _, change := range changelog {
t.Errorf("%s got = %v, want %v", strings.Join(change.Path, "."), awsutil.Prettify(change.From), awsutil.Prettify(change.To))
}
}
})
}
}

View File

@ -0,0 +1,47 @@
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 AzurermResourceGroupEnumerator struct {
repository repository.ResourcesRepository
factory resource.ResourceFactory
}
func NewAzurermResourceGroupEnumerator(repo repository.ResourcesRepository, factory resource.ResourceFactory) *AzurermResourceGroupEnumerator {
return &AzurermResourceGroupEnumerator{
repository: repo,
factory: factory,
}
}
func (e *AzurermResourceGroupEnumerator) SupportedType() resource.ResourceType {
return azurerm.AzureResourceGroupResourceType
}
func (e *AzurermResourceGroupEnumerator) Enumerate() ([]*resource.Resource, error) {
groups, err := e.repository.ListAllResourceGroups()
if err != nil {
return nil, remoteerror.NewResourceListingError(err, string(e.SupportedType()))
}
results := make([]*resource.Resource, 0)
for _, group := range groups {
results = append(
results,
e.factory.CreateAbstractResource(
string(e.SupportedType()),
*group.ID,
map[string]interface{}{
"name": *group.Name,
},
),
)
}
return results, err
}

View File

@ -0,0 +1,48 @@
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 AzurermRouteTableEnumerator struct {
repository repository.NetworkRepository
factory resource.ResourceFactory
}
func NewAzurermRouteTableEnumerator(repo repository.NetworkRepository, factory resource.ResourceFactory) *AzurermRouteTableEnumerator {
return &AzurermRouteTableEnumerator{
repository: repo,
factory: factory,
}
}
func (e *AzurermRouteTableEnumerator) SupportedType() resource.ResourceType {
return azurerm.AzureRouteTableResourceType
}
func (e *AzurermRouteTableEnumerator) Enumerate() ([]*resource.Resource, error) {
resources, err := e.repository.ListAllRouteTables()
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{}{
"name": *res.Name,
},
),
)
}
return results, err
}

View File

@ -0,0 +1,51 @@
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 AzurermSubnetEnumerator struct {
repository repository.NetworkRepository
factory resource.ResourceFactory
}
func NewAzurermSubnetEnumerator(repo repository.NetworkRepository, factory resource.ResourceFactory) *AzurermSubnetEnumerator {
return &AzurermSubnetEnumerator{
repository: repo,
factory: factory,
}
}
func (e *AzurermSubnetEnumerator) SupportedType() resource.ResourceType {
return azurerm.AzureSubnetResourceType
}
func (e *AzurermSubnetEnumerator) Enumerate() ([]*resource.Resource, error) {
networks, err := e.repository.ListAllVirtualNetworks()
if err != nil {
return nil, remoteerror.NewResourceListingErrorWithType(err, string(e.SupportedType()), azurerm.AzureVirtualNetworkResourceType)
}
results := make([]*resource.Resource, 0)
for _, network := range networks {
resources, err := e.repository.ListAllSubnets(network)
if err != nil {
return nil, remoteerror.NewResourceListingError(err, string(e.SupportedType()))
}
for _, res := range resources {
results = append(
results,
e.factory.CreateAbstractResource(
string(e.SupportedType()),
*res.ID,
map[string]interface{}{},
),
)
}
}
return results, err
}

View File

@ -43,14 +43,18 @@ func Init(
storageAccountRepo := repository.NewStorageRepository(con, providerConfig, c) storageAccountRepo := repository.NewStorageRepository(con, providerConfig, c)
networkRepo := repository.NewNetworkRepository(con, providerConfig, c) networkRepo := repository.NewNetworkRepository(con, providerConfig, c)
resourcesRepo := repository.NewResourcesRepository(con, providerConfig, c)
providerLibrary.AddProvider(terraform.AZURE, provider) providerLibrary.AddProvider(terraform.AZURE, provider)
remoteLibrary.AddEnumerator(NewAzurermStorageAccountEnumerator(storageAccountRepo, factory)) remoteLibrary.AddEnumerator(NewAzurermStorageAccountEnumerator(storageAccountRepo, factory))
remoteLibrary.AddEnumerator(NewAzurermStorageContainerEnumerator(storageAccountRepo, factory)) remoteLibrary.AddEnumerator(NewAzurermStorageContainerEnumerator(storageAccountRepo, factory))
remoteLibrary.AddEnumerator(NewAzurermVirtualNetworkEnumerator(networkRepo, factory)) remoteLibrary.AddEnumerator(NewAzurermVirtualNetworkEnumerator(networkRepo, factory))
remoteLibrary.AddEnumerator(NewAzurermRouteTableEnumerator(networkRepo, factory))
remoteLibrary.AddEnumerator(NewAzurermResourceGroupEnumerator(resourcesRepo, factory))
remoteLibrary.AddEnumerator(NewAzurermSubnetEnumerator(networkRepo, factory))
err = resourceSchemaRepository.Init(terraform.AZURE, version, provider.Schema()) err = resourceSchemaRepository.Init(terraform.AZURE, provider.Version(), provider.Schema())
if err != nil { if err != nil {
return err return err
} }

View File

@ -12,6 +12,53 @@ type MockNetworkRepository struct {
mock.Mock mock.Mock
} }
// ListAllRouteTables provides a mock function with given fields:
func (_m *MockNetworkRepository) ListAllRouteTables() ([]*armnetwork.RouteTable, error) {
ret := _m.Called()
var r0 []*armnetwork.RouteTable
if rf, ok := ret.Get(0).(func() []*armnetwork.RouteTable); ok {
r0 = rf()
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).([]*armnetwork.RouteTable)
}
}
var r1 error
if rf, ok := ret.Get(1).(func() error); ok {
r1 = rf()
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// ListAllVirtualNetworks provides a mock function with given fields:
// ListAllSubnets provides a mock function with given fields: virtualNetwork
func (_m *MockNetworkRepository) ListAllSubnets(virtualNetwork *armnetwork.VirtualNetwork) ([]*armnetwork.Subnet, error) {
ret := _m.Called(virtualNetwork)
var r0 []*armnetwork.Subnet
if rf, ok := ret.Get(0).(func(*armnetwork.VirtualNetwork) []*armnetwork.Subnet); ok {
r0 = rf(virtualNetwork)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).([]*armnetwork.Subnet)
}
}
var r1 error
if rf, ok := ret.Get(1).(func(*armnetwork.VirtualNetwork) error); ok {
r1 = rf(virtualNetwork)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// ListAllVirtualNetwork provides a mock function with given fields: // ListAllVirtualNetwork provides a mock function with given fields:
func (_m *MockNetworkRepository) ListAllVirtualNetworks() ([]*armnetwork.VirtualNetwork, error) { func (_m *MockNetworkRepository) ListAllVirtualNetworks() ([]*armnetwork.VirtualNetwork, error) {
ret := _m.Called() ret := _m.Called()

View File

@ -0,0 +1,36 @@
// Code generated by mockery v0.0.0-dev. DO NOT EDIT.
package repository
import (
armresources "github.com/Azure/azure-sdk-for-go/sdk/resources/armresources"
mock "github.com/stretchr/testify/mock"
)
// MockResourcesRepository is an autogenerated mock type for the ResourcesRepository type
type MockResourcesRepository struct {
mock.Mock
}
// ListAllResourceGroups provides a mock function with given fields:
func (_m *MockResourcesRepository) ListAllResourceGroups() ([]*armresources.ResourceGroup, error) {
ret := _m.Called()
var r0 []*armresources.ResourceGroup
if rf, ok := ret.Get(0).(func() []*armresources.ResourceGroup); ok {
r0 = rf()
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).([]*armresources.ResourceGroup)
}
}
var r1 error
if rf, ok := ret.Get(1).(func() error); ok {
r1 = rf()
} else {
r1 = ret.Error(1)
}
return r0, r1
}

View File

@ -0,0 +1,29 @@
// Code generated by mockery v0.0.0-dev. DO NOT EDIT.
package repository
import (
armresources "github.com/Azure/azure-sdk-for-go/sdk/resources/armresources"
mock "github.com/stretchr/testify/mock"
)
// mockResourcesClient is an autogenerated mock type for the resourcesClient type
type mockResourcesClient struct {
mock.Mock
}
// List provides a mock function with given fields: options
func (_m *mockResourcesClient) List(options *armresources.ResourceGroupsListOptions) resourcesListPager {
ret := _m.Called(options)
var r0 resourcesListPager
if rf, ok := ret.Get(0).(func(*armresources.ResourceGroupsListOptions) resourcesListPager); ok {
r0 = rf(options)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(resourcesListPager)
}
}
return r0
}

View File

@ -0,0 +1,58 @@
// Code generated by mockery v0.0.0-dev. DO NOT EDIT.
package repository
import (
context "context"
armresources "github.com/Azure/azure-sdk-for-go/sdk/resources/armresources"
mock "github.com/stretchr/testify/mock"
)
// mockResourcesListPager is an autogenerated mock type for the resourcesListPager type
type mockResourcesListPager struct {
mock.Mock
}
// Err provides a mock function with given fields:
func (_m *mockResourcesListPager) 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 *mockResourcesListPager) 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 *mockResourcesListPager) PageResponse() armresources.ResourceGroupsListResponse {
ret := _m.Called()
var r0 armresources.ResourceGroupsListResponse
if rf, ok := ret.Get(0).(func() armresources.ResourceGroupsListResponse); ok {
r0 = rf()
} else {
r0 = ret.Get(0).(armresources.ResourceGroupsListResponse)
}
return r0
}

View File

@ -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"
)
// mockRouteTablesClient is an autogenerated mock type for the routeTablesClient type
type mockRouteTablesClient struct {
mock.Mock
}
// ListAll provides a mock function with given fields: options
func (_m *mockRouteTablesClient) ListAll(options *armnetwork.RouteTablesListAllOptions) routeTablesListAllPager {
ret := _m.Called(options)
var r0 routeTablesListAllPager
if rf, ok := ret.Get(0).(func(*armnetwork.RouteTablesListAllOptions) routeTablesListAllPager); ok {
r0 = rf(options)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(routeTablesListAllPager)
}
}
return r0
}

View File

@ -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"
)
// mockRouteTablesListAllPager is an autogenerated mock type for the routeTablesListAllPager type
type mockRouteTablesListAllPager struct {
mock.Mock
}
// Err provides a mock function with given fields:
func (_m *mockRouteTablesListAllPager) 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 *mockRouteTablesListAllPager) 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 *mockRouteTablesListAllPager) PageResponse() armnetwork.RouteTablesListAllResponse {
ret := _m.Called()
var r0 armnetwork.RouteTablesListAllResponse
if rf, ok := ret.Get(0).(func() armnetwork.RouteTablesListAllResponse); ok {
r0 = rf()
} else {
r0 = ret.Get(0).(armnetwork.RouteTablesListAllResponse)
}
return r0
}

View File

@ -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"
)
// mockSubnetsClient is an autogenerated mock type for the subnetsClient type
type mockSubnetsClient struct {
mock.Mock
}
// List provides a mock function with given fields: resourceGroupName, virtualNetworkName, options
func (_m *mockSubnetsClient) List(resourceGroupName string, virtualNetworkName string, options *armnetwork.SubnetsListOptions) subnetsListPager {
ret := _m.Called(resourceGroupName, virtualNetworkName, options)
var r0 subnetsListPager
if rf, ok := ret.Get(0).(func(string, string, *armnetwork.SubnetsListOptions) subnetsListPager); ok {
r0 = rf(resourceGroupName, virtualNetworkName, options)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(subnetsListPager)
}
}
return r0
}

View File

@ -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"
)
// mockSubnetsListPager is an autogenerated mock type for the subnetsListPager type
type mockSubnetsListPager struct {
mock.Mock
}
// Err provides a mock function with given fields:
func (_m *mockSubnetsListPager) 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 *mockSubnetsListPager) 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 *mockSubnetsListPager) PageResponse() armnetwork.SubnetsListResponse {
ret := _m.Called()
var r0 armnetwork.SubnetsListResponse
if rf, ok := ret.Get(0).(func() armnetwork.SubnetsListResponse); ok {
r0 = rf()
} else {
r0 = ret.Get(0).(armnetwork.SubnetsListResponse)
}
return r0
}

View File

@ -2,15 +2,40 @@ package repository
import ( import (
"context" "context"
"fmt"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm" "github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
"github.com/Azure/azure-sdk-for-go/sdk/network/armnetwork" "github.com/Azure/azure-sdk-for-go/sdk/network/armnetwork"
"github.com/Azure/go-autorest/autorest/azure"
"github.com/cloudskiff/driftctl/pkg/remote/azurerm/common" "github.com/cloudskiff/driftctl/pkg/remote/azurerm/common"
"github.com/cloudskiff/driftctl/pkg/remote/cache" "github.com/cloudskiff/driftctl/pkg/remote/cache"
) )
type NetworkRepository interface { type NetworkRepository interface {
ListAllVirtualNetworks() ([]*armnetwork.VirtualNetwork, error) ListAllVirtualNetworks() ([]*armnetwork.VirtualNetwork, error)
ListAllRouteTables() ([]*armnetwork.RouteTable, error)
ListAllSubnets(virtualNetwork *armnetwork.VirtualNetwork) ([]*armnetwork.Subnet, error)
}
type subnetsListPager interface {
pager
PageResponse() armnetwork.SubnetsListResponse
}
type subnetsClient interface {
List(resourceGroupName, virtualNetworkName string, options *armnetwork.SubnetsListOptions) subnetsListPager
}
type subnetsClientImpl struct {
client *armnetwork.SubnetsClient
}
func (s subnetsClientImpl) List(resourceGroupName, virtualNetworkName string, options *armnetwork.SubnetsListOptions) subnetsListPager {
return s.client.List(resourceGroupName, virtualNetworkName, options)
}
type virtualNetworksClient interface {
ListAll(options *armnetwork.VirtualNetworksListAllOptions) virtualNetworksListAllPager
} }
type virtualNetworksListAllPager interface { type virtualNetworksListAllPager interface {
@ -18,10 +43,6 @@ type virtualNetworksListAllPager interface {
PageResponse() armnetwork.VirtualNetworksListAllResponse PageResponse() armnetwork.VirtualNetworksListAllResponse
} }
type virtualNetworksClient interface {
ListAll(options *armnetwork.VirtualNetworksListAllOptions) virtualNetworksListAllPager
}
type virtualNetworksClientImpl struct { type virtualNetworksClientImpl struct {
client *armnetwork.VirtualNetworksClient client *armnetwork.VirtualNetworksClient
} }
@ -30,14 +51,35 @@ func (c virtualNetworksClientImpl) ListAll(options *armnetwork.VirtualNetworksLi
return c.client.ListAll(options) return c.client.ListAll(options)
} }
type routeTablesClient interface {
ListAll(options *armnetwork.RouteTablesListAllOptions) routeTablesListAllPager
}
type routeTablesListAllPager interface {
pager
PageResponse() armnetwork.RouteTablesListAllResponse
}
type routeTablesClientImpl struct {
client *armnetwork.RouteTablesClient
}
func (c routeTablesClientImpl) ListAll(options *armnetwork.RouteTablesListAllOptions) routeTablesListAllPager {
return c.client.ListAll(options)
}
type networkRepository struct { type networkRepository struct {
virtualNetworksClient virtualNetworksClient virtualNetworksClient virtualNetworksClient
routeTableClient routeTablesClient
subnetsClient subnetsClient
cache cache.Cache cache cache.Cache
} }
func NewNetworkRepository(con *arm.Connection, config common.AzureProviderConfig, cache cache.Cache) *networkRepository { func NewNetworkRepository(con *arm.Connection, config common.AzureProviderConfig, cache cache.Cache) *networkRepository {
return &networkRepository{ return &networkRepository{
&virtualNetworksClientImpl{client: armnetwork.NewVirtualNetworksClient(con, config.SubscriptionID)}, &virtualNetworksClientImpl{client: armnetwork.NewVirtualNetworksClient(con, config.SubscriptionID)},
&routeTablesClientImpl{client: armnetwork.NewRouteTablesClient(con, config.SubscriptionID)},
&subnetsClientImpl{client: armnetwork.NewSubnetsClient(con, config.SubscriptionID)},
cache, cache,
} }
} }
@ -66,3 +108,59 @@ func (s *networkRepository) ListAllVirtualNetworks() ([]*armnetwork.VirtualNetwo
return results, nil return results, nil
} }
func (s *networkRepository) ListAllRouteTables() ([]*armnetwork.RouteTable, error) {
if v := s.cache.Get("ListAllRouteTables"); v != nil {
return v.([]*armnetwork.RouteTable), nil
}
pager := s.routeTableClient.ListAll(nil)
results := make([]*armnetwork.RouteTable, 0)
for pager.NextPage(context.Background()) {
resp := pager.PageResponse()
if err := pager.Err(); err != nil {
return nil, err
}
results = append(results, resp.RouteTablesListAllResult.RouteTableListResult.Value...)
}
if err := pager.Err(); err != nil {
return nil, err
}
s.cache.Put("ListAllRouteTables", results)
return results, nil
}
func (s *networkRepository) ListAllSubnets(virtualNetwork *armnetwork.VirtualNetwork) ([]*armnetwork.Subnet, error) {
cacheKey := fmt.Sprintf("ListAllSubnets_%s", *virtualNetwork.ID)
if v := s.cache.Get(cacheKey); v != nil {
return v.([]*armnetwork.Subnet), nil
}
res, err := azure.ParseResourceID(*virtualNetwork.ID)
if err != nil {
return nil, err
}
pager := s.subnetsClient.List(res.ResourceGroup, *virtualNetwork.Name, nil)
results := make([]*armnetwork.Subnet, 0)
for pager.NextPage(context.Background()) {
resp := pager.PageResponse()
if err := pager.Err(); err != nil {
return nil, err
}
results = append(results, resp.SubnetsListResult.SubnetListResult.Value...)
}
if err := pager.Err(); err != nil {
return nil, err
}
s.cache.Put(cacheKey, results)
return results, nil
}

View File

@ -1,6 +1,7 @@
package repository package repository
import ( import (
"fmt"
"reflect" "reflect"
"testing" "testing"
@ -186,3 +187,381 @@ func Test_ListAllVirtualNetwork_Error(t *testing.T) {
assert.Equal(t, expectedErr, err) assert.Equal(t, expectedErr, err)
assert.Nil(t, got) assert.Nil(t, got)
} }
func Test_ListAllRouteTables_MultiplesResults(t *testing.T) {
expected := []*armnetwork.RouteTable{
{
Resource: armnetwork.Resource{
ID: to.StringPtr("table1"),
},
},
{
Resource: armnetwork.Resource{
ID: to.StringPtr("table2"),
},
},
{
Resource: armnetwork.Resource{
ID: to.StringPtr("table3"),
},
},
{
Resource: armnetwork.Resource{
ID: to.StringPtr("table4"),
},
},
}
fakeClient := &mockRouteTablesClient{}
mockPager := &mockRouteTablesListAllPager{}
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.RouteTablesListAllResponse{
RouteTablesListAllResult: armnetwork.RouteTablesListAllResult{
RouteTableListResult: armnetwork.RouteTableListResult{
Value: expected[:2],
},
},
}).Times(1)
mockPager.On("PageResponse").Return(armnetwork.RouteTablesListAllResponse{
RouteTablesListAllResult: armnetwork.RouteTablesListAllResult{
RouteTableListResult: armnetwork.RouteTableListResult{
Value: expected[2:],
},
},
}).Times(1)
fakeClient.On("ListAll", mock.Anything).Return(mockPager)
c := &cache.MockCache{}
c.On("Get", "ListAllRouteTables").Return(nil).Times(1)
c.On("Put", "ListAllRouteTables", expected).Return(true).Times(1)
s := &networkRepository{
routeTableClient: fakeClient,
cache: c,
}
got, err := s.ListAllRouteTables()
if err != nil {
t.Errorf("ListAllRouteTables() error = %v", err)
return
}
mockPager.AssertExpectations(t)
fakeClient.AssertExpectations(t)
c.AssertExpectations(t)
if !reflect.DeepEqual(got, expected) {
t.Errorf("ListAllRouteTables() got = %v, want %v", got, expected)
}
}
func Test_ListAllRouteTables_MultiplesResults_WithCache(t *testing.T) {
expected := []*armnetwork.RouteTable{
{
Resource: armnetwork.Resource{
ID: to.StringPtr("table1"),
},
},
}
fakeClient := &mockRouteTablesClient{}
c := &cache.MockCache{}
c.On("Get", "ListAllRouteTables").Return(expected).Times(1)
s := &networkRepository{
routeTableClient: fakeClient,
cache: c,
}
got, err := s.ListAllRouteTables()
if err != nil {
t.Errorf("ListAllRouteTables() error = %v", err)
return
}
fakeClient.AssertExpectations(t)
c.AssertExpectations(t)
if !reflect.DeepEqual(got, expected) {
t.Errorf("ListAllRouteTables() got = %v, want %v", got, expected)
}
}
func Test_ListAllRouteTables_Error_OnPageResponse(t *testing.T) {
fakeClient := &mockRouteTablesClient{}
expectedErr := errors.New("unexpected error")
mockPager := &mockRouteTablesListAllPager{}
mockPager.On("Err").Return(expectedErr).Times(1)
mockPager.On("NextPage", mock.Anything).Return(true).Times(1)
mockPager.On("PageResponse").Return(armnetwork.RouteTablesListAllResponse{}).Times(1)
fakeClient.On("ListAll", mock.Anything).Return(mockPager)
s := &networkRepository{
routeTableClient: fakeClient,
cache: cache.New(0),
}
got, err := s.ListAllRouteTables()
mockPager.AssertExpectations(t)
fakeClient.AssertExpectations(t)
assert.Equal(t, expectedErr, err)
assert.Nil(t, got)
}
func Test_ListAllRouteTables_Error(t *testing.T) {
fakeClient := &mockRouteTablesClient{}
expectedErr := errors.New("unexpected error")
mockPager := &mockRouteTablesListAllPager{}
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{
routeTableClient: fakeClient,
cache: cache.New(0),
}
got, err := s.ListAllRouteTables()
mockPager.AssertExpectations(t)
fakeClient.AssertExpectations(t)
assert.Equal(t, expectedErr, err)
assert.Nil(t, got)
}
func Test_ListAllSubnets_MultiplesResults(t *testing.T) {
network := &armnetwork.VirtualNetwork{
Resource: armnetwork.Resource{
Name: to.StringPtr("network1"),
ID: to.StringPtr("/subscriptions/7bfb2c5c-0000-0000-0000-fffa356eb406/resourceGroups/test-dev/providers/Microsoft.Network/virtualNetworks/network1"),
},
}
expected := []*armnetwork.Subnet{
{
SubResource: armnetwork.SubResource{
ID: to.StringPtr("subnet1"),
},
},
{
SubResource: armnetwork.SubResource{
ID: to.StringPtr("subnet2"),
},
},
{
SubResource: armnetwork.SubResource{
ID: to.StringPtr("subnet3"),
},
},
{
SubResource: armnetwork.SubResource{
ID: to.StringPtr("subnet4"),
},
},
}
fakeClient := &mockSubnetsClient{}
mockPager := &mockSubnetsListPager{}
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.SubnetsListResponse{
SubnetsListResult: armnetwork.SubnetsListResult{
SubnetListResult: armnetwork.SubnetListResult{
Value: []*armnetwork.Subnet{
{
SubResource: armnetwork.SubResource{
ID: to.StringPtr("subnet1"),
},
},
{
SubResource: armnetwork.SubResource{
ID: to.StringPtr("subnet2"),
},
},
},
},
},
}).Times(1)
mockPager.On("PageResponse").Return(armnetwork.SubnetsListResponse{
SubnetsListResult: armnetwork.SubnetsListResult{
SubnetListResult: armnetwork.SubnetListResult{
Value: []*armnetwork.Subnet{
{
SubResource: armnetwork.SubResource{
ID: to.StringPtr("subnet3"),
},
},
{
SubResource: armnetwork.SubResource{
ID: to.StringPtr("subnet4"),
},
},
},
},
},
}).Times(1)
fakeClient.On("List", "test-dev", "network1", mock.Anything).Return(mockPager)
c := &cache.MockCache{}
cacheKey := fmt.Sprintf("ListAllSubnets_%s", *network.ID)
c.On("Get", cacheKey).Return(nil).Times(1)
c.On("Put", cacheKey, expected).Return(true).Times(1)
s := &networkRepository{
subnetsClient: fakeClient,
cache: c,
}
got, err := s.ListAllSubnets(network)
if err != nil {
t.Errorf("ListAllSubnets() error = %v", err)
return
}
mockPager.AssertExpectations(t)
fakeClient.AssertExpectations(t)
c.AssertExpectations(t)
if !reflect.DeepEqual(got, expected) {
t.Errorf("ListAllSubnets() got = %v, want %v", got, expected)
}
}
func Test_ListAllSubnets_MultiplesResults_WithCache(t *testing.T) {
network := &armnetwork.VirtualNetwork{
Resource: armnetwork.Resource{
ID: to.StringPtr("networkID"),
},
}
expected := []*armnetwork.Subnet{
{
Name: to.StringPtr("network1"),
},
}
fakeClient := &mockSubnetsClient{}
c := &cache.MockCache{}
c.On("Get", "ListAllSubnets_networkID").Return(expected).Times(1)
s := &networkRepository{
subnetsClient: fakeClient,
cache: c,
}
got, err := s.ListAllSubnets(network)
if err != nil {
t.Errorf("ListAllSubnets() error = %v", err)
return
}
fakeClient.AssertExpectations(t)
c.AssertExpectations(t)
if !reflect.DeepEqual(got, expected) {
t.Errorf("ListAllSubnets() got = %v, want %v", got, expected)
}
}
func Test_ListAllSubnets_Error_OnPageResponse(t *testing.T) {
network := &armnetwork.VirtualNetwork{
Resource: armnetwork.Resource{
Name: to.StringPtr("network1"),
ID: to.StringPtr("/subscriptions/7bfb2c5c-0000-0000-0000-fffa356eb406/resourceGroups/test-dev/providers/Microsoft.Network/virtualNetworks/network1"),
},
}
fakeClient := &mockSubnetsClient{}
expectedErr := errors.New("unexpected error")
mockPager := &mockSubnetsListPager{}
mockPager.On("Err").Return(expectedErr).Times(1)
mockPager.On("NextPage", mock.Anything).Return(true).Times(1)
mockPager.On("PageResponse").Return(armnetwork.SubnetsListResponse{}).Times(1)
fakeClient.On("List", "test-dev", "network1", mock.Anything).Return(mockPager)
s := &networkRepository{
subnetsClient: fakeClient,
cache: cache.New(0),
}
got, err := s.ListAllSubnets(network)
mockPager.AssertExpectations(t)
fakeClient.AssertExpectations(t)
assert.Equal(t, expectedErr, err)
assert.Nil(t, got)
}
func Test_ListAllSubnets_Error(t *testing.T) {
network := &armnetwork.VirtualNetwork{
Resource: armnetwork.Resource{
Name: to.StringPtr("network1"),
ID: to.StringPtr("/subscriptions/7bfb2c5c-0000-0000-0000-fffa356eb406/resourceGroups/test-dev/providers/Microsoft.Network/virtualNetworks/network1"),
},
}
fakeClient := &mockSubnetsClient{}
expectedErr := errors.New("unexpected error")
mockPager := &mockSubnetsListPager{}
mockPager.On("Err").Return(expectedErr).Times(1)
mockPager.On("NextPage", mock.Anything).Return(false).Times(1)
fakeClient.On("List", "test-dev", "network1", mock.Anything).Return(mockPager)
s := &networkRepository{
subnetsClient: fakeClient,
cache: cache.New(0),
}
got, err := s.ListAllSubnets(network)
mockPager.AssertExpectations(t)
fakeClient.AssertExpectations(t)
assert.Equal(t, expectedErr, err)
assert.Nil(t, got)
}
func Test_ListAllSubnets_ErrorOnInvalidNetworkID(t *testing.T) {
network := &armnetwork.VirtualNetwork{
Resource: armnetwork.Resource{
Name: to.StringPtr("network1"),
ID: to.StringPtr("foobar"),
},
}
fakeClient := &mockSubnetsClient{}
expectedErr := errors.New("parsing failed for foobar. Invalid resource Id format")
s := &networkRepository{
subnetsClient: fakeClient,
cache: cache.New(0),
}
got, err := s.ListAllSubnets(network)
fakeClient.AssertExpectations(t)
assert.Equal(t, expectedErr.Error(), err.Error())
assert.Nil(t, got)
}

View File

@ -0,0 +1,67 @@
package repository
import (
"context"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
"github.com/Azure/azure-sdk-for-go/sdk/resources/armresources"
"github.com/cloudskiff/driftctl/pkg/remote/azurerm/common"
"github.com/cloudskiff/driftctl/pkg/remote/cache"
)
type ResourcesRepository interface {
ListAllResourceGroups() ([]*armresources.ResourceGroup, error)
}
type resourcesListPager interface {
pager
PageResponse() armresources.ResourceGroupsListResponse
}
type resourcesClient interface {
List(options *armresources.ResourceGroupsListOptions) resourcesListPager
}
type resourcesClientImpl struct {
client *armresources.ResourceGroupsClient
}
func (c resourcesClientImpl) List(options *armresources.ResourceGroupsListOptions) resourcesListPager {
return c.client.List(options)
}
type resourcesRepository struct {
client resourcesClient
cache cache.Cache
}
func NewResourcesRepository(con *arm.Connection, config common.AzureProviderConfig, cache cache.Cache) *resourcesRepository {
return &resourcesRepository{
&resourcesClientImpl{armresources.NewResourceGroupsClient(con, config.SubscriptionID)},
cache,
}
}
func (s *resourcesRepository) ListAllResourceGroups() ([]*armresources.ResourceGroup, error) {
cacheKey := "resourcesListAllResourceGroups"
if v := s.cache.Get(cacheKey); v != nil {
return v.([]*armresources.ResourceGroup), nil
}
pager := s.client.List(nil)
results := make([]*armresources.ResourceGroup, 0)
for pager.NextPage(context.Background()) {
resp := pager.PageResponse()
if err := pager.Err(); err != nil {
return nil, err
}
results = append(results, resp.ResourceGroupsListResult.Value...)
}
if err := pager.Err(); err != nil {
return nil, err
}
s.cache.Put(cacheKey, results)
return results, nil
}

View File

@ -0,0 +1,152 @@
package repository
import (
"reflect"
"testing"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/resources/armresources"
"github.com/cloudskiff/driftctl/pkg/remote/cache"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
func Test_Resources_ListAllResourceGroups(t *testing.T) {
expectedResults := []*armresources.ResourceGroup{
{
ID: to.StringPtr("/subscriptions/008b5f48-1b66-4d92-a6b6-d215b4c9b473/resourceGroups/elie-dev"),
Name: to.StringPtr("elie-dev"),
},
{
ID: to.StringPtr("/subscriptions/008b5f48-1b66-4d92-a6b6-d215b4c9b473/resourceGroups/william-dev"),
Name: to.StringPtr("william-dev"),
},
{
ID: to.StringPtr("/subscriptions/008b5f48-1b66-4d92-a6b6-d215b4c9b473/resourceGroups/driftctl-sj-tests"),
Name: to.StringPtr("driftctl-sj-tests"),
},
}
testcases := []struct {
name string
mocks func(*mockResourcesListPager, *cache.MockCache)
expected []*armresources.ResourceGroup
wantErr string
}{
{
name: "should return resource groups",
mocks: func(mockPager *mockResourcesListPager, mockCache *cache.MockCache) {
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(armresources.ResourceGroupsListResponse{
ResourceGroupsListResult: armresources.ResourceGroupsListResult{
ResourceGroupListResult: armresources.ResourceGroupListResult{
Value: []*armresources.ResourceGroup{
{
ID: to.StringPtr("/subscriptions/008b5f48-1b66-4d92-a6b6-d215b4c9b473/resourceGroups/elie-dev"),
Name: to.StringPtr("elie-dev"),
},
{
ID: to.StringPtr("/subscriptions/008b5f48-1b66-4d92-a6b6-d215b4c9b473/resourceGroups/william-dev"),
Name: to.StringPtr("william-dev"),
},
},
},
},
}).Times(1)
mockPager.On("PageResponse").Return(armresources.ResourceGroupsListResponse{
ResourceGroupsListResult: armresources.ResourceGroupsListResult{
ResourceGroupListResult: armresources.ResourceGroupListResult{
Value: []*armresources.ResourceGroup{
{
ID: to.StringPtr("/subscriptions/008b5f48-1b66-4d92-a6b6-d215b4c9b473/resourceGroups/driftctl-sj-tests"),
Name: to.StringPtr("driftctl-sj-tests"),
},
},
},
},
}).Times(1)
mockCache.On("Get", "resourcesListAllResourceGroups").Return(nil).Times(1)
mockCache.On("Put", "resourcesListAllResourceGroups", expectedResults).Return(true).Times(1)
},
expected: expectedResults,
},
{
name: "should hit cache and return resource groups",
mocks: func(mockPager *mockResourcesListPager, mockCache *cache.MockCache) {
mockCache.On("Get", "resourcesListAllResourceGroups").Return(expectedResults).Times(1)
},
expected: expectedResults,
},
{
name: "should return remote error",
mocks: func(mockPager *mockResourcesListPager, mockCache *cache.MockCache) {
mockPager.On("NextPage", mock.Anything).Return(true).Times(1)
mockPager.On("PageResponse").Return(armresources.ResourceGroupsListResponse{
ResourceGroupsListResult: armresources.ResourceGroupsListResult{
ResourceGroupListResult: armresources.ResourceGroupListResult{
Value: []*armresources.ResourceGroup{},
},
},
}).Times(1)
mockPager.On("Err").Return(errors.New("remote error")).Times(1)
mockCache.On("Get", "resourcesListAllResourceGroups").Return(nil).Times(1)
},
wantErr: "remote error",
},
{
name: "should return remote error after fetching all pages",
mocks: func(mockPager *mockResourcesListPager, mockCache *cache.MockCache) {
mockPager.On("NextPage", mock.Anything).Return(true).Times(1)
mockPager.On("NextPage", mock.Anything).Return(false).Times(1)
mockPager.On("PageResponse").Return(armresources.ResourceGroupsListResponse{
ResourceGroupsListResult: armresources.ResourceGroupsListResult{
ResourceGroupListResult: armresources.ResourceGroupListResult{
Value: []*armresources.ResourceGroup{},
},
},
}).Times(1)
mockPager.On("Err").Return(nil).Times(1)
mockPager.On("Err").Return(errors.New("remote error")).Times(1)
mockCache.On("Get", "resourcesListAllResourceGroups").Return(nil).Times(1)
},
wantErr: "remote error",
},
}
for _, tt := range testcases {
t.Run(tt.name, func(t *testing.T) {
fakeClient := &mockResourcesClient{}
mockPager := &mockResourcesListPager{}
mockCache := &cache.MockCache{}
fakeClient.On("List", mock.Anything).Maybe().Return(mockPager)
tt.mocks(mockPager, mockCache)
s := &resourcesRepository{
client: fakeClient,
cache: mockCache,
}
got, err := s.ListAllResourceGroups()
if tt.wantErr != "" {
assert.EqualError(t, err, tt.wantErr)
} else {
assert.Nil(t, err)
}
fakeClient.AssertExpectations(t)
mockPager.AssertExpectations(t)
mockCache.AssertExpectations(t)
if !reflect.DeepEqual(got, tt.expected) {
t.Errorf("ListAllResourceGroups() got = %v, want %v", got, tt.expected)
}
})
}
}

View File

@ -112,3 +112,229 @@ func TestAzurermVirtualNetwork(t *testing.T) {
}) })
} }
} }
func TestAzurermRouteTables(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 route tables",
mocks: func(repository *repository.MockNetworkRepository, alerter *mocks.AlerterInterface) {
repository.On("ListAllRouteTables").Return([]*armnetwork.RouteTable{}, nil)
},
assertExpected: func(t *testing.T, got []*resource.Resource) {
assert.Len(t, got, 0)
},
},
{
test: "error listing route tables",
mocks: func(repository *repository.MockNetworkRepository, alerter *mocks.AlerterInterface) {
repository.On("ListAllRouteTables").Return(nil, dummyError)
},
wantErr: error2.NewResourceListingError(dummyError, resourceazure.AzureRouteTableResourceType),
},
{
test: "multiple route tables",
mocks: func(repository *repository.MockNetworkRepository, alerter *mocks.AlerterInterface) {
repository.On("ListAllRouteTables").Return([]*armnetwork.RouteTable{
{
Resource: armnetwork.Resource{
ID: to.StringPtr("route1"),
Name: to.StringPtr("route1"),
},
},
{
Resource: armnetwork.Resource{
ID: to.StringPtr("route2"),
Name: to.StringPtr("route2"),
},
},
}, nil)
},
assertExpected: func(t *testing.T, got []*resource.Resource) {
assert.Len(t, got, 2)
assert.Equal(t, got[0].ResourceId(), "route1")
assert.Equal(t, got[0].ResourceType(), resourceazure.AzureRouteTableResourceType)
assert.Equal(t, got[1].ResourceId(), "route2")
assert.Equal(t, got[1].ResourceType(), resourceazure.AzureRouteTableResourceType)
},
},
}
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.NewAzurermRouteTableEnumerator(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)
})
}
}
func TestAzurermSubnets(t *testing.T) {
dummyError := errors.New("this is an error")
networks := []*armnetwork.VirtualNetwork{
{
Resource: armnetwork.Resource{
ID: to.StringPtr("network1"),
},
},
{
Resource: armnetwork.Resource{
ID: to.StringPtr("network2"),
},
},
}
tests := []struct {
test string
mocks func(*repository.MockNetworkRepository, *mocks.AlerterInterface)
assertExpected func(t *testing.T, got []*resource.Resource)
wantErr error
}{
{
test: "no subnets",
mocks: func(repository *repository.MockNetworkRepository, alerter *mocks.AlerterInterface) {
repository.On("ListAllVirtualNetworks").Return(networks, nil)
repository.On("ListAllSubnets", networks[0]).Return([]*armnetwork.Subnet{}, nil).Times(1)
repository.On("ListAllSubnets", networks[1]).Return([]*armnetwork.Subnet{}, nil).Times(1)
},
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.NewResourceListingErrorWithType(dummyError, resourceazure.AzureSubnetResourceType, resourceazure.AzureVirtualNetworkResourceType),
},
{
test: "error listing subnets",
mocks: func(repository *repository.MockNetworkRepository, alerter *mocks.AlerterInterface) {
repository.On("ListAllVirtualNetworks").Return(networks, nil)
repository.On("ListAllSubnets", networks[0]).Return(nil, dummyError).Times(1)
},
wantErr: error2.NewResourceListingError(dummyError, resourceazure.AzureSubnetResourceType),
},
{
test: "multiple subnets",
mocks: func(repository *repository.MockNetworkRepository, alerter *mocks.AlerterInterface) {
repository.On("ListAllVirtualNetworks").Return(networks, nil)
repository.On("ListAllSubnets", networks[0]).Return([]*armnetwork.Subnet{
{
SubResource: armnetwork.SubResource{
ID: to.StringPtr("subnet1"),
},
},
{
SubResource: armnetwork.SubResource{
ID: to.StringPtr("subnet2"),
},
},
}, nil).Times(1)
repository.On("ListAllSubnets", networks[1]).Return([]*armnetwork.Subnet{
{
SubResource: armnetwork.SubResource{
ID: to.StringPtr("subnet3"),
},
},
{
SubResource: armnetwork.SubResource{
ID: to.StringPtr("subnet4"),
},
},
}, nil).Times(1)
},
assertExpected: func(t *testing.T, got []*resource.Resource) {
assert.Len(t, got, 4)
assert.Equal(t, got[0].ResourceId(), "subnet1")
assert.Equal(t, got[0].ResourceType(), resourceazure.AzureSubnetResourceType)
assert.Equal(t, got[1].ResourceId(), "subnet2")
assert.Equal(t, got[1].ResourceType(), resourceazure.AzureSubnetResourceType)
assert.Equal(t, got[2].ResourceId(), "subnet3")
assert.Equal(t, got[2].ResourceType(), resourceazure.AzureSubnetResourceType)
assert.Equal(t, got[3].ResourceId(), "subnet4")
assert.Equal(t, got[3].ResourceType(), resourceazure.AzureSubnetResourceType)
},
},
}
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.NewAzurermSubnetEnumerator(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)
})
}
}

View File

@ -0,0 +1,110 @@
package remote
import (
"testing"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/resources/armresources"
"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 TestAzurermResourceGroups(t *testing.T) {
dummyError := errors.New("this is an error")
tests := []struct {
test string
mocks func(*repository.MockResourcesRepository, *mocks.AlerterInterface)
assertExpected func(t *testing.T, got []*resource.Resource)
wantErr error
}{
{
test: "no resource group",
mocks: func(repository *repository.MockResourcesRepository, alerter *mocks.AlerterInterface) {
repository.On("ListAllResourceGroups").Return([]*armresources.ResourceGroup{}, nil)
},
assertExpected: func(t *testing.T, got []*resource.Resource) {
assert.Len(t, got, 0)
},
},
{
test: "error listing resource groups",
mocks: func(repository *repository.MockResourcesRepository, alerter *mocks.AlerterInterface) {
repository.On("ListAllResourceGroups").Return(nil, dummyError)
},
wantErr: error2.NewResourceListingError(dummyError, resourceazure.AzureResourceGroupResourceType),
},
{
test: "multiple resource groups",
mocks: func(repository *repository.MockResourcesRepository, alerter *mocks.AlerterInterface) {
repository.On("ListAllResourceGroups").Return([]*armresources.ResourceGroup{
{
ID: to.StringPtr("group1"),
Name: to.StringPtr("group1"),
},
{
ID: to.StringPtr("group2"),
Name: to.StringPtr("group2"),
},
}, nil)
},
assertExpected: func(t *testing.T, got []*resource.Resource) {
assert.Len(t, got, 2)
assert.Equal(t, got[0].ResourceId(), "group1")
assert.Equal(t, got[0].ResourceType(), resourceazure.AzureResourceGroupResourceType)
assert.Equal(t, got[1].ResourceId(), "group2")
assert.Equal(t, got[1].ResourceType(), resourceazure.AzureResourceGroupResourceType)
},
},
}
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.MockResourcesRepository{}
c.mocks(fakeRepo, alerter)
var repo repository.ResourcesRepository = fakeRepo
remoteLibrary.AddEnumerator(azurerm.NewAzurermResourceGroupEnumerator(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)
})
}
}

View File

@ -0,0 +1,16 @@
package azurerm
import "github.com/cloudskiff/driftctl/pkg/resource"
const AzureResourceGroupResourceType = "azurerm_resource_group"
func initAzureResourceGroupMetadata(resourceSchemaRepository resource.SchemaRepositoryInterface) {
resourceSchemaRepository.SetHumanReadableAttributesFunc(AzureResourceGroupResourceType, func(res *resource.Resource) map[string]string {
val := res.Attrs
attrs := make(map[string]string)
if name := val.GetString("name"); name != nil && *name != "" {
attrs["Name"] = *name
}
return attrs
})
}

View File

@ -0,0 +1,31 @@
package azurerm_test
import (
"testing"
"github.com/cloudskiff/driftctl/test"
"github.com/cloudskiff/driftctl/test/acceptance"
)
func TestAcc_Azure_ResourceGroup(t *testing.T) {
acceptance.Run(t, acceptance.AccTestCase{
TerraformVersion: "0.15.5",
Paths: []string{"./testdata/acc/azurerm_resource_group"},
Args: []string{
"scan",
"--to", "azure+tf",
"--filter", "Type=='azurerm_resource_group' && contains(Id, 'acc-test-res-group-')",
},
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

@ -0,0 +1,18 @@
package azurerm
import (
"github.com/cloudskiff/driftctl/pkg/resource"
)
const AzureRouteTableResourceType = "azurerm_route_table"
func initAzureRouteTableMetaData(resourceSchemaRepository resource.SchemaRepositoryInterface) {
resourceSchemaRepository.SetHumanReadableAttributesFunc(AzureRouteTableResourceType, func(res *resource.Resource) map[string]string {
attrs := make(map[string]string)
if v := res.Attributes().GetString("name"); v != nil && *v != "" {
attrs["Name"] = *v
}
return attrs
})
}

View File

@ -0,0 +1,31 @@
package azurerm_test
import (
"testing"
"github.com/cloudskiff/driftctl/test"
"github.com/cloudskiff/driftctl/test/acceptance"
)
func TestAcc_Azure_RouteTable(t *testing.T) {
acceptance.Run(t, acceptance.AccTestCase{
TerraformVersion: "0.15.5",
Paths: []string{"./testdata/acc/azurerm_route_table"},
Args: []string{
"scan",
"--to", "azure+tf",
"--filter", "Type=='azurerm_route_table'",
},
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,3 @@
package azurerm
const AzureSubnetResourceType = "azurerm_subnet"

View File

@ -0,0 +1,32 @@
package azurerm_test
import (
"testing"
"github.com/cloudskiff/driftctl/test"
"github.com/cloudskiff/driftctl/test/acceptance"
)
func TestAcc_Azure_Subnet(t *testing.T) {
acceptance.Run(t, acceptance.AccTestCase{
TerraformVersion: "0.15.5",
Paths: []string{"./testdata/acc/azurerm_subnet"},
Args: []string{
"scan",
"--to", "azure+tf",
"--filter", "Type=='azurerm_subnet' || Type=='azurerm_virtual_network'",
},
Checks: []acceptance.AccCheck{
{
Check: func(result *test.ScanResult, stdout string, err error) {
if err != nil {
t.Fatal(err)
}
result.AssertInfrastructureIsInSync()
// We should have one azurerm_virtual_network and two azurerm_subnet
result.AssertManagedCount(3)
},
},
},
})
}

View File

@ -4,4 +4,6 @@ import "github.com/cloudskiff/driftctl/pkg/resource"
func InitResourcesMetadata(resourceSchemaRepository resource.SchemaRepositoryInterface) { func InitResourcesMetadata(resourceSchemaRepository resource.SchemaRepositoryInterface) {
initAzureVirtualNetworkMetaData(resourceSchemaRepository) initAzureVirtualNetworkMetaData(resourceSchemaRepository)
initAzureRouteTableMetaData(resourceSchemaRepository)
initAzureResourceGroupMetadata(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/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",
]
}

View File

@ -0,0 +1,22 @@
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~> 2.71.0"
}
}
}
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "test-1" {
name = "acc-test-res-group-1"
location = "West Europe"
}
resource "azurerm_resource_group" "test-2" {
name = "acc-test-res-group-2"
location = "West Europe"
}

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/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",
]
}

View File

@ -0,0 +1,22 @@
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_route_table" "table1" {
name = "table1"
location = data.azurerm_resource_group.qa1.location
resource_group_name = data.azurerm_resource_group.qa1.name
}

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/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",
]
}

View File

@ -0,0 +1,36 @@
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"]
subnet {
address_prefix = "10.0.2.0/24"
name = "nested-subnet"
}
}
resource "azurerm_subnet" "example" {
name = "non-nested-subnet"
resource_group_name = data.azurerm_resource_group.qa1.name
virtual_network_name = azurerm_virtual_network.test.name
address_prefixes = ["10.0.1.0/24"]
}

View File

@ -23,9 +23,8 @@ func TestAcc_Google_ComputeFirewall(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
result.AssertInfrastructureIsInSync()
result.AssertManagedCount(3) result.AssertManagedCount(3)
result.AssertDriftCountTotal(0)
result.AssertUnmanagedCount(4) // Default VPCs
}, },
}, },
}, },

View File

@ -9,6 +9,10 @@ terraform {
} }
} }
resource "google_compute_network" "driftctl-unittest-instance" {
name = "driftctl-unittest-instance"
}
resource "google_compute_instance" "default" { resource "google_compute_instance" "default" {
name = "test" name = "test"
machine_type = "e2-medium" machine_type = "e2-medium"
@ -21,6 +25,6 @@ resource "google_compute_instance" "default" {
} }
network_interface { network_interface {
network = "default" network = google_compute_network.driftctl-unittest-instance.name
} }
} }

View File

@ -84,6 +84,9 @@ var supportedTypes = map[string]struct{}{
"azurerm_storage_account": {}, "azurerm_storage_account": {},
"azurerm_storage_container": {}, "azurerm_storage_container": {},
"azurerm_virtual_network": {}, "azurerm_virtual_network": {},
"azurerm_route_table": {},
"azurerm_resource_group": {},
"azurerm_subnet": {},
} }
func IsResourceTypeSupported(ty string) bool { func IsResourceTypeSupported(ty string) bool {