-
Notifications
You must be signed in to change notification settings - Fork 25
/
integration.go
122 lines (103 loc) · 3.32 KB
/
integration.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
//
// Copyright 2023 The Chainloop Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package data
import (
"context"
"time"
"github.com/chainloop-dev/chainloop/app/controlplane/internal/biz"
"github.com/chainloop-dev/chainloop/app/controlplane/internal/data/ent"
"github.com/chainloop-dev/chainloop/app/controlplane/internal/data/ent/integration"
"github.com/chainloop-dev/chainloop/app/controlplane/internal/data/ent/integrationattachment"
"github.com/go-kratos/kratos/v2/log"
"github.com/google/uuid"
)
type IntegrationRepo struct {
data *Data
log *log.Helper
}
func NewIntegrationRepo(data *Data, logger log.Logger) biz.IntegrationRepo {
return &IntegrationRepo{
data: data,
log: log.NewHelper(logger),
}
}
func (r *IntegrationRepo) Create(ctx context.Context, opts *biz.IntegrationCreateOpts) (*biz.Integration, error) {
integration, err := r.data.db.Integration.Create().
SetOrganizationID(opts.OrgID).
SetKind(opts.Kind).
SetDescription(opts.Description).
SetSecretName(opts.SecretName).
SetConfiguration(opts.Config).
Save(ctx)
if err != nil {
return nil, err
}
return entIntegrationToBiz(integration), nil
}
func (r *IntegrationRepo) List(ctx context.Context, orgID uuid.UUID) ([]*biz.Integration, error) {
integrations, err := orgScopedQuery(r.data.db, orgID).
QueryIntegrations().
Where(integration.DeletedAtIsNil()).
Order(ent.Desc(integration.FieldCreatedAt)).
All(ctx)
if err != nil {
return nil, err
}
result := make([]*biz.Integration, 0, len(integrations))
for _, i := range integrations {
result = append(result, entIntegrationToBiz(i))
}
return result, nil
}
func (r *IntegrationRepo) FindByIDInOrg(ctx context.Context, orgID, id uuid.UUID) (*biz.Integration, error) {
integration, err := orgScopedQuery(r.data.db, orgID).
QueryIntegrations().
Where(integration.ID(id)).
Where(integration.DeletedAtIsNil()).
Only(ctx)
if err != nil && !ent.IsNotFound(err) {
return nil, err
} else if integration == nil {
return nil, nil
}
return entIntegrationToBiz(integration), nil
}
func (r *IntegrationRepo) SoftDelete(ctx context.Context, id uuid.UUID) error {
tx, err := r.data.db.Tx(ctx)
if err != nil {
return err
}
// soft-delete attachments associated with this workflow
if err := tx.IntegrationAttachment.Update().Where(integrationattachment.HasIntegrationWith(integration.ID(id))).SetDeletedAt(time.Now()).Exec(ctx); err != nil {
return err
}
if err := tx.Integration.UpdateOneID(id).SetDeletedAt(time.Now()).Exec(ctx); err != nil {
return err
}
return tx.Commit()
}
func entIntegrationToBiz(i *ent.Integration) *biz.Integration {
if i == nil {
return nil
}
return &biz.Integration{
ID: i.ID,
Kind: i.Kind,
Description: i.Description,
CreatedAt: toTimePtr(i.CreatedAt),
SecretName: i.SecretName,
Config: i.Configuration,
}
}