Skip to content

Commit 246a829

Browse files
committed
feat: Add Rancher edit iam-auth-enabled option and remove iprestrictions
Signed-off-by: Thomas Coudert <thomas.coudert@ovhcloud.com>
1 parent ddcddb2 commit 246a829

File tree

4 files changed

+103
-40
lines changed

4 files changed

+103
-40
lines changed

doc/ovhcloud_cloud_rancher_edit.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@ ovhcloud cloud rancher edit <rancher_id> [flags]
99
### Options
1010

1111
```
12-
--editor Use a text editor to define parameters
13-
-h, --help help for edit
14-
--ip-restrictions stringArray List of IP restrictions (expected format: '<cidrBlock>,<description>')
15-
--name string Name of the managed Rancher service
16-
--plan string Plan of the managed Rancher service (OVHCLOUD_EDITION, STANDARD)
17-
--version string Version of the managed Rancher service
12+
-h, --help help for edit
13+
--iam-auth-enabled Allow Rancher to use identities managed by OVHcloud IAM (Identity and Access Management) to control access
14+
--name string Name of the managed Rancher service
15+
--plan string Plan of the managed Rancher service (OVHCLOUD_EDITION, STANDARD)
16+
--version string Version of the managed Rancher service
1817
```
1918

2019
### Options inherited from parent commands

internal/cmd/cloud_rancher.go

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,29 +32,48 @@ func initCloudRancherCommand(cloudCmd *cobra.Command) {
3232
Args: cobra.ExactArgs(1),
3333
})
3434

35+
addInteractiveEditorFlag(getRancherEditCmd())
36+
rancherCmd.AddCommand(getRancherEditCmd())
37+
38+
rancherCmd.AddCommand(getRancherCreateCmd())
39+
40+
rancherCmd.AddCommand(&cobra.Command{
41+
Use: "delete <rancher_id>",
42+
Short: "Delete a specific Rancher service",
43+
Run: cloud.DeleteRancher,
44+
Args: cobra.ExactArgs(1),
45+
})
46+
47+
cloudCmd.AddCommand(rancherCmd)
48+
}
49+
50+
func getRancherEditCmd() *cobra.Command {
3551
editRancherCmd := &cobra.Command{
3652
Use: "edit <rancher_id>",
3753
Short: "Edit the given Rancher service",
3854
Run: cloud.EditRancher,
3955
Args: cobra.ExactArgs(1),
4056
}
57+
4158
editRancherCmd.Flags().StringVar(&cloud.RancherSpec.TargetSpec.Name, "name", "", "Name of the managed Rancher service")
4259
editRancherCmd.Flags().StringVar(&cloud.RancherSpec.TargetSpec.Plan, "plan", "", "Plan of the managed Rancher service (OVHCLOUD_EDITION, STANDARD)")
4360
editRancherCmd.Flags().StringVar(&cloud.RancherSpec.TargetSpec.Version, "version", "", "Version of the managed Rancher service")
44-
editRancherCmd.Flags().StringArrayVar(&cloud.RancherSpec.TargetSpec.CLIIPRestrictions, "ip-restrictions", nil, "List of IP restrictions (expected format: '<cidrBlock>,<description>')")
45-
addInteractiveEditorFlag(editRancherCmd)
46-
rancherCmd.AddCommand(editRancherCmd)
4761

48-
rancherCmd.AddCommand(getRancherCreateCmd())
62+
var iamAuthEnabled bool
63+
editRancherCmd.Flags().BoolVar(&iamAuthEnabled, "iam-auth-enabled", false, "Allow Rancher to use identities managed by OVHcloud IAM (Identity and Access Management) to control access")
64+
cloud.RancherSpec.TargetSpec.IAMAuthEnabled = &iamAuthEnabled
4965

50-
rancherCmd.AddCommand(&cobra.Command{
51-
Use: "delete <rancher_id>",
52-
Short: "Delete a specific Rancher service",
53-
Run: cloud.DeleteRancher,
54-
Args: cobra.ExactArgs(1),
55-
})
66+
editRancherCmd.PreRunE = func(cmd *cobra.Command, args []string) error {
67+
if cmd.Flags().Changed("iam-auth-enabled") {
68+
cloud.RancherSpec.TargetSpec.IAMAuthEnabled = &iamAuthEnabled
69+
} else {
70+
cloud.RancherSpec.TargetSpec.IAMAuthEnabled = nil
71+
}
5672

57-
cloudCmd.AddCommand(rancherCmd)
73+
return nil
74+
}
75+
76+
return editRancherCmd
5877
}
5978

