Skip to content

Commit 2df99ce

Browse files
nithyatsuCopilot
andcommitted
wip
Co-authored-by: Copilot <copilot@github.com>
1 parent 9886f05 commit 2df99ce

3 files changed

Lines changed: 40 additions & 9 deletions

File tree

pkg/cli/cmd/radinit/preview/environment.go

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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

3537
const (
@@ -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

232251
func (r *Runner) buildExistingEnvironmentList(existing []corerpv20250801.EnvironmentResource) []string {

pkg/cli/cmd/radinit/preview/init_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ func Test_Validate(t *testing.T) {
180180
// Configure an existing environment - but then choose to create a new one
181181
setExistingEnvironments(mocks.ApplicationManagementClient, []corerpv20250801.EnvironmentResource{
182182
{
183+
ID: to.Ptr("/planes/radius/local/resourceGroups/cool-existing-env/providers/Radius.Core/environments/cool-existing-env"),
183184
Name: to.Ptr("cool-existing-env"),
184185
},
185186
})
@@ -215,6 +216,7 @@ func Test_Validate(t *testing.T) {
215216
// Configure an existing environment - but then choose to create a new one
216217
setExistingEnvironments(mocks.ApplicationManagementClient, []corerpv20250801.EnvironmentResource{
217218
{
219+
ID: to.Ptr("/planes/radius/local/resourceGroups/cool-existing-env/providers/Radius.Core/environments/cool-existing-env"),
218220
Name: to.Ptr("cool-existing-env"),
219221
},
220222
})
@@ -245,6 +247,7 @@ func Test_Validate(t *testing.T) {
245247
// Configure an existing environment - but then choose to create a new one
246248
setExistingEnvironments(mocks.ApplicationManagementClient, []corerpv20250801.EnvironmentResource{
247249
{
250+
ID: to.Ptr("/planes/radius/local/resourceGroups/cool-existing-env/providers/Radius.Core/environments/cool-existing-env"),
248251
Name: to.Ptr("cool-existing-env"),
249252
},
250253
})
@@ -416,6 +419,7 @@ func Test_Validate(t *testing.T) {
416419
// Configure an existing environment - but then choose to create a new one
417420
setExistingEnvironments(mocks.ApplicationManagementClient, []corerpv20250801.EnvironmentResource{
418421
{
422+
ID: to.Ptr("/planes/radius/local/resourceGroups/cool-existing-env/providers/Radius.Core/environments/cool-existing-env"),
419423
Name: to.Ptr("cool-existing-env"),
420424
},
421425
})
@@ -483,6 +487,7 @@ func Test_Validate(t *testing.T) {
483487
// Configure an existing environment - this will be chosen automatically
484488
setExistingEnvironments(mocks.ApplicationManagementClient, []corerpv20250801.EnvironmentResource{
485489
{
490+
ID: to.Ptr("/planes/radius/local/resourceGroups/myenv/providers/Radius.Core/environments/myenv"),
486491
Name: to.Ptr("myenv"),
487492
},
488493
})
@@ -507,6 +512,7 @@ func Test_Validate(t *testing.T) {
507512
// Configure an existing environment - this will be chosen automatically
508513
setExistingEnvironments(mocks.ApplicationManagementClient, []corerpv20250801.EnvironmentResource{
509514
{
515+
ID: to.Ptr("/planes/radius/local/resourceGroups/default/providers/Radius.Core/environments/default"),
510516
Name: to.Ptr("default"),
511517
},
512518
})
@@ -530,9 +536,11 @@ func Test_Validate(t *testing.T) {
530536
// Configure an existing environment - user has to choose
531537
setExistingEnvironments(mocks.ApplicationManagementClient, []corerpv20250801.EnvironmentResource{
532538
{
539+
ID: to.Ptr("/planes/radius/local/resourceGroups/dev/providers/Radius.Core/environments/dev"),
533540
Name: to.Ptr("dev"),
534541
},
535542
{
543+
ID: to.Ptr("/planes/radius/local/resourceGroups/prod/providers/Radius.Core/environments/prod"),
536544
Name: to.Ptr("prod"),
537545
},
538546
})

pkg/cli/cmd/radinit/preview/options.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ type environmentOptions struct {
4949
Create bool
5050
Name string
5151
Namespace string
52+
// ResourceGroup is the name of the resource group that contains (or will contain) the environment.
53+
// For an existing environment this is parsed from the environment's resource ID. For a new
54+
// environment this defaults to the environment name.
55+
ResourceGroup string
5256
}
5357

5458
// cloudProviderOptions holds all of the options that will be used to initialize cloud providers.
@@ -120,7 +124,7 @@ func (r *Runner) enterInitOptions(ctx context.Context) (*initOptions, *workspace
120124
workspace.Name = ws.Name
121125
}
122126

123-
workspace.Environment = fmt.Sprintf("/planes/radius/local/resourceGroups/%s/providers/Radius.Core/environments/%s", options.Environment.Name, options.Environment.Name)
124-
workspace.Scope = fmt.Sprintf("/planes/radius/local/resourceGroups/%s", options.Environment.Name)
127+
workspace.Environment = fmt.Sprintf("/planes/radius/local/resourceGroups/%s/providers/Radius.Core/environments/%s", options.Environment.ResourceGroup, options.Environment.Name)
128+
workspace.Scope = fmt.Sprintf("/planes/radius/local/resourceGroups/%s", options.Environment.ResourceGroup)
125129
return &options, workspace, nil
126130
}

0 commit comments

Comments
 (0)