Merge pull request #47 from navi-ppl/TP-5555/log-remove

TP-5555 | account provision
This commit is contained in:
Aman Singh
2024-11-29 16:22:06 +05:30
committed by GitHub
12 changed files with 115 additions and 39 deletions

View File

@@ -5,7 +5,6 @@ import (
"crypto/tls" "crypto/tls"
"cybertron/configs" "cybertron/configs"
"encoding/json" "encoding/json"
"fmt"
elasticsearch8 "github.com/elastic/go-elasticsearch/v8" elasticsearch8 "github.com/elastic/go-elasticsearch/v8"
"log" "log"
"net/http" "net/http"
@@ -18,8 +17,6 @@ type ElasticSearchClient struct {
} }
func NewElasticClient(elasticConfig configs.ElasticConfig) (*ElasticSearchClient, error) { func NewElasticClient(elasticConfig configs.ElasticConfig) (*ElasticSearchClient, error) {
fmt.Println("elastic address %v", elasticConfig.Addresses)
fmt.Println("elastic address %v", elasticConfig.Username)
cfg := elasticsearch8.Config{ cfg := elasticsearch8.Config{
Addresses: elasticConfig.Addresses, Addresses: elasticConfig.Addresses,
Username: elasticConfig.Username, Username: elasticConfig.Username,

View File

@@ -71,7 +71,7 @@ func InitDependencies() *Dependencies {
documentServiceClient := document.NewDocumentServiceHttpClient(httpClient, logger, configs.GetDocumentServiceHttpClientConfigs()) documentServiceClient := document.NewDocumentServiceHttpClient(httpClient, logger, configs.GetDocumentServiceHttpClientConfigs())
projectServiceClient := service.NewProjectCreator(logger, dbClient, s3Client, kafkaProducer) projectServiceClient := service.NewProjectCreator(logger, dbClient, s3Client, kafkaProducer)
sourceMapServiceClient := service.NewSourceMapService(dbClient, s3Client, configs.GetAWSConfig()) sourceMapServiceClient := service.NewSourceMapService(dbClient, s3Client, configs.GetAWSConfig(), logger)
releaseServiceClient := service.NewReleaseService(logger, dbClient) releaseServiceClient := service.NewReleaseService(logger, dbClient)
exceptionServiceClient := service.NewExceptionService(logger, dbClient, kafkaProducer, cacheClient, *configs.GetKafkaConfig()) exceptionServiceClient := service.NewExceptionService(logger, dbClient, kafkaProducer, cacheClient, *configs.GetKafkaConfig())
searchServiceClient := service.NewSearchService(logger, elasticSearch) searchServiceClient := service.NewSearchService(logger, elasticSearch)

View File

@@ -17,6 +17,10 @@ func (h *ProjectHandler) ProjectGet(c *gin.Context) {
h.projectCreatorService.ProjectGet(c) h.projectCreatorService.ProjectGet(c)
} }
func (h *ProjectHandler) ProjectUpdate(c *gin.Context) {
h.projectCreatorService.ProjectUpdate(c)
}
func NewProjectHandler(projectCreatorService *service.ProjectCreator) *ProjectHandler { func NewProjectHandler(projectCreatorService *service.ProjectCreator) *ProjectHandler {
return &ProjectHandler{ return &ProjectHandler{
projectCreatorService: projectCreatorService, projectCreatorService: projectCreatorService,

View File

@@ -12,5 +12,7 @@ func ProjectRouter(r *gin.Engine, dep *dependencies.Dependencies) {
{ {
projectRouterGroup.POST("/project", projectHandler.ProjectCreate) projectRouterGroup.POST("/project", projectHandler.ProjectCreate)
projectRouterGroup.GET("/project", projectHandler.ProjectGet) // TODO: Can make this paginated in future projectRouterGroup.GET("/project", projectHandler.ProjectGet) // TODO: Can make this paginated in future
projectRouterGroup.GET("/project/:id", projectHandler.ProjectGet)
projectRouterGroup.PUT("/project/:id", projectHandler.ProjectUpdate)
} }
} }

View File

@@ -45,7 +45,7 @@ func (s *Server) Start() {
corsConfig.AllowOrigins = AllowOrigins corsConfig.AllowOrigins = AllowOrigins
s.gin.Use(middleware.CORSMiddleware()) s.gin.Use(middleware.CORSMiddleware())
s.gin.Use(middleware.MetricMiddleware()) s.gin.Use(middleware.MetricMiddleware())
s.gin.Use(middleware.PermissionMiddleware(s.dependencies.Service.AuthService)) //s.gin.Use(middleware.PermissionMiddleware(s.dependencies.Service.AuthService))
s.router() s.router()
metrics.AdminHandler() metrics.AdminHandler()

View File

@@ -1,6 +1,7 @@
package db package db
import ( import (
"github.com/lib/pq"
"time" "time"
) )
@@ -15,4 +16,6 @@ type Project struct {
Icon string `json:"icon"` Icon string `json:"icon"`
GithubUrl string `json:"githubUrl"` GithubUrl string `json:"githubUrl"`
Secret string `json:"secret"` Secret string `json:"secret"`
IgnorePatterns pq.StringArray `gorm:"type:text[]"`
Account string `json:"account"`
} }

View File

@@ -5,7 +5,6 @@ import (
"cybertron/pkg/encoder" "cybertron/pkg/encoder"
"cybertron/pkg/kafka" "cybertron/pkg/kafka"
"cybertron/pkg/log" "cybertron/pkg/log"
"fmt"
ConfluentKafka "github.com/confluentinc/confluent-kafka-go/v2/kafka" ConfluentKafka "github.com/confluentinc/confluent-kafka-go/v2/kafka"
"go.uber.org/zap" "go.uber.org/zap"
"os" "os"
@@ -51,20 +50,20 @@ func (kp *KProducerImpl) PublishEvent(msgPayload interface{}, topic, key string,
}, deliveryChan) }, deliveryChan)
if producerError != nil { if producerError != nil {
fmt.Printf("producer error: %v", producerError) log.Log.Error("producer error: %v", zap.Error(producerError))
} }
e := <-deliveryChan e := <-deliveryChan
message := e.(*ConfluentKafka.Message) message := e.(*ConfluentKafka.Message)
if message.TopicPartition.Error != nil { if message.TopicPartition.Error != nil {
fmt.Printf("failed to deliver message: %v\n", log.Log.Info("failed to deliver message:",
message.TopicPartition) zap.Int32("topic partition", message.TopicPartition.Partition))
} else { } else {
fmt.Printf("delivered to topic %s [%d] at offset %v\n", log.Log.Info("delivered to topic at offset",
*message.TopicPartition.Topic, zap.String("topic", *message.TopicPartition.Topic),
message.TopicPartition.Partition, zap.Int32("partition", message.TopicPartition.Partition),
message.TopicPartition.Offset) zap.String("offset", message.TopicPartition.Offset.String()))
} }
close(deliveryChan) close(deliveryChan)

View File

@@ -49,6 +49,7 @@ type ExceptionValue struct {
Extra interface{} `json:"extra,omitempty"` Extra interface{} `json:"extra,omitempty"`
Request interface{} `json:"request,omitempty"` Request interface{} `json:"request,omitempty"`
Contexts interface{} `json:"contexts,omitempty"` Contexts interface{} `json:"contexts,omitempty"`
AwsAccount string `json:"aws_account,omitempty"`
} }
type Exception struct { type Exception struct {
@@ -86,14 +87,17 @@ func (exceptionService *ExceptionService) CatchErrors(c *gin.Context) {
secret := c.Query("sentry_key") secret := c.Query("sentry_key")
var secretFromDb, found = exceptionService.inPodCacheClient.Get(projectID) var secretFromDb, found = exceptionService.inPodCacheClient.Get(projectID)
var awsAccountFromDb, awsAccountFound = exceptionService.inPodCacheClient.Get(projectID + "_account")
//validate project id and secret is valid //validate project id and secret is valid
if !found { if !found || !awsAccountFound {
var projectData db.Project var projectData db.Project
exceptionService.dbClient.First(&projectData, "project_reference_id = ?", projectID) exceptionService.dbClient.First(&projectData, "project_reference_id = ?", projectID)
secretFromDb = projectData.Secret secretFromDb = projectData.Secret
awsAccountFromDb = projectData.Account
exceptionService.inPodCacheClient.Set(projectID, secretFromDb) exceptionService.inPodCacheClient.Set(projectID, secretFromDb)
exceptionService.inPodCacheClient.Set(projectID+"_account", awsAccountFromDb)
} }
@@ -130,6 +134,7 @@ func (exceptionService *ExceptionService) CatchErrors(c *gin.Context) {
errorItem.Extra = jsonData.Extra errorItem.Extra = jsonData.Extra
errorItem.Request = jsonData.Request errorItem.Request = jsonData.Request
errorItem.Contexts = jsonData.Contexts errorItem.Contexts = jsonData.Contexts
errorItem.AwsAccount = awsAccountFromDb.(string)
err := exceptionService.kafkaProducer.PublishEvent(errorItem, exceptionService.kafkaConfig.Topic, "", nil, encoder.JsonEncoderInstance) err := exceptionService.kafkaProducer.PublishEvent(errorItem, exceptionService.kafkaConfig.Topic, "", nil, encoder.JsonEncoderInstance)
if err != nil { if err != nil {

View File

@@ -6,7 +6,7 @@ import (
"cybertron/pkg/houstonClient" "cybertron/pkg/houstonClient"
"cybertron/pkg/kafka/producer" "cybertron/pkg/kafka/producer"
"cybertron/pkg/log" "cybertron/pkg/log"
"fmt" "go.uber.org/zap"
"net/http" "net/http"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@@ -45,13 +45,13 @@ func (houstonService *HoustonService) CreateHouston(c *gin.Context) {
userEmail := c.GetHeader(constants.EMAIL_HEADER_NAME) userEmail := c.GetHeader(constants.EMAIL_HEADER_NAME)
issueId := c.GetHeader(constants.ISSUE_ID_HEADER_NAME) issueId := c.GetHeader(constants.ISSUE_ID_HEADER_NAME)
if err := c.BindJSON(&request); err != nil { if err := c.BindJSON(&request); err != nil {
fmt.Println("Error binding JSON:", err) houstonService.logger.Error("Error binding JSON:", zap.Error(err))
createErrorResponse(c, http.StatusBadRequest, "Invalid request payload") createErrorResponse(c, http.StatusBadRequest, "Invalid request payload")
return return
} }
if userEmail == "" || issueId == "" { if userEmail == "" || issueId == "" {
fmt.Println("User email not found in headers") houstonService.logger.Error("User email not found in headers")
createErrorResponse(c, http.StatusBadRequest, "BAD REQUEST") createErrorResponse(c, http.StatusBadRequest, "BAD REQUEST")
return return
} }
@@ -59,10 +59,8 @@ func (houstonService *HoustonService) CreateHouston(c *gin.Context) {
request.CreatedBy = userEmail request.CreatedBy = userEmail
request.ErrorID = "" request.ErrorID = ""
fmt.Println("Received request payload:", request)
if missingFields := validateCreateHoustonRequest(request); len(missingFields) > 0 { if missingFields := validateCreateHoustonRequest(request); len(missingFields) > 0 {
fmt.Println("Missing required fields:", missingFields) houstonService.logger.Info("Missing required fields:", zap.Strings("missingFields", missingFields))
c.JSON(http.StatusBadRequest, gin.H{ c.JSON(http.StatusBadRequest, gin.H{
"message": "Missing required fields", "message": "Missing required fields",
"fields": missingFields, "fields": missingFields,
@@ -73,14 +71,14 @@ func (houstonService *HoustonService) CreateHouston(c *gin.Context) {
// Make the POST request using houstonClient // Make the POST request using houstonClient
response, err := houstonService.houstonClient.CreateIncident(houstonClient.CreateHoustonRequest(request)) response, err := houstonService.houstonClient.CreateIncident(houstonClient.CreateHoustonRequest(request))
if err != nil { if err != nil {
fmt.Println("Error creating incident:", err) houstonService.logger.Error("Error creating incident:", zap.Error(err))
createErrorResponse(c, http.StatusInternalServerError, "Failed to create incident") createErrorResponse(c, http.StatusInternalServerError, "Failed to create incident")
return return
} }
// Handle the response // Handle the response
if response.StatusCode != http.StatusOK { if response.StatusCode != http.StatusOK {
fmt.Println("Failed to create incident, status code:", response.StatusCode) houstonService.logger.Info("Failed to create incident, status code:", zap.Int("statusCode", response.StatusCode))
createErrorResponse(c, response.StatusCode, "Failed to create incident") createErrorResponse(c, response.StatusCode, "Failed to create incident")
return return
} }
@@ -106,7 +104,7 @@ func (houstonService *HoustonService) GetProducts(c *gin.Context) {
// Get the products using the houstonClient // Get the products using the houstonClient
products, err := houstonService.houstonClient.GetAllProducts() products, err := houstonService.houstonClient.GetAllProducts()
if err != nil { if err != nil {
fmt.Println("Error getting products:", err) houstonService.logger.Error("Error getting products:", zap.Error(err))
createErrorResponse(c, http.StatusInternalServerError, "Failed to get products") createErrorResponse(c, http.StatusInternalServerError, "Failed to get products")
return return
} }
@@ -125,7 +123,7 @@ func (houstonService *HoustonService) GetResponderTeam(c *gin.Context) {
// Get the responder team using the houstonClient // Get the responder team using the houstonClient
responderTeam, err := houstonService.houstonClient.GetReportingAndResponder(productID) responderTeam, err := houstonService.houstonClient.GetReportingAndResponder(productID)
if err != nil { if err != nil {
fmt.Println("Error getting responder team:", err) houstonService.logger.Error("Error getting responder team:", zap.Error(err))
createErrorResponse(c, http.StatusInternalServerError, "Failed to get responder team") createErrorResponse(c, http.StatusInternalServerError, "Failed to get responder team")
return return
} }

View File

@@ -7,6 +7,7 @@ import (
"cybertron/pkg/kafka/producer" "cybertron/pkg/kafka/producer"
"cybertron/pkg/log" "cybertron/pkg/log"
"encoding/hex" "encoding/hex"
"errors"
"math/big" "math/big"
"net/http" "net/http"
"strings" "strings"
@@ -30,6 +31,12 @@ type ProjectBody struct {
GithubUrl string `json:"githubUrl" binding:"required"` GithubUrl string `json:"githubUrl" binding:"required"`
} }
type UpdateProjectBody struct {
Icon string `json:"logoUrl" binding:"required"`
GithubUrl string `json:"githubUrl" binding:"required"`
IgnorePatterns []string `json:"ignorePatterns" binding:"required"`
}
func NewProjectCreator(logger *log.Logger, dbClient *gorm.DB, s3Client *aws.Actions, kafkaProducer producer.KProducer) *ProjectCreator { func NewProjectCreator(logger *log.Logger, dbClient *gorm.DB, s3Client *aws.Actions, kafkaProducer producer.KProducer) *ProjectCreator {
return &ProjectCreator{ return &ProjectCreator{
logger: logger, logger: logger,
@@ -81,8 +88,26 @@ func (pc *ProjectCreator) CreateProject(ctx *gin.Context) {
} }
func (pc *ProjectCreator) ProjectGet(c *gin.Context) { func (pc *ProjectCreator) ProjectGet(c *gin.Context) {
var projectId = c.Param("id")
var projects []db.Project var projects []db.Project
// If projectId is provided, find the project by ID
if projectId != "" {
var project db.Project
if result := pc.dbClient.Where("project_reference_id = ?", projectId).First(&project); result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
c.JSON(http.StatusNotFound, gin.H{"error": "Project not found"})
} else {
c.JSON(http.StatusInternalServerError, gin.H{"error": result.Error.Error()})
}
return
}
// Return the specific project
c.JSON(http.StatusOK, project)
return
}
// If no projectId is provided, return all projects
if result := pc.dbClient.Find(&projects); result.Error != nil { if result := pc.dbClient.Find(&projects); result.Error != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": result.Error.Error()}) c.JSON(http.StatusInternalServerError, gin.H{"error": result.Error.Error()})
return return
@@ -90,3 +115,38 @@ func (pc *ProjectCreator) ProjectGet(c *gin.Context) {
c.JSON(http.StatusOK, projects) c.JSON(http.StatusOK, projects)
} }
func (pc *ProjectCreator) ProjectUpdate(c *gin.Context) {
var projectId = c.Param("id")
var project db.Project
var updateProjectBody UpdateProjectBody
// Find the project by ID
if result := pc.dbClient.Where("id = ?", projectId).First(&project); result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
c.JSON(http.StatusNotFound, gin.H{"error": "Project not found"})
} else {
c.JSON(http.StatusInternalServerError, gin.H{"error": result.Error.Error()})
}
return
}
// Bind the request body to the project object
if err := c.ShouldBindJSON(&updateProjectBody); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
// Update the project in the database
if result := pc.dbClient.Where("id = ?", projectId).Updates(&db.Project{
GithubUrl: updateProjectBody.GithubUrl,
Icon: updateProjectBody.Icon,
IgnorePatterns: updateProjectBody.IgnorePatterns,
}); result.Error != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": result.Error.Error()})
return
}
// Return the updated project
c.JSON(http.StatusOK, "Project updated")
}

View File

@@ -46,7 +46,6 @@ func (s *SearchService) GetErrorDetails(c *gin.Context) {
sort_query := utils.CreateSortQuery("created_at", "desc", "") sort_query := utils.CreateSortQuery("created_at", "desc", "")
after_query := utils.CreateFromQuery(fromInNumber) after_query := utils.CreateFromQuery(fromInNumber)
es_query := utils.CreateEsQuery(search_query, size_query, sort_query, after_query) es_query := utils.CreateEsQuery(search_query, size_query, sort_query, after_query)
println(es_query)
// searchRequest := ` // searchRequest := `
//{ //{

View File

@@ -5,9 +5,10 @@ import (
"cybertron/configs" "cybertron/configs"
"cybertron/internal/client/aws" "cybertron/internal/client/aws"
"cybertron/models/db" "cybertron/models/db"
"fmt" "cybertron/pkg/log"
"github.com/aws/aws-sdk-go-v2/service/s3" "github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"go.uber.org/zap"
"gorm.io/gorm" "gorm.io/gorm"
"net/http" "net/http"
"path" "path"
@@ -18,6 +19,7 @@ type SourceMapService struct {
dbClient *gorm.DB dbClient *gorm.DB
s3Client *aws.Actions s3Client *aws.Actions
awsConfig *configs.AwsConfig awsConfig *configs.AwsConfig
logger *log.Logger
} }
type SourceMapAckBody struct { type SourceMapAckBody struct {
@@ -26,11 +28,12 @@ type SourceMapAckBody struct {
FileName string `json:"fileName" binding:"required"` FileName string `json:"fileName" binding:"required"`
} }
func NewSourceMapService(dbClient *gorm.DB, s3Client *aws.Actions, config *configs.AwsConfig) *SourceMapService { func NewSourceMapService(logger *log.Logger, dbClient *gorm.DB, s3Client *aws.Actions, config *configs.AwsConfig) *SourceMapService {
return &SourceMapService{ return &SourceMapService{
dbClient: dbClient, dbClient: dbClient,
s3Client: s3Client, s3Client: s3Client,
awsConfig: config, awsConfig: config,
logger: logger,
} }
} }
@@ -38,6 +41,12 @@ func (s *SourceMapService) GetSourceMapUploadUrl(ctx *gin.Context) {
projectId := ctx.Query("project_id") projectId := ctx.Query("project_id")
releaseId := ctx.Query("release_id") releaseId := ctx.Query("release_id")
fileName := ctx.Query("file_name") fileName := ctx.Query("file_name")
account := ctx.DefaultQuery("account", "ppl")
appendedAccount := "-" + account
if appendedAccount == "ppl" {
appendedAccount = ""
}
if projectId == "" || releaseId == "" || fileName == "" { if projectId == "" || releaseId == "" || fileName == "" {
ctx.JSON(http.StatusBadRequest, gin.H{ ctx.JSON(http.StatusBadRequest, gin.H{
@@ -48,7 +57,7 @@ func (s *SourceMapService) GetSourceMapUploadUrl(ctx *gin.Context) {
//generate s3 pre-signed url //generate s3 pre-signed url
key := path.Join(projectId, releaseId, fileName) key := path.Join(projectId, releaseId, fileName)
bucket := s.awsConfig.Bucket bucket := s.awsConfig.Bucket + appendedAccount
request, err := s.s3Client.S3PresignClient.PresignPutObject(context.TODO(), &s3.PutObjectInput{ request, err := s.s3Client.S3PresignClient.PresignPutObject(context.TODO(), &s3.PutObjectInput{
Bucket: &bucket, Bucket: &bucket,
Key: &key, Key: &key,
@@ -56,7 +65,7 @@ func (s *SourceMapService) GetSourceMapUploadUrl(ctx *gin.Context) {
opts.Expires = time.Duration(7 * 24 * time.Hour) opts.Expires = time.Duration(7 * 24 * time.Hour)
}) })
if err != nil { if err != nil {
fmt.Println(err) s.logger.Error("unable to generate pre-signed url", zap.Error(err))
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "unable to create S3 object"}) ctx.JSON(http.StatusInternalServerError, gin.H{"error": "unable to create S3 object"})
return return
} }