Skip to content

Commit 2034229

Browse files
authored
feat(cli): add verbosity to cdsctl admin database commands (#6246)
Signed-off-by: francois samin <francois.samin@corp.ovh.com>
1 parent 0ddece0 commit 2034229

File tree

4 files changed

+95
-28
lines changed

4 files changed

+95
-28
lines changed

cli/cdsctl/admin_database.go

+40-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package main
22

33
import (
44
"fmt"
5+
"sort"
6+
"strconv"
57

68
"github.com/spf13/cobra"
79

@@ -129,16 +131,33 @@ var adminDatabaseSignatureRoll = cli.Command{
129131
Name: "entity",
130132
AllowEmpty: true,
131133
},
134+
Flags: []cli.Flag{
135+
{
136+
Name: "index",
137+
Usage: "Resume from a specific index (only available for one entity)",
138+
IsValid: func(s string) bool {
139+
if s == "" {
140+
return true
141+
}
142+
_, err := strconv.Atoi(s)
143+
return err == nil
144+
},
145+
},
146+
},
132147
}
133148

134149
func adminDatabaseSignatureRollFunc(args cli.Values) error {
135150
entities := args.GetStringSlice("entity")
136151
if len(entities) == 0 {
137152
return client.AdminDatabaseSignaturesRollAllEntities(args.GetString(argServiceName))
138153
}
139-
154+
idx, _ := args.GetInt64("index")
155+
if len(entities) > 1 && idx > 0 {
156+
return fmt.Errorf("--index can only be used with one entity")
157+
}
158+
sort.Strings(entities)
140159
for _, e := range entities {
141-
if err := client.AdminDatabaseSignaturesRollEntity(args.GetString(argServiceName), e); err != nil {
160+
if err := client.AdminDatabaseSignaturesRollEntity(args.GetString(argServiceName), e, &idx); err != nil {
142161
return err
143162
}
144163
}
@@ -182,16 +201,33 @@ var adminDatabaseEncryptionRoll = cli.Command{
182201
Name: "entity",
183202
AllowEmpty: true,
184203
},
204+
Flags: []cli.Flag{
205+
{
206+
Name: "index",
207+
Usage: "Resume from a specific index (only available for one entity)",
208+
IsValid: func(s string) bool {
209+
if s == "" {
210+
return true
211+
}
212+
_, err := strconv.Atoi(s)
213+
return err == nil
214+
},
215+
},
216+
},
185217
}
186218

187219
func adminDatabaseEncryptionRollFunc(args cli.Values) error {
188220
entities := args.GetStringSlice("entity")
189221
if len(entities) == 0 {
190222
return client.AdminDatabaseRollAllEncryptedEntities(args.GetString(argServiceName))
191223
}
192-
224+
idx, _ := args.GetInt64("index")
225+
if len(entities) > 1 && idx > 0 {
226+
return fmt.Errorf("--index can only be used with one entity")
227+
}
228+
sort.Strings(entities)
193229
for _, e := range entities {
194-
if err := client.AdminDatabaseRollEncryptedEntity(args.GetString(argServiceName), e); err != nil {
230+
if err := client.AdminDatabaseRollEncryptedEntity(args.GetString(argServiceName), e, &idx); err != nil {
195231
return err
196232
}
197233
}

sdk/cdsclient/client_admin.go

+37-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import (
88
"fmt"
99
"net/http"
1010
"net/url"
11+
"time"
1112

13+
"github.com/ovh/cds/cli"
1214
"github.com/ovh/cds/sdk"
1315
)
1416

@@ -234,7 +236,7 @@ func (c *client) AdminDatabaseSignaturesResume(service string) (sdk.CanonicalFor
234236
return res, err
235237
}
236238

237-
func (c *client) AdminDatabaseSignaturesRollEntity(service string, e string) error {
239+
func (c *client) AdminDatabaseSignaturesRollEntity(service string, e string, idx *int64) error {
238240
resume, err := c.AdminDatabaseSignaturesResume(service)
239241
if err != nil {
240242
return err
@@ -245,19 +247,34 @@ func (c *client) AdminDatabaseSignaturesRollEntity(service string, e string) err
245247
}
246248

247249
for _, s := range resume[e] {
250+
ctx, cancel := context.WithCancel(context.Background())
251+
defer cancel()
252+
253+
var display = new(cli.Display)
254+
display.Printf("Rolling %v...", e)
255+
display.Do(ctx)
256+
248257
url := fmt.Sprintf("/admin/database/signature/%s/%s", e, s.Signer)
249258
var pks []string
250259
var f = c.switchServiceCallFunc(service, http.MethodGet, url, nil, &pks)
251260
if _, err := f(); err != nil {
252261
return err
253262
}
254263

255-
for _, pk := range pks {
264+
for i, pk := range pks {
265+
if idx != nil && *idx > int64(i) {
266+
continue
267+
}
268+
display.Printf("Rolling %v (%d/%d)...", e, i+1, len(pks))
256269
url := fmt.Sprintf("/admin/database/signature/%s/roll/%s", e, pk)
257270
var f = c.switchServiceCallFunc(service, http.MethodPost, url, nil, nil)
258271
if _, err := f(); err != nil {
259272
return err
260273
}
274+
if i == len(pks)-1 {
275+
display.Printf("Rolling %v (%d/%d) - DONE\n", e, i+1, len(pks))
276+
time.Sleep(time.Second)
277+
}
261278
}
262279
}
263280
return nil
@@ -270,7 +287,7 @@ func (c *client) AdminDatabaseSignaturesRollAllEntities(service string) error {
270287
}
271288

272289
for e := range resume {
273-
if err := c.AdminDatabaseSignaturesRollEntity(service, e); err != nil {
290+
if err := c.AdminDatabaseSignaturesRollEntity(service, e, nil); err != nil {
274291
return err
275292
}
276293
}
@@ -284,7 +301,7 @@ func (c *client) AdminDatabaseListEncryptedEntities(service string) ([]string, e
284301
return res, err
285302
}
286303

287-
func (c *client) AdminDatabaseRollEncryptedEntity(service string, e string) error {
304+
func (c *client) AdminDatabaseRollEncryptedEntity(service string, e string, idx *int64) error {
288305
url := fmt.Sprintf("/admin/database/encryption/%s", e)
289306
var pks []string
290307

@@ -293,12 +310,26 @@ func (c *client) AdminDatabaseRollEncryptedEntity(service string, e string) erro
293310
return err
294311
}
295312

296-
for _, pk := range pks {
313+
for i, pk := range pks {
314+
ctx, cancel := context.WithCancel(context.Background())
315+
defer cancel()
316+
317+
var display = new(cli.Display)
318+
display.Printf("Rolling %v...", e)
319+
display.Do(ctx)
320+
if idx != nil && *idx > int64(i) {
321+
continue
322+
}
323+
display.Printf("Rolling %v (%d/%d)...", e, i+1, len(pks))
297324
url := fmt.Sprintf("/admin/database/encryption/%s/roll/%s", e, pk)
298325
var f = c.switchServiceCallFunc(service, http.MethodPost, url, nil, nil)
299326
if _, err := f(); err != nil {
300327
return err
301328
}
329+
if i == len(pks)-1 {
330+
display.Printf("Rolling %v (%d/%d) - DONE\n", e, i+1, len(pks))
331+
time.Sleep(time.Second)
332+
}
302333
}
303334

304335
return nil
@@ -310,7 +341,7 @@ func (c *client) AdminDatabaseRollAllEncryptedEntities(service string) error {
310341
return err
311342
}
312343
for _, e := range entities {
313-
if err := c.AdminDatabaseRollEncryptedEntity(service, e); err != nil {
344+
if err := c.AdminDatabaseRollEncryptedEntity(service, e, nil); err != nil {
314345
return err
315346
}
316347
}

sdk/cdsclient/interface.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ type Admin interface {
4141
AdminDatabaseMigrationUnlock(service string, id string) error
4242
AdminDatabaseMigrationsList(service string) ([]sdk.DatabaseMigrationStatus, error)
4343
AdminDatabaseSignaturesResume(service string) (sdk.CanonicalFormUsageResume, error)
44-
AdminDatabaseSignaturesRollEntity(service string, e string) error
44+
AdminDatabaseSignaturesRollEntity(service string, e string, idx *int64) error
4545
AdminDatabaseSignaturesRollAllEntities(service string) error
4646
AdminDatabaseListEncryptedEntities(service string) ([]string, error)
47-
AdminDatabaseRollEncryptedEntity(service string, e string) error
47+
AdminDatabaseRollEncryptedEntity(service string, e string, idx *int64) error
4848
AdminDatabaseRollAllEncryptedEntities(service string) error
4949
AdminCDSMigrationList() ([]sdk.Migration, error)
5050
AdminCDSMigrationCancel(id int64) error

sdk/cdsclient/mock_cdsclient/interface_mock.go

+16-16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)