-
Notifications
You must be signed in to change notification settings - Fork 45
/
repository.go
206 lines (163 loc) · 5.4 KB
/
repository.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Copyright The Linux Foundation and each contributor to CommunityBridge.
// SPDX-License-Identifier: MIT
package store
import (
"context"
"encoding/json"
"fmt"
"strings"
"github.com/sirupsen/logrus"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
"github.com/communitybridge/easycla/cla-backend-go/utils"
log "github.com/communitybridge/easycla/cla-backend-go/logging"
)
// DBStore represents DB Model for the store table
type DBStore struct {
Key string `dynamodbav:"key"`
Value string `dynamodbav:"value"`
Expire float64 `dynamodbav:"expire"`
}
// Repository interface
type Repository interface {
SetActiveSignatureMetaData(ctx context.Context, key string, expire int64, value string) error
GetActiveSignatureMetaData(ctx context.Context, UserId string) (map[string]interface{}, error)
DeleteActiveSignatureMetaData(ctx context.Context, key string) error
}
type repo struct {
stage string
dynamoDBClient *dynamodb.DynamoDB
storeTableName string
}
// NewRepository initiates Store repository instance
func NewRepository(awsSession *session.Session, stage string) Repository {
return repo{
stage: stage,
dynamoDBClient: dynamodb.New(awsSession),
storeTableName: fmt.Sprintf("cla-%s-store", stage),
}
}
// GetActiveSignatureMetaData returns active signature meta data
func (r repo) GetActiveSignatureMetaData(ctx context.Context, userId string) (map[string]interface{}, error) {
f := logrus.Fields{
"functionName": "v2.store.repository.GetActiveSignatureMetaData",
utils.XREQUESTID: ctx.Value(utils.XREQUESTID),
"userId": userId,
}
var metadata map[string]interface{}
log.WithFields(f).Debugf("querying for user: %s", userId)
key := fmt.Sprintf("active_signature:%s", userId)
result, err := r.dynamoDBClient.GetItem(&dynamodb.GetItemInput{
TableName: &r.storeTableName,
Key: map[string]*dynamodb.AttributeValue{
"key": {
S: &key,
},
},
})
if err != nil {
log.WithFields(f).WithError(err).Warn("problem querying store table")
return metadata, err
}
if result.Item == nil {
log.WithFields(f).Warn("no record found")
return metadata, nil
}
var jsonStr string
err = dynamodbattribute.Unmarshal(result.Item["value"], &jsonStr)
if err != nil {
log.WithFields(f).WithError(err).Warn("problem unmarshalling store record")
return metadata, err
}
formatJson := strings.ReplaceAll(jsonStr, "\\\"", "\"")
formatJson = strings.Trim(formatJson, "\"")
log.WithFields(f).Debugf("format: %s", formatJson)
jsonErr := json.Unmarshal([]byte(formatJson), &metadata)
if jsonErr != nil {
log.WithFields(f).WithError(jsonErr).Warn("problem unmarshalling json string for metadata")
return nil, jsonErr
}
log.WithFields(f).Debugf("metadata: %+v", metadata)
return metadata, nil
}
// func findDifferences(str1, str2 string) string {
// f := logrus.Fields{
// "functionName": "findDifference",
// }
// var differences string
// // Find the minimum length of the two strings
// minLength := len(str1)
// if len(str2) < minLength {
// minLength = len(str2)
// }
// // Compare each character and append the differences to the result string
// for i := 0; i < minLength; i++ {
// if str1[i] != str2[i] {
// differences += string(str1[i]) + string(str2[i]) + " "
// log.WithFields(f).Debugf("%s and %s", string(str1[i]), string(str2[i]))
// }
// }
// // If the strings have different lengths, append the remaining characters
// if len(str1) > len(str2) {
// differences += str1[minLength:]
// } else if len(str2) > len(str1) {
// differences += str2[minLength:]
// }
// return differences
// }
// SetActiveSignatureMetaData sets active signature meta data
func (r repo) SetActiveSignatureMetaData(ctx context.Context, key string, expire int64, value string) error {
f := logrus.Fields{
"functionName": "v2.store.repository.SetActiveSignatureMetaData",
utils.XREQUESTID: ctx.Value(utils.XREQUESTID),
"key": key,
"value": value,
"expire": expire,
}
store := DBStore{
Key: key,
Value: value,
Expire: float64(expire),
}
log.WithFields(f).Debugf("key: %s ", store.Key)
log.WithFields(f).Debugf("value: %+s ", store.Value)
v, err := dynamodbattribute.MarshalMap(store)
if err != nil {
log.WithFields(f).WithError(err).Warn("problem marshalling store record")
return err
}
log.WithFields(f).Debugf("Marshalled values: %+v", v)
_, err = r.dynamoDBClient.PutItem(&dynamodb.PutItemInput{
Item: v,
TableName: &r.storeTableName,
})
if err != nil {
log.WithFields(f).WithError(err).Warn("unable to save store record")
return err
}
log.WithFields(f).Debugf("Signature meta record data saved: %+v ", store)
return nil
}
func (r repo) DeleteActiveSignatureMetaData(ctx context.Context, key string) error {
f := logrus.Fields{
"functionName": "v2.store.repository.DeleteActiveSignatureMetaData",
utils.XREQUESTID: ctx.Value(utils.XREQUESTID),
"key": key,
}
log.WithFields(f).Debugf("key: %s ", key)
_, err := r.dynamoDBClient.DeleteItem(&dynamodb.DeleteItemInput{
Key: map[string]*dynamodb.AttributeValue{
"key": {
S: &key,
},
},
TableName: &r.storeTableName,
})
if err != nil {
log.WithFields(f).WithError(err).Warn("unable to delete store record")
return err
}
log.WithFields(f).Debugf("Signature meta record data deleted: %+v ", key)
return nil
}