Merge pull request #644 from foostan/add_validation_of_deregistration
Validate ServiceID/CheckID when deregistering.
This commit is contained in:
commit
6d27dc3548
|
@ -637,6 +637,11 @@ func (a *Agent) RemoveService(serviceID string, persist bool) error {
|
||||||
consul.ConsulServiceID)
|
consul.ConsulServiceID)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Validate ServiceID
|
||||||
|
if serviceID == "" {
|
||||||
|
return fmt.Errorf("ServiceID missing")
|
||||||
|
}
|
||||||
|
|
||||||
// Remove service immeidately
|
// Remove service immeidately
|
||||||
a.state.RemoveService(serviceID)
|
a.state.RemoveService(serviceID)
|
||||||
|
|
||||||
|
@ -757,6 +762,11 @@ func (a *Agent) AddCheck(check *structs.HealthCheck, chkType *CheckType, persist
|
||||||
// RemoveCheck is used to remove a health check.
|
// RemoveCheck is used to remove a health check.
|
||||||
// The agent will make a best effort to ensure it is deregistered
|
// The agent will make a best effort to ensure it is deregistered
|
||||||
func (a *Agent) RemoveCheck(checkID string, persist bool) error {
|
func (a *Agent) RemoveCheck(checkID string, persist bool) error {
|
||||||
|
// Validate CheckID
|
||||||
|
if checkID == "" {
|
||||||
|
return fmt.Errorf("CheckID missing")
|
||||||
|
}
|
||||||
|
|
||||||
// Add to the local state for anti-entropy
|
// Add to the local state for anti-entropy
|
||||||
a.state.RemoveCheck(checkID)
|
a.state.RemoveCheck(checkID)
|
||||||
|
|
||||||
|
|
|
@ -247,6 +247,11 @@ func TestAgent_RemoveService(t *testing.T) {
|
||||||
t.Fatalf("should have errored")
|
t.Fatalf("should have errored")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Remove without an ID
|
||||||
|
if err := agent.RemoveService("", false); err == nil {
|
||||||
|
t.Fatalf("should have errored")
|
||||||
|
}
|
||||||
|
|
||||||
// Removing a service with a single check works
|
// Removing a service with a single check works
|
||||||
{
|
{
|
||||||
srv := &structs.NodeService{
|
srv := &structs.NodeService{
|
||||||
|
@ -406,6 +411,11 @@ func TestAgent_RemoveCheck(t *testing.T) {
|
||||||
t.Fatalf("err: %v", err)
|
t.Fatalf("err: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Remove without an ID
|
||||||
|
if err := agent.RemoveCheck("", false); err == nil {
|
||||||
|
t.Fatalf("should have errored")
|
||||||
|
}
|
||||||
|
|
||||||
health := &structs.HealthCheck{
|
health := &structs.HealthCheck{
|
||||||
Node: "foo",
|
Node: "foo",
|
||||||
CheckID: "mem",
|
CheckID: "mem",
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package agent
|
package agent
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -408,6 +409,10 @@ func (l *localState) syncChanges() error {
|
||||||
|
|
||||||
// deleteService is used to delete a service from the server
|
// deleteService is used to delete a service from the server
|
||||||
func (l *localState) deleteService(id string) error {
|
func (l *localState) deleteService(id string) error {
|
||||||
|
if id == "" {
|
||||||
|
return fmt.Errorf("ServiceID missing")
|
||||||
|
}
|
||||||
|
|
||||||
req := structs.DeregisterRequest{
|
req := structs.DeregisterRequest{
|
||||||
Datacenter: l.config.Datacenter,
|
Datacenter: l.config.Datacenter,
|
||||||
Node: l.config.NodeName,
|
Node: l.config.NodeName,
|
||||||
|
@ -425,6 +430,10 @@ func (l *localState) deleteService(id string) error {
|
||||||
|
|
||||||
// deleteCheck is used to delete a service from the server
|
// deleteCheck is used to delete a service from the server
|
||||||
func (l *localState) deleteCheck(id string) error {
|
func (l *localState) deleteCheck(id string) error {
|
||||||
|
if id == "" {
|
||||||
|
return fmt.Errorf("CheckID missing")
|
||||||
|
}
|
||||||
|
|
||||||
req := structs.DeregisterRequest{
|
req := structs.DeregisterRequest{
|
||||||
Datacenter: l.config.Datacenter,
|
Datacenter: l.config.Datacenter,
|
||||||
Node: l.config.NodeName,
|
Node: l.config.NodeName,
|
||||||
|
|
Loading…
Reference in New Issue