cscli machines delete: return an error if machines doesn't exist (#1689)
* cscli machines delete: return an error if machines doesn't exist
This commit is contained in:
parent
16b1ab06a9
commit
1002affc16
|
@ -164,7 +164,7 @@ cscli bouncers add MyBouncerName -k %s`, generatePassword(32)),
|
||||||
for _, bouncerID := range args {
|
for _, bouncerID := range args {
|
||||||
err := dbClient.DeleteBouncer(bouncerID)
|
err := dbClient.DeleteBouncer(bouncerID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("unable to delete bouncer: %s", err)
|
log.Fatalf("unable to delete bouncer '%s': %s", bouncerID, err)
|
||||||
}
|
}
|
||||||
log.Infof("bouncer '%s' deleted successfully", bouncerID)
|
log.Infof("bouncer '%s' deleted successfully", bouncerID)
|
||||||
}
|
}
|
||||||
|
|
|
@ -322,7 +322,7 @@ cscli machines add MyTestMachine --password MyPassword
|
||||||
for _, machineID := range args {
|
for _, machineID := range args {
|
||||||
err := dbClient.DeleteWatcher(machineID)
|
err := dbClient.DeleteWatcher(machineID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("unable to delete machine: %s", err)
|
log.Errorf("unable to delete machine '%s': %s", machineID, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Infof("machine '%s' deleted successfully", machineID)
|
log.Infof("machine '%s' deleted successfully", machineID)
|
||||||
|
|
|
@ -47,19 +47,24 @@ func (c *Client) CreateBouncer(name string, ipAddr string, apiKey string, authTy
|
||||||
if ent.IsConstraintError(err) {
|
if ent.IsConstraintError(err) {
|
||||||
return nil, fmt.Errorf("bouncer %s already exists", name)
|
return nil, fmt.Errorf("bouncer %s already exists", name)
|
||||||
}
|
}
|
||||||
return nil, fmt.Errorf("unable to save api key in database: %s", err)
|
return nil, fmt.Errorf("unable to create bouncer: %s", err)
|
||||||
}
|
}
|
||||||
return bouncer, nil
|
return bouncer, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) DeleteBouncer(name string) error {
|
func (c *Client) DeleteBouncer(name string) error {
|
||||||
_, err := c.Ent.Bouncer.
|
nbDeleted, err := c.Ent.Bouncer.
|
||||||
Delete().
|
Delete().
|
||||||
Where(bouncer.NameEQ(name)).
|
Where(bouncer.NameEQ(name)).
|
||||||
Exec(c.CTX)
|
Exec(c.CTX)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to save api key in database: %s", err)
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if nbDeleted == 0 {
|
||||||
|
return fmt.Errorf("bouncer doesn't exist")
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -68,7 +73,7 @@ func (c *Client) UpdateBouncerLastPull(lastPull time.Time, ID int) error {
|
||||||
SetLastPull(lastPull).
|
SetLastPull(lastPull).
|
||||||
Save(c.CTX)
|
Save(c.CTX)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to update machine in database: %s", err)
|
return fmt.Errorf("unable to update machine last pull in database: %s", err)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -105,13 +105,18 @@ func (c *Client) QueryPendingMachine() ([]*ent.Machine, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) DeleteWatcher(name string) error {
|
func (c *Client) DeleteWatcher(name string) error {
|
||||||
_, err := c.Ent.Machine.
|
nbDeleted, err := c.Ent.Machine.
|
||||||
Delete().
|
Delete().
|
||||||
Where(machine.MachineIdEQ(name)).
|
Where(machine.MachineIdEQ(name)).
|
||||||
Exec(c.CTX)
|
Exec(c.CTX)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to save api key in database: %s", err)
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if nbDeleted == 0 {
|
||||||
|
return fmt.Errorf("machine doesn't exist")
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -147,7 +152,7 @@ func (c *Client) UpdateMachineIP(ipAddr string, ID int) error {
|
||||||
SetIpAddress(ipAddr).
|
SetIpAddress(ipAddr).
|
||||||
Save(c.CTX)
|
Save(c.CTX)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to update machine in database: %s", err)
|
return fmt.Errorf("unable to update machine IP in database: %s", err)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -157,7 +162,7 @@ func (c *Client) UpdateMachineVersion(ipAddr string, ID int) error {
|
||||||
SetVersion(ipAddr).
|
SetVersion(ipAddr).
|
||||||
Save(c.CTX)
|
Save(c.CTX)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to update machine in database: %s", err)
|
return fmt.Errorf("unable to update machine version in database: %s", err)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,6 +53,6 @@ teardown() {
|
||||||
@test "delete the bouncer multiple times, even if it does not exist" {
|
@test "delete the bouncer multiple times, even if it does not exist" {
|
||||||
run -0 cscli bouncers add ciTestBouncer
|
run -0 cscli bouncers add ciTestBouncer
|
||||||
run -0 cscli bouncers delete ciTestBouncer
|
run -0 cscli bouncers delete ciTestBouncer
|
||||||
run -0 cscli bouncers delete ciTestBouncer
|
run -1 cscli bouncers delete ciTestBouncer
|
||||||
run -0 cscli bouncers delete foobarbaz
|
run -1 cscli bouncers delete foobarbaz
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue