From 5fe8e50f7d9f09f6be07ab9030a2f621a35de3e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ryo=20Arima=F0=9F=8C=B1?= <118931098+ryo-arima@users.noreply.github.com> Date: Sun, 10 Nov 2024 08:27:40 +0900 Subject: [PATCH] =?UTF-8?q?=F0=9F=8C=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/agent/app/.gitkeep | 0 pkg/entity/model/agent.go | 12 +++++ pkg/entity/request/agent.go | 12 +++++ pkg/entity/request/command.go | 18 +++++++ pkg/entity/response/agent.go | 15 ++++++ pkg/server/controller/user_private.go | 75 +++++++++++++++++++++++---- 6 files changed, 122 insertions(+), 10 deletions(-) create mode 100644 cmd/agent/app/.gitkeep create mode 100644 pkg/entity/request/command.go diff --git a/cmd/agent/app/.gitkeep b/cmd/agent/app/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/pkg/entity/model/agent.go b/pkg/entity/model/agent.go index 8b53790..e2eda33 100644 --- a/pkg/entity/model/agent.go +++ b/pkg/entity/model/agent.go @@ -1 +1,13 @@ package model + +type Agents struct { + ID uint `gorm:"primaryKey,autoIncrement"` + UUID string `gorm:"type:char(36);not null;"` + Shells []string `gorm:"shells"` + Name string `gorm:"not null"` + IP string `gorm:"not null"` + Status string `gorm:"type:enum('UP','ERROR','UNKNOWN');default:'DOWN'"` + CreatedAt *string `gorm:"type:datetime(0);not null"` + UpdatedAt *string `gorm:"type:datetime(0);not null"` + DeletedAt *string `gorm:"type:datetime(0)"` +} diff --git a/pkg/entity/request/agent.go b/pkg/entity/request/agent.go index 725b8fc..77cd7a3 100644 --- a/pkg/entity/request/agent.go +++ b/pkg/entity/request/agent.go @@ -1 +1,13 @@ package request + +type AgentRequest struct { + Agent Agent `json:"agent"` +} + +type Agent struct { + ID string `json:"id"` + UUID string `json:"uuid"` + Name string `json:"name" validate:"required"` + IP string `json:"ip" validate:"required"` + Status string `json:"status"` +} diff --git a/pkg/entity/request/command.go b/pkg/entity/request/command.go new file mode 100644 index 0000000..8279690 --- /dev/null +++ b/pkg/entity/request/command.go @@ -0,0 +1,18 @@ +package request + +type CommandRequest struct { + Command Command `json:"command"` +} + +type Command struct { + AgentUUID string `json:"agent_uuid"` + AgentName string `json:"agent_name"` + Body []CommandBody `json:"body"` +} + +type CommandBody struct { + Environments string `json:"environments"` + Shell string `json:"shell"` + Args string `json:"args"` + Timeout int `json:"timeout"` +} diff --git a/pkg/entity/response/agent.go b/pkg/entity/response/agent.go index a467149..8619f99 100644 --- a/pkg/entity/response/agent.go +++ b/pkg/entity/response/agent.go @@ -1 +1,16 @@ package response + +type CommandResponse struct { + Command Command `json:"command"` +} + +type Command struct { + AgentUUID string `json:"agent_uuid"` + AgentName string `json:"agent_name"` + Results []CommandResult `json:"results"` +} + +type CommandResult struct { + Code string `json:"code"` + Message string `json:"message"` +} diff --git a/pkg/server/controller/user_private.go b/pkg/server/controller/user_private.go index 53bdfa0..e3531ea 100644 --- a/pkg/server/controller/user_private.go +++ b/pkg/server/controller/user_private.go @@ -5,6 +5,7 @@ import ( "net/http" "github.com/gin-gonic/gin" + "github.com/go-playground/validator/v10" "github.com/ryo-arima/mark1/pkg/entity/model" "github.com/ryo-arima/mark1/pkg/entity/request" "github.com/ryo-arima/mark1/pkg/entity/response" @@ -44,13 +45,39 @@ func (userController userControllerForPrivate) GetUsers(c *gin.Context) { func (userController userControllerForPrivate) CreateUser(c *gin.Context) { var userRequest request.UserRequest + // Bind the request with the struct if err := c.Bind(&userRequest); err != nil { - c.JSON(http.StatusBadRequest, &response.UserResponse{Code: "SERVER_CONTROLLER_CREATE__FOR__001", Message: err.Error(), Users: []response.User{}}) - return + c.JSON(http.StatusBadRequest, &response.UserResponse{Code: "SERVER_CONTROLLER_GET__FOR__001", Message: err.Error(), Users: []response.User{}}) } - var userModel model.Users - res := userController.UserRepository.CreateUser(userModel) - c.JSON(http.StatusOK, res) + validate := validator.New() + err := validate.Struct(userRequest) + if err != nil { + c.JSON(http.StatusBadRequest, &response.UserResponse{Code: "SERVER_CONTROLLER_GET__FOR__002", Message: err.Error(), Users: []response.User{}}) + } else { + c.JSON(http.StatusOK, &response.UserResponse{Code: "SERVER_CONTROLLER_GET__FOR__003", Message: "Validation passed", Users: []response.User{}}) + } + + hash, err := middleware.GenHash(userRequest.User.Password) + if err != nil { + c.JSON(http.StatusBadRequest, &response.UserResponse{Code: "SERVER_CONTROLLER_GET__FOR__002", Message: err.Error(), Users: []response.User{}}) + } + + userModel := model.Users{ + UUID: middleware.NewUUID(), + Name: userRequest.User.Name, + Password: hash, + Email: userRequest.User.Email, + CreatedAt: middleware.GetNowTime(), + UpdatedAt: middleware.GetNowTime(), + } + userController.UserRepository.CreateUser(userModel) + createdUser := userController.UserRepository.FindUserByUUID(userModel.UUID) + response := response.UserResponse{ + Code: "SERVER_CONTROLLER_CREATE__FOR__001", + Message: "User created successfully", + Users: []response.User{{UUID: createdUser.UUID, Name: createdUser.Name, Email: createdUser.Email, CreatedAt: *createdUser.CreatedAt, UpdatedAt: *createdUser.UpdatedAt}}, + } + c.JSON(http.StatusOK, response) return } @@ -60,9 +87,33 @@ func (userController userControllerForPrivate) UpdateUser(c *gin.Context) { c.JSON(http.StatusBadRequest, &response.UserResponse{Code: "SERVER_CONTROLLER_UPDATE__FOR__001", Message: err.Error(), Users: []response.User{}}) return } - var userModel model.Users - res := userController.UserRepository.UpdateUser(userModel) - c.JSON(http.StatusOK, res) + + // Find the existing user + existingUser := userController.UserRepository.FindUserByUUID(userRequest.User.UUID) + if existingUser.UUID == "" { + c.JSON(http.StatusNotFound, &response.UserResponse{Code: "SERVER_CONTROLLER_UPDATE__FOR__002", Message: "User not found", Users: []response.User{}}) + return + } + + // Update user fields + existingUser.Name = userRequest.User.Name + if existingUser.Email != userRequest.User.Email { + existingUser.Email = userRequest.User.Email + existingUser.Status = "EmailNotVerified" + } + existingUser.UpdatedAt = middleware.GetNowTime() + + // Save the updated user + userController.UserRepository.UpdateUser(existingUser) + + // Prepare the response + updatedUser := userController.UserRepository.FindUserByUUID(existingUser.UUID) + response := response.UserResponse{ + Code: "SERVER_CONTROLLER_UPDATE__FOR__003", + Message: "User updated successfully", + Users: []response.User{{UUID: updatedUser.UUID, Name: updatedUser.Name, Email: updatedUser.Email, Status: updatedUser.Status, CreatedAt: *updatedUser.CreatedAt, UpdatedAt: *updatedUser.UpdatedAt}}, + } + c.JSON(http.StatusOK, response) return } @@ -72,8 +123,12 @@ func (userController userControllerForPrivate) DeleteUser(c *gin.Context) { c.JSON(http.StatusBadRequest, &response.UserResponse{Code: "SERVER_CONTROLLER_DELETE__FOR__001", Message: err.Error(), Users: []response.User{}}) return } - var uuid string - res := userController.UserRepository.DeleteUser(uuid) + existingUser := userController.UserRepository.FindUserByUUID(userRequest.User.UUID) + if existingUser.UUID == "" { + c.JSON(http.StatusNotFound, &response.UserResponse{Code: "SERVER_CONTROLLER_UPDATE__FOR__002", Message: "User not found", Users: []response.User{}}) + return + } + res := userController.UserRepository.DeleteUser(existingUser.UUID) c.JSON(http.StatusOK, res) return }