@@ -26,6 +26,7 @@ type LocationFilterPredicate func(loc account.Location) bool
2626type Prompter interface {
2727 PromptSubscription (ctx context.Context , msg string ) (subscriptionId string , err error )
2828 PromptLocation (ctx context.Context , subId string , msg string , filter LocationFilterPredicate ) (string , error )
29+ PromptResource (ctx context.Context , subId string , msg string , resourceType string ) (string , error )
2930 PromptResourceGroup (ctx context.Context ) (string , error )
3031}
3132
@@ -111,6 +112,42 @@ func (p *DefaultPrompter) PromptLocation(
111112 return loc , nil
112113}
113114
115+ func (p * DefaultPrompter ) PromptResource (
116+ ctx context.Context ,
117+ subId string ,
118+ msg string ,
119+ resourceType string ,
120+ ) (string , error ) {
121+ options := azapi.ListResourcesOptions {
122+ ResourceType : resourceType ,
123+ }
124+ resources , err := p .resourceService .ListResources (ctx , p .env .GetSubscriptionId (), & options )
125+ if err != nil {
126+ return "" , fmt .Errorf ("listing resources: %w" , err )
127+ }
128+
129+ slices .SortFunc (resources , func (a , b * azapi.Resource ) int {
130+ return strings .Compare (a .Name , b .Name )
131+ })
132+
133+ // TODO: Add `optional` field to allow something like "Create a new resource" (similar to resources groups below) and return ""?
134+ choices := make ([]string , len (resources ))
135+ for idx , resource := range resources {
136+ // TODO: Get location display names from account manager instead?
137+ choices [idx ] = fmt .Sprintf ("%d. %s (%s)" , idx + 1 , resource .Name , resource .Location )
138+ }
139+
140+ choice , err := p .console .Select (ctx , input.ConsoleOptions {
141+ Message : msg ,
142+ Options : choices ,
143+ })
144+ if err != nil {
145+ return "" , fmt .Errorf ("selecting resource: %w" , err )
146+ }
147+
148+ return resources [choice ].Name , nil
149+ }
150+
114151func (p * DefaultPrompter ) PromptResourceGroup (ctx context.Context ) (string , error ) {
115152 // Get current resource groups
116153 groups , err := p .resourceService .ListResourceGroup (ctx , p .env .GetSubscriptionId (), nil )
0 commit comments