6079
func getRancherCreateCmd() *cobra.Command {
@@ -105,7 +124,7 @@ There are three ways to define the creation parameters:
105124
rancherCreateCmd.Flags().StringVar(&cloud.RancherSpec.TargetSpec.Name, "name", "", "Name of the managed Rancher service")
106125
rancherCreateCmd.Flags().StringVar(&cloud.RancherSpec.TargetSpec.Plan, "plan", "", "Plan of the managed Rancher service (available plans can be listed using 'cloud reference rancher list-plans' command)")
107126
rancherCreateCmd.Flags().StringVar(&cloud.RancherSpec.TargetSpec.Version, "version", "", "Version of the managed Rancher service (available versions can be listed using 'cloud reference rancher list-versions' command)")
108-
rancherCreateCmd.Flags().BoolVar(&cloud.RancherSpec.TargetSpec.IAMAuthEnabled, "iam-auth-enabled", false, "Allow Rancher to use identities managed by OVHcloud IAM (Identity and Access Management) to control access")
127+
rancherCreateCmd.Flags().BoolVar(cloud.RancherSpec.TargetSpec.IAMAuthEnabled, "iam-auth-enabled", false, "Allow Rancher to use identities managed by OVHcloud IAM (Identity and Access Management) to control access")
109128

110129
// Common flags for other means to define parameters
111130
addInitParameterFileFlag(rancherCreateCmd, assets.CloudV2OpenapiSchema, "/cloud/project/{serviceName}/rancher", "post", cloud.CloudRancherCreationExample, nil)

internal/cmd/cloud_rancher_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ func (ms *MockSuite) TestCloudRancherCreateCmd(assert, require *td.T) {
2020
tdhttpmock.JSONBody(td.JSON(`
2121
{
2222
"targetSpec": {
23+
"iamAuthEnabled": false,
2324
"name": "test-rancher",
2425
"plan": "OVHCLOUD_EDITION",
2526
"version": "2.11.3"
@@ -40,6 +41,7 @@ func (ms *MockSuite) TestCloudRancherCreateCmdJSONFormat(assert, require *td.T)
4041
tdhttpmock.JSONBody(td.JSON(`
4142
{
4243
"targetSpec": {
44+
"iamAuthEnabled": false,
4345
"name": "test-rancher",
4446
"plan": "OVHCLOUD_EDITION",
4547
"version": "2.11.3"
@@ -60,6 +62,7 @@ func (ms *MockSuite) TestCloudRancherCreateCmdYAMLFormat(assert, require *td.T)
6062
tdhttpmock.JSONBody(td.JSON(`
6163
{
6264
"targetSpec": {
65+
"iamAuthEnabled": false,
6366
"name": "test-rancher",
6467
"plan": "OVHCLOUD_EDITION",
6568
"version": "2.11.3"
@@ -83,6 +86,7 @@ func (ms *MockSuite) TestCloudRancherCreateCmdCustomFormat(assert, require *td.T
8386
tdhttpmock.JSONBody(td.JSON(`
8487
{
8588
"targetSpec": {
89+
"iamAuthEnabled": false,
8690
"name": "test-rancher",
8791
"plan": "OVHCLOUD_EDITION",
8892
"version": "2.11.3"
@@ -96,3 +100,45 @@ func (ms *MockSuite) TestCloudRancherCreateCmdCustomFormat(assert, require *td.T
96100
require.CmpNoError(err)
97101
assert.String(out, `["rancher-12345"]`)
98102
}
103+
104+
func (ms *MockSuite) TestCloudRancherCreateCmdWithIamAuthEnabledTrue(assert, require *td.T) {
105+
httpmock.RegisterMatcherResponder(http.MethodPost,
106+
"https://eu.api.ovh.com/v2/publicCloud/project/fakeProjectID/rancher",
107+
tdhttpmock.JSONBody(td.JSON(`
108+
{
109+
"targetSpec": {
110+
"iamAuthEnabled": true,
111+
"name": "test-rancher",
112+
"plan": "OVHCLOUD_EDITION",
113+
"version": "2.11.3"
114+
}
115+
}`),
116+
),
117+
httpmock.NewStringResponder(200, `{"id": "rancher-12345"}`),
118+
)
119+
120+
out, err := cmd.Execute("cloud", "rancher", "create", "--cloud-project", "fakeProjectID", "--name", "test-rancher", "--plan", "OVHCLOUD_EDITION", "--version", "2.11.3", "--iam-auth-enabled=true")
121+
require.CmpNoError(err)
122+
assert.String(out, `✅ Rancher test-rancher created successfully (id: rancher-12345)`)
123+
}
124+
125+
func (ms *MockSuite) TestCloudRancherCreateCmdWithIamAuthEnabledFalse(assert, require *td.T) {
126+
httpmock.RegisterMatcherResponder(http.MethodPost,
127+
"https://eu.api.ovh.com/v2/publicCloud/project/fakeProjectID/rancher",
128+
tdhttpmock.JSONBody(td.JSON(`
129+
{
130+
"targetSpec": {
131+
"iamAuthEnabled": false,
132+
"name": "test-rancher",
133+
"plan": "OVHCLOUD_EDITION",
134+
"version": "2.11.3"
135+
}
136+
}`),
137+
),
138+
httpmock.NewStringResponder(200, `{"id": "rancher-12345"}`),
139+
)
140+
141+
out, err := cmd.Execute("cloud", "rancher", "create", "--cloud-project", "fakeProjectID", "--name", "test-rancher", "--plan", "OVHCLOUD_EDITION", "--version", "2.11.3", "--iam-auth-enabled=false")
142+
require.CmpNoError(err)
143+
assert.String(out, `✅ Rancher test-rancher created successfully (id: rancher-12345)`)
144+
}

internal/services/cloud/cloud_rancher.go

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
_ "embed"
99
"fmt"
1010
"net/url"
11-
"strings"
1211

1312
"github.com/ovh/ovhcloud-cli/internal/assets"
1413
"github.com/ovh/ovhcloud-cli/internal/display"
@@ -29,20 +28,20 @@ var (
2928

3029
RancherSpec struct {
3130
TargetSpec struct {
32-
IAMAuthEnabled bool `json:"iamAuthEnabled,omitempty"`
33-
Name string `json:"name,omitempty"`
34-
Plan string `json:"plan,omitempty"`
35-
Version string `json:"version,omitempty"`
36-
IPRestrictions []rancherIPRestriction `json:"ipRestrictions,omitempty"`
37-
CLIIPRestrictions []string `json:"-"`
31+
IAMAuthEnabled *bool `json:"iamAuthEnabled,omitempty"`
32+
Name string `json:"name,omitempty"`
33+
Plan string `json:"plan,omitempty"`
34+
Version string `json:"version,omitempty"`
35+
// IPRestrictions []rancherIPRestriction `json:"ipRestrictions,omitempty"`
36+
// CLIIPRestrictions []string `json:"-"`
3837
} `json:"targetSpec"`
3938
}
4039
)
4140

42-
type rancherIPRestriction struct {
43-
CIDRBlock string `json:"cidrBlock"`
44-
Description string `json:"description"`
45-
}
41+
//type rancherIPRestriction struct {
42+
// CIDRBlock string `json:"cidrBlock"`
43+
// Description string `json:"description"`
44+
//}
4645

4746
func ListCloudRanchers(_ *cobra.Command, _ []string) {
4847
projectID, err := getConfiguredCloudProject()
@@ -65,17 +64,17 @@ func GetRancher(_ *cobra.Command, args []string) {
6564
}
6665

6766
func EditRancher(cmd *cobra.Command, args []string) {
68-
for _, ipRestriction := range RancherSpec.TargetSpec.CLIIPRestrictions {
69-
parts := strings.Split(ipRestriction, ",")
70-
if len(parts) != 2 {
71-
display.OutputError(&flags.OutputFormatConfig, "Invalid IP restriction format: %s. Expected format: '<cidrBlock>,<description>'", ipRestriction)
72-
return
73-
}
74-
RancherSpec.TargetSpec.IPRestrictions = append(RancherSpec.TargetSpec.IPRestrictions, rancherIPRestriction{
75-
CIDRBlock: parts[0],
76-
Description: parts[1],
77-
})
78-
}
67+
//for _, ipRestriction := range RancherSpec.TargetSpec.CLIIPRestrictions {
68+
// parts := strings.Split(ipRestriction, ",")
69+
// if len(parts) != 2 {
70+
// display.OutputError(&flags.OutputFormatConfig, "Invalid IP restriction format: %s. Expected format: '<cidrBlock>,<description>'", ipRestriction)
71+
// return
72+
// }
73+
// RancherSpec.TargetSpec.IPRestrictions = append(RancherSpec.TargetSpec.IPRestrictions, rancherIPRestriction{
74+
// CIDRBlock: parts[0],
75+
// Description: parts[1],
76+
// })
77+
//}
7978

8079
projectID, err := getConfiguredCloudProject()
8180
if err != nil {

0 commit comments

Comments
 (0)