/
google_service_account_key_repository.go
96 lines (77 loc) · 2.94 KB
/
google_service_account_key_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
package repository
import (
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-common-module/tracing"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-postgres-repository/entity"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/log"
"github.com/pkg/errors"
"golang.org/x/net/context"
"gorm.io/gorm"
)
type GoogleServiceAccountKeyRepository interface {
GetApiKeyByTenantService(ctx context.Context, tenantId, serviceId string) (string, error)
SaveKey(ctx context.Context, tenant, key, value string) error
DeleteKey(ctx context.Context, tenant, key string) error
}
type GoogleServiceAccountKeyRepositoryImpl struct {
gormDb *gorm.DB
}
func NewGoogleServiceAccountKeyRepository(gormDb *gorm.DB) *GoogleServiceAccountKeyRepositoryImpl {
return &GoogleServiceAccountKeyRepositoryImpl{gormDb: gormDb}
}
func (repo *GoogleServiceAccountKeyRepositoryImpl) GetApiKeyByTenantService(ctx context.Context, tenantName, serviceId string) (string, error) {
span, _ := opentracing.StartSpanFromContext(ctx, "GoogleServiceAccountKeyRepository.GetApiKeyByTenantService")
defer span.Finish()
span.LogFields(log.String("tenantName", tenantName), log.String("serviceId", serviceId))
result := entity.GoogleServiceAccountKey{}
err := repo.gormDb.First(&result, "tenant_name = ? AND key = ?", tenantName, serviceId).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
span.LogFields(log.String("result", "record not found"))
return "", nil
} else {
tracing.TraceErr(span, err)
return "", errors.Wrap(err, "GetApiKeyByTenantService")
}
}
return result.Value, nil
}
func (repo *GoogleServiceAccountKeyRepositoryImpl) SaveKey(ctx context.Context, tenant, key, value string) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "GoogleServiceAccountKeyRepository.SaveKey")
defer span.Finish()
span.LogFields(log.String("tenant", tenant), log.String("key", key), log.String("value", value))
existing, err := repo.GetApiKeyByTenantService(ctx, tenant, key)
if err != nil {
return err
}
if existing != "" {
return nil
}
newKey := entity.GoogleServiceAccountKey{
TenantName: tenant,
Key: key,
Value: value,
}
result := repo.gormDb.Save(&newKey)
if result.Error != nil {
return errors.Wrap(result.Error, "SaveKey")
}
return nil
}
func (repo *GoogleServiceAccountKeyRepositoryImpl) DeleteKey(ctx context.Context, tenant, key string) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "GoogleServiceAccountKeyRepository.SaveKey")
defer span.Finish()
span.LogFields(log.String("tenant", tenant), log.String("key", key))
existing, err := repo.GetApiKeyByTenantService(ctx, tenant, key)
if err != nil {
return err
}
if existing != "" {
return nil
}
err = repo.gormDb.Delete(&entity.GoogleServiceAccountKey{}, "tenant_name = ? AND key = ?", tenant, key).Error
if err != nil {
return errors.Wrap(err, "DeleteKey")
}
return nil
}