Skip to content

Commit d981c56

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 05f4f5e commit d981c56

File tree

4 files changed

+106
-41
lines changed

4 files changed

+106
-41
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: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,7 @@ func initCloudRancherCommand(cloudCmd *cobra.Command) {
3232
Args: cobra.ExactArgs(1),
3333
})
3434

35-
editRancherCmd := &cobra.Command{
36-
Use: "edit <rancher_id>",
37-
Short: "Edit the given Rancher service",
38-
Run: cloud.EditRancher,
39-
Args: cobra.ExactArgs(1),
40-
}
41-
editRancherCmd.Flags().StringVar(&cloud.RancherSpec.TargetSpec.Name, "name", "", "Name of the managed Rancher service")
42-
editRancherCmd.Flags().StringVar(&cloud.RancherSpec.TargetSpec.Plan, "plan", "", "Plan of the managed Rancher service (OVHCLOUD_EDITION, STANDARD)")
43-
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)
35+
rancherCmd.AddCommand(getRancherEditCmd())
4736

4837
rancherCmd.AddCommand(getRancherCreateCmd())
4938

@@ -64,6 +53,38 @@ func initCloudRancherCommand(cloudCmd *cobra.Command) {
6453
cloudCmd.AddCommand(rancherCmd)
6554
}
6655

56+
func getRancherEditCmd() *cobra.Command {
57+
editRancherCmd := &cobra.Command{
58+
Use: "edit <rancher_id>",
59+
Short: "Edit the given Rancher service",
60+
Run: cloud.EditRancher,
61+
Args: cobra.ExactArgs(1),
62+
}
63+
64+
editRancherCmd.Flags().StringVar(&cloud.RancherSpec.TargetSpec.Name, "name", "", "Name of the managed Rancher service")
65+
editRancherCmd.Flags().StringVar(&cloud.RancherSpec.TargetSpec.Plan, "plan", "", "Plan of the managed Rancher service (OVHCLOUD_EDITION, STANDARD)")
66+
editRancherCmd.Flags().StringVar(&cloud.RancherSpec.TargetSpec.Version, "version", "", "Version of the managed Rancher service")
67+
68+
var iamAuthEnabled bool
69+
editRancherCmd.Flags().BoolVar(&iamAuthEnabled, "iam-auth-enabled", false, "Allow Rancher to use identities managed by OVHcloud IAM (Identity and Access Management) to control access")
70+
cloud.RancherSpec.TargetSpec.IAMAuthEnabled = &iamAuthEnabled
71+
72+
// Handle optional iam-auth-enabled boolean
73+
editRancherCmd.PreRunE = func(cmd *cobra.Command, args []string) error {
74+
if cmd.Flags().Changed("iam-auth-enabled") {
75+
cloud.RancherSpec.TargetSpec.IAMAuthEnabled = &iamAuthEnabled
76+
} else {
77+
cloud.RancherSpec.TargetSpec.IAMAuthEnabled = nil
78+
}
79+
80+
return nil
81+
}
82+
83+
addInteractiveEditorFlag(editRancherCmd)
84+
85+
return editRancherCmd
86+
}
87+
6788
func getRancherCreateCmd() *cobra.Command {
6889
rancherCreateCmd := &cobra.Command{
6990
Use: "create",
@@ -112,7 +133,7 @@ There are three ways to define the creation parameters:
112133
rancherCreateCmd.Flags().StringVar(&cloud.RancherSpec.TargetSpec.Name, "name", "", "Name of the managed Rancher service")
113134
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)")
114135
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)")
115-
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")
136+
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")
116137

