Skip to content

Commit 2fcfd7b

Browse files
authored
Merge pull request ovh#64 from thcdrt/dev/thcdrt/rancher-improve-create-and-edit
Improve Rancher create and edit
2 parents b62ccec + 9d5dd36 commit 2fcfd7b

File tree

5 files changed

+158
-43
lines changed

5 files changed

+158
-43
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/assets/api-schemas/cloud_v2.json

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@
5959
"ERROR",
6060
"PENDING",
6161
"RUNNING",
62-
"SCHEDULED"
62+
"SCHEDULED",
63+
"WAITING_USER_INPUT"
6364
]
6465
},
6566
"common.Event": {
@@ -239,7 +240,8 @@
239240
"ERROR",
240241
"PENDING",
241242
"RUNNING",
242-
"SCHEDULED"
243+
"SCHEDULED",
244+
"WAITING_USER_INPUT"
243245
]
244246
},
245247
"duration": {
@@ -831,6 +833,10 @@
831833
"type": "object",
832834
"description": "Target specification of the managed Rancher service",
833835
"properties": {
836+
"iamAuthEnabled": {
837+
"type": "boolean",
838+
"description": "Allows Rancher to use identities managed by OVHcloud IAM (Identity and Access Management) to control access"
839+
},
834840
"name": {
835841
"type": "string",
836842
"description": "Name of the managed Rancher service"
@@ -864,6 +870,12 @@
864870
"format": "password",
865871
"readOnly": true
866872
},
873+
"iamAuthEnabled": {
874+
"type": "boolean",
875+
"description": "Allows Rancher to use identities managed by OVHcloud IAM (Identity and Access Management) to control access",
876+
"nullable": true,
877+
"readOnly": true
878+
},
867879
"ipRestrictions": {
868880
"type": "array",
869881
"description": "List of allowed CIDR blocks for a managed Rancher service's IP restrictions. When empty, any IP is allowed",
@@ -933,6 +945,10 @@
933945
"type": "object",
934946
"description": "Target specification of the managed Rancher service",
935947
"properties": {
948+
"iamAuthEnabled": {
949+
"type": "boolean",
950+
"description": "Allows Rancher to use identities managed by OVHcloud IAM (Identity and Access Management) to control access"
951+
},
936952
"ipRestrictions": {
937953
"type": "array",
938954
"description": "List of allowed CIDR blocks for a managed Rancher service's IP restrictions. When empty, any IP is allowed",
@@ -1122,6 +1138,25 @@
11221138
"format": "time",
11231139
"example": "15:04:05"
11241140
}
1141+
},
1142+
"securitySchemes": {
1143+
"oAuth2AuthCode": {
1144+
"type": "oauth2",
1145+
"description": "Oauth2",
1146+
"x-client-id": "1bb9c7df371741c0",
1147+
"x-client-secret": "a5b4de870aca620d10fbf63cd18d205b",
1148+
"flows": {
1149+
"authorizationCode": {
1150+
"authorizationUrl": "https://www.ovh.com/auth/oauth2/authorize",
1151+
"tokenUrl": "https://www.ovh.com/auth/oauth2/token",
1152+
"scopes": {
1153+
"account/all": "Manage your account",
1154+
"all": "Manage your whole account and all your services",
1155+
"services/all": "Manage your services lifecycle and billing"
1156+
}
1157+
}
1158+
}
1159+
}
11251160
}
11261161
},
11271162
"paths": {
@@ -1391,6 +1426,7 @@
13911426
{
13921427
"createdAt": "2020-12-31T07:39:19Z",
13931428
"currentState": {
1429+
"iamAuthEnabled": false,
13941430
"ipRestrictions": [
13951431
{
13961432
"cidrBlock": "198.51.100.1/32",
@@ -1416,6 +1452,7 @@
14161452
"id": "d6b6579e-8d60-4487-bf08-8b4ddf98f7d3",
14171453
"resourceStatus": "READY",
14181454
"targetSpec": {
1455+
"iamAuthEnabled": false,
14191456
"ipRestrictions": [
14201457
{
14211458
"cidrBlock": "198.51.100.1/32",
@@ -1431,6 +1468,7 @@
14311468
{
14321469
"createdAt": "2020-12-31T13:37:46Z",
14331470
"currentState": {
1471+
"iamAuthEnabled": false,
14341472
"ipRestrictions": [],
14351473
"name": "rancher2",
14361474
"plan": "STANDARD",
@@ -1448,6 +1486,7 @@
14481486
"id": "222ce105-a3f7-44c4-a7d3-dbb5983c045d",
14491487
"resourceStatus": "UPDATING",
14501488
"targetSpec": {
1489+
"iamAuthEnabled": false,
14511490
"ipRestrictions": [],
14521491
"name": "rancher2",
14531492
"plan": "STANDARD",
@@ -1539,6 +1578,7 @@
15391578
"Create a STANDARD managed Rancher on the latest version": {
15401579
"value": {
15411580
"targetSpec": {
1581+
"iamAuthEnabled": false,
15421582
"name": "my_rancher",
15431583
"plan": "STANDARD"
15441584
}
@@ -1548,6 +1588,7 @@
15481588
"Create an OVHCLOUD_EDITION managed Rancher on a specific version": {
15491589
"value": {
15501590
"targetSpec": {
1591+
"iamAuthEnabled": false,
15511592
"name": "my_rancher",
15521593
"plan": "OVHCLOUD_EDITION",
15531594
"version": "1.0.0"
@@ -1573,6 +1614,7 @@
15731614
"createdAt": "2020-12-31T07:39:19Z",
15741615
"currentState": {
15751616
"bootstrapPassword": "jL%IctBOu)-$D@wa",
1617+
"iamAuthEnabled": false,
15761618
"ipRestrictions": [],
15771619
"name": "my_rancher",
15781620
"plan": "OVHCLOUD_EDITION",
@@ -1589,6 +1631,7 @@
15891631
"id": "d6b6579e-8d60-4487-bf08-8b4ddf98f7d3",
15901632
"resourceStatus": "CREATING",
15911633
"targetSpec": {
1634+
"iamAuthEnabled": false,
15921635
"ipRestrictions": [],
15931636
"name": "my_rancher",
15941637
"plan": "OVHCLOUD_EDITION",
@@ -1691,6 +1734,7 @@
16911734
"value": {
16921735
"createdAt": "2020-12-31T07:39:19Z",
16931736
"currentState": {
1737+
"iamAuthEnabled": false,
16941738
"ipRestrictions": [],
16951739
"name": "my_rancher",
16961740
"networking": {
@@ -1717,6 +1761,7 @@
17171761
"id": "d6b6579e-8d60-4487-bf08-8b4ddf98f7d3",
17181762
"resourceStatus": "DELETING",
17191763
"targetSpec": {
1764+
"iamAuthEnabled": false,
17201765
"ipRestrictions": [],
17211766
"name": "my_rancher",
17221767
"plan": "OVHCLOUD_EDITION",
@@ -1817,6 +1862,7 @@
18171862
"value": {
18181863
"createdAt": "2020-12-31T07:39:19Z",
18191864
"currentState": {
1865+
"iamAuthEnabled": false,
18201866
"ipRestrictions": [
18211867
{
18221868
"cidrBlock": "198.51.100.1/32",
@@ -1841,6 +1887,7 @@
18411887
"id": "d6b6579e-8d60-4487-bf08-8b4ddf98f7d3",
18421888
"resourceStatus": "READY",
18431889
"targetSpec": {
1890+
"iamAuthEnabled": false,
18441891
"ipRestrictions": [
18451892
{
18461893
"cidrBlock": "198.51.100.1/32",
@@ -1945,6 +1992,7 @@
19451992
"Update rancher to plan STANDARD": {
19461993
"value": {
19471994
"targetSpec": {
1995+
"iamAuthEnabled": false,
19481996
"name": "my_rancher",
19491997
"plan": "STANDARD",
19501998
"version": "1.0.0"
@@ -1969,6 +2017,7 @@
19692017
"value": {
19702018
"createdAt": "2020-12-31T07:39:19Z",
19712019
"currentState": {
2020+
"iamAuthEnabled": false,
19722021
"ipRestrictions": [],
19732022
"name": "my_rancher",
19742023
"networking": {
@@ -1995,6 +2044,7 @@
19952044
"id": "d6b6579e-8d60-4487-bf08-8b4ddf98f7d3",
19962045
"resourceStatus": "UPDATING",
19972046
"targetSpec": {
2047+
"iamAuthEnabled": false,
19982048
"ipRestrictions": [],
19992049
"name": "my_rancher",
20002050
"plan": "OVHCLOUD_EDITION",

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+
}

0 commit comments

Comments
 (0)