Skip to content

Commit 9a54f58

Browse files
committed
add API for retrieving possible state transitions for a particular kind of connection
Signed-off-by: MUzairS15 <[email protected]>
1 parent feb9c58 commit 9a54f58

12 files changed

+108
-50
lines changed

server/handlers/connections_handlers.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ import (
1010
"github.com/gofrs/uuid"
1111
"github.com/gorilla/mux"
1212
"github.com/layer5io/meshery/server/models"
13+
"github.com/layer5io/meshery/server/models/connections"
1314
"github.com/layer5io/meshkit/models/events"
1415
)
1516

16-
type connectionStatusPayload map[uuid.UUID]models.ConnectionStatus
17+
type connectionStatusPayload map[uuid.UUID]connections.ConnectionStatus
1718

1819
// swagger:route POST /api/integrations/connections PostConnection idPostConnection
1920
// Handle POST request for creating a new connection
@@ -191,6 +192,23 @@ func (h *Handler) GetConnectionsStatus(w http.ResponseWriter, req *http.Request,
191192
}
192193
}
193194

195+
// swagger:route GET /api/integrations/connections/{connectionKind}/transitions GetAvailableTransitionsByKind idGetConnectionsStatus
196+
// Handle GET request for getting all possible connection transitions
197+
//
198+
// Get all possible state transitions for a particular connection kind.
199+
// responses:
200+
// 200: mesheryConnectionsStatusPage
201+
func (h *Handler) GetPossibleTransitionsByKind(w http.ResponseWriter, req *http.Request, _ *models.Preference, user *models.User, provider models.Provider) {
202+
connectionKind := mux.Vars(req)["connectionKind"]
203+
transitions := connections.PossibleTransitionnsMap[connectionKind]
204+
205+
err := json.NewEncoder(w).Encode(transitions)
206+
if err != nil {
207+
http.Error(w, models.ErrMarshal(err, "connection transitions").Error(), http.StatusInternalServerError)
208+
return
209+
}
210+
}
211+
194212
func (h *Handler) UpdateConnectionStatus(w http.ResponseWriter, req *http.Request, _ *models.Preference, user *models.User, provider models.Provider) {
195213
connectionStatusPayload := &connectionStatusPayload{}
196214
defer func() {
@@ -214,7 +232,7 @@ func (h *Handler) UpdateConnectionStatus(w http.ResponseWriter, req *http.Reques
214232
var statusCode int
215233
for id, status := range *connectionStatusPayload {
216234
eventBuilder.ActedUpon(id)
217-
var updatedConnection *models.Connection
235+
var updatedConnection *connections.Connection
218236
var err error
219237
updatedConnection, statusCode, err = provider.UpdateConnectionStatusByID(req, id, status)
220238
fmt.Println("tetete_", updatedConnection)
@@ -251,7 +269,7 @@ func (h *Handler) UpdateConnection(w http.ResponseWriter, req *http.Request, _ *
251269

252270
userID := uuid.FromStringOrNil(user.ID)
253271

254-
connection := &models.Connection{}
272+
connection := &connections.Connection{}
255273
err = json.Unmarshal(bd, connection)
256274
obj := "connection"
257275
if err != nil {

server/handlers/doc.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535

3636
"github.com/go-openapi/strfmt"
3737
"github.com/layer5io/meshery/server/models"
38+
"github.com/layer5io/meshery/server/models/connections"
3839
"github.com/layer5io/meshkit/models/events"
3940
SMP "github.com/layer5io/service-mesh-performance/spec"
4041
v1 "k8s.io/api/core/v1"
@@ -504,14 +505,14 @@ type mesheryApplicationTypesResponseWrapper struct {
504505
// swagger:response mesheryConnectionResponseWrapper
505506
type mesheryConnectionResponseWrapper struct {
506507
// in: body
507-
Body models.Connection
508+
Body connections.Connection
508509
}
509510

510511
// Returns all connections Status
511512
// swagger:response mesheryConnectionsStatusPage
512513
type mesheryConnectionsStatusPage struct {
513514
// in: body
514-
Body *models.ConnectionsStatusPage
515+
Body *connections.ConnectionsStatusPage
515516
}
516517

517518
// Returns environment
@@ -541,3 +542,9 @@ type eventsResponseWrapper struct {
541542
// in: body
542543
Body *models.EventsResponse
543544
}
545+
546+
// swagger:response loadTestPreferencesWrapper
547+
type possibleTransitions struct {
548+
//in: body
549+
Body map[string]map[connections.ConnectionStatus][]connections.ConnectionStatus
550+
}

server/handlers/k8sconfig_handler.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
mutil "github.com/layer5io/meshery/server/helpers/utils"
1414

15+
"github.com/layer5io/meshery/server/models/connections"
1516
mcore "github.com/layer5io/meshery/server/models/meshmodel/core"
1617
meshmodelv1alpha1 "github.com/layer5io/meshkit/models/meshmodel/core/v1alpha1"
1718

@@ -113,13 +114,13 @@ func (h *Handler) addK8SConfig(user *models.User, _ *models.Preference, w http.R
113114

114115
status := connection.Status
115116

116-
if status == models.CONNECTED {
117+
if status == connections.CONNECTED {
117118
saveK8sContextResponse.ConnectedContexts = append(saveK8sContextResponse.ConnectedContexts, *ctx)
118119
eventBuilder.WithSeverity(events.Informational).WithDescription(fmt.Sprintf("Connection already exists with Kubernetes context \"%s\" at %s", ctx.Name, ctx.Server))
119-
} else if status == models.IGNORED {
120+
} else if status == connections.IGNORED {
120121
saveK8sContextResponse.IgnoredContexts = append(saveK8sContextResponse.IgnoredContexts, *ctx)
121122
eventBuilder.WithSeverity(events.Informational).WithDescription(fmt.Sprintf("Kubernetes context \"%s\" is set to ignored state.", ctx.Name))
122-
} else if status == models.REGISTERED {
123+
} else if status == connections.REGISTERED {
123124
saveK8sContextResponse.RegisteredContexts = append(saveK8sContextResponse.RegisteredContexts, *ctx)
124125
eventBuilder.WithSeverity(events.Informational).WithDescription(fmt.Sprintf("Connection registered with kubernetes context \"%s\" at %s.", ctx.Name, ctx.Server))
125126
h.config.K8scontextChannel.PublishContext()

server/models/connections.go renamed to server/models/connections/connections.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package models
1+
package connections
22

33
import (
44
"database/sql"
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package connections
2+
3+
var PossibleTransitionnsMap = map[string]map[ConnectionStatus][]ConnectionStatus{
4+
"Kubernetes": {
5+
DISCOVERED: {},
6+
REGISTERED: {
7+
CONNECTED, IGNORED,
8+
},
9+
CONNECTED: {
10+
DISCONNECTED, MAINTENANCE, DELETED, NOTFOUND,
11+
},
12+
IGNORED: {
13+
DELETED, NOTFOUND,
14+
},
15+
MAINTENANCE: {
16+
REGISTERED, CONNECTED, IGNORED, NOTFOUND,
17+
},
18+
DISCONNECTED: {
19+
CONNECTED, DELETED,
20+
},
21+
DELETED: {},
22+
NOTFOUND: {
23+
DISCOVERED,
24+
},
25+
},
26+
}

server/models/default_local_provider.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"time"
2020

2121
"github.com/gofrs/uuid"
22+
"github.com/layer5io/meshery/server/models/connections"
2223
"github.com/layer5io/meshkit/database"
2324
"github.com/layer5io/meshkit/logger"
2425
"github.com/layer5io/meshkit/utils"
@@ -184,7 +185,7 @@ func (l *DefaultLocalProvider) HandleUnAuthenticated(w http.ResponseWriter, req
184185
http.Redirect(w, req, "/user/login", http.StatusFound)
185186
}
186187

187-
func (l *DefaultLocalProvider) SaveK8sContext(_ string, k8sContext K8sContext) (Connection, error) {
188+
func (l *DefaultLocalProvider) SaveK8sContext(_ string, k8sContext K8sContext) (connections.Connection, error) {
188189
return l.MesheryK8sContextPersister.SaveMesheryK8sContext(k8sContext)
189190
}
190191

@@ -959,35 +960,35 @@ func (l *DefaultLocalProvider) ExtensionProxy(_ *http.Request) (*ExtensionProxyR
959960
return nil, ErrLocalProviderSupport
960961
}
961962

962-
func (l *DefaultLocalProvider) SaveConnection(_ *http.Request, _ *ConnectionPayload, _ string, _ bool) (*Connection, error) {
963+
func (l *DefaultLocalProvider) SaveConnection(_ *http.Request, _ *ConnectionPayload, _ string, _ bool) (*connections.Connection, error) {
963964
return nil, ErrLocalProviderSupport
964965
}
965966

966-
func (l *DefaultLocalProvider) GetConnections(_ *http.Request, _ string, _, _ int, _, _ string) (*ConnectionPage, error) {
967+
func (l *DefaultLocalProvider) GetConnections(_ *http.Request, _ string, _, _ int, _, _ string) (*connections.ConnectionPage, error) {
967968
return nil, ErrLocalProviderSupport
968969
}
969970

970971
func (l *DefaultLocalProvider) GetConnectionsByKind(_ *http.Request, _ string, _, _ int, _, _, _ string) (*map[string]interface{}, error) {
971972
return nil, ErrLocalProviderSupport
972973
}
973974

974-
func (l *DefaultLocalProvider) GetConnectionsStatus(_ *http.Request, _ string) (*ConnectionsStatusPage, error) {
975+
func (l *DefaultLocalProvider) GetConnectionsStatus(_ *http.Request, _ string) (*connections.ConnectionsStatusPage, error) {
975976
return nil, ErrLocalProviderSupport
976977
}
977978

978-
func (l *DefaultLocalProvider) UpdateConnection(_ *http.Request, _ *Connection) (*Connection, error) {
979+
func (l *DefaultLocalProvider) UpdateConnection(_ *http.Request, _ *connections.Connection) (*connections.Connection, error) {
979980
return nil, ErrLocalProviderSupport
980981
}
981982

982-
func (l *DefaultLocalProvider) UpdateConnectionStatusByID(req *http.Request, connectionID uuid.UUID, connectionStatus ConnectionStatus) (*Connection, int, error) {
983+
func (l *DefaultLocalProvider) UpdateConnectionStatusByID(req *http.Request, connectionID uuid.UUID, connectionStatus connections.ConnectionStatus) (*connections.Connection, int, error) {
983984
return nil, http.StatusForbidden, ErrLocalProviderSupport
984985
}
985986

986-
func (l *DefaultLocalProvider) UpdateConnectionById(_ *http.Request, _ *ConnectionPayload, _ string) (*Connection, error) {
987+
func (l *DefaultLocalProvider) UpdateConnectionById(_ *http.Request, _ *ConnectionPayload, _ string) (*connections.Connection, error) {
987988
return nil, ErrLocalProviderSupport
988989
}
989990

990-
func (l *DefaultLocalProvider) DeleteConnection(_ *http.Request, _ uuid.UUID) (*Connection, error) {
991+
func (l *DefaultLocalProvider) DeleteConnection(_ *http.Request, _ uuid.UUID) (*connections.Connection, error) {
991992
return nil, ErrLocalProviderSupport
992993
}
993994

server/models/handlers.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ type HandlerInterface interface {
187187

188188
SaveConnection(w http.ResponseWriter, req *http.Request, prefObj *Preference, user *User, provider Provider)
189189
GetConnections(w http.ResponseWriter, req *http.Request, prefObj *Preference, user *User, provider Provider)
190+
GetAvailableTransitionsByKind(w http.ResponseWriter, req *http.Request, prefObj *Preference, user *User, provider Provider)
190191
GetConnectionsByKind(w http.ResponseWriter, req *http.Request, prefObj *Preference, user *User, provider Provider)
191192
GetConnectionsStatus(w http.ResponseWriter, req *http.Request, prefObj *Preference, user *User, provider Provider)
192193
UpdateConnectionStatus(w http.ResponseWriter, req *http.Request, prefObj *Preference, user *User, provider Provider)

server/models/meshery_k8scontext_persister.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"strings"
66

7+
"github.com/layer5io/meshery/server/models/connections"
78
"github.com/layer5io/meshkit/database"
89
"gorm.io/gorm"
910
)
@@ -63,8 +64,8 @@ func (mkcp *MesheryK8sContextPersister) DeleteMesheryK8sContext(id string) (K8sC
6364
return context, nil
6465
}
6566

66-
func (mkcp *MesheryK8sContextPersister) SaveMesheryK8sContext(mkc K8sContext) (Connection, error) {
67-
conn := Connection{}
67+
func (mkcp *MesheryK8sContextPersister) SaveMesheryK8sContext(mkc K8sContext) (connections.Connection, error) {
68+
conn := connections.Connection{}
6869
if mkc.ID == "" {
6970
id, err := K8sContextGenerateID(mkc)
7071
if err != nil {

server/models/meshery_pattern.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"github.com/gofrs/uuid"
77
"github.com/layer5io/meshery/server/internal/sql"
88
"gopkg.in/yaml.v2"
9-
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
109
types "k8s.io/apimachinery/pkg/types"
1110
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1211
)
@@ -21,7 +20,7 @@ type ListMetaApplyConfiguration struct {
2120
}
2221

2322
type StatusCauseApplyConfiguration struct {
24-
Type *v1.CauseType `json:"reason,omitempty"`
23+
Type *metav1.CauseType `json:"reason,omitempty"`
2524
Message *string `json:"message,omitempty"`
2625
Field *string `json:"field,omitempty"`
2726
}

server/models/providers.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"net/http"
55

66
"github.com/gofrs/uuid"
7+
"github.com/layer5io/meshery/server/models/connections"
78
"github.com/layer5io/meshkit/broker"
89
"github.com/layer5io/meshkit/database"
910
"github.com/layer5io/meshkit/logger"
@@ -208,7 +209,7 @@ type ConnectionPayload struct {
208209
SubType string `json:"sub_type,omitempty"`
209210
Type string `json:"type,omitempty"`
210211
MetaData map[string]interface{} `json:"metadata,omitempty"`
211-
Status ConnectionStatus `json:"status,omitempty"`
212+
Status connections.ConnectionStatus `json:"status,omitempty"`
212213
CredentialSecret map[string]interface{} `json:"credential_secret,omitempty"`
213214
Name string `json:"name,omitempty"`
214215
}
@@ -378,7 +379,7 @@ type Provider interface {
378379
GetResult(tokenVal string, resultID uuid.UUID) (*MesheryResult, error)
379380
RecordPreferences(req *http.Request, userID string, data *Preference) error
380381

381-
SaveK8sContext(token string, k8sContext K8sContext) (Connection, error)
382+
SaveK8sContext(token string, k8sContext K8sContext) (connections.Connection, error)
382383
GetK8sContexts(token, page, pageSize, search, order string, withCredentials bool) ([]byte, error)
383384
DeleteK8sContext(token, id string) (K8sContext, error)
384385
GetK8sContext(token, connectionID string) (K8sContext, error)
@@ -443,14 +444,14 @@ type Provider interface {
443444

444445
ExtensionProxy(req *http.Request) (*ExtensionProxyResponse, error)
445446

446-
SaveConnection(req *http.Request, conn *ConnectionPayload, token string, skipTokenCheck bool) (*Connection, error)
447-
GetConnections(req *http.Request, userID string, page, pageSize int, search, order string) (*ConnectionPage, error)
447+
SaveConnection(req *http.Request, conn *ConnectionPayload, token string, skipTokenCheck bool) (*connections.Connection, error)
448+
GetConnections(req *http.Request, userID string, page, pageSize int, search, order string) (*connections.ConnectionPage, error)
448449
GetConnectionsByKind(req *http.Request, userID string, page, pageSize int, search, order, connectionKind string) (*map[string]interface{}, error)
449-
GetConnectionsStatus(req *http.Request, userID string) (*ConnectionsStatusPage, error)
450-
UpdateConnection(req *http.Request, conn *Connection) (*Connection, error)
451-
UpdateConnectionById(req *http.Request, conn *ConnectionPayload, connId string) (*Connection, error)
452-
UpdateConnectionStatusByID(req *http.Request, connectionID uuid.UUID, connectionStatus ConnectionStatus) (*Connection, int, error)
453-
DeleteConnection(req *http.Request, connID uuid.UUID) (*Connection, error)
450+
GetConnectionsStatus(req *http.Request, userID string) (*connections.ConnectionsStatusPage, error)
451+
UpdateConnection(req *http.Request, conn *connections.Connection) (*connections.Connection, error)
452+
UpdateConnectionById(req *http.Request, conn *ConnectionPayload, connId string) (*connections.Connection, error)
453+
UpdateConnectionStatusByID(req *http.Request, connectionID uuid.UUID, connectionStatus connections.ConnectionStatus) (*connections.Connection, int, error)
454+
DeleteConnection(req *http.Request, connID uuid.UUID) (*connections.Connection, error)
454455
DeleteMesheryConnection() error
455456

456457
SaveUserCredential(req *http.Request, credential *Credential) error

0 commit comments

Comments
 (0)