Skip to content

Commit a94ad99

Browse files
committed
Bug. add validation for delete cluster
1 parent 1259197 commit a94ad99

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

cmd/server/handlers.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -253,16 +253,27 @@ func (s *server) DeleteCluster(ctx context.Context, in *pb.IDRequest) (*pb.Simpl
253253
}
254254
clusterId := in.GetId()
255255

256-
if _, err := clusterInfoClient.GetCluster(ctx, &pb.GetClusterRequest{ClusterId: clusterId}); err != nil {
256+
res, err := clusterInfoClient.GetCluster(ctx, &pb.GetClusterRequest{ClusterId: clusterId})
257+
if err != nil {
257258
log.Error("Failed to get cluster info err : ", err)
258259
return &pb.SimpleResponse{
259260
Code: pb.Code_NOT_FOUND,
260261
Error: &pb.Error{
261-
Msg: fmt.Sprintf("Invalid cluster Id %s", clusterId),
262+
Msg: fmt.Sprintf("Could not find Cluster with ID %s", clusterId),
262263
},
263264
}, err
264265
}
265266

267+
if res.GetCluster().GetStatus() == pb.ClusterStatus_DELETING || res.GetCluster().GetStatus() == pb.ClusterStatus_DELETED {
268+
log.Error("The cluster has been already deleted. status : ", res.GetCluster().GetStatus())
269+
return &pb.SimpleResponse{
270+
Code: pb.Code_NOT_FOUND,
271+
Error: &pb.Error{
272+
Msg: fmt.Sprintf("Could not find cluster with ID. %s", clusterId),
273+
},
274+
}, errors.New(fmt.Sprintf("Could not find cluster with ID. %s", clusterId))
275+
}
276+
266277
nameSpace := "argo"
267278
workflow := "tks-remove-usercluster"
268279
appGroup := "tks-cluster-aws"

cmd/server/handlers_test.go

+21
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,27 @@ func TestDeleteCluster(t *testing.T) {
360360
require.Equal(t, res.Code, pb.Code_NOT_FOUND)
361361
},
362362
},
363+
{
364+
name: "THE_CLUSTER_ALREADY_DELETED",
365+
in: &pb.IDRequest{
366+
Id: uuid.New().String(),
367+
},
368+
buildStubs: func(mockArgoClient *mockargo.MockClient, mockClusterInfoClient *mocktks.MockClusterInfoServiceClient) {
369+
mockClusterInfoClient.EXPECT().GetCluster(gomock.Any(), gomock.Any()).Times(1).
370+
Return(
371+
&pb.GetClusterResponse{
372+
Code: pb.Code_OK_UNSPECIFIED,
373+
Error: nil,
374+
Cluster: &pb.Cluster{
375+
Status: pb.ClusterStatus_DELETED,
376+
},
377+
}, nil)
378+
},
379+
checkResponse: func(req *pb.IDRequest, res *pb.SimpleResponse, err error) {
380+
require.Error(t, err)
381+
require.Equal(t, res.Code, pb.Code_NOT_FOUND)
382+
},
383+
},
363384
{
364385
name: "FAILED_TO_CALL_WORKFLOW",
365386
in: &pb.IDRequest{

0 commit comments

Comments
 (0)