commit
f46b18d12f
|
@ -453,6 +453,7 @@ func TestTerraformStateReader_Azure_Resources(t *testing.T) {
|
|||
{name: "private dns txt record", dirName: "azurerm_private_dns_txt_record", wantErr: false},
|
||||
{name: "images", dirName: "azurerm_image", wantErr: false},
|
||||
{name: "ssh public key", dirName: "azurerm_ssh_public_key", wantErr: false},
|
||||
{name: "load balancer rule", dirName: "azurerm_lb_rule", wantErr: false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
[
|
||||
{
|
||||
"Id": "/subscriptions/8cb43347-a79f-4bb2-a8b4-c838b41fa5a5/resourceGroups/raphael-dev/providers/Microsoft.Network/loadBalancers/TestLoadBalancer/loadBalancingRules/LBRule2",
|
||||
"Type": "azurerm_lb_rule",
|
||||
"Attrs": {
|
||||
"backend_address_pool_id": "",
|
||||
"backend_port": 80,
|
||||
"disable_outbound_snat": false,
|
||||
"enable_floating_ip": false,
|
||||
"enable_tcp_reset": false,
|
||||
"frontend_ip_configuration_id": "/subscriptions/8cb43347-a79f-4bb2-a8b4-c838b41fa5a5/resourceGroups/raphael-dev/providers/Microsoft.Network/loadBalancers/TestLoadBalancer/frontendIPConfigurations/PublicIPAddress",
|
||||
"frontend_ip_configuration_name": "PublicIPAddress",
|
||||
"frontend_port": 80,
|
||||
"id": "/subscriptions/8cb43347-a79f-4bb2-a8b4-c838b41fa5a5/resourceGroups/raphael-dev/providers/Microsoft.Network/loadBalancers/TestLoadBalancer/loadBalancingRules/LBRule2",
|
||||
"idle_timeout_in_minutes": 4,
|
||||
"load_distribution": "Default",
|
||||
"loadbalancer_id": "/subscriptions/8cb43347-a79f-4bb2-a8b4-c838b41fa5a5/resourceGroups/raphael-dev/providers/Microsoft.Network/loadBalancers/TestLoadBalancer",
|
||||
"name": "LBRule2",
|
||||
"probe_id": "",
|
||||
"protocol": "Tcp",
|
||||
"resource_group_name": "raphael-dev"
|
||||
}
|
||||
}
|
||||
]
|
|
@ -0,0 +1,45 @@
|
|||
{
|
||||
"version": 4,
|
||||
"terraform_version": "1.0.0",
|
||||
"serial": 342,
|
||||
"lineage": "9566e18d-6080-4aa8-e9a6-4c38905cf68f",
|
||||
"outputs": {},
|
||||
"resources": [
|
||||
{
|
||||
"mode": "managed",
|
||||
"type": "azurerm_lb_rule",
|
||||
"name": "example-2",
|
||||
"provider": "provider[\"registry.terraform.io/hashicorp/azurerm\"]",
|
||||
"instances": [
|
||||
{
|
||||
"schema_version": 0,
|
||||
"attributes": {
|
||||
"backend_address_pool_id": "",
|
||||
"backend_port": 80,
|
||||
"disable_outbound_snat": false,
|
||||
"enable_floating_ip": false,
|
||||
"enable_tcp_reset": false,
|
||||
"frontend_ip_configuration_id": "/subscriptions/8cb43347-a79f-4bb2-a8b4-c838b41fa5a5/resourceGroups/raphael-dev/providers/Microsoft.Network/loadBalancers/TestLoadBalancer/frontendIPConfigurations/PublicIPAddress",
|
||||
"frontend_ip_configuration_name": "PublicIPAddress",
|
||||
"frontend_port": 80,
|
||||
"id": "/subscriptions/8cb43347-a79f-4bb2-a8b4-c838b41fa5a5/resourceGroups/raphael-dev/providers/Microsoft.Network/loadBalancers/TestLoadBalancer/loadBalancingRules/LBRule2",
|
||||
"idle_timeout_in_minutes": 4,
|
||||
"load_distribution": "Default",
|
||||
"loadbalancer_id": "/subscriptions/8cb43347-a79f-4bb2-a8b4-c838b41fa5a5/resourceGroups/raphael-dev/providers/Microsoft.Network/loadBalancers/TestLoadBalancer",
|
||||
"name": "LBRule2",
|
||||
"probe_id": "",
|
||||
"protocol": "Tcp",
|
||||
"resource_group_name": "raphael-dev",
|
||||
"timeouts": null
|
||||
},
|
||||
"sensitive_attributes": [],
|
||||
"private": "eyJlMmJmYjczMC1lY2FhLTExZTYtOGY4OC0zNDM2M2JjN2M0YzAiOnsiY3JlYXRlIjoxODAwMDAwMDAwMDAwLCJkZWxldGUiOjE4MDAwMDAwMDAwMDAsInJlYWQiOjMwMDAwMDAwMDAwMCwidXBkYXRlIjoxODAwMDAwMDAwMDAwfX0=",
|
||||
"dependencies": [
|
||||
"azurerm_lb.example",
|
||||
"data.azurerm_resource_group.raphael-dev"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
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 AzurermLoadBalancerRuleEnumerator struct {
|
||||
repository repository.NetworkRepository
|
||||
factory resource.ResourceFactory
|
||||
}
|
||||
|
||||
func NewAzurermLoadBalancerRuleEnumerator(repo repository.NetworkRepository, factory resource.ResourceFactory) *AzurermLoadBalancerRuleEnumerator {
|
||||
return &AzurermLoadBalancerRuleEnumerator{
|
||||
repository: repo,
|
||||
factory: factory,
|
||||
}
|
||||
}
|
||||
|
||||
func (e *AzurermLoadBalancerRuleEnumerator) SupportedType() resource.ResourceType {
|
||||
return azurerm.AzureLoadBalancerRuleResourceType
|
||||
}
|
||||
|
||||
func (e *AzurermLoadBalancerRuleEnumerator) Enumerate() ([]*resource.Resource, error) {
|
||||
loadBalancers, err := e.repository.ListAllLoadBalancers()
|
||||
if err != nil {
|
||||
return nil, remoteerror.NewResourceListingErrorWithType(err, string(e.SupportedType()), azurerm.AzureLoadBalancerResourceType)
|
||||
}
|
||||
|
||||
results := make([]*resource.Resource, 0)
|
||||
|
||||
for _, res := range loadBalancers {
|
||||
rules, err := e.repository.ListLoadBalancerRules(res)
|
||||
if err != nil {
|
||||
return nil, remoteerror.NewResourceListingError(err, string(e.SupportedType()))
|
||||
}
|
||||
|
||||
for _, rule := range rules {
|
||||
results = append(
|
||||
results,
|
||||
e.factory.CreateAbstractResource(
|
||||
string(e.SupportedType()),
|
||||
*rule.ID,
|
||||
map[string]interface{}{
|
||||
"name": *rule.Name,
|
||||
"loadbalancer_id": *res.ID,
|
||||
},
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
return results, err
|
||||
}
|
|
@ -67,6 +67,9 @@ func Init(
|
|||
remoteLibrary.AddEnumerator(NewAzurermNetworkSecurityGroupEnumerator(networkRepo, factory))
|
||||
remoteLibrary.AddDetailsFetcher(azurerm.AzureNetworkSecurityGroupResourceType, common.NewGenericDetailsFetcher(azurerm.AzureNetworkSecurityGroupResourceType, provider, deserializer))
|
||||
remoteLibrary.AddEnumerator(NewAzurermLoadBalancerEnumerator(networkRepo, factory))
|
||||
remoteLibrary.AddEnumerator(NewAzurermLoadBalancerRuleEnumerator(networkRepo, factory))
|
||||
remoteLibrary.AddDetailsFetcher(azurerm.AzureLoadBalancerRuleResourceType, common.NewGenericDetailsFetcher(azurerm.AzureLoadBalancerRuleResourceType, provider, deserializer))
|
||||
|
||||
remoteLibrary.AddEnumerator(NewAzurermPrivateDNSZoneEnumerator(privateDNSRepo, factory))
|
||||
remoteLibrary.AddDetailsFetcher(azurerm.AzurePrivateDNSZoneResourceType, common.NewGenericDetailsFetcher(azurerm.AzurePrivateDNSZoneResourceType, provider, deserializer))
|
||||
remoteLibrary.AddEnumerator(NewAzurermPrivateDNSARecordEnumerator(privateDNSRepo, factory))
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
// Code generated by mockery v0.0.0-dev. DO NOT EDIT.
|
||||
// Code generated by mockery v2.8.0. DO NOT EDIT.
|
||||
|
||||
package repository
|
||||
|
||||
|
@ -172,3 +172,26 @@ func (_m *MockNetworkRepository) ListAllVirtualNetworks() ([]*armnetwork.Virtual
|
|||
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
// ListLoadBalancerRules provides a mock function with given fields: _a0
|
||||
func (_m *MockNetworkRepository) ListLoadBalancerRules(_a0 *armnetwork.LoadBalancer) ([]*armnetwork.LoadBalancingRule, error) {
|
||||
ret := _m.Called(_a0)
|
||||
|
||||
var r0 []*armnetwork.LoadBalancingRule
|
||||
if rf, ok := ret.Get(0).(func(*armnetwork.LoadBalancer) []*armnetwork.LoadBalancingRule); ok {
|
||||
r0 = rf(_a0)
|
||||
} else {
|
||||
if ret.Get(0) != nil {
|
||||
r0 = ret.Get(0).([]*armnetwork.LoadBalancingRule)
|
||||
}
|
||||
}
|
||||
|
||||
var r1 error
|
||||
if rf, ok := ret.Get(1).(func(*armnetwork.LoadBalancer) error); ok {
|
||||
r1 = rf(_a0)
|
||||
} else {
|
||||
r1 = ret.Error(1)
|
||||
}
|
||||
|
||||
return r0, r1
|
||||
}
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
// Code generated by mockery v2.8.0. DO NOT EDIT.
|
||||
|
||||
package repository
|
||||
|
||||
import (
|
||||
armnetwork "github.com/Azure/azure-sdk-for-go/sdk/network/armnetwork"
|
||||
mock "github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
// mockLoadBalancerRulesClient is an autogenerated mock type for the loadBalancerRulesClient type
|
||||
type mockLoadBalancerRulesClient struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
// List provides a mock function with given fields: _a0, _a1, _a2
|
||||
func (_m *mockLoadBalancerRulesClient) List(_a0 string, _a1 string, _a2 *armnetwork.LoadBalancerLoadBalancingRulesListOptions) loadBalancerRulesListAllPager {
|
||||
ret := _m.Called(_a0, _a1, _a2)
|
||||
|
||||
var r0 loadBalancerRulesListAllPager
|
||||
if rf, ok := ret.Get(0).(func(string, string, *armnetwork.LoadBalancerLoadBalancingRulesListOptions) loadBalancerRulesListAllPager); ok {
|
||||
r0 = rf(_a0, _a1, _a2)
|
||||
} else {
|
||||
if ret.Get(0) != nil {
|
||||
r0 = ret.Get(0).(loadBalancerRulesListAllPager)
|
||||
}
|
||||
}
|
||||
|
||||
return r0
|
||||
}
|
|
@ -0,0 +1,58 @@
|
|||
// Code generated by mockery v2.8.0. 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"
|
||||
)
|
||||
|
||||
// mockLoadBalancerRulesListAllPager is an autogenerated mock type for the loadBalancerRulesListAllPager type
|
||||
type mockLoadBalancerRulesListAllPager struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
// Err provides a mock function with given fields:
|
||||
func (_m *mockLoadBalancerRulesListAllPager) 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 *mockLoadBalancerRulesListAllPager) 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 *mockLoadBalancerRulesListAllPager) PageResponse() armnetwork.LoadBalancerLoadBalancingRulesListResponse {
|
||||
ret := _m.Called()
|
||||
|
||||
var r0 armnetwork.LoadBalancerLoadBalancingRulesListResponse
|
||||
if rf, ok := ret.Get(0).(func() armnetwork.LoadBalancerLoadBalancingRulesListResponse); ok {
|
||||
r0 = rf()
|
||||
} else {
|
||||
r0 = ret.Get(0).(armnetwork.LoadBalancerLoadBalancingRulesListResponse)
|
||||
}
|
||||
|
||||
return r0
|
||||
}
|
|
@ -19,6 +19,7 @@ type NetworkRepository interface {
|
|||
ListAllPublicIPAddresses() ([]*armnetwork.PublicIPAddress, error)
|
||||
ListAllSecurityGroups() ([]*armnetwork.NetworkSecurityGroup, error)
|
||||
ListAllLoadBalancers() ([]*armnetwork.LoadBalancer, error)
|
||||
ListLoadBalancerRules(*armnetwork.LoadBalancer) ([]*armnetwork.LoadBalancingRule, error)
|
||||
}
|
||||
|
||||
type publicIPAddressesClient interface {
|
||||
|
@ -140,6 +141,23 @@ func (s loadBalancersClientImpl) ListAll(options *armnetwork.LoadBalancersListAl
|
|||
return s.client.ListAll(options)
|
||||
}
|
||||
|
||||
type loadBalancerRulesListAllPager interface {
|
||||
pager
|
||||
PageResponse() armnetwork.LoadBalancerLoadBalancingRulesListResponse
|
||||
}
|
||||
|
||||
type loadBalancerRulesClient interface {
|
||||
List(string, string, *armnetwork.LoadBalancerLoadBalancingRulesListOptions) loadBalancerRulesListAllPager
|
||||
}
|
||||
|
||||
type loadBalancerRulesClientImpl struct {
|
||||
client *armnetwork.LoadBalancerLoadBalancingRulesClient
|
||||
}
|
||||
|
||||
func (s loadBalancerRulesClientImpl) List(resourceGroupName string, loadBalancerName string, options *armnetwork.LoadBalancerLoadBalancingRulesListOptions) loadBalancerRulesListAllPager {
|
||||
return s.client.List(resourceGroupName, loadBalancerName, options)
|
||||
}
|
||||
|
||||
type networkRepository struct {
|
||||
virtualNetworksClient virtualNetworksClient
|
||||
routeTableClient routeTablesClient
|
||||
|
@ -147,7 +165,8 @@ type networkRepository struct {
|
|||
firewallsClient firewallsClient
|
||||
publicIPAddressesClient publicIPAddressesClient
|
||||
networkSecurityGroupsClient networkSecurityGroupsClient
|
||||
loadbalancersClient loadBalancersClient
|
||||
loadBalancersClient loadBalancersClient
|
||||
loadBalancerRulesClient loadBalancerRulesClient
|
||||
cache cache.Cache
|
||||
}
|
||||
|
||||
|
@ -160,6 +179,7 @@ func NewNetworkRepository(con *arm.Connection, config common.AzureProviderConfig
|
|||
&publicIPAddressesClientImpl{client: armnetwork.NewPublicIPAddressesClient(con, config.SubscriptionID)},
|
||||
&networkSecurityGroupsClientImpl{client: armnetwork.NewNetworkSecurityGroupsClient(con, config.SubscriptionID)},
|
||||
&loadBalancersClientImpl{client: armnetwork.NewLoadBalancersClient(con, config.SubscriptionID)},
|
||||
&loadBalancerRulesClientImpl{armnetwork.NewLoadBalancerLoadBalancingRulesClient(con, config.SubscriptionID)},
|
||||
cache,
|
||||
}
|
||||
}
|
||||
|
@ -331,11 +351,12 @@ func (s *networkRepository) ListAllSecurityGroups() ([]*armnetwork.NetworkSecuri
|
|||
|
||||
func (s *networkRepository) ListAllLoadBalancers() ([]*armnetwork.LoadBalancer, error) {
|
||||
cacheKey := "networkListAllLoadBalancers"
|
||||
if v := s.cache.Get(cacheKey); v != nil {
|
||||
defer s.cache.Unlock(cacheKey)
|
||||
if v := s.cache.GetAndLock(cacheKey); v != nil {
|
||||
return v.([]*armnetwork.LoadBalancer), nil
|
||||
}
|
||||
|
||||
pager := s.loadbalancersClient.ListAll(nil)
|
||||
pager := s.loadBalancersClient.ListAll(nil)
|
||||
results := make([]*armnetwork.LoadBalancer, 0)
|
||||
for pager.NextPage(context.Background()) {
|
||||
resp := pager.PageResponse()
|
||||
|
@ -352,3 +373,32 @@ func (s *networkRepository) ListAllLoadBalancers() ([]*armnetwork.LoadBalancer,
|
|||
s.cache.Put(cacheKey, results)
|
||||
return results, nil
|
||||
}
|
||||
|
||||
func (s *networkRepository) ListLoadBalancerRules(loadBalancer *armnetwork.LoadBalancer) ([]*armnetwork.LoadBalancingRule, error) {
|
||||
cacheKey := fmt.Sprintf("networkListLoadBalancerRules_%s", *loadBalancer.ID)
|
||||
if v := s.cache.Get(cacheKey); v != nil {
|
||||
return v.([]*armnetwork.LoadBalancingRule), nil
|
||||
}
|
||||
|
||||
loadBalancerResource, err := azure.ParseResourceID(*loadBalancer.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pager := s.loadBalancerRulesClient.List(loadBalancerResource.ResourceGroup, loadBalancerResource.ResourceName, &armnetwork.LoadBalancerLoadBalancingRulesListOptions{})
|
||||
results := make([]*armnetwork.LoadBalancingRule, 0)
|
||||
for pager.NextPage(context.Background()) {
|
||||
resp := pager.PageResponse()
|
||||
if err := pager.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
results = append(results, resp.Value...)
|
||||
}
|
||||
|
||||
if err := pager.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
s.cache.Put(cacheKey, results)
|
||||
return results, nil
|
||||
}
|
||||
|
|
|
@ -1006,15 +1006,17 @@ func Test_Network_ListAllLoadBalancers(t *testing.T) {
|
|||
}).Times(1)
|
||||
pager.On("Err").Return(nil).Times(2)
|
||||
|
||||
mockCache.On("Get", "networkListAllLoadBalancers").Return(nil).Times(1)
|
||||
mockCache.On("GetAndLock", "networkListAllLoadBalancers").Return(nil).Times(1)
|
||||
mockCache.On("Put", "networkListAllLoadBalancers", expectedResults).Return(false).Times(1)
|
||||
mockCache.On("Unlock", "networkListAllLoadBalancers").Return(nil).Times(1)
|
||||
},
|
||||
expected: expectedResults,
|
||||
},
|
||||
{
|
||||
name: "should hit cache and return load balancers",
|
||||
mocks: func(pager *mockLoadBalancersListAllPager, mockCache *cache.MockCache) {
|
||||
mockCache.On("Get", "networkListAllLoadBalancers").Return(expectedResults).Times(1)
|
||||
mockCache.On("GetAndLock", "networkListAllLoadBalancers").Return(expectedResults).Times(1)
|
||||
mockCache.On("Unlock", "networkListAllLoadBalancers").Return(nil).Times(1)
|
||||
},
|
||||
expected: expectedResults,
|
||||
},
|
||||
|
@ -1026,7 +1028,8 @@ func Test_Network_ListAllLoadBalancers(t *testing.T) {
|
|||
pager.On("PageResponse").Return(armnetwork.LoadBalancersListAllResponse{}).Times(1)
|
||||
pager.On("Err").Return(errors.New("remote error")).Times(1)
|
||||
|
||||
mockCache.On("Get", "networkListAllLoadBalancers").Return(nil).Times(1)
|
||||
mockCache.On("GetAndLock", "networkListAllLoadBalancers").Return(nil).Times(1)
|
||||
mockCache.On("Unlock", "networkListAllLoadBalancers").Return(nil).Times(1)
|
||||
},
|
||||
wantErr: "remote error",
|
||||
},
|
||||
|
@ -1043,7 +1046,7 @@ func Test_Network_ListAllLoadBalancers(t *testing.T) {
|
|||
tt.mocks(fakePager, mockCache)
|
||||
|
||||
s := &networkRepository{
|
||||
loadbalancersClient: fakeClient,
|
||||
loadBalancersClient: fakeClient,
|
||||
cache: mockCache,
|
||||
}
|
||||
got, err := s.ListAllLoadBalancers()
|
||||
|
@ -1062,3 +1065,108 @@ func Test_Network_ListAllLoadBalancers(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_Network_ListLoadBalancerRules(t *testing.T) {
|
||||
expectedResults := []*armnetwork.LoadBalancingRule{
|
||||
{
|
||||
SubResource: armnetwork.SubResource{
|
||||
ID: to.StringPtr("lbrule-1"),
|
||||
},
|
||||
Name: to.StringPtr("lbrule-1"),
|
||||
},
|
||||
{
|
||||
SubResource: armnetwork.SubResource{
|
||||
ID: to.StringPtr("lbrule-1"),
|
||||
},
|
||||
Name: to.StringPtr("lbrule-1"),
|
||||
},
|
||||
}
|
||||
|
||||
testcases := []struct {
|
||||
name string
|
||||
loadBalancer *armnetwork.LoadBalancer
|
||||
mocks func(*mockLoadBalancerRulesClient, *mockLoadBalancerRulesListAllPager, *cache.MockCache)
|
||||
expected []*armnetwork.LoadBalancingRule
|
||||
wantErr string
|
||||
}{
|
||||
{
|
||||
name: "should return load balancer rules",
|
||||
loadBalancer: &armnetwork.LoadBalancer{
|
||||
Resource: armnetwork.Resource{ID: to.StringPtr("/subscriptions/xxx/resourceGroups/driftctl/providers/Microsoft.Network/loadBalancers/TestLoadBalancer/frontendIPConfigurations/PublicIPAddress")},
|
||||
},
|
||||
mocks: func(client *mockLoadBalancerRulesClient, pager *mockLoadBalancerRulesListAllPager, mockCache *cache.MockCache) {
|
||||
client.On("List", "driftctl", "PublicIPAddress", &armnetwork.LoadBalancerLoadBalancingRulesListOptions{}).Return(pager)
|
||||
|
||||
pager.On("NextPage", context.Background()).Return(true).Times(1)
|
||||
pager.On("NextPage", context.Background()).Return(false).Times(1)
|
||||
pager.On("PageResponse").Return(armnetwork.LoadBalancerLoadBalancingRulesListResponse{
|
||||
LoadBalancerLoadBalancingRulesListResult: armnetwork.LoadBalancerLoadBalancingRulesListResult{
|
||||
LoadBalancerLoadBalancingRuleListResult: armnetwork.LoadBalancerLoadBalancingRuleListResult{
|
||||
Value: expectedResults,
|
||||
},
|
||||
},
|
||||
}).Times(1)
|
||||
pager.On("Err").Return(nil).Times(2)
|
||||
|
||||
mockCache.On("Get", "networkListLoadBalancerRules_/subscriptions/xxx/resourceGroups/driftctl/providers/Microsoft.Network/loadBalancers/TestLoadBalancer/frontendIPConfigurations/PublicIPAddress").Return(nil).Times(1)
|
||||
mockCache.On("Put", "networkListLoadBalancerRules_/subscriptions/xxx/resourceGroups/driftctl/providers/Microsoft.Network/loadBalancers/TestLoadBalancer/frontendIPConfigurations/PublicIPAddress", expectedResults).Return(false).Times(1)
|
||||
},
|
||||
expected: expectedResults,
|
||||
},
|
||||
{
|
||||
name: "should hit cache and return load balancers",
|
||||
loadBalancer: &armnetwork.LoadBalancer{
|
||||
Resource: armnetwork.Resource{ID: to.StringPtr("lb-1")},
|
||||
},
|
||||
mocks: func(client *mockLoadBalancerRulesClient, pager *mockLoadBalancerRulesListAllPager, mockCache *cache.MockCache) {
|
||||
mockCache.On("Get", "networkListLoadBalancerRules_lb-1").Return(expectedResults).Times(1)
|
||||
},
|
||||
expected: expectedResults,
|
||||
},
|
||||
{
|
||||
name: "should return remote error",
|
||||
loadBalancer: &armnetwork.LoadBalancer{
|
||||
Resource: armnetwork.Resource{ID: to.StringPtr("/subscriptions/xxx/resourceGroups/driftctl/providers/Microsoft.Network/loadBalancers/TestLoadBalancer/frontendIPConfigurations/PublicIPAddress")},
|
||||
},
|
||||
mocks: func(client *mockLoadBalancerRulesClient, pager *mockLoadBalancerRulesListAllPager, mockCache *cache.MockCache) {
|
||||
client.On("List", "driftctl", "PublicIPAddress", &armnetwork.LoadBalancerLoadBalancingRulesListOptions{}).Return(pager)
|
||||
|
||||
pager.On("NextPage", context.Background()).Return(true).Times(1)
|
||||
pager.On("NextPage", context.Background()).Return(false).Times(1)
|
||||
pager.On("PageResponse").Return(armnetwork.LoadBalancerLoadBalancingRulesListResponse{}).Times(1)
|
||||
pager.On("Err").Return(errors.New("remote error")).Times(1)
|
||||
|
||||
mockCache.On("Get", "networkListLoadBalancerRules_/subscriptions/xxx/resourceGroups/driftctl/providers/Microsoft.Network/loadBalancers/TestLoadBalancer/frontendIPConfigurations/PublicIPAddress").Return(nil).Times(1)
|
||||
},
|
||||
wantErr: "remote error",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range testcases {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
fakePager := &mockLoadBalancerRulesListAllPager{}
|
||||
fakeClient := &mockLoadBalancerRulesClient{}
|
||||
mockCache := &cache.MockCache{}
|
||||
|
||||
tt.mocks(fakeClient, fakePager, mockCache)
|
||||
|
||||
s := &networkRepository{
|
||||
loadBalancerRulesClient: fakeClient,
|
||||
cache: mockCache,
|
||||
}
|
||||
got, err := s.ListLoadBalancerRules(tt.loadBalancer)
|
||||
if tt.wantErr != "" {
|
||||
assert.EqualError(t, err, tt.wantErr)
|
||||
} else {
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
fakeClient.AssertExpectations(t)
|
||||
mockCache.AssertExpectations(t)
|
||||
|
||||
if !reflect.DeepEqual(got, tt.expected) {
|
||||
t.Errorf("ListAllLoadBalancers() got = %v, want %v", got, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -882,3 +882,131 @@ func TestAzurermLoadBalancers(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAzurermLoadBalancerRules(t *testing.T) {
|
||||
|
||||
dummyError := errors.New("this is an error")
|
||||
|
||||
tests := []struct {
|
||||
test string
|
||||
dirName string
|
||||
mocks func(*repository.MockNetworkRepository, *mocks.AlerterInterface)
|
||||
wantErr error
|
||||
}{
|
||||
{
|
||||
test: "no load balancer rule",
|
||||
dirName: "azurerm_lb_rule_empty",
|
||||
mocks: func(repository *repository.MockNetworkRepository, alerter *mocks.AlerterInterface) {
|
||||
loadbalancer := &armnetwork.LoadBalancer{
|
||||
Resource: armnetwork.Resource{
|
||||
ID: to.StringPtr("/subscriptions/8cb43347-a79f-4bb2-a8b4-c838b41fa5a5/resourceGroups/raphael-dev/providers/Microsoft.Network/loadBalancers/TestLoadBalancer/frontendIPConfigurations/PublicIPAddress"),
|
||||
Name: to.StringPtr("testlb"),
|
||||
},
|
||||
}
|
||||
|
||||
repository.On("ListAllLoadBalancers").Return([]*armnetwork.LoadBalancer{loadbalancer}, nil)
|
||||
|
||||
repository.On("ListLoadBalancerRules", loadbalancer).Return([]*armnetwork.LoadBalancingRule{}, nil)
|
||||
},
|
||||
},
|
||||
{
|
||||
test: "error listing load balancer rules",
|
||||
dirName: "azurerm_lb_rule_empty",
|
||||
mocks: func(repository *repository.MockNetworkRepository, alerter *mocks.AlerterInterface) {
|
||||
repository.On("ListAllLoadBalancers").Return(nil, dummyError)
|
||||
},
|
||||
wantErr: error2.NewResourceListingErrorWithType(dummyError, resourceazure.AzureLoadBalancerRuleResourceType, resourceazure.AzureLoadBalancerResourceType),
|
||||
},
|
||||
{
|
||||
test: "multiple load balancer rules",
|
||||
dirName: "azurerm_lb_rule_multiple",
|
||||
mocks: func(repository *repository.MockNetworkRepository, alerter *mocks.AlerterInterface) {
|
||||
loadbalancer := &armnetwork.LoadBalancer{
|
||||
Resource: armnetwork.Resource{
|
||||
ID: to.StringPtr("/subscriptions/8cb43347-a79f-4bb2-a8b4-c838b41fa5a5/resourceGroups/raphael-dev/providers/Microsoft.Network/loadBalancers/TestLoadBalancer/frontendIPConfigurations/PublicIPAddress"),
|
||||
Name: to.StringPtr("TestLoadBalancer"),
|
||||
},
|
||||
}
|
||||
|
||||
repository.On("ListAllLoadBalancers").Return([]*armnetwork.LoadBalancer{loadbalancer}, nil)
|
||||
|
||||
repository.On("ListLoadBalancerRules", loadbalancer).Return([]*armnetwork.LoadBalancingRule{
|
||||
{
|
||||
SubResource: armnetwork.SubResource{
|
||||
ID: to.StringPtr("/subscriptions/8cb43347-a79f-4bb2-a8b4-c838b41fa5a5/resourceGroups/raphael-dev/providers/Microsoft.Network/loadBalancers/TestLoadBalancer/loadBalancingRules/LBRule"),
|
||||
},
|
||||
Name: to.StringPtr("LBRule"),
|
||||
},
|
||||
{
|
||||
SubResource: armnetwork.SubResource{
|
||||
ID: to.StringPtr("/subscriptions/8cb43347-a79f-4bb2-a8b4-c838b41fa5a5/resourceGroups/raphael-dev/providers/Microsoft.Network/loadBalancers/TestLoadBalancer/loadBalancingRules/LBRule2"),
|
||||
},
|
||||
Name: to.StringPtr("LBRule2"),
|
||||
},
|
||||
}, 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.MockNetworkRepository{}
|
||||
c.mocks(fakeRepo, alerter)
|
||||
|
||||
var repo repository.NetworkRepository = fakeRepo
|
||||
providerVersion := "2.71.0"
|
||||
realProvider, err := terraform2.InitTestAzureProvider(providerLibrary, providerVersion)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
provider := terraform2.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.NewNetworkRepository(con, realProvider.GetConfig(), cache.New(0))
|
||||
}
|
||||
|
||||
remoteLibrary.AddEnumerator(azurerm.NewAzurermLoadBalancerRuleEnumerator(repo, factory))
|
||||
remoteLibrary.AddDetailsFetcher(resourceazure.AzureLoadBalancerRuleResourceType, common.NewGenericDetailsFetcher(resourceazure.AzureLoadBalancerRuleResourceType, 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.AzureLoadBalancerRuleResourceType, c.dirName, provider, deserializer, shouldUpdate, tt)
|
||||
alerter.AssertExpectations(tt)
|
||||
fakeRepo.AssertExpectations(tt)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"Typ": "WyJvYmplY3QiLHsiYmFja2VuZF9hZGRyZXNzX3Bvb2xfaWQiOiJzdHJpbmciLCJiYWNrZW5kX3BvcnQiOiJudW1iZXIiLCJkaXNhYmxlX291dGJvdW5kX3NuYXQiOiJib29sIiwiZW5hYmxlX2Zsb2F0aW5nX2lwIjoiYm9vbCIsImVuYWJsZV90Y3BfcmVzZXQiOiJib29sIiwiZnJvbnRlbmRfa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xZmE1YTUvcmVzb3VyY2VHcm91cHMvcmFwaGFlbC1kZXYvcHJvdmlkZXJzL01pY3Jvc29mdC5OZXR3b3JrL2xvYWRCYWxhbmNlcnMvVGVzdExvYWRCYWxhbmNlci9sb2FkQmFsYW5jaW5nUnVsZXMvTEJSdWxlIiwiaWRsZV90aW1lb3V0X2luX21pbnV0ZXMiOjQsImxvYWRfZGlzdHJpYnV0aW9uIjoiRGVmYXVsdCIsImxvYWRiYWxhbmNlcl9pZCI6Ii9zdWJzY3JpcHRpb25zLzhjYjQzMzQ3LWE3OWYtNGJiMi1hOGI0LWM4MzhiNDFmYTVhNS9yZXNvdXJjZUdyb3Vwcy9yYXBoYWVsLWRldi9wcm92aWRlcnMvTWljcm9zb2Z0Lk5ldHdvcmsvbG9hZEJhbGFuY2Vycy9UZXN0TG9hZEJhbGFuY2VyIiwibmFtZSI6IkxCUnVsZSIsInByb2JlX2lkIjoiIiwicHJvdG9jb2wiOiJUY3AiLCJyZXNvdXJjZV9ncm91cF9uYW1lIjoicmFwaGFlbC1kZXYiLCJ0aW1lb3V0cyI6eyJjcmVhdGUiOm51bGwsImRlbGV0ZSI6bnVsbCwicmVhZCI6bnVsbCwidXBkYXRlIjpudWxsfX0=",
|
||||
"Err": null
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
[
|
||||
{
|
||||
"backend_address_pool_id": "",
|
||||
"backend_port": 80,
|
||||
"disable_outbound_snat": false,
|
||||
"enable_floating_ip": false,
|
||||
"enable_tcp_reset": false,
|
||||
"frontend_ip_configuration_id": "/subscriptions/8cb43347-a79f-4bb2-a8b4-c838b41fa5a5/resourceGroups/raphael-dev/providers/Microsoft.Network/loadBalancers/TestLoadBalancer/frontendIPConfigurations/PublicIPAddress",
|
||||
"frontend_ip_configuration_name": "PublicIPAddress",
|
||||
"frontend_port": 80,
|
||||
"id": "/subscriptions/8cb43347-a79f-4bb2-a8b4-c838b41fa5a5/resourceGroups/raphael-dev/providers/Microsoft.Network/loadBalancers/TestLoadBalancer/loadBalancingRules/LBRule2",
|
||||
"idle_timeout_in_minutes": 4,
|
||||
"load_distribution": "Default",
|
||||
"loadbalancer_id": "/subscriptions/8cb43347-a79f-4bb2-a8b4-c838b41fa5a5/resourceGroups/raphael-dev/providers/Microsoft.Network/loadBalancers/TestLoadBalancer",
|
||||
"name": "LBRule2",
|
||||
"probe_id": "",
|
||||
"protocol": "Tcp",
|
||||
"resource_group_name": "raphael-dev",
|
||||
"timeouts": null
|
||||
},
|
||||
{
|
||||
"backend_address_pool_id": "",
|
||||
"backend_port": 3389,
|
||||
"disable_outbound_snat": false,
|
||||
"enable_floating_ip": false,
|
||||
"enable_tcp_reset": false,
|
||||
"frontend_ip_configuration_id": "/subscriptions/8cb43347-a79f-4bb2-a8b4-c838b41fa5a5/resourceGroups/raphael-dev/providers/Microsoft.Network/loadBalancers/TestLoadBalancer/frontendIPConfigurations/PublicIPAddress",
|
||||
"frontend_ip_configuration_name": "PublicIPAddress",
|
||||
"frontend_port": 3389,
|
||||
"id": "/subscriptions/8cb43347-a79f-4bb2-a8b4-c838b41fa5a5/resourceGroups/raphael-dev/providers/Microsoft.Network/loadBalancers/TestLoadBalancer/loadBalancingRules/LBRule",
|
||||
"idle_timeout_in_minutes": 4,
|
||||
"load_distribution": "Default",
|
||||
"loadbalancer_id": "/subscriptions/8cb43347-a79f-4bb2-a8b4-c838b41fa5a5/resourceGroups/raphael-dev/providers/Microsoft.Network/loadBalancers/TestLoadBalancer",
|
||||
"name": "LBRule",
|
||||
"probe_id": "",
|
||||
"protocol": "Tcp",
|
||||
"resource_group_name": "raphael-dev",
|
||||
"timeouts": null
|
||||
}
|
||||
]
|
|
@ -0,0 +1,24 @@
|
|||
package azurerm
|
||||
|
||||
import "github.com/cloudskiff/driftctl/pkg/resource"
|
||||
|
||||
const AzureLoadBalancerRuleResourceType = "azurerm_lb_rule"
|
||||
|
||||
func initAzureLoadBalancerRuleMetadata(resourceSchemaRepository resource.SchemaRepositoryInterface) {
|
||||
resourceSchemaRepository.SetNormalizeFunc(AzureLoadBalancerRuleResourceType, func(res *resource.Resource) {
|
||||
res.Attributes().SafeDelete([]string{"timeouts"})
|
||||
})
|
||||
resourceSchemaRepository.SetResolveReadAttributesFunc(AzureLoadBalancerRuleResourceType, func(res *resource.Resource) map[string]string {
|
||||
return map[string]string{
|
||||
"loadbalancer_id": *res.Attributes().GetString("loadbalancer_id"),
|
||||
}
|
||||
})
|
||||
resourceSchemaRepository.SetHumanReadableAttributesFunc(AzureLoadBalancerRuleResourceType, func(res *resource.Resource) map[string]string {
|
||||
attrs := make(map[string]string)
|
||||
if name := res.Attributes().GetString("name"); name != nil && *name != "" {
|
||||
attrs["Name"] = *name
|
||||
}
|
||||
return attrs
|
||||
})
|
||||
resourceSchemaRepository.SetFlags(AzureLoadBalancerRuleResourceType, resource.FlagDeepMode)
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package azurerm_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/cloudskiff/driftctl/test"
|
||||
"github.com/cloudskiff/driftctl/test/acceptance"
|
||||
)
|
||||
|
||||
func TestAcc_Azure_LoadBalancerRules(t *testing.T) {
|
||||
acceptance.Run(t, acceptance.AccTestCase{
|
||||
TerraformVersion: "0.15.5",
|
||||
Paths: []string{"./testdata/acc/azurerm_lb_rule"},
|
||||
Args: []string{
|
||||
"scan",
|
||||
"--to", "azure+tf",
|
||||
},
|
||||
Checks: []acceptance.AccCheck{
|
||||
{
|
||||
Check: func(result *test.ScanResult, stdout string, err error) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
result.AssertInfrastructureIsInSync()
|
||||
result.AssertManagedCount(1)
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
|
@ -24,4 +24,5 @@ func InitResourcesMetadata(resourceSchemaRepository resource.SchemaRepositoryInt
|
|||
initAzureImageMetaData(resourceSchemaRepository)
|
||||
initAzureSSHPublicKeyMetaData(resourceSchemaRepository)
|
||||
initAzurePrivateDNSCNameRecordMetaData(resourceSchemaRepository)
|
||||
initAzureLoadBalancerRuleMetadata(resourceSchemaRepository)
|
||||
}
|
||||
|
|
|
@ -35,6 +35,7 @@ func TestAzureMetadata_Flags(t *testing.T) {
|
|||
AzurePrivateDNSTXTRecordResourceType: {resource.FlagDeepMode},
|
||||
AzureImageResourceType: {},
|
||||
AzureSSHPublicKeyResourceType: {resource.FlagDeepMode},
|
||||
AzureLoadBalancerRuleResourceType: {resource.FlagDeepMode},
|
||||
}
|
||||
|
||||
schemaRepository := testresource.InitFakeSchemaRepository(tf.AZURE, "2.71.0")
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
*
|
||||
!azurerm_lb_rule
|
|
@ -0,0 +1,39 @@
|
|||
# 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",
|
||||
]
|
||||
}
|
||||
|
||||
provider "registry.terraform.io/hashicorp/random" {
|
||||
version = "3.1.0"
|
||||
hashes = [
|
||||
"h1:BZMEPucF+pbu9gsPk0G0BHx7YP04+tKdq2MrRDF1EDM=",
|
||||
"zh:2bbb3339f0643b5daa07480ef4397bd23a79963cc364cdfbb4e86354cb7725bc",
|
||||
"zh:3cd456047805bf639fbf2c761b1848880ea703a054f76db51852008b11008626",
|
||||
"zh:4f251b0eda5bb5e3dc26ea4400dba200018213654b69b4a5f96abee815b4f5ff",
|
||||
"zh:7011332745ea061e517fe1319bd6c75054a314155cb2c1199a5b01fe1889a7e2",
|
||||
"zh:738ed82858317ccc246691c8b85995bc125ac3b4143043219bd0437adc56c992",
|
||||
"zh:7dbe52fac7bb21227acd7529b487511c91f4107db9cc4414f50d04ffc3cab427",
|
||||
"zh:a3a9251fb15f93e4cfc1789800fc2d7414bbc18944ad4c5c98f466e6477c42bc",
|
||||
"zh:a543ec1a3a8c20635cf374110bd2f87c07374cf2c50617eee2c669b3ceeeaa9f",
|
||||
"zh:d9ab41d556a48bd7059f0810cf020500635bfc696c9fc3adab5ea8915c1d886b",
|
||||
"zh:d9e13427a7d011dbd654e591b0337e6074eef8c3b9bb11b2e39eaaf257044fd7",
|
||||
"zh:f7605bd1437752114baf601bdf6931debe6dc6bfe3006eb7e9bb9080931dca8a",
|
||||
]
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
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_public_ip" "example" {
|
||||
name = "PublicIPForLB"
|
||||
location = "West US"
|
||||
resource_group_name = data.azurerm_resource_group.qa1.name
|
||||
allocation_method = "Static"
|
||||
}
|
||||
|
||||
resource "azurerm_lb" "example" {
|
||||
name = "TestLoadBalancer"
|
||||
location = "West US"
|
||||
resource_group_name = data.azurerm_resource_group.qa1.name
|
||||
|
||||
frontend_ip_configuration {
|
||||
name = "PublicIPAddress"
|
||||
public_ip_address_id = azurerm_public_ip.example.id
|
||||
}
|
||||
}
|
||||
|
||||
resource "azurerm_lb_rule" "example" {
|
||||
resource_group_name = data.azurerm_resource_group.qa1.name
|
||||
loadbalancer_id = azurerm_lb.example.id
|
||||
name = "LBRule"
|
||||
protocol = "Tcp"
|
||||
frontend_port = 3389
|
||||
backend_port = 3389
|
||||
frontend_ip_configuration_name = "PublicIPAddress"
|
||||
}
|
|
@ -200,6 +200,7 @@ var supportedTypes = map[string]ResourceTypeMeta{
|
|||
"azurerm_public_ip": {},
|
||||
"azurerm_network_security_group": {},
|
||||
"azurerm_lb": {},
|
||||
"azurerm_lb_rule": {},
|
||||
"azurerm_private_dns_zone": {},
|
||||
"azurerm_private_dns_a_record": {},
|
||||
"azurerm_private_dns_aaaa_record": {},
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
package terraform
|
||||
|
||||
import (
|
||||
"crypto/sha1"
|
||||
gojson "encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"sort"
|
||||
|
||||
"github.com/cloudskiff/driftctl/pkg/terraform"
|
||||
|
@ -93,7 +95,20 @@ func (p *FakeTerraformProvider) getFileName(args terraform.ReadResourceArgs) str
|
|||
for _, k := range keys {
|
||||
suffix = fmt.Sprintf("%s-%s", suffix, args.Attributes[k])
|
||||
}
|
||||
fileName := fmt.Sprintf("%s-%s%s.res.golden.json", args.Ty, args.ID, suffix)
|
||||
|
||||
// ext4 and many other filesystems has a maximum filename length of 255 bytes
|
||||
// See https://en.wikipedia.org/wiki/Comparison_of_file_systems#Limits
|
||||
// Solution: we create a SHA1 hash of the filename so the length stay constant
|
||||
// We should do that no matter the length, but it requires to regenerate every single file
|
||||
// TODO: Use SHA1 filenames for all resource golden files
|
||||
resourceUID := fmt.Sprintf("%s-%s%s", args.Ty, args.ID, suffix)
|
||||
if len(resourceUID) > 239 {
|
||||
h := sha1.New()
|
||||
_, _ = io.WriteString(h, resourceUID)
|
||||
resourceUID = fmt.Sprintf("%x", h.Sum(nil))
|
||||
}
|
||||
|
||||
fileName := fmt.Sprintf("%s.res.golden.json", resourceUID)
|
||||
return fileName
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue