Skip to content

Commit

Permalink
Merge pull request #1089 from cloudskiff/add_azurerm_route_table
Browse files Browse the repository at this point in the history
Add azurerm_route_table
  • Loading branch information
eliecharra authored Oct 4, 2021
2 parents c8f74fd + ce3760b commit dd4c896
Show file tree
Hide file tree
Showing 14 changed files with 544 additions and 1 deletion.
48 changes: 48 additions & 0 deletions pkg/remote/azurerm/azurerm_route_table_enumerator.go
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions pkg/remote/azurerm/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func Init(
remoteLibrary.AddEnumerator(NewAzurermStorageAccountEnumerator(storageAccountRepo, factory))
remoteLibrary.AddEnumerator(NewAzurermStorageContainerEnumerator(storageAccountRepo, factory))
remoteLibrary.AddEnumerator(NewAzurermVirtualNetworkEnumerator(networkRepo, factory))
remoteLibrary.AddEnumerator(NewAzurermRouteTableEnumerator(networkRepo, factory))

err = resourceSchemaRepository.Init(terraform.AZURE, provider.Version(), provider.Schema())
if err != nil {
Expand Down
25 changes: 24 additions & 1 deletion pkg/remote/azurerm/repository/mock_NetworkRepository.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions pkg/remote/azurerm/repository/mock_routeTablesClient.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 58 additions & 0 deletions pkg/remote/azurerm/repository/mock_routeTablesListAllPager.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions pkg/remote/azurerm/repository/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

type NetworkRepository interface {
ListAllVirtualNetworks() ([]*armnetwork.VirtualNetwork, error)
ListAllRouteTables() ([]*armnetwork.RouteTable, error)
}

type virtualNetworksListAllPager interface {
Expand All @@ -30,14 +31,33 @@ func (c virtualNetworksClientImpl) ListAll(options *armnetwork.VirtualNetworksLi
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 {
virtualNetworksClient virtualNetworksClient
routeTableClient routeTablesClient
cache cache.Cache
}

func NewNetworkRepository(con *arm.Connection, config common.AzureProviderConfig, cache cache.Cache) *networkRepository {
return &networkRepository{
&virtualNetworksClientImpl{client: armnetwork.NewVirtualNetworksClient(con, config.SubscriptionID)},
&routeTablesClientImpl{client: armnetwork.NewRouteTablesClient(con, config.SubscriptionID)},
cache,
}
}
Expand Down Expand Up @@ -66,3 +86,27 @@ func (s *networkRepository) ListAllVirtualNetworks() ([]*armnetwork.VirtualNetwo

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
}
Loading

0 comments on commit dd4c896

Please sign in to comment.