Merge pull request #1213 from cloudskiff/fea/azurerm_private_dns_cname_record

add azurerm private dns cname record
main
Elie 2021-11-18 16:48:53 +01:00 committed by GitHub
commit 05bd1d1ef8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 726 additions and 57 deletions

View File

@ -445,6 +445,7 @@ func TestTerraformStateReader_Azure_Resources(t *testing.T) {
{name: "private dns zone", dirName: "azurerm_private_dns_zone", wantErr: false},
{name: "private dns a record", dirName: "azurerm_private_dns_a_record", wantErr: false},
{name: "private dns aaaa record", dirName: "azurerm_private_dns_aaaa_record", wantErr: false},
{name: "private dns cname record", dirName: "azurerm_private_dns_cname_record", wantErr: false},
{name: "images", dirName: "azurerm_image", wantErr: false},
{name: "ssh public key", dirName: "azurerm_ssh_public_key", wantErr: false},
}

View File

@ -0,0 +1,28 @@
[
{
"Id": "/subscriptions/7bfb2c5c-7308-46ed-8ae4-fffa356eb406/resourceGroups/martin-dev/providers/Microsoft.Network/privateDnsZones/thisisatestusingtf.com/CNAME/othertest",
"Type": "azurerm_private_dns_cname_record",
"Attrs": {
"fqdn": "othertest.thisisatestusingtf.com.",
"id": "/subscriptions/7bfb2c5c-7308-46ed-8ae4-fffa356eb406/resourceGroups/martin-dev/providers/Microsoft.Network/privateDnsZones/thisisatestusingtf.com/CNAME/othertest",
"name": "othertest",
"record": "othertest.com",
"resource_group_name": "martin-dev",
"ttl": 300,
"zone_name": "thisisatestusingtf.com"
}
},
{
"Id": "/subscriptions/7bfb2c5c-7308-46ed-8ae4-fffa356eb406/resourceGroups/martin-dev/providers/Microsoft.Network/privateDnsZones/thisisatestusingtf.com/CNAME/test",
"Type": "azurerm_private_dns_cname_record",
"Attrs": {
"fqdn": "test.thisisatestusingtf.com.",
"id": "/subscriptions/7bfb2c5c-7308-46ed-8ae4-fffa356eb406/resourceGroups/martin-dev/providers/Microsoft.Network/privateDnsZones/thisisatestusingtf.com/CNAME/test",
"name": "test",
"record": "test.com",
"resource_group_name": "martin-dev",
"ttl": 300,
"zone_name": "thisisatestusingtf.com"
}
}
]

View File

@ -0,0 +1,65 @@
{
"version": 4,
"terraform_version": "0.14.5",
"serial": 19,
"lineage": "7f22faf7-4655-0f68-3b00-9099a46450a7",
"outputs": {},
"resources": [
{
"mode": "managed",
"type": "azurerm_private_dns_cname_record",
"name": "othertestcname",
"provider": "provider[\"registry.terraform.io/hashicorp/azurerm\"]",
"instances": [
{
"schema_version": 0,
"attributes": {
"fqdn": "othertest.thisisatestusingtf.com.",
"id": "/subscriptions/7bfb2c5c-7308-46ed-8ae4-fffa356eb406/resourceGroups/martin-dev/providers/Microsoft.Network/privateDnsZones/thisisatestusingtf.com/CNAME/othertest",
"name": "othertest",
"record": "othertest.com",
"resource_group_name": "martin-dev",
"tags": null,
"timeouts": null,
"ttl": 300,
"zone_name": "thisisatestusingtf.com"
},
"sensitive_attributes": [],
"private": "eyJlMmJmYjczMC1lY2FhLTExZTYtOGY4OC0zNDM2M2JjN2M0YzAiOnsiY3JlYXRlIjoxODAwMDAwMDAwMDAwLCJkZWxldGUiOjE4MDAwMDAwMDAwMDAsInJlYWQiOjMwMDAwMDAwMDAwMCwidXBkYXRlIjoxODAwMDAwMDAwMDAwfX0=",
"dependencies": [
"azurerm_private_dns_zone.testzone",
"data.azurerm_resource_group.martin"
]
}
]
},
{
"mode": "managed",
"type": "azurerm_private_dns_cname_record",
"name": "testcname",
"provider": "provider[\"registry.terraform.io/hashicorp/azurerm\"]",
"instances": [
{
"schema_version": 0,
"attributes": {
"fqdn": "test.thisisatestusingtf.com.",
"id": "/subscriptions/7bfb2c5c-7308-46ed-8ae4-fffa356eb406/resourceGroups/martin-dev/providers/Microsoft.Network/privateDnsZones/thisisatestusingtf.com/CNAME/test",
"name": "test",
"record": "test.com",
"resource_group_name": "martin-dev",
"tags": null,
"timeouts": null,
"ttl": 300,
"zone_name": "thisisatestusingtf.com"
},
"sensitive_attributes": [],
"private": "eyJlMmJmYjczMC1lY2FhLTExZTYtOGY4OC0zNDM2M2JjN2M0YzAiOnsiY3JlYXRlIjoxODAwMDAwMDAwMDAwLCJkZWxldGUiOjE4MDAwMDAwMDAwMDAsInJlYWQiOjMwMDAwMDAwMDAwMCwidXBkYXRlIjoxODAwMDAwMDAwMDAwfX0=",
"dependencies": [
"azurerm_private_dns_zone.testzone",
"data.azurerm_resource_group.martin"
]
}
]
}
]
}

View File

@ -0,0 +1,54 @@
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 AzurermPrivateDNSCNameRecordEnumerator struct {
repository repository.PrivateDNSRepository
factory resource.ResourceFactory
}
func NewAzurermPrivateDNSCNameRecordEnumerator(repo repository.PrivateDNSRepository, factory resource.ResourceFactory) *AzurermPrivateDNSCNameRecordEnumerator {
return &AzurermPrivateDNSCNameRecordEnumerator{
repository: repo,
factory: factory,
}
}
func (e *AzurermPrivateDNSCNameRecordEnumerator) SupportedType() resource.ResourceType {
return azurerm.AzurePrivateDNSCNameRecordResourceType
}
func (e *AzurermPrivateDNSCNameRecordEnumerator) Enumerate() ([]*resource.Resource, error) {
zones, err := e.repository.ListAllPrivateZones()
if err != nil {
return nil, remoteerror.NewResourceListingErrorWithType(err, string(e.SupportedType()), azurerm.AzurePrivateDNSZoneResourceType)
}
results := make([]*resource.Resource, 0)
for _, zone := range zones {
records, err := e.repository.ListAllCNAMERecords(zone)
if err != nil {
return nil, remoteerror.NewResourceListingError(err, string(e.SupportedType()))
}
for _, record := range records {
results = append(
results,
e.factory.CreateAbstractResource(
string(e.SupportedType()),
*record.ID,
map[string]interface{}{},
),
)
}
}
return results, err
}

View File

