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:
AlteredCoder 2022-07-28 17:32:12 +02:00 committed by GitHub
parent 16b1ab06a9
commit 1002affc16
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 22 additions and 12 deletions

View file

@ -164,7 +164,7 @@ cscli bouncers add MyBouncerName -k %s`, generatePassword(32)),
for _, bouncerID := range args {
err := dbClient.DeleteBouncer(bouncerID)
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)
}

View file

@ -322,7 +322,7 @@ cscli machines add MyTestMachine --password MyPassword
for _, machineID := range args {
err := dbClient.DeleteWatcher(machineID)
if err != nil {
log.Errorf("unable to delete machine: %s", err)
log.Errorf("unable to delete machine '%s': %s", machineID, err)
return
}
log.Infof("machine '%s' deleted successfully", machineID)

View file

@ -47,19 +47,24 @@ func (c *Client) CreateBouncer(name string, ipAddr string, apiKey string, authTy
if ent.IsConstraintError(err) {
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
}
func (c *Client) DeleteBouncer(name string) error {
_, err := c.Ent.Bouncer.
nbDeleted, err := c.Ent.Bouncer.
Delete().
Where(bouncer.NameEQ(name)).
Exec(c.CTX)
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
}
@ -68,7 +73,7 @@ func (c *Client) UpdateBouncerLastPull(lastPull time.Time, ID int) error {
SetLastPull(lastPull).
Save(c.CTX)
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
}

View file

@ -105,13 +105,18 @@ func (c *Client) QueryPendingMachine() ([]*ent.Machine, error) {
}
func (c *Client) DeleteWatcher(name string) error {
_, err := c.Ent.Machine.
nbDeleted, err := c.Ent.Machine.
Delete().
Where(machine.MachineIdEQ(name)).
Exec(c.CTX)
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
}
@ -147,7 +152,7 @@ func (c *Client) UpdateMachineIP(ipAddr string, ID int) error {
SetIpAddress(ipAddr).
Save(c.CTX)
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
}
@ -157,7 +162,7 @@ func (c *Client) UpdateMachineVersion(ipAddr string, ID int) error {
SetVersion(ipAddr).
Save(c.CTX)
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
}

View file

@ -53,6 +53,6 @@ teardown() {
@test "delete the bouncer multiple times, even if it does not exist" {
run -0 cscli bouncers add ciTestBouncer
run -0 cscli bouncers delete ciTestBouncer
run -0 cscli bouncers delete ciTestBouncer
run -0 cscli bouncers delete foobarbaz
run -1 cscli bouncers delete ciTestBouncer
run -1 cscli bouncers delete foobarbaz
}