117138
// Common flags for other means to define parameters
118139
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"
@@ -108,3 +112,45 @@ func (ms *MockSuite) TestCloudRancherResetAdminCredentialsCmd(assert, require *t
108112

109113
assert.String(out, `✅ New Rancher service password for user admin: new-secret`)
110114
}
115+
116+
func (ms *MockSuite) TestCloudRancherCreateCmdWithIamAuthEnabledTrue(assert, require *td.T) {
117+
httpmock.RegisterMatcherResponder(http.MethodPost,
118+
"https://eu.api.ovh.com/v2/publicCloud/project/fakeProjectID/rancher",
119+
tdhttpmock.JSONBody(td.JSON(`
120+
{
121+
"targetSpec": {
122+
"iamAuthEnabled": true,
123+
"name": "test-rancher",
124+
"plan": "OVHCLOUD_EDITION",
125+
"version": "2.11.3"
126+
}
127+
}`),
128+
),
129+
httpmock.NewStringResponder(200, `{"id": "rancher-12345"}`),
130+
)
131+
132+
out, err := cmd.Execute("cloud", "rancher", "create", "--cloud-project", "fakeProjectID", "--name", "test-rancher", "--plan", "OVHCLOUD_EDITION", "--version", "2.11.3", "--iam-auth-enabled=true")
133+
require.CmpNoError(err)
134+
assert.String(out, `✅ Rancher test-rancher created successfully (id: rancher-12345)`)
135+
}
136+
137+
func (ms *MockSuite) TestCloudRancherCreateCmdWithIamAuthEnabledFalse(assert, require *td.T) {
138+
httpmock.RegisterMatcherResponder(http.MethodPost,
139+
"https://eu.api.ovh.com/v2/publicCloud/project/fakeProjectID/rancher",
140+
tdhttpmock.JSONBody(td.JSON(`
141+
{
142+
"targetSpec": {
143+
"iamAuthEnabled": false,
144+
"name": "test-rancher",
145+
"plan": "OVHCLOUD_EDITION",
146+
"version": "2.11.3"
147+
}
148+
}`),
149+
),
150+
httpmock.NewStringResponder(200, `{"id": "rancher-12345"}`),
151+
)
152+
153+
out, err := cmd.Execute("cloud", "rancher", "create", "--cloud-project", "fakeProjectID", "--name", "test-rancher", "--plan", "OVHCLOUD_EDITION", "--version", "2.11.3", "--iam-auth-enabled=false")
154+
require.CmpNoError(err)
155+
assert.String(out, `✅ Rancher test-rancher created successfully (id: rancher-12345)`)
156+
}

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,21 +28,21 @@ 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

4241
type (
43-
rancherIPRestriction struct {
44-
CIDRBlock string `json:"cidrBlock"`
45-
Description string `json:"description"`
46-
}
42+
//type rancherIPRestriction struct {
43+
// CIDRBlock string `json:"cidrBlock"`
44+
// Description string `json:"description"`
45+
//}
4746

4847
rancherUser struct {
4948
Username string `json:"username"`
@@ -72,17 +71,17 @@ func GetRancher(_ *cobra.Command, args []string) {
7271
}
7372

7473
func EditRancher(cmd *cobra.Command, args []string) {
75-
for _, ipRestriction := range RancherSpec.TargetSpec.CLIIPRestrictions {
76-
parts := strings.Split(ipRestriction, ",")
77-
if len(parts) != 2 {
78-
display.OutputError(&flags.OutputFormatConfig, "Invalid IP restriction format: %s. Expected format: '<cidrBlock>,<description>'", ipRestriction)
79-
return
80-
}
81-
RancherSpec.TargetSpec.IPRestrictions = append(RancherSpec.TargetSpec.IPRestrictions, rancherIPRestriction{
82-
CIDRBlock: parts[0],
83-
Description: parts[1],
84-
})
85-
}
74+
//for _, ipRestriction := range RancherSpec.TargetSpec.CLIIPRestrictions {
75+
// parts := strings.Split(ipRestriction, ",")
76+
// if len(parts) != 2 {
77+
// display.OutputError(&flags.OutputFormatConfig, "Invalid IP restriction format: %s. Expected format: '<cidrBlock>,<description>'", ipRestriction)
78+
// return
79+
// }
80+
// RancherSpec.TargetSpec.IPRestrictions = append(RancherSpec.TargetSpec.IPRestrictions, rancherIPRestriction{
81+
// CIDRBlock: parts[0],
82+
// Description: parts[1],
83+
// })
84+
//}
8685

8786
projectID, err := getConfiguredCloudProject()
8887
if err != nil {

0 commit comments

Comments
 (0)