mirror of
https://github.com/mattermost/mattermost.git
synced 2026-04-24 15:47:47 -04:00
* Adds Remote Cluster related API endpoints
New endpoints for the following routes are added:
- Get Remote Clusters at `GET /api/v4/remotecluster`
- Create Remote Cluster at `POST /api/v4/remotecluster`
- Accept Remote Cluster invite at `POST
/api/v4/remotecluster/accept_invite`
- Generate Remote Cluster invite at `POST
/api/v4/remotecluster/{remote_id}/generate_invite`
- Get Remote Cluster at `GET /api/v4/remotecluster/{remote_id}`
- Patch Remote Cluster at `PATCH /api/v4/remotecluster/{remote_id}`
- Delete Remote Cluster at `DELETE /api/v4/remotecluster/{remote_id}`
These endpoints are planned to be used from the system console, and
gated through the `manage_secure_connections` permission.
* Update server/channels/api4/remote_cluster_test.go
Co-authored-by: Doug Lauder <wiggin77@warpmail.net>
* Fix AppError names
---------
Co-authored-by: Doug Lauder <wiggin77@warpmail.net>
Co-authored-by: Mattermost Build <build@mattermost.com>
81 lines
2.1 KiB
Go
81 lines
2.1 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
package remotecluster
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/mattermost/mattermost/server/public/model"
|
|
)
|
|
|
|
// AcceptInvitation is called when accepting an invitation to connect with a remote cluster.
|
|
func (rcs *Service) AcceptInvitation(invite *model.RemoteClusterInvite, name string, displayName, creatorId string, siteURL string) (*model.RemoteCluster, error) {
|
|
rc := &model.RemoteCluster{
|
|
RemoteId: invite.RemoteId,
|
|
Name: name,
|
|
DisplayName: displayName,
|
|
Token: model.NewId(),
|
|
RemoteToken: invite.Token,
|
|
SiteURL: invite.SiteURL,
|
|
CreatorId: creatorId,
|
|
}
|
|
|
|
rcSaved, err := rcs.server.GetStore().RemoteCluster().Save(rc)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// confirm the invitation with the originating site
|
|
frame, err := makeConfirmFrame(rcSaved, siteURL)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
url := fmt.Sprintf("%s/%s", rcSaved.SiteURL, ConfirmInviteURL)
|
|
|
|
resp, err := rcs.sendFrameToRemote(PingTimeout, rc, frame, url)
|
|
if err != nil {
|
|
rcs.server.GetStore().RemoteCluster().Delete(rcSaved.RemoteId)
|
|
return nil, err
|
|
}
|
|
|
|
var response Response
|
|
err = json.Unmarshal(resp, &response)
|
|
if err != nil {
|
|
rcs.server.GetStore().RemoteCluster().Delete(rcSaved.RemoteId)
|
|
return nil, fmt.Errorf("invalid response from remote server: %w", err)
|
|
}
|
|
|
|
if !response.IsSuccess() {
|
|
rcs.server.GetStore().RemoteCluster().Delete(rcSaved.RemoteId)
|
|
return nil, errors.New(response.Err)
|
|
}
|
|
|
|
// issue the first ping right away. The goroutine will exit when ping completes or PingTimeout exceeded.
|
|
go rcs.PingNow(rcSaved)
|
|
|
|
return rcSaved, nil
|
|
}
|
|
|
|
func makeConfirmFrame(rc *model.RemoteCluster, siteURL string) (*model.RemoteClusterFrame, error) {
|
|
confirm := model.RemoteClusterInvite{
|
|
RemoteId: rc.RemoteId,
|
|
SiteURL: siteURL,
|
|
Token: rc.Token,
|
|
}
|
|
confirmRaw, err := json.Marshal(confirm)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
msg := model.NewRemoteClusterMsg(InvitationTopic, confirmRaw)
|
|
|
|
frame := &model.RemoteClusterFrame{
|
|
RemoteId: rc.RemoteId,
|
|
Msg: msg,
|
|
}
|
|
return frame, nil
|
|
}
|