@ -75,6 +75,9 @@ func Init(
remoteLibrary.AddEnumerator(NewAzurermPrivateDNSAAAARecordEnumerator(privateDNSRepo, factory))
remoteLibrary.AddDetailsFetcher(azurerm.AzurePrivateDNSAAAARecordResourceType, common.NewGenericDetailsFetcher(azurerm.AzurePrivateDNSAAAARecordResourceType, provider, deserializer))
remoteLibrary.AddEnumerator(NewAzurermPrivateDNSCNameRecordEnumerator(privateDNSRepo, factory))
remoteLibrary.AddDetailsFetcher(azurerm.AzurePrivateDNSCNameRecordResourceType, common.NewGenericDetailsFetcher(azurerm.AzurePrivateDNSCNameRecordResourceType, provider, deserializer))
remoteLibrary.AddEnumerator(NewAzurermImageEnumerator(computeRepo, factory))
remoteLibrary.AddEnumerator(NewAzurermSSHPublicKeyEnumerator(computeRepo, factory))
remoteLibrary.AddDetailsFetcher(azurerm.AzureSSHPublicKeyResourceType, common.NewGenericDetailsFetcher(azurerm.AzureSSHPublicKeyResourceType, provider, deserializer))

View File

@ -58,6 +58,29 @@ func (_m *MockPrivateDNSRepository) ListAllARecords(zone *armprivatedns.PrivateZ
return r0, r1
}
// ListAllCNAMERecords provides a mock function with given fields: zone
func (_m *MockPrivateDNSRepository) ListAllCNAMERecords(zone *armprivatedns.PrivateZone) ([]*armprivatedns.RecordSet, error) {
ret := _m.Called(zone)
var r0 []*armprivatedns.RecordSet
if rf, ok := ret.Get(0).(func(*armprivatedns.PrivateZone) []*armprivatedns.RecordSet); ok {
r0 = rf(zone)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).([]*armprivatedns.RecordSet)
}
}
var r1 error
if rf, ok := ret.Get(1).(func(*armprivatedns.PrivateZone) error); ok {
r1 = rf(zone)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// ListAllPrivateZones provides a mock function with given fields:
func (_m *MockPrivateDNSRepository) ListAllPrivateZones() ([]*armprivatedns.PrivateZone, error) {
ret := _m.Called()

View File

@ -15,6 +15,7 @@ type PrivateDNSRepository interface {
ListAllPrivateZones() ([]*armprivatedns.PrivateZone, error)
ListAllARecords(zone *armprivatedns.PrivateZone) ([]*armprivatedns.RecordSet, error)
ListAllAAAARecords(zone *armprivatedns.PrivateZone) ([]*armprivatedns.RecordSet, error)
ListAllCNAMERecords(zone *armprivatedns.PrivateZone) ([]*armprivatedns.RecordSet, error)
}
type privateDNSZoneListPager interface {
@ -98,11 +99,6 @@ func (s *privateDNSRepository) listAllRecords(zone *armprivatedns.PrivateZone) (
}
func (s *privateDNSRepository) ListAllARecords(zone *armprivatedns.PrivateZone) ([]*armprivatedns.RecordSet, error) {
cacheKey := fmt.Sprintf("privateDNSListAllARecords-%s", *zone.ID)
if v := s.cache.Get(cacheKey); v != nil {
return v.([]*armprivatedns.RecordSet), nil
}
records, err := s.listAllRecords(zone)
if err != nil {
return nil, err
@ -115,18 +111,10 @@ func (s *privateDNSRepository) ListAllARecords(zone *armprivatedns.PrivateZone)
results = append(results, record)
}
s.cache.Put(cacheKey, results)
return results, nil
}
func (s *privateDNSRepository) ListAllAAAARecords(zone *armprivatedns.PrivateZone) ([]*armprivatedns.RecordSet, error) {
cacheKey := fmt.Sprintf("privateDNSListAllAAAARecords-%s", *zone.ID)
if v := s.cache.Get(cacheKey); v != nil {
return v.([]*armprivatedns.RecordSet), nil
}
records, err := s.listAllRecords(zone)
if err != nil {
return nil, err
@ -139,15 +127,30 @@ func (s *privateDNSRepository) ListAllAAAARecords(zone *armprivatedns.PrivateZon
results = append(results, record)
}
return results, nil
}
s.cache.Put(cacheKey, results)
func (s *privateDNSRepository) ListAllCNAMERecords(zone *armprivatedns.PrivateZone) ([]*armprivatedns.RecordSet, error) {
records, err := s.listAllRecords(zone)
if err != nil {
return nil, err
}
results := make([]*armprivatedns.RecordSet, 0)
for _, record := range records {
if record.Properties.CnameRecord == nil {
continue
}
results = append(results, record)
}
return results, nil
}
func (s *privateDNSRepository) ListAllPrivateZones() ([]*armprivatedns.PrivateZone, error) {
cacheKey := "privateDNSListAllPrivateZones"
if v := s.cache.Get(cacheKey); v != nil {
v := s.cache.GetAndLock(cacheKey)
defer s.cache.Unlock(cacheKey)
if v != nil {
return v.([]*armprivatedns.PrivateZone), nil
}

View File

@ -100,7 +100,8 @@ func Test_ListAllPrivateZones_MultiplesResults(t *testing.T) {
fakeClient.On("List", mock.Anything).Return(mockPager)
c := &cache.MockCache{}
c.On("Get", "privateDNSListAllPrivateZones").Return(nil).Times(1)
c.On("GetAndLock", "privateDNSListAllPrivateZones").Return(nil).Times(1)
c.On("Unlock", "privateDNSListAllPrivateZones").Times(1)
c.On("Put", "privateDNSListAllPrivateZones", expected).Return(true).Times(1)
s := &privateDNSRepository{
zoneClient: fakeClient,
@ -136,7 +137,9 @@ func Test_ListAllPrivateZones_MultiplesResults_WithCache(t *testing.T) {
fakeClient := &mockPrivateZonesClient{}
c := &cache.MockCache{}
c.On("Get", "privateDNSListAllPrivateZones").Return(expected).Times(1)
c.On("GetAndLock", "privateDNSListAllPrivateZones").Return(expected).Times(1)
c.On("Unlock", "privateDNSListAllPrivateZones").Times(1)
s := &privateDNSRepository{
zoneClient: fakeClient,
cache: c,
@ -279,8 +282,6 @@ func Test_ListAllARecords_MultiplesResults(t *testing.T) {
fakeRecordSetClient.On("List", "rgid", "zone", (*armprivatedns.RecordSetsListOptions)(nil)).Return(mockPager)
c := &cache.MockCache{}
c.On("Get", "privateDNSListAllARecords-/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com").Return(nil).Times(1)
c.On("Put", "privateDNSListAllARecords-/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com", expected).Return(true).Times(1)
c.On("GetAndLock", "privateDNSlistAllRecords-/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com").Return(nil).Times(1)
c.On("Unlock", "privateDNSlistAllRecords-/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com").Return().Times(1)
c.On("Put", "privateDNSlistAllRecords-/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com", mock.Anything).Return(true).Times(1)
@ -319,13 +320,19 @@ func Test_ListAllARecords_MultiplesResults_WithCache(t *testing.T) {
ID: to.StringPtr("record1"),
},
},
Properties: &armprivatedns.RecordSetProperties{
ARecords: []*armprivatedns.ARecord{
{IPv4Address: to.StringPtr("ip")},
},
},
},
}
fakeRecordSetClient := &mockPrivateRecordSetClient{}
c := &cache.MockCache{}
c.On("Get", "privateDNSListAllARecords-/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com").Return(expected).Times(1)
c.On("GetAndLock", "privateDNSlistAllRecords-/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com").Return(expected).Times(1)
c.On("Unlock", "privateDNSlistAllRecords-/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com").Times(1)
s := &privateDNSRepository{
recordClient: fakeRecordSetClient,
cache: c,
@ -482,8 +489,6 @@ func Test_ListAllAAAARecords_MultiplesResults(t *testing.T) {
fakeRecordSetClient.On("List", "rgid", "zone", (*armprivatedns.RecordSetsListOptions)(nil)).Return(mockPager)
c := &cache.MockCache{}
c.On("Get", "privateDNSListAllAAAARecords-/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com").Return(nil).Times(1)
c.On("Put", "privateDNSListAllAAAARecords-/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com", expected).Return(true).Times(1)
c.On("GetAndLock", "privateDNSlistAllRecords-/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com").Return(nil).Times(1)
c.On("Unlock", "privateDNSlistAllRecords-/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com").Return().Times(1)
c.On("Put", "privateDNSlistAllRecords-/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com", mock.Anything).Return(true).Times(1)
@ -522,13 +527,19 @@ func Test_ListAllAAAARecords_MultiplesResults_WithCache(t *testing.T) {
ID: to.StringPtr("record1"),
},
},
Properties: &armprivatedns.RecordSetProperties{
AaaaRecords: []*armprivatedns.AaaaRecord{
{IPv6Address: to.StringPtr("ip")},
},
},
},
}
fakeRecordSetClient := &mockPrivateRecordSetClient{}
c := &cache.MockCache{}
c.On("Get", "privateDNSListAllAAAARecords-/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com").Return(expected).Times(1)
c.On("GetAndLock", "privateDNSlistAllRecords-/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com").Return(expected).Times(1)
c.On("Unlock", "privateDNSlistAllRecords-/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com").Times(1)
s := &privateDNSRepository{
recordClient: fakeRecordSetClient,
cache: c,
@ -588,3 +599,212 @@ func Test_ListAllAAAARecords_Error(t *testing.T) {
}
// endregion
// region CNAMERecord
func Test_ListAllCNAMERecords_MultiplesResults(t *testing.T) {
expected := []*armprivatedns.RecordSet{
{
ProxyResource: armprivatedns.ProxyResource{
Resource: armprivatedns.Resource{
ID: to.StringPtr("record1"),
},
},
Properties: &armprivatedns.RecordSetProperties{
CnameRecord: &armprivatedns.CnameRecord{
Cname: to.StringPtr("cname"),
},
},
},
{
ProxyResource: armprivatedns.ProxyResource{
Resource: armprivatedns.Resource{
ID: to.StringPtr("record3"),
},
},
Properties: &armprivatedns.RecordSetProperties{
CnameRecord: &armprivatedns.CnameRecord{
Cname: to.StringPtr("cname"),
},
},
},
}
fakeRecordSetClient := &mockPrivateRecordSetClient{}
mockPager := &mockPrivateDNSRecordSetListPager{}
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(armprivatedns.RecordSetsListResponse{
RecordSetsListResult: armprivatedns.RecordSetsListResult{
RecordSetListResult: armprivatedns.RecordSetListResult{
Value: []*armprivatedns.RecordSet{
{
ProxyResource: armprivatedns.ProxyResource{
Resource: armprivatedns.Resource{
ID: to.StringPtr("record1"),
},
},
Properties: &armprivatedns.RecordSetProperties{
CnameRecord: &armprivatedns.CnameRecord{
Cname: to.StringPtr("cname"),
},
},
},
{
ProxyResource: armprivatedns.ProxyResource{
Resource: armprivatedns.Resource{
ID: to.StringPtr("record2"),
},
},
Properties: &armprivatedns.RecordSetProperties{},
},
},
},
},
}).Times(1)
mockPager.On("PageResponse").Return(armprivatedns.RecordSetsListResponse{
RecordSetsListResult: armprivatedns.RecordSetsListResult{
RecordSetListResult: armprivatedns.RecordSetListResult{
Value: []*armprivatedns.RecordSet{
{
ProxyResource: armprivatedns.ProxyResource{
Resource: armprivatedns.Resource{
ID: to.StringPtr("record3"),
},
},
Properties: &armprivatedns.RecordSetProperties{
CnameRecord: &armprivatedns.CnameRecord{
Cname: to.StringPtr("cname"),
},
},
},
{
ProxyResource: armprivatedns.ProxyResource{
Resource: armprivatedns.Resource{
ID: to.StringPtr("record4"),
},
},
Properties: &armprivatedns.RecordSetProperties{},
},
},
},
},
}).Times(1)
fakeRecordSetClient.On("List", "rgid", "zone", (*armprivatedns.RecordSetsListOptions)(nil)).Return(mockPager)
c := &cache.MockCache{}
c.On("GetAndLock", "privateDNSlistAllRecords-/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com").Return(nil).Times(1)
c.On("Unlock", "privateDNSlistAllRecords-/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com").Return().Times(1)
c.On("Put", "privateDNSlistAllRecords-/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com", mock.Anything).Return(true).Times(1)
s := &privateDNSRepository{
recordClient: fakeRecordSetClient,
cache: c,
}
got, err := s.ListAllCNAMERecords(&armprivatedns.PrivateZone{
TrackedResource: armprivatedns.TrackedResource{
Resource: armprivatedns.Resource{
ID: to.StringPtr("/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com"),
Name: to.StringPtr("zone"),
},
},
})
if err != nil {
t.Errorf("ListAllCNAMERecords() error = %v", err)
return
}
mockPager.AssertExpectations(t)
fakeRecordSetClient.AssertExpectations(t)
c.AssertExpectations(t)
if !reflect.DeepEqual(got, expected) {
t.Errorf("ListAllCNAMERecords() got = %v, want %v", got, expected)
}
}
func Test_ListAllCNAMERecords_MultiplesResults_WithCache(t *testing.T) {
expected := []*armprivatedns.RecordSet{
{
ProxyResource: armprivatedns.ProxyResource{
Resource: armprivatedns.Resource{
ID: to.StringPtr("record1"),
},
},
Properties: &armprivatedns.RecordSetProperties{
CnameRecord: &armprivatedns.CnameRecord{
Cname: to.StringPtr("cname"),
},
},
},
}
fakeRecordSetClient := &mockPrivateRecordSetClient{}
c := &cache.MockCache{}
c.On("GetAndLock", "privateDNSlistAllRecords-/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com").Return(expected).Times(1)
c.On("Unlock", "privateDNSlistAllRecords-/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com").Return().Times(1)
s := &privateDNSRepository{
recordClient: fakeRecordSetClient,
cache: c,
}
got, err := s.ListAllCNAMERecords(&armprivatedns.PrivateZone{
TrackedResource: armprivatedns.TrackedResource{
Resource: armprivatedns.Resource{
ID: to.StringPtr("/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com"),
Name: to.StringPtr("zone"),
},
},
})
if err != nil {
t.Errorf("ListAllCNAMERecords() error = %v", err)
return
}
fakeRecordSetClient.AssertExpectations(t)
c.AssertExpectations(t)
if !reflect.DeepEqual(got, expected) {
t.Errorf("ListAllCNAMERecords() got = %v, want %v", got, expected)
}
}
func Test_ListAllCNAMERecords_Error(t *testing.T) {
fakeClient := &mockPrivateRecordSetClient{}
expectedErr := errors.New("unexpected error")
mockPager := &mockPrivateDNSRecordSetListPager{}
mockPager.On("Err").Return(expectedErr).Times(1)
mockPager.On("NextPage", mock.Anything).Return(true).Times(1)
mockPager.On("PageResponse").Return(armprivatedns.RecordSetsListResponse{}).Times(1)
fakeClient.On("List", "rgid", "zone", (*armprivatedns.RecordSetsListOptions)(nil)).Return(mockPager)
s := &privateDNSRepository{
recordClient: fakeClient,
cache: cache.New(0),
}
got, err := s.ListAllCNAMERecords(&armprivatedns.PrivateZone{
TrackedResource: armprivatedns.TrackedResource{
Resource: armprivatedns.Resource{
ID: to.StringPtr("/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com"),
Name: to.StringPtr("zone"),
},
},
})
mockPager.AssertExpectations(t)
fakeClient.AssertExpectations(t)
assert.Equal(t, expectedErr, err)
assert.Nil(t, got)
}
// endregion

View File

@ -456,3 +456,146 @@ func TestAzurermPrivateDNSAAAARecord(t *testing.T) {
})
}
}
func TestAzurermPrivateDNSCNAMERecord(t *testing.T) {
dummyError := errors.New("this is an error")
tests := []struct {
test string
dirName string
mocks func(*repository.MockPrivateDNSRepository, *mocks.AlerterInterface)
wantErr error
}{
{
test: "no private cname record",
dirName: "azurerm_private_dns_cname_record_empty",
mocks: func(repository *repository.MockPrivateDNSRepository, alerter *mocks.AlerterInterface) {
repository.On("ListAllPrivateZones").Return([]*armprivatedns.PrivateZone{}, nil)
},
},
{
test: "error listing private zone",
dirName: "azurerm_private_dns_cname_record_empty",
mocks: func(repository *repository.MockPrivateDNSRepository, alerter *mocks.AlerterInterface) {
repository.On("ListAllPrivateZones").Return(nil, dummyError)
},
wantErr: remoteerr.NewResourceListingErrorWithType(dummyError, resourceazure.AzurePrivateDNSCNameRecordResourceType, resourceazure.AzurePrivateDNSZoneResourceType),
},
{
test: "error listing private cname records",
dirName: "azurerm_private_dns_cname_record_empty",
mocks: func(repository *repository.MockPrivateDNSRepository, alerter *mocks.AlerterInterface) {
repository.On("ListAllPrivateZones").Return([]*armprivatedns.PrivateZone{
{
TrackedResource: armprivatedns.TrackedResource{
Resource: armprivatedns.Resource{
ID: to.StringPtr("/subscriptions/7bfb2c5c-7308-46ed-8ae4-fffa356eb406/resourceGroups/martin-dev/providers/Microsoft.Network/privateDnsZones/thisisatestusingtf.com"),
Name: to.StringPtr("thisisatestusingtf.com"),
},
},
},
}, nil)
repository.On("ListAllCNAMERecords", mock.Anything).Return(nil, dummyError)
},
wantErr: remoteerr.NewResourceListingError(dummyError, resourceazure.AzurePrivateDNSCNameRecordResourceType),
},
{
test: "multiple private cname records",
dirName: "azurerm_private_dns_cname_record_multiple",
mocks: func(repository *repository.MockPrivateDNSRepository, alerter *mocks.AlerterInterface) {
repository.On("ListAllPrivateZones").Return([]*armprivatedns.PrivateZone{
{
TrackedResource: armprivatedns.TrackedResource{
Resource: armprivatedns.Resource{
ID: to.StringPtr("/subscriptions/7bfb2c5c-7308-46ed-8ae4-fffa356eb406/resourceGroups/martin-dev/providers/Microsoft.Network/privateDnsZones/thisisatestusingtf.com"),
Name: to.StringPtr("thisisatestusingtf.com"),
},
},
},
}, nil)
repository.On("ListAllCNAMERecords", mock.Anything).Return([]*armprivatedns.RecordSet{
{
ProxyResource: armprivatedns.ProxyResource{
Resource: armprivatedns.Resource{
ID: to.StringPtr("/subscriptions/7bfb2c5c-7308-46ed-8ae4-fffa356eb406/resourceGroups/martin-dev/providers/Microsoft.Network/privateDnsZones/thisisatestusingtf.com/CNAME/test"),
Name: to.StringPtr("test"),
},
},
},
{
ProxyResource: armprivatedns.ProxyResource{
Resource: armprivatedns.Resource{
ID: to.StringPtr("/subscriptions/7bfb2c5c-7308-46ed-8ae4-fffa356eb406/resourceGroups/martin-dev/providers/Microsoft.Network/privateDnsZones/thisisatestusingtf.com/CNAME/othertest"),
Name: to.StringPtr("othertest"),
},
},
},
}, nil).Once()
},
},
}
providerVersion := "2.71.0"
schemaRepository := testresource.InitFakeSchemaRepository("azurerm", providerVersion)
resourceazure.InitResourcesMetadata(schemaRepository)
factory := terraform.NewTerraformResourceFactory(schemaRepository)
deserializer := resource.NewDeserializer(factory)
for _, c := range tests {
t.Run(c.test, func(tt *testing.T) {
shouldUpdate := c.dirName == *goldenfile.Update
scanOptions := ScannerOptions{Deep: true}
providerLibrary := terraform.NewProviderLibrary()
remoteLibrary := common.NewRemoteLibrary()
// Initialize mocks
alerter := &mocks.AlerterInterface{}
fakeRepo := &repository.MockPrivateDNSRepository{}
c.mocks(fakeRepo, alerter)
var repo repository.PrivateDNSRepository = fakeRepo
providerVersion := "2.71.0"
realProvider, err := terraformtest.InitTestAzureProvider(providerLibrary, providerVersion)
if err != nil {
t.Fatal(err)
}
provider := terraformtest.NewFakeTerraformProvider(realProvider)
provider.WithResponse(c.dirName)
// Replace mock by real resources if we are in update mode
if shouldUpdate {
err := realProvider.Init()
if err != nil {
t.Fatal(err)
}
provider.ShouldUpdate()
cred, err := azidentity.NewDefaultAzureCredential(&azidentity.DefaultAzureCredentialOptions{})
if err != nil {
t.Fatal(err)
}
con := arm.NewDefaultConnection(cred, nil)
repo = repository.NewPrivateDNSRepository(con, realProvider.GetConfig(), cache.New(0))
}
remoteLibrary.AddEnumerator(azurerm.NewAzurermPrivateDNSCNameRecordEnumerator(repo, factory))
remoteLibrary.AddDetailsFetcher(resourceazure.AzurePrivateDNSCNameRecordResourceType, common.NewGenericDetailsFetcher(resourceazure.AzurePrivateDNSCNameRecordResourceType, provider, deserializer))
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
}
test.TestAgainstGoldenFile(got, resourceazure.AzurePrivateDNSCNameRecordResourceType, c.dirName, provider, deserializer, shouldUpdate, tt)
alerter.AssertExpectations(tt)
fakeRepo.AssertExpectations(tt)
})
}
}

View File

@ -0,0 +1,5 @@
{
"Typ": "WyJvYmplY3QiLHsiZnFkbiI6InN0cmluZyIsImlkIjoic3RyaW5nIiwibmFtZSI6InN0cmluZyIsInJlY29yZCI6InN0cmluZyIsInJlc291cmNlX2dyb3VwX25hbWUiOiJzdHJpbmciLCJ0YWdzIjpbIm1hcCIsInN0cmluZyJdLCJ0aW1lb3V0cyI6WyJvYmplY3QiLHsiY3JlYXRlIjoic3RyaW5nIiwiZGVsZXRlIjoic3RyaW5nIiwicmVhZCI6InN0cmluZyIsInVwZGF0ZSI6InN0cmluZyJ9XSwidHRsIjoibnVtYmVyIiwiem9uZV9uYW1lIjoic3RyaW5nIn1d",
"Val": "eyJmcWRuIjoib3RoZXJ0ZXN0LnRoaXNpc2F0ZXN0dXNpbmd0Zi5jb20uIiwiaWQiOiIvc3Vic2NyaXB0aW9ucy83YmZiMmM1Yy03MzA4LTQ2ZWQtOGFlNC1mZmZhMzU2ZWI0MDYvcmVzb3VyY2VHcm91cHMvbWFydGluLWRldi9wcm92aWRlcnMvTWljcm9zb2Z0Lk5ldHdvcmsvcHJpdmF0ZURuc1pvbmVzL3RoaXNpc2F0ZXN0dXNpbmd0Zi5jb20vQ05BTUUvb3RoZXJ0ZXN0IiwibmFtZSI6Im90aGVydGVzdCIsInJlY29yZCI6Im90aGVydGVzdC5jb20iLCJyZXNvdXJjZV9ncm91cF9uYW1lIjoibWFydGluLWRldiIsInRhZ3MiOnt9LCJ0aW1lb3V0cyI6eyJjcmVhdGUiOm51bGwsImRlbGV0ZSI6bnVsbCwicmVhZCI6bnVsbCwidXBkYXRlIjpudWxsfSwidHRsIjozMDAsInpvbmVfbmFtZSI6InRoaXNpc2F0ZXN0dXNpbmd0Zi5jb20ifQ==",
"Err": null
}

View File

@ -0,0 +1,5 @@
{
"Typ": "WyJvYmplY3QiLHsiZnFkbiI6InN0cmluZyIsImlkIjoic3RyaW5nIiwibmFtZSI6InN0cmluZyIsInJlY29yZCI6InN0cmluZyIsInJlc291cmNlX2dyb3VwX25hbWUiOiJzdHJpbmciLCJ0YWdzIjpbIm1hcCIsInN0cmluZyJdLCJ0aW1lb3V0cyI6WyJvYmplY3QiLHsiY3JlYXRlIjoic3RyaW5nIiwiZGVsZXRlIjoic3RyaW5nIiwicmVhZCI6InN0cmluZyIsInVwZGF0ZSI6InN0cmluZyJ9XSwidHRsIjoibnVtYmVyIiwiem9uZV9uYW1lIjoic3RyaW5nIn1d",
"Val": "eyJmcWRuIjoidGVzdC50aGlzaXNhdGVzdHVzaW5ndGYuY29tLiIsImlkIjoiL3N1YnNjcmlwdGlvbnMvN2JmYjJjNWMtNzMwOC00NmVkLThhZTQtZmZmYTM1NmViNDA2L3Jlc291cmNlR3JvdXBzL21hcnRpbi1kZXYvcHJvdmlkZXJzL01pY3Jvc29mdC5OZXR3b3JrL3ByaXZhdGVEbnNab25lcy90aGlzaXNhdGVzdHVzaW5ndGYuY29tL0NOQU1FL3Rlc3QiLCJuYW1lIjoidGVzdCIsInJlY29yZCI6InRlc3QuY29tIiwicmVzb3VyY2VfZ3JvdXBfbmFtZSI6Im1hcnRpbi1kZXYiLCJ0YWdzIjp7fSwidGltZW91dHMiOnsiY3JlYXRlIjpudWxsLCJkZWxldGUiOm51bGwsInJlYWQiOm51bGwsInVwZGF0ZSI6bnVsbH0sInR0bCI6MzAwLCJ6b25lX25hbWUiOiJ0aGlzaXNhdGVzdHVzaW5ndGYuY29tIn0=",
"Err": null
}

View File

@ -0,0 +1,24 @@
[
{
"fqdn": "test.thisisatestusingtf.com.",
"id": "/subscriptions/7bfb2c5c-7308-46ed-8ae4-fffa356eb406/resourceGroups/martin-dev/providers/Microsoft.Network/privateDnsZones/thisisatestusingtf.com/CNAME/test",
"name": "test",
"record": "test.com",
"resource_group_name": "martin-dev",
"tags": null,
"timeouts": null,
"ttl": 300,
"zone_name": "thisisatestusingtf.com"
},
{
"fqdn": "othertest.thisisatestusingtf.com.",
"id": "/subscriptions/7bfb2c5c-7308-46ed-8ae4-fffa356eb406/resourceGroups/martin-dev/providers/Microsoft.Network/privateDnsZones/thisisatestusingtf.com/CNAME/othertest",
"name": "othertest",
"record": "othertest.com",
"resource_group_name": "martin-dev",
"tags": null,
"timeouts": null,
"ttl": 300,
"zone_name": "thisisatestusingtf.com"
}
]

View File

@ -0,0 +1,24 @@
package azurerm
import "github.com/cloudskiff/driftctl/pkg/resource"
const AzurePrivateDNSCNameRecordResourceType = "azurerm_private_dns_cname_record"
func initAzurePrivateDNSCNameRecordMetaData(resourceSchemaRepository resource.SchemaRepositoryInterface) {
resourceSchemaRepository.SetFlags(AzurePrivateDNSCNameRecordResourceType, resource.FlagDeepMode)
resourceSchemaRepository.SetNormalizeFunc(AzurePrivateDNSCNameRecordResourceType, func(res *resource.Resource) {
res.Attributes().SafeDelete([]string{"timeouts"})
})
resourceSchemaRepository.SetHumanReadableAttributesFunc(AzurePrivateDNSCNameRecordResourceType, 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
}
if zone := val.GetString("zone_name"); zone != nil && *zone != "" {
attrs["Zone"] = *zone
}
return attrs
})
}

View File

@ -0,0 +1,30 @@
package azurerm_test
import (
"testing"
"github.com/cloudskiff/driftctl/test"
"github.com/cloudskiff/driftctl/test/acceptance"
)
func TestAcc_Azure_PrivateDNSCNAMERecord(t *testing.T) {
acceptance.Run(t, acceptance.AccTestCase{
TerraformVersion: "0.15.5",
Paths: []string{"./testdata/acc/azurerm_private_dns_cname_record"},
Args: []string{
"scan",
"--to", "azure+tf", "--deep",
},
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

@ -19,4 +19,5 @@ func InitResourcesMetadata(resourceSchemaRepository resource.SchemaRepositoryInt
initAzurePrivateDNSAAAARecordMetaData(resourceSchemaRepository)
initAzureImageMetaData(resourceSchemaRepository)
initAzureSSHPublicKeyMetaData(resourceSchemaRepository)
initAzurePrivateDNSCNameRecordMetaData(resourceSchemaRepository)
}

View File

@ -28,6 +28,7 @@ func TestAzureMetadata_Flags(t *testing.T) {
AzurePrivateDNSZoneResourceType: {resource.FlagDeepMode},
AzurePrivateDNSARecordResourceType: {resource.FlagDeepMode},
AzurePrivateDNSAAAARecordResourceType: {resource.FlagDeepMode},
AzurePrivateDNSCNameRecordResourceType: {resource.FlagDeepMode},
AzureImageResourceType: {},
AzureSSHPublicKeyResourceType: {resource.FlagDeepMode},
}

View File

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

View File

@ -0,0 +1,36 @@
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~> 2.71.0"
}
}
}
provider "azurerm" {
features {}
}
data "azurerm_resource_group" "example" {
name = "driftctl-qa-1"
}
resource "azurerm_private_dns_zone" "testzone" {
name = "this-zone-is-a-test-for-driftctl.com"
resource_group_name = data.azurerm_resource_group.example.name
}
resource "azurerm_private_dns_cname_record" "othertestcname" {
name = "othertest"
zone_name = azurerm_private_dns_zone.testzone.name
resource_group_name = data.azurerm_resource_group.example.name
ttl = 300
record = "othertest.com"
}
resource "azurerm_private_dns_cname_record" "testcname" {
name = "test"
zone_name = azurerm_private_dns_zone.testzone.name
resource_group_name = data.azurerm_resource_group.example.name
ttl = 300
record = "test.com"
}

View File

@ -202,6 +202,7 @@ var supportedTypes = map[string]ResourceTypeMeta{
"azurerm_private_dns_zone": {},
"azurerm_private_dns_a_record": {},
"azurerm_private_dns_aaaa_record": {},
"azurerm_private_dns_cname_record": {},
"azurerm_image": {},
"azurerm_ssh_public_key": {},
}