-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Dqlite remove endpoint (KU-1719) (#57)
- Loading branch information
1 parent
664ceaa
commit 1ce0520
Showing
14 changed files
with
361 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package v2 | ||
|
||
const ( | ||
// CAPIAuthTokenHeader is the header used to pass the CAPI auth token. | ||
CAPIAuthTokenHeader = "capi-auth-token" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package v2 | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
|
||
snaputil "github.com/canonical/microk8s-cluster-agent/pkg/snap/util" | ||
) | ||
|
||
// RemoveFromDqliteRequest represents a request to remove a node from the dqlite cluster. | ||
type RemoveFromDqliteRequest struct { | ||
// RemoveEndpoint is the endpoint of the node to remove from the dqlite cluster. | ||
RemoveEndpoint string `json:"remove_endpoint"` | ||
} | ||
|
||
// RemoveFromDqlite implements the "POST /v2/dqlite/remove" endpoint and removes a node from the dqlite cluster. | ||
func (a *API) RemoveFromDqlite(ctx context.Context, req RemoveFromDqliteRequest, token string) (int, error) { | ||
isValid, err := a.Snap.IsCAPIAuthTokenValid(token) | ||
if err != nil { | ||
return http.StatusInternalServerError, fmt.Errorf("failed to validate CAPI auth token: %w", err) | ||
} | ||
|
||
if !isValid { | ||
return http.StatusUnauthorized, fmt.Errorf("invalid CAPI auth token %q", token) | ||
} | ||
|
||
if err := snaputil.RemoveNodeFromDqlite(ctx, a.Snap, req.RemoveEndpoint); err != nil { | ||
return http.StatusInternalServerError, fmt.Errorf("failed to remove node from dqlite: %w", err) | ||
} | ||
|
||
return http.StatusOK, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package v2_test | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"net/http" | ||
"testing" | ||
|
||
. "github.com/onsi/gomega" | ||
|
||
v2 "github.com/canonical/microk8s-cluster-agent/pkg/api/v2" | ||
"github.com/canonical/microk8s-cluster-agent/pkg/snap/mock" | ||
) | ||
|
||
func TestRemove(t *testing.T) { | ||
t.Run("RemoveFails", func(t *testing.T) { | ||
cmdErr := errors.New("failed to run command") | ||
apiv2 := &v2.API{ | ||
Snap: &mock.Snap{ | ||
RunCommandErr: cmdErr, | ||
CAPIAuthTokenValid: true, | ||
}, | ||
} | ||
|
||
rc, err := apiv2.RemoveFromDqlite(context.Background(), v2.RemoveFromDqliteRequest{RemoveEndpoint: "1.1.1.1:1234"}, "token") | ||
|
||
g := NewWithT(t) | ||
g.Expect(err).To(MatchError(cmdErr)) | ||
g.Expect(rc).To(Equal(http.StatusInternalServerError)) | ||
}) | ||
|
||
t.Run("InvalidToken", func(t *testing.T) { | ||
apiv2 := &v2.API{ | ||
Snap: &mock.Snap{ | ||
CAPIAuthTokenValid: false, // explicitly set to false | ||
}, | ||
} | ||
|
||
rc, err := apiv2.RemoveFromDqlite(context.Background(), v2.RemoveFromDqliteRequest{RemoveEndpoint: "1.1.1.1:1234"}, "token") | ||
|
||
g := NewWithT(t) | ||
g.Expect(err).To(HaveOccurred()) | ||
g.Expect(rc).To(Equal(http.StatusUnauthorized)) | ||
}) | ||
|
||
t.Run("TokenFileNotFound", func(t *testing.T) { | ||
tokenErr := errors.New("token file not found") | ||
apiv2 := &v2.API{ | ||
Snap: &mock.Snap{ | ||
CAPIAuthTokenError: tokenErr, | ||
}, | ||
} | ||
|
||
rc, err := apiv2.RemoveFromDqlite(context.Background(), v2.RemoveFromDqliteRequest{RemoveEndpoint: "1.1.1.1:1234"}, "token") | ||
|
||
g := NewWithT(t) | ||
g.Expect(err).To(MatchError(tokenErr)) | ||
g.Expect(rc).To(Equal(http.StatusInternalServerError)) | ||
}) | ||
|
||
t.Run("RemovesSuccessfully", func(t *testing.T) { | ||
apiv2 := &v2.API{ | ||
Snap: &mock.Snap{ | ||
CAPIAuthTokenValid: true, | ||
}, | ||
} | ||
|
||
rc, err := apiv2.RemoveFromDqlite(context.Background(), v2.RemoveFromDqliteRequest{RemoveEndpoint: "1.1.1.1:1234"}, "token") | ||
|
||
g := NewWithT(t) | ||
g.Expect(err).ToNot(HaveOccurred()) | ||
g.Expect(rc).To(Equal(http.StatusOK)) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.