-
Notifications
You must be signed in to change notification settings - Fork 63
/
kubectl_cluster_setting.go
86 lines (77 loc) · 2.89 KB
/
kubectl_cluster_setting.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
package service
import (
"context"
"database/sql"
"time"
"github.com/google/uuid"
"github.com/paralus/paralus/internal/constants"
"github.com/paralus/paralus/internal/dao"
"github.com/paralus/paralus/internal/models"
"github.com/paralus/paralus/proto/types/sentry"
"github.com/uptrace/bun"
"google.golang.org/protobuf/types/known/timestamppb"
)
// KubectlClusterSettingsService is the interface for kubectl cluster setting operations
type KubectlClusterSettingsService interface {
Get(ctx context.Context, orgID string, clusterID string) (*sentry.KubectlClusterSettings, error)
Patch(ctx context.Context, kc *sentry.KubectlClusterSettings) error
}
// kubectlClusterSettingsService implements KubectlClusterSettingsService
type kubectlClusterSettingsService struct {
db *bun.DB
}
// NewKubectlClusterSettingsService return new kubectl cluster setting service
func NewkubectlClusterSettingsService(db *bun.DB) KubectlClusterSettingsService {
return &kubectlClusterSettingsService{db}
}
func (kcs *kubectlClusterSettingsService) Get(ctx context.Context, orgID string, clusterID string) (*sentry.KubectlClusterSettings, error) {
kc, err := dao.GetkubectlClusterSettings(ctx, kcs.db, uuid.MustParse(orgID), clusterID)
if err == sql.ErrNoRows {
return nil, constants.ErrNotFound
} else if err != nil {
return nil, err
}
return prepareKubectlSettingResponse(kc), nil
}
func (kcs *kubectlClusterSettingsService) Patch(ctx context.Context, kc *sentry.KubectlClusterSettings) error {
return kcs.db.RunInTx(ctx, &sql.TxOptions{}, func(ctx context.Context, tx bun.Tx) error {
_, err := dao.GetkubectlClusterSettings(ctx, tx, uuid.MustParse(kc.OrganizationID), kc.Name)
if err != nil {
if err == sql.ErrNoRows {
kcsdb := convertToKubeCtlSettingModel(kc)
kcsdb.CreatedAt = time.Now()
return dao.CreatekubectlClusterSettings(ctx, tx, kcsdb)
}
return err
}
kcsdb := convertToKubeCtlSettingModel(kc)
kcsdb.ModifiedAt = time.Now()
return dao.UpdatekubectlClusterSettings(ctx, tx, kcsdb)
})
}
func convertToKubeCtlSettingModel(kcs *sentry.KubectlClusterSettings) *models.KubectlClusterSetting {
kcsm := &models.KubectlClusterSetting{
Name: kcs.Name,
OrganizationId: uuid.MustParse(kcs.OrganizationID),
DisableWebKubectl: kcs.DisableWebKubectl,
DisableCliKubectl: kcs.DisableCLIKubectl,
}
if kcs.PartnerID != "" {
kcsm.PartnerId, _ = uuid.Parse(kcs.PartnerID)
}
return kcsm
}
func prepareKubectlSettingResponse(kcs *models.KubectlClusterSetting) *sentry.KubectlClusterSettings {
kc := &sentry.KubectlClusterSettings{
Name: kcs.Name,
OrganizationID: kcs.OrganizationId.String(),
DisableWebKubectl: kcs.DisableWebKubectl,
DisableCLIKubectl: kcs.DisableCliKubectl,
CreatedAt: timestamppb.New(kcs.CreatedAt),
ModifiedAt: timestamppb.New(kcs.ModifiedAt),
}
if kcs.PartnerId != uuid.Nil {
kc.PartnerID = kcs.PartnerId.String()
}
return kc
}