Skip to content

Commit

Permalink
Check if backend exists after failing to add a new one (#31)
Browse files Browse the repository at this point in the history
* Check if backend exists after failing to add a new one
  • Loading branch information
AlfredBroda authored Sep 18, 2020
1 parent f01b78a commit e92795c
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 17 deletions.
2 changes: 1 addition & 1 deletion action/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func register(client vaas.Client, cfg CommonConfig, weight int, dcName string, t
ResourceURI: "",
}
log.Infof("Adding address %q port %d to director %q (%d)", cfg.Address, cfg.Port, director.Name, director.ID)
backendID, err := client.AddBackend(&backend)
backendID, err := client.AddBackend(&backend, director)

if err == nil {
log.Infof("Received VaaS backend id: %s", backendID)
Expand Down
30 changes: 22 additions & 8 deletions vaas/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,10 @@ type Task struct {
type Client interface {
FindDirector(string) (*Director, error)
FindDirectorID(string) (int, error)
AddBackend(*Backend) (string, error)
AddBackend(*Backend, *Director) (string, error)
DeleteBackend(int) error
GetDC(string) (*DC, error)
FindBackend(director *Director, address string, port int) (*Backend, error)
FindBackendID(director string, address string, port int) (int, error)
}

Expand Down Expand Up @@ -142,15 +143,20 @@ func (c *defaultClient) FindDirectorID(name string) (int, error) {
}

// AddBackend adds backend in VaaS director.
func (c *defaultClient) AddBackend(backend *Backend) (string, error) {
func (c *defaultClient) AddBackend(backend *Backend, director *Director) (string, error) {
request, err := c.newRequest("POST", c.host+apiBackendPath, backend)
if err != nil {
return "", err
}

response, err := c.doRequest(request, backend)
if err != nil {
return "", err
backend, newErr := c.FindBackend(director, backend.Address, backend.Port)
if newErr != nil {
log.Errorf("failed finding backend: %s", err)
return "", err
}
return backend.ResourceURI, nil
}

return response.Header.Get("Location"), nil
Expand Down Expand Up @@ -200,29 +206,37 @@ func (c *defaultClient) FindBackendID(director string, address string, port int)
return 0, fmt.Errorf("cannot determine director ID: %s", err)
}

backend, err := c.FindBackend(directorFound, address, port)
if err != nil {
return 0, errors.New("backend not found")
}
return *backend.ID, nil
}

func (c *defaultClient) FindBackend(director *Director, address string, port int) (*Backend, error) {
request, err := c.newRequest("GET", c.host+apiBackendPath, nil)
if err != nil {
return 0, fmt.Errorf("could not create backend list request: %s", err)
return nil, fmt.Errorf("could not create backend list request: %s", err)
}

query := request.URL.Query()
query.Add("address", address)
query.Add("director", fmt.Sprintf("%d", directorFound.ID))
query.Add("director", fmt.Sprintf("%d", director.ID))
query.Add("port", fmt.Sprintf("%d", port))
request.URL.RawQuery = query.Encode()

var backendList BackendList
if _, err := c.doRequest(request, &backendList); err != nil {
return 0, fmt.Errorf("backend list fetch failed: %s", err)
return nil, fmt.Errorf("backend list fetch failed: %s", err)
}

for _, backend := range backendList.Objects {
log.Debugf("Backend found: %+v\n", backend)
if backend.Address == address && backend.Port == port {
return *backend.ID, nil
return &backend, nil
}
}
return 0, errors.New("backend not found")
return nil, errors.New("backend not found")
}

func (c *defaultClient) newRequest(method, url string, body interface{}) (*http.Request, error) {
Expand Down
63 changes: 55 additions & 8 deletions vaas/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestNoFailureWhenFindingDirectorByName(t *testing.T) {
assert.Equal(t, applicationJSON, r.Header.Get(acceptHeader))
if r.URL.Path == "/api/v0.1/director/" && r.URL.RawQuery == "api_key=api-key&name=director&username=username" && r.Method == "GET" {
var dList = DirectorList{
Objects: []Director{{ID: expectedID, Name: "director"}},
Objects: []Director{*createDirector(expectedID)},
}

var data, _ = json.Marshal(dList)
Expand Down Expand Up @@ -68,11 +68,42 @@ func TestBackendRegistrationFailureAfterVaasServerError(t *testing.T) {

client := NewClient(ts.URL, "username", "api-key")

_, err := client.AddBackend(&Backend{})
_, err := client.AddBackend(createBackend(), createDirector(123))

assert.Error(t, err)
}

func TestBackendRegistrationWhenBackendExists(t *testing.T) {
backendURI := "backendURI"
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, applicationJSON, r.Header.Get(contentTypeHeader))
assert.Equal(t, applicationJSON, r.Header.Get(acceptHeader))
if r.Method == http.MethodPost {
http.Error(w, "Internal server error", http.StatusInternalServerError)
}
var bList = BackendList{
Objects: []Backend{*createBackendWithUri(backendURI)},
}

var data, _ = json.Marshal(bList)

var _, err = w.Write(data)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
}

w.WriteHeader(http.StatusOK)
}))
defer ts.Close()

client := NewClient(ts.URL, "username", "api-key")

backendResp, err := client.AddBackend(createBackend(), createDirector(123))

assert.NoError(t, err)
assert.Equal(t, backendURI, backendResp)
}

func TestBackendRemovalFailureAfterVaasServerError(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, applicationJSON, r.Header.Get(contentTypeHeader))
Expand Down Expand Up @@ -133,12 +164,7 @@ func TestIfBackendLocationIsSetFromVaasResponseHeader(t *testing.T) {

client := NewClient(ts.URL, "username", "api-key")

location, err := client.AddBackend(&Backend{
Address: "127.0.0.1",
DirectorURL: "director",
DC: DC{1, "DC1", "api/dc/1", "dc1"},
Port: 8080,
})
location, err := client.AddBackend(createBackend(), createDirector(123))

require.NoError(t, err)
assert.Equal(t, "location", location)
Expand Down Expand Up @@ -182,6 +208,27 @@ func TestNoFailureWhenRemovingNonExistingBackendInVaas(t *testing.T) {
assert.NoError(t, err)
}

func createBackend() *Backend {
return createBackendWithUri("uri")
}

func createBackendWithUri(backendURI string) *Backend {
return &Backend{
Address: "127.0.0.1",
DirectorURL: "directorURL",
ResourceURI: backendURI,
DC: DC{1, "DC1", "api/dc/1", "dc1"},
Port: 8080,
}
}

func createDirector(ID int) *Director {
return &Director{
ID: ID,
Name: "director",
}
}

var mockAddBackendResponse = []byte(`{
"address":"192.168.199.34",
"between_bytes_timeout":"1",
Expand Down

0 comments on commit e92795c

Please sign in to comment.