Skip to content

Commit

Permalink
feat(instance): cache image auto complete result (#2503)
Browse files Browse the repository at this point in the history
  • Loading branch information
Codelax authored Sep 14, 2022
1 parent f3f8169 commit d10397a
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions internal/namespaces/instance/v1/custom_server_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -644,20 +644,26 @@ func sanitizeVolumeMap(serverName string, volumes map[string]*instance.VolumeSer
return m
}

// Caching listImage response for shell completion
var completeListImagesCache *marketplace.ListImagesResponse

func instanceServerCreateImageAutoCompleteFunc(ctx context.Context, prefix string) core.AutocompleteSuggestions {
suggestions := core.AutocompleteSuggestions(nil)

client := core.ExtractClient(ctx)
api := marketplace.NewAPI(client)

res, err := api.ListImages(&marketplace.ListImagesRequest{}, scw.WithAllPages())
if err != nil {
return nil
if completeListImagesCache == nil {
res, err := api.ListImages(&marketplace.ListImagesRequest{}, scw.WithAllPages())
if err != nil {
return nil
}
completeListImagesCache = res
}

prefix = strings.ToLower(strings.Replace(prefix, "-", "_", -1))

for _, image := range res.Images {
for _, image := range completeListImagesCache.Images {
if strings.HasPrefix(image.Label, prefix) {
suggestions = append(suggestions, image.Label)
}
Expand Down

0 comments on commit d10397a

Please sign in to comment.