Skip to content

Commit

Permalink
Run resource initialization sequentially
Browse files Browse the repository at this point in the history
  • Loading branch information
rotemavni committed Feb 3, 2021
1 parent 9bd9081 commit 08e3753
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
24 changes: 11 additions & 13 deletions cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,18 +149,19 @@ func initAllServicesResources(providersMapping *terraformutils.ProvidersMapping,
var wg sync.WaitGroup
wg.Add(numOfResources)

failedServices := make(chan string, numOfResources)
failedServices := []string{}

for _, service := range options.Resources {
serviceProvider := providersMapping.AddServiceToProvider(service)
err := serviceProvider.Init(args)
if err != nil {
return err
}
go initServiceResourcesWorker(service, serviceProvider, options, providerWrapper, &wg, failedServices)
err = initServiceResources(service, serviceProvider, options, providerWrapper)
if err != nil {
failedServices = append(failedServices, service)
}
}
wg.Wait()
close(failedServices)

// remove providers that failed to init their service
providersMapping.RemoveServices(failedServices)
Expand Down Expand Up @@ -190,29 +191,26 @@ func importFromPlan(providerMapping *terraformutils.ProvidersMapping, options Im
return ImportFromPlan(providerMapping.GetBaseProvider(), plan)
}

func initServiceResourcesWorker(service string, provider terraformutils.ProviderGenerator,
options ImportOptions, providerWrapper *providerwrapper.ProviderWrapper, wg *sync.WaitGroup, failedServices chan string) {
func initServiceResources(service string, provider terraformutils.ProviderGenerator,
options ImportOptions, providerWrapper *providerwrapper.ProviderWrapper) error {
log.Println(provider.GetName() + " importing... " + service)
err := provider.InitService(service, options.Verbose)
if err != nil {
failedServices <- service
log.Printf("%s error importing %s, err: %s\n", provider.GetName(), service, err)
wg.Done()
return
return err
}
provider.GetService().ParseFilters(options.Filter)
err = provider.GetService().InitResources()
if err != nil {
failedServices <- service
log.Printf("%s error initializing resources in service %s, err: %s\n", provider.GetName(), service, err)
wg.Done()
return
return err
}

provider.GetService().PopulateIgnoreKeys(providerWrapper)
provider.GetService().InitialCleanup()
log.Println(provider.GetName() + " done importing " + service)
wg.Done()

return nil
}

func ImportFromPlan(provider terraformutils.ProviderGenerator, plan *ImportPlan) error {
Expand Down
4 changes: 2 additions & 2 deletions terraformutils/providers_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ func (p *ProvidersMapping) GetServices() []string {
return services
}

func (p *ProvidersMapping) RemoveServices(services chan string) {
for service := range services {
func (p *ProvidersMapping) RemoveServices(services []string) {
for _, service := range services {
delete(p.Services, service)

matchingProvider := p.serviceToProvider[service]
Expand Down

0 comments on commit 08e3753

Please sign in to comment.