Skip to content

Commit

Permalink
azurerm: support importing within resource group
Browse files Browse the repository at this point in the history
Introduce an option: `-R`/`--resource-group` to allow user to restrict
the import range within the specified resource group.

For those subscription level (or above) resources, if user has specified
this option, will not be imported.
  • Loading branch information
magodo committed Aug 16, 2020
1 parent 30c8abd commit 4a9abb5
Show file tree
Hide file tree
Showing 26 changed files with 453 additions and 162 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,7 @@ export ARM_CLIENT_SECRET=[CLIENT_SECRET]
export ARM_TENANT_ID=[TENANT_ID]
./terraformer import azure -r resource_group
./terraformer import azure -R my_resource_group -r virtual_network,resource_group
```

List of supported Azure resources:
Expand Down
31 changes: 16 additions & 15 deletions cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,22 @@ import (
)

type ImportOptions struct {
Resources []string
PathPattern string
PathOutput string
State string
Bucket string
Profile string
Verbose bool
Zone string
Regions []string
Projects []string
Connect bool
Compact bool
Filter []string
Plan bool `json:"-"`
Output string
Resources []string
PathPattern string
PathOutput string
State string
Bucket string
Profile string
Verbose bool
Zone string
Regions []string
Projects []string
ResourceGroup string
Connect bool
Compact bool
Filter []string
Plan bool `json:"-"`
Output string
}

const DefaultPathPattern = "{output}/{provider}/{service}/"
Expand Down
3 changes: 2 additions & 1 deletion cmd/provider_cmd_azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func newCmdAzureImporter(options ImportOptions) *cobra.Command {
Long: "Import current state to Terraform configuration from Azure",
RunE: func(cmd *cobra.Command, args []string) error {
provider := newAzureProvider()
err := Import(provider, options, []string{})
err := Import(provider, options, []string{options.ResourceGroup})
if err != nil {
return err
}
Expand All @@ -37,6 +37,7 @@ func newCmdAzureImporter(options ImportOptions) *cobra.Command {

cmd.AddCommand(listCmd(newAzureProvider()))
baseProviderFlags(cmd.PersistentFlags(), &options, "resource_group", "resource_group=name1:name2:name3")
cmd.PersistentFlags().StringVarP(&options.ResourceGroup, "resource-group", "R", "", "")
return cmd
}

Expand Down
11 changes: 10 additions & 1 deletion providers/azure/analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,16 @@ func (g *AnalysisGenerator) listServiceServers() ([]terraformutils.Resource, err
AnalysisClient := analysisservices.NewServersClient(g.Args["config"].(authentication.Config).SubscriptionID)
AnalysisClient.Authorizer = g.Args["authorizer"].(autorest.Authorizer)

servers, err := AnalysisClient.List(ctx)
var (
servers analysisservices.Servers
err error
)

if rg := g.Args["resource_group"].(string); rg != "" {
servers, err = AnalysisClient.ListByResourceGroup(ctx, rg)
} else {
servers, err = AnalysisClient.List(ctx)
}
if err != nil {
return nil, err
}
Expand Down
10 changes: 9 additions & 1 deletion providers/azure/app_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,15 @@ func (g AppServiceGenerator) listApps() ([]terraformutils.Resource, error) {

appServiceClient := web.NewAppsClient(g.Args["config"].(authentication.Config).SubscriptionID)
appServiceClient.Authorizer = g.Args["authorizer"].(autorest.Authorizer)
appsIterator, err := appServiceClient.ListComplete(ctx)
var (
appsIterator web.AppCollectionIterator
err error
)
if rg := g.Args["resource_group"].(string); rg != "" {
appsIterator, err = appServiceClient.ListByResourceGroupComplete(ctx, rg, nil)
} else {
appsIterator, err = appServiceClient.ListComplete(ctx)
}
if err != nil {
return nil, err
}
Expand Down
11 changes: 7 additions & 4 deletions providers/azure/azure_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ import (

type AzureProvider struct { //nolint
terraformutils.Provider
config authentication.Config
authorizer autorest.Authorizer
config authentication.Config
authorizer autorest.Authorizer
resourceGroup string
}

func (p *AzureProvider) setEnvConfig() error {
Expand Down Expand Up @@ -105,6 +106,7 @@ func (p *AzureProvider) Init(args []string) error {
return err
}
p.authorizer = authorizer
p.resourceGroup = args[0]

return nil
}
Expand Down Expand Up @@ -180,8 +182,9 @@ func (p *AzureProvider) InitService(serviceName string, verbose bool) error {
p.Service.SetVerbose(verbose)
p.Service.SetProviderName(p.GetName())
p.Service.SetArgs(map[string]interface{}{
"config": p.config,
"authorizer": p.authorizer,
"config": p.config,
"authorizer": p.authorizer,
"resource_group": p.resourceGroup,
})
return nil
}
26 changes: 22 additions & 4 deletions providers/azure/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,16 @@ func (g *ContainerGenerator) listAndAddForContainerGroup() ([]terraformutils.Res
ContainerGroupsClient := containerinstance.NewContainerGroupsClient(subscriptionID)
ContainerGroupsClient.Authorizer = g.Args["authorizer"].(autorest.Authorizer)

containerGroupIterator, err := ContainerGroupsClient.ListComplete(ctx)
var (
containerGroupIterator containerinstance.ContainerGroupListResultIterator
err error
)

if rg := g.Args["resource_group"].(string); rg != "" {
containerGroupIterator, err = ContainerGroupsClient.ListByResourceGroupComplete(ctx, rg)
} else {
containerGroupIterator, err = ContainerGroupsClient.ListComplete(ctx)
}
if err != nil {
return nil, err
}
Expand All @@ -51,7 +60,7 @@ func (g *ContainerGenerator) listAndAddForContainerGroup() ([]terraformutils.Res

if err := containerGroupIterator.Next(); err != nil {
log.Println(err)
break
return resources, err
}
}

Expand Down Expand Up @@ -93,7 +102,16 @@ func (g *ContainerGenerator) listAndAddForContainerRegistry() ([]terraformutils.
ContainerRegistriesClient := containerregistry.NewRegistriesClient(subscriptionID)
ContainerRegistriesClient.Authorizer = g.Args["authorizer"].(autorest.Authorizer)

containerRegistryIterator, err := ContainerRegistriesClient.ListComplete(ctx)
var (
containerRegistryIterator containerregistry.RegistryListResultIterator
err error
)

if rg := g.Args["resource_group"].(string); rg != "" {
containerRegistryIterator, err = ContainerRegistriesClient.ListByResourceGroupComplete(ctx, rg)
} else {
containerRegistryIterator, err = ContainerRegistriesClient.ListComplete(ctx)
}
if err != nil {
return nil, err
}
Expand All @@ -119,7 +137,7 @@ func (g *ContainerGenerator) listAndAddForContainerRegistry() ([]terraformutils.

if err := containerRegistryIterator.Next(); err != nil {
log.Println(err)
break
return resources, err
}
}

Expand Down
10 changes: 9 additions & 1 deletion providers/azure/cosmosdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,15 @@ func (g *CosmosDBGenerator) listAndAddForDatabaseAccounts() ([]terraformutils.Re
DatabaseAccountsClient := documentdb.NewDatabaseAccountsClient(subscriptionID, subscriptionID)
DatabaseAccountsClient.Authorizer = g.Args["authorizer"].(autorest.Authorizer)

accounts, err := DatabaseAccountsClient.List(ctx)
var (
accounts documentdb.DatabaseAccountsListResult
err error
)
if rg := g.Args["resource_group"].(string); rg != "" {
accounts, err = DatabaseAccountsClient.ListByResourceGroup(ctx, rg)
} else {
accounts, err = DatabaseAccountsClient.List(ctx)
}
if err != nil {
return nil, err
}
Expand Down
44 changes: 40 additions & 4 deletions providers/azure/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,15 @@ func (g *DatabasesGenerator) getMariaDBServers() ([]mariadb.Server, error) {
Client := mariadb.NewServersClient(SubscriptionID)
Client.Authorizer = Authorizer

Servers, err := Client.List(ctx)
var (
Servers mariadb.ServerListResult
err error
)
if rg := g.Args["resource_group"].(string); rg != "" {
Servers, err = Client.ListByResourceGroup(ctx, rg)
} else {
Servers, err = Client.List(ctx)
}
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -205,7 +213,16 @@ func (g *DatabasesGenerator) getMySQLServers() ([]mysql.Server, error) {
Client := mysql.NewServersClient(SubscriptionID)
Client.Authorizer = Authorizer

Servers, err := Client.List(ctx)
var (
Servers mysql.ServerListResult
err error
)

if rg := g.Args["resource_group"].(string); rg != "" {
Servers, err = Client.ListByResourceGroup(ctx, rg)
} else {
Servers, err = Client.List(ctx)
}
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -373,7 +390,17 @@ func (g *DatabasesGenerator) getPostgreSQLServers() ([]postgresql.Server, error)
Client := postgresql.NewServersClient(SubscriptionID)
Client.Authorizer = Authorizer

Servers, err := Client.List(ctx)
var (
Servers postgresql.ServerListResult
err error
)

if rg := g.Args["resource_group"].(string); rg != "" {
Servers, err = Client.ListByResourceGroup(ctx, rg)
} else {
Servers, err = Client.List(ctx)
}

if err != nil {
return nil, err
}
Expand Down Expand Up @@ -533,7 +560,16 @@ func (g *DatabasesGenerator) getSQLServers() ([]sql.Server, error) {
Client := sql.NewServersClient(SubscriptionID)
Client.Authorizer = Authorizer

ServerPages, err := Client.List(ctx)
var (
ServerPages sql.ServerListResultPage
err error
)

if rg := g.Args["resource_group"].(string); rg != "" {
ServerPages, err = Client.ListByResourceGroup(ctx, rg)
} else {
ServerPages, err = Client.List(ctx)
}
if err != nil {
return nil, err
}
Expand Down
22 changes: 16 additions & 6 deletions providers/azure/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type DiskGenerator struct {
AzureService
}

func (g DiskGenerator) createResources(diskListIterator compute.DiskListIterator) []terraformutils.Resource {
func (g DiskGenerator) createResources(diskListIterator compute.DiskListIterator) ([]terraformutils.Resource, error) {
var resources []terraformutils.Resource
for diskListIterator.NotDone() {
disk := diskListIterator.Value()
Expand All @@ -40,21 +40,31 @@ func (g DiskGenerator) createResources(diskListIterator compute.DiskListIterator
[]string{}))
if err := diskListIterator.Next(); err != nil {
log.Println(err)
break
return resources, err
}
}
return resources
return resources, nil
}

func (g *DiskGenerator) InitResources() error {
ctx := context.Background()
disksClient := compute.NewDisksClient(g.Args["config"].(authentication.Config).SubscriptionID)

disksClient.Authorizer = g.Args["authorizer"].(autorest.Authorizer)
output, err := disksClient.ListComplete(ctx)

var (
output compute.DiskListIterator
err error
)

if rg := g.Args["resource_group"].(string); rg != "" {
output, err = disksClient.ListByResourceGroupComplete(ctx, rg)
} else {
output, err = disksClient.ListComplete(ctx)
}
if err != nil {
return err
}
g.Resources = g.createResources(output)
return nil
g.Resources, err = g.createResources(output)
return err
}
16 changes: 13 additions & 3 deletions providers/azure/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (g *DNSGenerator) listRecordSets(resourceGroupName string, zoneName string,

if err := recordSetIterator.Next(); err != nil {
log.Println(err)
break
return resources, err
}

}
Expand All @@ -83,7 +83,17 @@ func (g *DNSGenerator) listAndAddForDNSZone() ([]terraformutils.Resource, error)
DNSZonesClient.Authorizer = g.Args["authorizer"].(autorest.Authorizer)

var pageSize int32 = 50
dnsZoneIterator, err := DNSZonesClient.ListComplete(ctx, &pageSize)

var (
dnsZoneIterator dns.ZoneListResultIterator
err error
)

if rg := g.Args["resource_group"].(string); rg != "" {
dnsZoneIterator, err = DNSZonesClient.ListByResourceGroupComplete(ctx, rg, &pageSize)
} else {
dnsZoneIterator, err = DNSZonesClient.ListComplete(ctx, &pageSize)
}
if err != nil {
return nil, err
}
Expand All @@ -109,7 +119,7 @@ func (g *DNSGenerator) listAndAddForDNSZone() ([]terraformutils.Resource, error)

if err := dnsZoneIterator.Next(); err != nil {
log.Println(err)
break
return resources, err
}
}

Expand Down
Loading

0 comments on commit 4a9abb5

Please sign in to comment.