func()

in cli/azd/internal/cmd/add/add_select_ai.go [350:466]


func (a *AddAction) aiDeploymentCatalog(
	ctx context.Context, subId string, excludeModels []project.AiServicesModel) (map[string]ModelCatalogKind, error) {
	allLocations, err := a.accountManager.GetLocations(ctx, subId)
	if err != nil {
		return nil, fmt.Errorf("getting locations: %w", err)
	}

	var sharedResults sync.Map
	var wg sync.WaitGroup
	for _, location := range allLocations {
		wg.Add(1)
		go func(location string) {
			defer wg.Done()
			results, err := a.supportedModelsInLocation(ctx, subId, location)
			if err != nil {
				// log the error and continue. Do not fail the entire operation when pulling location error
				log.Println("error getting models in location", location, ":", err, "skipping")
				return
			}
			var filterSkusWithZeroCapacity []ModelList
			for _, model := range results {
				if len(model.Model.Skus) == 0 {
					continue
				}
				var skus []ModelSku
				for _, sku := range model.Model.Skus {
					if sku.Capacity.Default > 0 {
						skus = append(skus, sku)
					}
				}
				if len(skus) == 0 {
					continue
				}
				model.Model.Skus = skus
				filterSkusWithZeroCapacity = append(filterSkusWithZeroCapacity, model)
			}
			sharedResults.Store(location, filterSkusWithZeroCapacity)
		}(location.Name)
	}
	wg.Wait()

	combinedResults := map[string]ModelCatalogKind{}
	sharedResults.Range(func(key, value any) bool {
		// cast should be safe as the call to sharedResults.Store() use a string key
		locationNameKey := key.(string)
		models := value.([]ModelList)
		for _, model := range models {
			if model.Kind == "OpenAI" {
				// OpenAI kind is part of the `Add OpenAI` where clients connect directly to the service w/o an AIProject
				continue
			}
			nameKey := model.Model.Name
			// check if model is in the exclude list
			if slices.ContainsFunc(excludeModels, func(m project.AiServicesModel) bool {
				return nameKey == m.Name &&
					model.Model.Format == m.Format &&
					model.Model.Version == m.Version &&
					slices.ContainsFunc(model.Model.Skus, func(sku ModelSku) bool { return sku.Name == m.Sku.Name })
			}) {
				// skip this model as it is in the exclude list
				// exclude list is used to remove models which might have been added to the project already
				// This validation is also blocking adding same model with different sku
				continue
			}
			kindKey := model.Kind
			versionKey := model.Model.Version
			modelKey, exists := combinedResults[nameKey]
			if !exists {
				combinedResults[nameKey] = ModelCatalogKind{
					Kinds: map[string]ModelCatalogVersions{
						kindKey: {
							Versions: map[string]ModelCatalog{
								versionKey: {
									ModelList: model,
									Locations: []string{locationNameKey},
								},
							},
						},
					},
				}
			} else {
				// nameKey exists - check if kindKey exists
				kindKeyMap, kindExists := modelKey.Kinds[kindKey]
				if !kindExists {
					// kindKey does not exist - add it
					modelKey.Kinds[kindKey] = ModelCatalogVersions{
						Versions: map[string]ModelCatalog{
							versionKey: {
								ModelList: model,
								Locations: []string{locationNameKey},
							},
						},
					}
				} else {
					// kindKey exists - check if versionKey exists
					versionList, versionExists := kindKeyMap.Versions[versionKey]
					if !versionExists {
						// versionKey does not exist - add it
						kindKeyMap.Versions[versionKey] = ModelCatalog{
							ModelList: model,
							Locations: []string{locationNameKey},
						}
					} else {
						// versionKey exists - add location to existing version
						versionList.Locations = append(versionList.Locations, locationNameKey)
						kindKeyMap.Versions[versionKey] = versionList
					}
					modelKey.Kinds[kindKey] = kindKeyMap
				}
				// update the combinedResults map
				combinedResults[nameKey] = modelKey
			}
		}
		return true
	})
	return combinedResults, nil
}