@@ -30,6 +30,8 @@ import (
3030 corerpv20250801 "github.com/radius-project/radius/pkg/corerp/api/v20250801preview"
3131 "github.com/radius-project/radius/pkg/to"
3232 ucp "github.com/radius-project/radius/pkg/ucp/api/v20231001preview"
33+ "github.com/radius-project/radius/pkg/ucp/resources"
34+ resources_radius "github.com/radius-project/radius/pkg/ucp/resources/radius"
3335)
3436
3537const (
@@ -164,15 +166,23 @@ func (r *Runner) enterEnvironmentOptions(ctx context.Context, workspace *workspa
164166 options .Environment .Create = true
165167 if ! options .Cluster .Install {
166168 // If Radius is already installed then look for an existing environment first.
167- name , err := r .selectExistingEnvironment (ctx , workspace )
169+ existing , err := r .selectExistingEnvironment (ctx , workspace )
168170 if err != nil {
169171 return err
170172 }
171173
172174 // For an existing environment we won't make changes, so we're done gathering options.
173- if name != nil {
174- options .Environment .Name = * name
175+ if existing != nil {
176+ options .Environment .Name = * existing . Name
175177 options .Environment .Create = false
178+
179+ // Derive the resource group from the existing environment's resource ID so we
180+ // don't assume the resource group name matches the environment name.
181+ id , err := resources .ParseResource (* existing .ID )
182+ if err != nil {
183+ return err
184+ }
185+ options .Environment .ResourceGroup = id .FindScope (resources_radius .ScopeResourceGroups )
176186 return nil
177187 }
178188 }
@@ -188,10 +198,13 @@ func (r *Runner) enterEnvironmentOptions(ctx context.Context, workspace *workspa
188198 return err
189199 }
190200
201+ // For a newly-created environment we put it in a resource group whose name matches the environment name.
202+ options .Environment .ResourceGroup = options .Environment .Name
203+
191204 return nil
192205}
193206
194- func (r * Runner ) selectExistingEnvironment (ctx context.Context , workspace * workspaces.Workspace ) (* string , error ) {
207+ func (r * Runner ) selectExistingEnvironment (ctx context.Context , workspace * workspaces.Workspace ) (* corerpv20250801. EnvironmentResource , error ) {
195208 client , err := r .ConnectionFactory .CreateApplicationsManagementClient (ctx , * workspace )
196209 if err != nil {
197210 return nil , err
@@ -209,9 +222,9 @@ func (r *Runner) selectExistingEnvironment(ctx context.Context, workspace *works
209222
210223 // Without any flags we take the default without asking if it's an option.
211224 if ! r .Full {
212- for _ , env := range environments {
225+ for i , env := range environments {
213226 if strings .EqualFold (defaultEnvironmentName , * env .Name ) {
214- return env . Name , nil
227+ return & environments [ i ] , nil
215228 }
216229 }
217230 }
@@ -226,7 +239,13 @@ func (r *Runner) selectExistingEnvironment(ctx context.Context, workspace *works
226239 return nil , nil
227240 }
228241
229- return & name , nil
242+ for i , env := range environments {
243+ if env .Name != nil && * env .Name == name {
244+ return & environments [i ], nil
245+ }
246+ }
247+
248+ return nil , nil
230249}
231250
232251func (r * Runner ) buildExistingEnvironmentList (existing []corerpv20250801.EnvironmentResource ) []string {
0 commit comments