...

Source file src/edge-infra.dev/pkg/edge/api/services/log_classifcation_service.go

Documentation: edge-infra.dev/pkg/edge/api/services

     1  package services
     2  
     3  /*
     4    TODO: TO BE DEPRECATED IN 0.25 @RS185722
     5  */
     6  import (
     7  	"context"
     8  	"database/sql"
     9  	"fmt"
    10  
    11  	"github.com/google/uuid"
    12  	"github.com/hashicorp/go-multierror"
    13  
    14  	"edge-infra.dev/pkg/edge/api/graph/mapper"
    15  	"edge-infra.dev/pkg/edge/api/graph/model"
    16  	sqlquery "edge-infra.dev/pkg/edge/api/sql"
    17  )
    18  
    19  //go:generate mockgen -destination=../mocks/mock_log_classification_service.go -package=mocks edge-infra.dev/pkg/edge/api/services LogClassificationService
    20  type LogClassificationService interface {
    21  	CreateLogClassification(ctx context.Context, bannerEdgeID string, logClassificationEdgeID string, newClusterConfig model.CreateClassificationInput) (*model.LogClassification, error)
    22  	UpdateLogClassification(ctx context.Context, bannerEdgeID string, logClassificationEdgeID string, updatedClusterConfig *model.UpdateClassificationInput) (*model.LogClassification, error)
    23  	GetLogClassification(ctx context.Context, bannerEdgeID string, logClassificationEdgeID string) (*model.LogClassification, error)
    24  	DeleteLogClassification(ctx context.Context, bannerEdgeID string, logClassificationEdgeID string) (bool, error)
    25  	GetLogClassificationsByBanner(ctx context.Context, bannerEdgeID string) ([]*model.LogClassification, error)
    26  }
    27  
    28  type logClassificationService struct {
    29  	SQLDB *sql.DB
    30  }
    31  
    32  func (l *logClassificationService) GetLogClassification(ctx context.Context, bannerEdgeID string, logClassificationEdgeID string) (*model.LogClassification, error) {
    33  	row := l.SQLDB.QueryRowContext(ctx, sqlquery.GetLogClassification, bannerEdgeID, logClassificationEdgeID)
    34  	logClassification := &model.LogClassification{}
    35  	if err := row.Scan(&logClassification.BannerEdgeID, &logClassification.LogClassificationEdgeID, &logClassification.Description, &logClassification.Pod, &logClassification.Container, &logClassification.Type, &logClassification.Class, &logClassification.Pattern); err != nil {
    36  		return nil, err
    37  	}
    38  	return logClassification, nil
    39  }
    40  
    41  func (l *logClassificationService) GetLogClassificationsByBanner(ctx context.Context, bannerEdgeID string) ([]*model.LogClassification, error) {
    42  	row, err := l.SQLDB.QueryContext(ctx, sqlquery.GetLogClassificationsByBanner, bannerEdgeID)
    43  	if err != nil {
    44  		return nil, err
    45  	}
    46  
    47  	logClassifications := []*model.LogClassification{}
    48  
    49  	for row.Next() {
    50  		logClassification := &model.LogClassification{}
    51  		if err = row.Scan(&logClassification.BannerEdgeID, &logClassification.LogClassificationEdgeID, &logClassification.Description, &logClassification.Pod, &logClassification.Container, &logClassification.Type, &logClassification.Class, &logClassification.Pattern); err != nil {
    52  			return nil, err
    53  		}
    54  		logClassifications = append(logClassifications, logClassification)
    55  	}
    56  	return logClassifications, nil
    57  }
    58  
    59  func (l *logClassificationService) DeleteLogClassification(ctx context.Context, bannerEdgeID string, logClassificationEdgeID string) (bool, error) {
    60  	_, err := l.SQLDB.ExecContext(ctx, sqlquery.DeleteLogClassification, bannerEdgeID, logClassificationEdgeID)
    61  	if err != nil {
    62  		return false, fmt.Errorf("Error Deleting Log Classification: %s", err.Error())
    63  	}
    64  	return true, nil
    65  }
    66  
    67  func (l *logClassificationService) CreateLogClassification(ctx context.Context, bannerEdgeID string, logClassificationEdgeID string, newClassification model.CreateClassificationInput) (*model.LogClassification, error) {
    68  	logClass, err := mapper.FromLogClassSelection(newClassification.Class)
    69  	if err != nil {
    70  		return nil, err
    71  	}
    72  
    73  	/* In reality, all API calls will be blank for this value, but
    74  	   for testing this value need to be parameterized. This will
    75  	   help to avoid the uuid.NewString() mismatch during testing.
    76  	   Also using goMocks.Any() causes issues in the DB saying:
    77  	   could not convert 0 argument gomock.anyMatcher - is anything to driver value: unsupported type gomock.anyMatcher, a struct
    78  	*/
    79  	if logClassificationEdgeID == "" {
    80  		logClassificationEdgeID = uuid.NewString()
    81  	}
    82  
    83  	dbClassification := &model.LogClassification{
    84  		BannerEdgeID:            bannerEdgeID,
    85  		LogClassificationEdgeID: logClassificationEdgeID,
    86  		Description:             newClassification.Description,
    87  		Pod:                     newClassification.Pod,
    88  		Container:               newClassification.Container,
    89  		Type:                    newClassification.Type,
    90  		Class:                   logClass,
    91  		Pattern:                 newClassification.Pattern,
    92  	}
    93  
    94  	err = l.queryLogClassification(ctx, sqlquery.CreateLogClassification, dbClassification)
    95  	if err != nil {
    96  		return nil, fmt.Errorf("Error Creating Log Classification: %s", err.Error())
    97  	}
    98  
    99  	return dbClassification, nil
   100  }
   101  
   102  func (l *logClassificationService) UpdateLogClassification(ctx context.Context, bannerEdgeID string, logClassificationEdgeID string, newClassification *model.UpdateClassificationInput) (*model.LogClassification, error) {
   103  	var logClass string
   104  	var err error
   105  	if newClassification.Class != nil {
   106  		logClass, err = mapper.FromLogClassSelection(*newClassification.Class)
   107  		if err != nil {
   108  			return nil, err
   109  		}
   110  	}
   111  	oldClassification, err := l.GetLogClassification(ctx, bannerEdgeID, logClassificationEdgeID)
   112  	if err != nil {
   113  		return nil, err
   114  	}
   115  
   116  	updatedClassification := mapper.ToNewClassification(newClassification, oldClassification, logClass)
   117  
   118  	err = l.queryLogClassification(ctx, sqlquery.UpdateLogClassification, updatedClassification)
   119  	if err != nil {
   120  		return nil, fmt.Errorf("Error Updating Log Classification: %s", err.Error())
   121  	}
   122  	return updatedClassification, nil
   123  }
   124  
   125  // To be used for the INSERT AND UPDATE functions.
   126  func (l logClassificationService) queryLogClassification(ctx context.Context, query string, classificationPayload *model.LogClassification) error {
   127  	transaction, err := l.SQLDB.BeginTx(ctx, nil)
   128  	if err != nil {
   129  		return err
   130  	}
   131  
   132  	if _, err := transaction.ExecContext(ctx, query, classificationPayload.BannerEdgeID, classificationPayload.LogClassificationEdgeID, classificationPayload.Description, classificationPayload.Pod, classificationPayload.Container, classificationPayload.Type, classificationPayload.Class, classificationPayload.Pattern); err != nil {
   133  		if rollbackErr := transaction.Rollback(); rollbackErr != nil {
   134  			return multierror.Append(err, rollbackErr)
   135  		}
   136  		return err
   137  	}
   138  
   139  	if err = transaction.Commit(); err != nil {
   140  		return err
   141  	}
   142  	return nil
   143  }
   144  
   145  // nolint
   146  func NewLogClassificationService(sqlDB *sql.DB) *logClassificationService {
   147  	return &logClassificationService{
   148  		SQLDB: sqlDB,
   149  	}
   150  }
   151  

View as plain text