cscli: add force enroll feature (#1430)
* cscli: add force enroll feature
This commit is contained in:
parent
ef20183ecb
commit
615895da9d
3 changed files with 13 additions and 6 deletions
|
@ -53,6 +53,7 @@ func NewConsoleCmd() *cobra.Command {
|
|||
}
|
||||
|
||||
name := ""
|
||||
overwrite := false
|
||||
tags := []string{}
|
||||
|
||||
cmdEnroll := &cobra.Command{
|
||||
|
@ -102,10 +103,14 @@ After running this command your will need to validate the enrollment in the weba
|
|||
URL: apiURL,
|
||||
VersionPrefix: "v2",
|
||||
})
|
||||
_, err = c.Auth.EnrollWatcher(context.Background(), args[0], name, tags)
|
||||
resp, err := c.Auth.EnrollWatcher(context.Background(), args[0], name, tags, overwrite)
|
||||
if err != nil {
|
||||
log.Fatalf("Could not enroll instance: %s", err)
|
||||
}
|
||||
if resp.Response.StatusCode == 200 && !overwrite {
|
||||
log.Warningf("Instance already enrolled. You can use '--overwrite' to force enroll")
|
||||
return
|
||||
}
|
||||
|
||||
SetConsoleOpts(csconfig.CONSOLE_CONFIGS, true)
|
||||
if err := csConfig.API.Server.DumpConsoleConfig(); err != nil {
|
||||
|
@ -117,6 +122,7 @@ After running this command your will need to validate the enrollment in the weba
|
|||
},
|
||||
}
|
||||
cmdEnroll.Flags().StringVarP(&name, "name", "n", "", "Name to display in the console")
|
||||
cmdEnroll.Flags().BoolVarP(&overwrite, "overwrite", "", false, "Force enroll the instance")
|
||||
cmdEnroll.Flags().StringSliceVarP(&tags, "tags", "t", tags, "Tags to display in the console")
|
||||
cmdConsole.AddCommand(cmdEnroll)
|
||||
|
||||
|
|
|
@ -16,6 +16,7 @@ type enrollRequest struct {
|
|||
EnrollKey string `json:"attachment_key"`
|
||||
Name string `json:"name"`
|
||||
Tags []string `json:"tags"`
|
||||
Overwrite bool `json:"overwrite"`
|
||||
}
|
||||
|
||||
func (s *AuthService) UnregisterWatcher(ctx context.Context) (*Response, error) {
|
||||
|
@ -63,9 +64,9 @@ func (s *AuthService) AuthenticateWatcher(ctx context.Context, auth models.Watch
|
|||
return resp, nil
|
||||
}
|
||||
|
||||
func (s *AuthService) EnrollWatcher(ctx context.Context, enrollKey string, name string, tags []string) (*Response, error) {
|
||||
func (s *AuthService) EnrollWatcher(ctx context.Context, enrollKey string, name string, tags []string, overwrite bool) (*Response, error) {
|
||||
u := fmt.Sprintf("%s/watchers/enroll", s.client.URLPrefix)
|
||||
req, err := s.client.NewRequest("POST", u, &enrollRequest{EnrollKey: enrollKey, Name: name, Tags: tags})
|
||||
req, err := s.client.NewRequest("POST", u, &enrollRequest{EnrollKey: enrollKey, Name: name, Tags: tags, Overwrite: overwrite})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -192,7 +192,7 @@ func TestWatcherEnroll(t *testing.T) {
|
|||
_, _ = buf.ReadFrom(r.Body)
|
||||
newStr := buf.String()
|
||||
log.Debugf("body -> %s", newStr)
|
||||
if newStr == `{"attachment_key":"goodkey","name":"","tags":[]}
|
||||
if newStr == `{"attachment_key":"goodkey","name":"","tags":[],"overwrite":false}
|
||||
` {
|
||||
log.Print("good key")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
@ -228,11 +228,11 @@ func TestWatcherEnroll(t *testing.T) {
|
|||
log.Fatalf("new api client: %s", err.Error())
|
||||
}
|
||||
|
||||
_, err = client.Auth.EnrollWatcher(context.Background(), "goodkey", "", []string{})
|
||||
_, err = client.Auth.EnrollWatcher(context.Background(), "goodkey", "", []string{}, false)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpect enroll err: %s", err)
|
||||
}
|
||||
|
||||
_, err = client.Auth.EnrollWatcher(context.Background(), "badkey", "", []string{})
|
||||
_, err = client.Auth.EnrollWatcher(context.Background(), "badkey", "", []string{}, false)
|
||||
assert.Contains(t, err.Error(), "the attachment key provided is not valid")
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue