Skip to content

Commit 4da9b47

Browse files
committed
feat: Add Rancher reset-admin-credentials command
Signed-off-by: Thomas Coudert <thomas.coudert@ovhcloud.com>
1 parent 747c474 commit 4da9b47

File tree

6 files changed

+89
-8
lines changed

6 files changed

+89
-8
lines changed

doc/ovhcloud.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ Below is the full list of primary sub‑commands available at the time of writin
122122
* [ovhcloud webhosting](ovhcloud_webhosting.md) - Retrieve information and manage your WebHosting services
123123
* [ovhcloud xdsl](ovhcloud_xdsl.md) - Retrieve information and manage your XDSL services
124124

125-
> **Tip**  Use `--json`, `--yaml`, or `--format` with a gval expression to integrate `ovhcloud` into scripts and automation pipelines.
125+
> **Tip** Use `--json`, `--yaml`, or `--format` with a gval expression to integrate `ovhcloud` into scripts and automation pipelines.
126126
127127
---
128128

@@ -139,9 +139,9 @@ Below is the full list of primary sub‑commands available at the time of writin
139139

140140
## Troubleshooting
141141

142-
* **Verbose output** — Use `--debug` to inspect raw API calls and responses.
143-
* **Authentication issues** — Run `ovhcloud login` again to regenerate valid API keys.
144-
* **Rate limits** — OVHcloud APIs impose rate limits; plan retries or exponential backoff in scripts.
142+
* **Verbose output** — Use `--debug` to inspect raw API calls and responses.
143+
* **Authentication issues** — Run `ovhcloud login` again to regenerate valid API keys.
144+
* **Rate limits** — OVHcloud APIs impose rate limits; plan retries or exponential backoff in scripts.
145145

146146
---
147147

doc/ovhcloud_cloud_rancher.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,5 @@ Manage Rancher services in the given cloud project
3535
* [ovhcloud cloud rancher edit](ovhcloud_cloud_rancher_edit.md) - Edit the given Rancher service
3636
* [ovhcloud cloud rancher get](ovhcloud_cloud_rancher_get.md) - Get a specific Rancher service
3737
* [ovhcloud cloud rancher list](ovhcloud_cloud_rancher_list.md) - List Rancher services
38+
* [ovhcloud cloud rancher reset-admin-credentials](ovhcloud_cloud_rancher_reset-admin-credentials.md) - Reset admin user credentials
3839

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
## ovhcloud cloud rancher reset-admin-credentials
2+
3+
Reset admin user credentials
4+
5+
```
6+
ovhcloud cloud rancher reset-admin-credentials <rancher_id> [flags]
7+
```
8+
9+
### Options
10+
11+
```
12+
-h, --help help for reset-admin-credentials
13+
```
14+
15+
### Options inherited from parent commands
16+
17+
```
18+
--cloud-project string Cloud project ID
19+
-d, --debug Activate debug mode (will log all HTTP requests details)
20+
-f, --format string Output value according to given format (expression using https://github.com/PaesslerAG/gval syntax)
21+
Examples:
22+
--format 'id' (to extract a single field)
23+
--format 'nested.field.subfield' (to extract a nested field)
24+
--format '[id, 'name']' (to extract multiple fields as an array)
25+
--format '{"newKey": oldKey, "otherKey": nested.field}' (to extract and rename fields in an object)
26+
--format 'name+","+type' (to extract and concatenate fields in a string)
27+
--format '(nbFieldA + nbFieldB) * 10' (to compute values from numeric fields)
28+
-e, --ignore-errors Ignore errors in API calls when it is not fatal to the execution
29+
-i, --interactive Interactive output
30+
-j, --json Output in JSON
31+
-y, --yaml Output in YAML
32+
```
33+
34+
### SEE ALSO
35+
36+
* [ovhcloud cloud rancher](ovhcloud_cloud_rancher.md) - Manage Rancher services in the given cloud project
37+

internal/cmd/cloud_rancher.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ func initCloudRancherCommand(cloudCmd *cobra.Command) {
4747

4848
rancherCmd.AddCommand(getRancherCreateCmd())
4949

50+
rancherCmd.AddCommand(&cobra.Command{
51+
Use: "reset-admin-credentials <rancher_id>",
52+
Short: "Reset admin user credentials",
53+
Run: cloud.ResetAdminCredentials,
54+
Args: cobra.ExactArgs(1),
55+
})
56+
5057
rancherCmd.AddCommand(&cobra.Command{
5158
Use: "delete <rancher_id>",
5259
Short: "Delete a specific Rancher service",

internal/cmd/cloud_rancher_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,15 @@ func (ms *MockSuite) TestCloudRancherCreateCmdCustomFormat(assert, require *td.T
9696
require.CmpNoError(err)
9797
assert.String(out, `["rancher-12345"]`)
9898
}
99+
func (ms *MockSuite) TestCloudRancherResetAdminCredentialsCmd(assert, require *td.T) {
100+
httpmock.RegisterMatcherResponder(http.MethodPost,
101+
"https://eu.api.ovh.com/v2/publicCloud/project/fakeProjectID/rancher/fakeRancherID/adminCredentials",
102+
httpmock.Matcher{},
103+
httpmock.NewStringResponder(200, `{"username":"admin","password":"new-secret"}`),
104+
)
105+
106+
out, err := cmd.Execute("cloud", "rancher", "reset-admin-credentials", "--cloud-project", "fakeProjectID", "fakeRancherID")
107+
require.CmpNoError(err)
108+
109+
assert.String(out, `✅ New Rancher service password for user admin: new-secret`)
110+
}

internal/services/cloud/cloud_rancher.go

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,17 @@ var (
3939
}
4040
)
4141

42-
type rancherIPRestriction struct {
43-
CIDRBlock string `json:"cidrBlock"`
44-
Description string `json:"description"`
45-
}
42+
type (
43+
rancherIPRestriction struct {
44+
CIDRBlock string `json:"cidrBlock"`
45+
Description string `json:"description"`
46+
}
47+
48+
rancherUser struct {
49+
Username string `json:"username"`
50+
Password string `json:"password"`
51+
}
52+
)
4653

4754
func ListCloudRanchers(_ *cobra.Command, _ []string) {
4855
projectID, err := getConfiguredCloudProject()
@@ -119,6 +126,23 @@ func CreateRancher(cmd *cobra.Command, args []string) {
119126
display.OutputInfo(&flags.OutputFormatConfig, rancher, "✅ Rancher %s created successfully (id: %s)", RancherSpec.TargetSpec.Name, rancher["id"])
120127
}
121128

129+
func ResetAdminCredentials(_ *cobra.Command, args []string) {
130+
projectID, err := getConfiguredCloudProject()
131+
if err != nil {
132+
display.OutputError(&flags.OutputFormatConfig, "%s", err)
133+
return
134+
}
135+
136+
var user rancherUser
137+
endpoint := fmt.Sprintf("/v2/publicCloud/project/%s/rancher/%s/adminCredentials", projectID, url.PathEscape(args[0]))
138+
if err := httpLib.Client.Post(endpoint, nil, &user); err != nil {
139+
display.OutputError(&flags.OutputFormatConfig, "failed to reset admin credentials for Rancher service: %s", err)
140+
return
141+
}
142+
143+
display.OutputInfo(&flags.OutputFormatConfig, nil, "✅ New Rancher service password for user %s: %s", user.Username, user.Password)
144+
}
145+
122146
func DeleteRancher(_ *cobra.Command, args []string) {
123147
projectID, err := getConfiguredCloudProject()
124148
if err != nil {

0 commit comments

Comments
 (0)