Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add azurerm_route_table #1089

Merged
merged 1 commit into from
Oct 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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