From db8fcf1ed3eba9d7345240ee30720f2f029a260d Mon Sep 17 00:00:00 2001 From: bupd Date: Tue, 5 Nov 2024 17:13:38 +0530 Subject: [PATCH] fix robot account issue Signed-off-by: bupd --- ground-control/internal/server/handlers.go | 20 ++++++++++++++++++++ ground-control/reg/harbor/robot.go | 21 +++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/ground-control/internal/server/handlers.go b/ground-control/internal/server/handlers.go index f636afc..f1e02c3 100644 --- a/ground-control/internal/server/handlers.go +++ b/ground-control/internal/server/handlers.go @@ -170,6 +170,26 @@ func (s *Server) registerSatelliteHandler(w http.ResponseWriter, r *http.Request return } + roboPresent,err := harbor.IsRobotPresent(r.Context(), req.Name) + if err != nil { + log.Println(err) + err := &AppError{ + Message: fmt.Sprintf("Error querying for robot account: %v", err.Error()), + Code: http.StatusBadRequest, + } + HandleAppError(w, err) + return + } + + if roboPresent { + err := &AppError{ + Message: fmt.Sprintf("Error: Robot Account name already present. Try with different name"), + Code: http.StatusBadRequest, + } + HandleAppError(w, err) + return + } + // Start a new transaction tx, err := s.db.BeginTx(r.Context(), nil) if err != nil { diff --git a/ground-control/reg/harbor/robot.go b/ground-control/reg/harbor/robot.go index 65ae0bc..c26bd25 100644 --- a/ground-control/reg/harbor/robot.go +++ b/ground-control/reg/harbor/robot.go @@ -25,6 +25,27 @@ func GetRobotDetails(r *robot.CreateRobotCreated) (int64, string, string) { return id, name, secret } +func IsRobotPresent(ctx context.Context, name string) (bool, error) { + client := GetClient() + + name = fmt.Sprintf("name=%s", name) + response, err := client.Robot.ListRobot( + ctx, + &robot.ListRobotParams{ + Q: &name, + }, + ) + if err != nil { + return false, fmt.Errorf("error: listing robot account: %v", err) + } + + if len(response.Payload) > 0 { + return true, nil + } + + return false, nil +} + func ListRobots(ctx context.Context, opts ListParams) (*robot.ListRobotOK, error) { client := GetClient() response, err := client.Robot.ListRobot(