1 package services
2
3
6 import (
7 "context"
8 "database/sql"
9 "errors"
10 "fmt"
11
12 "github.com/google/uuid"
13 "github.com/hashicorp/go-multierror"
14
15 "edge-infra.dev/pkg/edge/api/graph/model"
16 sqlquery "edge-infra.dev/pkg/edge/api/sql"
17 )
18
19
20 type LogClassificationLabelsService interface {
21 GetLogClassificationLabel(ctx context.Context, logClassificationLabelEdgeID string, logClassificationEdgeID string, labelEdgeID string) (*model.LogClassificationLabel, error)
22 GetLogClassificationLabelsByLabel(ctx context.Context, labelEdgeID string) ([]*model.LogClassificationLabel, error)
23 GetLogClassificationLabelsByBanner(ctx context.Context, bannerEdgeID string) ([]*model.LogClassificationLabel, error)
24 CreateLogClassificationLabel(ctx context.Context, logClassificationLabelEdgeID string, logClassificationEdgeID string, labelEdgeID string) (string, error)
25 UpdateLogClassificationLabel(ctx context.Context, logClassificationEdgeID string, labelEdgeID string) (string, error)
26 DeleteLogClassificationLabel(ctx context.Context, logClassificationLabelEdgeID string, logClassificationEdgeID string, labelEdgeID string) (bool, error)
27 }
28
29 type logClassificationLabelsService struct {
30 SQLDB *sql.DB
31 }
32
33 func (l *logClassificationLabelsService) GetLogClassificationLabel(ctx context.Context, logClassificationLabelEdgeID string, logClassificationEdgeID string, labelEdgeID string) (*model.LogClassificationLabel, error) {
34 row := l.SQLDB.QueryRowContext(ctx, sqlquery.GetLogClassificationLabel, labelEdgeID, logClassificationEdgeID, logClassificationLabelEdgeID)
35
36 logClassificationLabel := &model.LogClassificationLabel{}
37
38 if err := row.Scan(&logClassificationLabel.LogClassificationLabelEdgeID, &logClassificationLabel.ClassificationEdgeID, &logClassificationLabel.BannerEdgeID,
39 &logClassificationLabel.LabelEdgeID, &logClassificationLabel.LabelName, &logClassificationLabel.Description, &logClassificationLabel.Pod,
40 &logClassificationLabel.Container, &logClassificationLabel.Type, &logClassificationLabel.Class, &logClassificationLabel.Pattern); err != nil {
41 return nil, err
42 }
43
44 return logClassificationLabel, nil
45 }
46
47 func (l *logClassificationLabelsService) GetLogClassificationLabelsByLabel(ctx context.Context, labelEdgeID string) ([]*model.LogClassificationLabel, error) {
48 if !isValidUUID(labelEdgeID) {
49 return nil, errors.New("Error Getting Log Classification Labels by Label: Label Edge ID not a valid UUID format")
50 }
51
52 return l.getMultipleClassificationLabels(ctx, sqlquery.GetLogClassificationLabelsByLabel, labelEdgeID)
53 }
54
55 func (l *logClassificationLabelsService) GetLogClassificationLabelsByBanner(ctx context.Context, bannerEdgeID string) ([]*model.LogClassificationLabel, error) {
56 if bannerEdgeID == "" {
57 return nil, errors.New("Error Getting Log Classification Labels by Banner: Banner Edge ID can not be blank")
58 }
59 if !isValidUUID(bannerEdgeID) {
60 return nil, errors.New("Error Getting Log Classification Labels by Banner: Banner Edge ID not a valid UUID format")
61 }
62
63 return l.getMultipleClassificationLabels(ctx, sqlquery.GetLogClassificationLabelsByBanner, bannerEdgeID)
64 }
65
66 func (l *logClassificationLabelsService) CreateLogClassificationLabel(ctx context.Context, logClassificationLabelEdgeID string, logClassificationEdgeID string, labelEdgeID string) (string, error) {
67 existingClassifications, err := l.GetLogClassificationLabelsByLabel(ctx, labelEdgeID)
68 if err != nil {
69 return "", err
70 }
71
72 for _, classificationLabel := range existingClassifications {
73 if logClassificationEdgeID == classificationLabel.ClassificationEdgeID {
74 return "", errors.New("Error Creating Log Classification Label: Classification is already tied to this label")
75 }
76 }
77
78
84 if logClassificationLabelEdgeID == "" {
85 logClassificationLabelEdgeID = uuid.NewString()
86 }
87
88 transaction, err := l.newTransaction(ctx)
89 if err != nil {
90 return "", fmt.Errorf("Error Creating Log Classification Label: %s", err.Error())
91 }
92
93 if _, err := transaction.ExecContext(ctx, sqlquery.CreateLogClassificationLabel, logClassificationLabelEdgeID, logClassificationEdgeID, labelEdgeID); err != nil {
94 if rollbackErr := transaction.Rollback(); rollbackErr != nil {
95 return "", multierror.Append(err, rollbackErr)
96 }
97 return "", err
98 }
99 err = transaction.Commit()
100 if err != nil {
101 return "", fmt.Errorf("Error Creating Log Classification Label: %s", err.Error())
102 }
103
104 return fmt.Sprintf("Entry Created Successfully. Entry ID is: %v", logClassificationLabelEdgeID), nil
105 }
106
107 func (l *logClassificationLabelsService) UpdateLogClassificationLabel(ctx context.Context, logClassificationEdgeID string, labelEdgeID string) (string, error) {
108 transaction, err := l.newTransaction(ctx)
109 if err != nil {
110 return "", fmt.Errorf("Error Updating Log Classification Label: %s", err.Error())
111 }
112
113 if _, err := transaction.ExecContext(ctx, sqlquery.UpdateLogClassificationLabel, labelEdgeID, logClassificationEdgeID); err != nil {
114 if rollbackErr := transaction.Rollback(); rollbackErr != nil {
115 return "", multierror.Append(err, rollbackErr)
116 }
117 return "", err
118 }
119 err = transaction.Commit()
120 if err != nil {
121 return "", fmt.Errorf("Error Updating Log Classification Label: %s", err.Error())
122 }
123
124 return fmt.Sprintf("Updated Successfully. Classification %v is now associated with label %v", logClassificationEdgeID, labelEdgeID), nil
125 }
126
127 func (l *logClassificationLabelsService) DeleteLogClassificationLabel(ctx context.Context, logClassificationLabelEdgeID string, logClassificationEdgeID string, labelEdgeID string) (bool, error) {
128 _, err := l.SQLDB.ExecContext(ctx, sqlquery.DeleteLogClassificationLabel, logClassificationLabelEdgeID, logClassificationEdgeID, labelEdgeID)
129 if err != nil {
130 return false, fmt.Errorf("Error Deleting Log Classification Label: %s", err.Error())
131 }
132 return true, nil
133 }
134
135 func (l *logClassificationLabelsService) newTransaction(ctx context.Context) (*sql.Tx, error) {
136 transaction, err := l.SQLDB.BeginTx(ctx, nil)
137 if err != nil {
138 return nil, err
139 }
140
141 return transaction, nil
142 }
143
144 func (l *logClassificationLabelsService) getMultipleClassificationLabels(ctx context.Context, query string, ID string) ([]*model.LogClassificationLabel, error) {
145 row, err := l.SQLDB.QueryContext(ctx, query, ID)
146 if err != nil {
147 return nil, fmt.Errorf("Error Getting Log Classification Labels: %s", err.Error())
148 }
149
150 logClassificationLabels := []*model.LogClassificationLabel{}
151
152 for row.Next() {
153 logClassificationLabel := &model.LogClassificationLabel{}
154 if err = row.Scan(&logClassificationLabel.LogClassificationLabelEdgeID, &logClassificationLabel.ClassificationEdgeID, &logClassificationLabel.BannerEdgeID,
155 &logClassificationLabel.LabelEdgeID, &logClassificationLabel.LabelName, &logClassificationLabel.Description, &logClassificationLabel.Pod,
156 &logClassificationLabel.Container, &logClassificationLabel.Type, &logClassificationLabel.Class, &logClassificationLabel.Pattern); err != nil {
157 return nil, fmt.Errorf("Error Getting Log Classification Labels: %s", err.Error())
158 }
159 logClassificationLabels = append(logClassificationLabels, logClassificationLabel)
160 }
161 return logClassificationLabels, nil
162 }
163
164 func isValidUUID(u string) bool {
165 if u == "" {
166 return false
167 }
168 _, err := uuid.Parse(u)
169 return err == nil
170 }
171
172
173 func NewLogClassificationLabelService(sqlDB *sql.DB) *logClassificationLabelsService {
174 return &logClassificationLabelsService{
175 SQLDB: sqlDB,
176 }
177 }
178
View as plain text