func()

in internal/resources/fetching/fetchers/azure/insights_batch_fetcher.go [47:90]


func (f *AzureInsightsBatchAssetFetcher) Fetch(ctx context.Context, cycleMetadata cycle.Metadata) error {
	f.log.Info("Starting AzureInsightsBatchAssetFetcher.Fetch")
	subscriptions, err := f.provider.GetSubscriptions(ctx, cycleMetadata)
	if err != nil {
		return fmt.Errorf("azure insights fetcher: error while receiving subscriptions: %w", err)
	}

	assets, err := f.provider.ListDiagnosticSettingsAssetTypes(ctx, cycleMetadata, lo.Keys(subscriptions))
	if err != nil {
		return fmt.Errorf("azure insights fetcher: error while receiving diagnostic settings: %w", err)
	}

	// group and send by subscription id
	subscriptionGroups := lo.GroupBy(assets, func(item inventory.AzureAsset) string {
		return item.SubscriptionId
	})

	for subId, subscription := range subscriptions {
		batchAssets := subscriptionGroups[subId]
		if batchAssets == nil {
			batchAssets = []inventory.AzureAsset{} // Use empty array instead of nil
		}

		select {
		case <-ctx.Done():
			err := ctx.Err()
			f.log.Infof("AzureInsightsBatchAssetFetcher.Fetch context err: %s", err.Error())
			return err
		case f.resourceCh <- fetching.ResourceInfo{
			CycleMetadata: cycleMetadata,
			Resource: &AzureBatchResource{
				typePair: typePair{
					Type:    fetching.MonitoringIdentity,
					SubType: fetching.AzureDiagnosticSettingsType,
				},
				Subscription: subscription,
				Assets:       batchAssets,
			},
		}:
		}
	}

	return nil
}